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

from flask import Flask
import os
import logg
import pandas as pd
import numpy as np
from pipeline import Model_Pipeline,CleaningTextData,FillingNaN,TfIdf
from sklearn.ensemble import GradientBoostingRegressor
import nltk
import ssl
import datetime

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context
nltk.download('stopwords')
nltk.download('wordnet')
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from flask import request, jsonify

log = None
app = Flask(__name__)

data=""
prediction = None
gbr_result = None

@app.route('/')
def hello_world():
	return str(data)

@app.route('/api/v1/resources/conv', methods=['GET'])
def api_filter():
    query_parameters = request.args

    id = query_parameters.get('id')
    campaign_id = query_parameters.get('campaign_id')
    country_code = query_parameters.get('country_code')
    date = query_parameters.get('date')
    original_spend = query_parameters.get('original_spend')
    impression = query_parameters.get('impression')
    reach = query_parameters.get('reach')
    click = query_parameters.get('click')
    conversion_values_usd = query_parameters.get('conversion_values_usd')
    project_id = query_parameters.get('project_id')
    fb_id = query_parameters.get('fb_id')
    campaign_name = query_parameters.get('campaign_name')
    start_time = query_parameters.get('start_time')
    currency_code = query_parameters.get('currency_code')

    d = {
        'id':id,
        'campaign_id':campaign_id,
        'country_code':country_code,
        'date':date,
        'original_spend':float(original_spend),
        'impression':impression,
        'reach':reach,
        'click':click,
        'conversion_values_usd':float(conversion_values_usd),
        'project_id':project_id,
        'fb_id':fb_id,
        'campaign_name':campaign_name,
        'start_time':start_time,
        'currency_code':currency_code,
    }

    print(d)

    if not (id or campaign_id or country_code or date or original_spend or impression or reach or click or project_id or fb_id):
        return page_not_found(404)

    df = pd.DataFrame(d, index=[0])

    #df = pd.read_csv("try_to_test.csv")
    df = df.set_index('id')

    print(df)

    submition_res = func(df)

    return jsonify(submition_res.to_json(orient="records"))

@app.errorhandler(404)
def page_not_found(e):
    return "
404

The resource could not be found.
", 404

def func(X_test):

    y_test = X_test['conversion_values_usd']

    del X_test['conversion_values_usd']

    data = pipeline.pipelineData(X_test)

    print("sdfknksdnfklsldnfnklsdnf")

    print(type(data))

    data['conversion_values_usd'] = y_test.values

    print("sdfknksdnfsdfklaslflnksalfdkklsldnfnklsdnf")
    data = pd.DataFrame(data)

    print(data)

    print(type(data))

    del data["start_time"]
    del data["currency_code"]
    del data["country_code"]

    data["date"] = data["date"].apply(lambda x: int(x.strftime('%w')))

    data = data.astype('float')

    prediction = gbr_result.predict(data)

    submition_res = pd.DataFrame({'id': list(X_test.index), 'conversion_predict': prediction})
    print(submition_res)

    return submition_res

if __name__ == "__main__":

    log_directory = 'log'
    if not os.path.exists(log_directory):
        os.makedirs(log_directory)

    pipeline = Model_Pipeline("dft_idf_final.joblib")
    gbr_result = Model_Pipeline("XGBRegressor.joblib")

    #log = logg.setup_logging('Server')
    #log = logg.get_log("Web-server")

    app.run(debug=False,host='0.0.0.0')


# explanations for member functions are provided in requirements.py
# each file that uses a Zip Tree should import it from this file.

from typing import TypeVar
import random

KeyType = TypeVar('KeyType')
ValType = TypeVar('ValType')

class Node:
    def __init__(self, key: KeyType, val: ValType, rank: int):
        self.key = key
        self.val = val
        self.rank = rank
        self.left = None
        self.right = None

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

    @staticmethod
    def get_random_rank() -> int:
        rk = 0
        while True:
            if random.randint(0, 1) == 0:
                return rk
            else:
                rk += 1

    def unzip(self, newNode: Node, top: Node):
        def unzip_lookup(k: int, node: Node):
            if not node:
                return (None, None)
            if node.key < k:
                (P, Q) = unzip_lookup(k, node.right)
                node.right = P
                return (node, Q)
            else:
                (P, Q) = unzip_lookup(k, node.left)
                node.left = Q
                return (P, node)

        return unzip_lookup(newNode.key, top)

    def insert(self, key: KeyType, val: ValType, rank: int = -1):
        if rank == -1:
            rank = self.get_random_rank()
        self.size += 1
        newNode = Node(key, val, rank)
        if not self.root:
            self.root = newNode
            return
        parent = None
        ptr = self.root
        while ptr and ptr.rank > newNode.rank:
            parent = ptr
            if newNode.key < ptr.key:
                ptr = ptr.left
            else:
                ptr = ptr.right
        while ptr and (ptr.rank == newNode.rank) and (ptr.key < newNode.key):
            parent = ptr
            ptr = ptr.right
        (P, Q) = self.unzip(newNode, ptr)
        newNode.left = P
        newNode.right = Q
        if not parent:
            self.root = newNode
        else:
            if newNode.key < parent.key:
                parent.left = newNode
            else:
                parent.right = newNode

    def zip(self, node: Node):
        def zipup(P: Node, Q: Node):
            if not P:
                return Q
            if not Q:
                return P
            if Q.rank > P.rank:
                Q.left = zipup(P, Q.left)
                return Q
            else:
                P.right = zipup(P.right, Q)
                return P

        return zipup(node.left, node.right)

    def remove(self, key: KeyType):
        self.size -= 1
        ptr = self.root
        parent = None
        direction = None
        while ptr.key != key:
            if ptr.key < key:
                parent = ptr
                ptr = ptr.right
                direction = "right"
            else:
                parent = ptr
                ptr = ptr.left
                direction = "left"

        n = self.zip(ptr)
        if not parent:
            self.root = n
        else:
            if direction == "left":
                parent.left = n
            else:
                parent.right = n

    def find(self, key: KeyType) -> ValType:
        ptr = self.root
        while ptr:
            if key == ptr.key:
                return ptr.val
            elif key < ptr.key:
                ptr = ptr.left
            else:
                ptr = ptr.right
        return None

    def get_size(self) -> int:
        return self.size

    def get_height_of_node(self, n: Node) -> int:
        if not n:
            return -1
        hl = self.get_height_of_node(n.left)
        hr = self.get_height_of_node(n.right)
        return 1 + max(hl, hr)

    def get_height(self) -> int:
        return self.get_height_of_node(self.root)

    def get_depth(self, key: KeyType):
        d = 0
        ptr = self.root
        while ptr:
            if key == ptr.key:
                return d
            else:
                d += 1
                if key < ptr.key:
                    ptr = ptr.left
                else:
                    ptr = ptr.right

# feel free to define new classes/methods in addition to the above
# fill in the definitions of each required member function (above),
# and any additional member functions you define


from collections import defaultdict, Counter

def read_input(file_path: str) -> list[tuple[list[str], list[str]]]:
    patterns_displayed_pairs = []
    with open(file_path, 'r') as fp:
        for line in fp.readlines():
            patterns, displayed = line.split('|')
            patterns = patterns.strip().split()
            displayed = displayed.strip().split()
            patterns_displayed_pairs.append((patterns, displayed))
    return patterns_displayed_pairs

def invert_dict(d: dict) -> dict:
    invert_dict = defaultdict(set)
    for k, v in d.items():
        invert_dict[v].add(k)
    return invert_dict

def solve_display(
        patterns: list[str], freq_to_segments: dict[int, set[str]], neighbors_to_segments: dict[int, set[str]]
) -> dict[str, str]:
    bad_segment_to_freq = Counter(list(''.join(patterns)))
    freq_to_bad_segments = invert_dict(bad_segment_to_freq)

    bad_segment_to_segment = {}
    # non ambiguous segments
    for freq, segments in freq_to_segments.items():
        if len(segments) == 1:
            segment = next(iter(segments))
            bad_segment = freq_to_bad_segments[freq].pop()
            bad_segment_to_segment[bad_segment] = segment
            del freq_to_bad_segments[freq]

    # ambiguous segments
    bad_segment_to_neighbors = defaultdict(int)
    for pattern in patterns:
        for segment in pattern:
            bad_segment_to_neighbors[segment] += len(pattern) - 1

    segments_used = set(bad_segment_to_segment.values())
    for bad_segments in freq_to_bad_segments.values():
        for bad_segment in bad_segments:
            bad_segment_neighbors = bad_segment_to_neighbors[bad_segment]
            candidates = neighbors_to_segments[bad_segment_neighbors]
            segment = next(iter(candidates - segments_used), None)
            bad_segment_to_segment[bad_segment] = segment
            segments_used.add(segment)

    return bad_segment_to_segment

def transform_segments(displayed_num: str, bad_segment_to_segment: dict[str, str]) -> str:
    trasnformed = []
    for bad_segment in displayed_num:
        trasnformed.append(bad_segment_to_segment[bad_segment])
    return ''.join(sorted(trasnformed))

def calc_number(
        displayed_nums: list[str], bad_segment_to_segment: dict[str, str], segment_to_num: dict[str, int]
) -> int:
    number = 0
    for displayed_num in displayed_nums:
        displayed_num = transform_segments(displayed_num, bad_segment_to_segment)
        displayed_num = segment_to_num[displayed_num]
        number = (number * 10) + displayed_num
    return number

def solve_displays(
        patterns_displayed_pairs: list[tuple[list[str], list[str]]],
        segment_to_num: dict[str, int],
        freq_to_segments: dict[int, set[str]],
        neighbors_to_segments: dict[int, set[str]]
) -> list[int]:
    numbers = []
    for patterns, displayed_nums in patterns_displayed_pairs:
        bad_segment_to_segment = solve_display(patterns, freq_to_segments, neighbors_to_segments)
        number = calc_number(displayed_nums, bad_segment_to_segment, segment_to_num)
        numbers.append(number)
    return numbers

if __name__ == '__main__':

    patterns_displayed_pairs = read_input('input.txt')

    num_to_segments = {
        0: 'abcefg',
        1: 'cf',
        2: 'acdeg',
        3: 'acdfg',
        4: 'bcdf',
        5: 'abdfg',
        6: 'abdefg',
        7: 'acf',
        8: 'abcdefg',
        9: 'abcdfg'
    }

    segment_to_freq = defaultdict(int)
    segment_to_neighbors = defaultdict(int)
    for segments in num_to_segments.values():
        for segment in segments:
            segment_to_freq[segment] += 1
            segment_to_neighbors[segment] += len(segments) - 1
    freq_to_segments = invert_dict(segment_to_freq)
    neighbors_to_segments = invert_dict(segment_to_neighbors)
    segment_to_num = {segments: num for num, segments in num_to_segments.items()}

    numbers = solve_displays(patterns_displayed_pairs, segment_to_num, freq_to_segments, neighbors_to_segments)
    print(sum(numbers))


import datetime
from django.shortcuts import render, redirect
from .models import Request, Review
from .forms import AddRequest, AddUserForm
from django.contrib.auth.decorators import permission_required
from .forms import RegistrForm
from django.contrib.auth.decorators import login_required

# Create your views here.
def home(request):
    return render(request, 'home.html')

def add_request(request):

    if request.method == 'POST':

        form = AddRequest(request.POST, request.FILES)

        if form.is_valid():
            new_request = Request()
            new_request.date = datetime.datetime.now()
            new_request.name = form.cleaned_data['name']
            new_request.surname = form.cleaned_data['surname']
            new_request.email = form.cleaned_data['email']
            new_request.company = form.cleaned_data['company']
            new_request.message = form.cleaned_data['message']
            new_request.save()
            return redirect('message_success')

    elif request.method == "GET":
        form = AddRequest()

    return render(request, 'add_request.html', {'form': form})

def message(request):
    success = Request.objects.last
    viewed_message = request.session.get("viewed_message",[])
    return render(request, 'message_success.html', {'message':success})

@permission_required('cargo.add_review')
def add_review(request):

    if request.method == 'POST':
        new_form = AddUserForm(request.POST, request.FILES)
        if new_form.is_valid():
            review = new_form.save(commit=False)
            review.date = datetime.datetime.now()
            review.save()
            new_form.save_m2m()

            return redirect('reviews')

    elif request.method == "GET":
        new_form = AddUserForm()

    return render(request, 'add_review.html', {'new_form': new_form})

def reviews(request):
    all_reviews = Review.objects.all().order_by('-date')
    viewed_reviews = request.session.get("viewed_reviews", [])
    return render(request, 'reviews.html', {'all_reviews':  all_reviews, 'viewed_reviews': viewed_reviews})

def review(request, id):
    try:
        review = Review.objects.get(id=id)
        viewed_reviews = request.session.get("viewed_reviews", [])
        if id not in viewed_reviews:
            viewed_reviews.append(id)
        request.session['viewed_reviews'] = viewed_reviews
    except:
        review = ""
    return render(request, 'review.html', {'review': review})

# def message2(request):
#     success = Review.objects.all
#     viewed_message = request.session.get("message2",[])
#     return render(request, 'message.html', {'message': success })

def regist(request):
    # Массив для передачи данных шаблонны
    data = {}
    # Проверка что есть запрос POST
    if request.method == 'POST':
        # Создаём форму
        form = RegistrForm(request.POST)
        # Валидация данных из формы
        if form.is_valid():
            # Сохраняем пользователя
            form.save()

            # Передача формы к рендару
            data['form'] = form
            # Передача надписи, если прошло всё успешно
            data['res'] = "Всё прошло успешно"
            # Рендаринг страницы
            return render(request, 'registration/register_done.html', data)
    else: # Иначе
        # Создаём форму
        form = RegistrForm()
        # Передаём форму для рендеринга
        data['form'] = form
        # Рендаринг страницы
    return render(request, 'registration/register.html', data)

# def reviews(request):
#     all_reviews = Review.objects.all()
#     viewed_reviews = request.session.get("viewed_reviews", [])
#     return render(request, 'reviews.html', {' reviews':  all_reviews})
#
#                   # , 'viewed_reviews': viewed_reviews})
#
# def review(request, id):
#     try:
#         review = Review.objects.get(id=id)
#         viewed_reviews = request.session.get("viewed_reviews", [])
#         if id not in viewed_reviews:
#             viewed_reviews.append(id)
#         request.session['viewed_reviews'] = viewed_reviews
#     except:
#         review = ""
#     return render(request, 'review.html', {'review': review})


"""
Data-Store Models
and a couple things to basically automatically create an API from a Model
"""
from google.appengine.ext import ndb
import datetime
import json
import logging

def university_key(name):
	return ndb.Key("University", name)

def course_id(dept, number):
	return dept+str(number)

def course_key(key_id):
	return ndb.Key("Course", key_id)

def can_cache_courses_key():
	"""Only one key for list of courses"""
	return ndb.Key("CanCache", 1)

class CanCache(ndb.Model):
	""" Super brilliant Idea: Lots of things will change very infrequently, so store the date it was last updated.
	If it wasn't updated, then list is cached client side.
	The benefit of this is in drastically reduces Data-Store Reads
	Reduce DB Reads => Save Money
	"""
	updated_at = ndb.DateTimeProperty(auto_now=True)

class University(ndb.Model):
	"""Model for College/Universities"""
	full_name = ndb.StringProperty(required=True)
	abbrev = ndb.StringProperty(required=True)
	city = ndb.StringProperty()
	state = ndb.StringProperty()

	courses = ndb.KeyProperty(repeated=True)

	# Date-Time Properties
	created_at = ndb.DateTimeProperty(auto_now_add=True)
	updated_at = ndb.DateTimeProperty(auto_now=True)

class Course(ndb.Model):
	"""Model for Courses"""
	department = ndb.StringProperty()
	number = ndb.IntegerProperty()
	name = ndb.StringProperty(required=True)

	ratings = ndb.KeyProperty(repeated=True)

	# List of emails so that user can only rate once
	raters = ndb.StringProperty(repeated=True)

	# Date-Time Properties
	created_at = ndb.DateTimeProperty(auto_now_add=True)
	updated_at = ndb.DateTimeProperty(auto_now=True)

class Rating(ndb.Model):
	"""Model for Ratings"""
	easiness = ndb.IntegerProperty(required=True)
	recommend = ndb.IntegerProperty(required=True)
	interesting = ndb.IntegerProperty(required=True)

	grade = ndb.IntegerProperty()

	professor = ndb.StringProperty()
	comment = ndb.StringProperty()
	book = ndb.StringProperty();

	user_email = ndb.StringProperty()

	# list of user's emails => safe hacking/trouble
	likes = ndb.StringProperty(repeated=True)
	dislikes = ndb.StringProperty(repeated=True)

	# Date-Time Properties
	created_at = ndb.DateTimeProperty(auto_now_add=True)
	updated_at = ndb.DateTimeProperty(auto_now=True)

def encode_to_dict(obj):
	"""Encode a data-store model to a dictionary"""
	result = {}

	for attr in dir(obj):
		if not attr.startswith("_") and not callable(getattr(obj,attr)):
			value = getattr(obj, attr)
			if isinstance(value, datetime.datetime):
				value = encode_date_time(value)
			elif isinstance(value, ndb.key.Key):
				value = dict(kind=value.kind(), id=value.id())
			result[attr] = value

	return result

def encode_date_time(dt_obj):
	result = {}

	# get attributes, but only the int ones
	for attr in dir(dt_obj):
		if not attr.startswith("_") and not callable(getattr(dt_obj, attr)):
			value = getattr(dt_obj, attr)
			if isinstance(value, int):
				result[attr] = value

	return result

def prepare_body(obj, key_method):
	"""Takes a decoded JSON object and prepares it for the data-store"""
	for key in obj:
		val = obj[key]
		if isinstance(val, dict):
			if val.get('kind'): # is a key
				val = key_method(kind=val['kind'], ID=val['id'], body=None)
			elif val.get('year'): # is a date-time
				val = datetime.datetime(**val)
	return obj

def API_GET(model_type, key_method, request, kind):
	"""One annoyance: for get: use key=id"""
	result_list = []

	if len(request.params) == 0: # no params, so return all stored objects -> "list mode"
		qry = model_type.query()
		for model in qry.iter():
				result_list.append(encode_to_dict(model))

	else:

		for param in request.params:
			if param == 'key':
				key = key_method(KIND=kind, ID=request.get("key"), BODY=None)
				result_list.append(encode_to_dict(key.get()))
			else:
				qry = model_type.query(getattr(model_type, param) == request.get(param))
				for model in qry.iter():
					result_list.append(encode_to_dict(model))

	#remove duplicates
	unique = []
	seen_id = {}
	for result in result_list:
		ID = result["key"]["id"]
		if not seen_id.get(ID):
			unique.append(result)
			seen_id[ID] = True

	return unique

def API_POST(model_type, key_method, request, kind):
	"""Return Success, Data
	Data is either an error message or the created key"""
	body = json.loads(request.body)

	#key = key_method(kind=kind, body=body, ID=None)

	#if key.get():
	#	return False, "Key already exists"
		# make new object in datastore
	body = prepare_body(obj=body, key_method=key_method)
	model = model_type(**body)

		#model.key = key
	model.put()

	return True, "key"

def API_PUT(model_type, key_method, request, kind):
	"""One pitfall: Whatever attribute defines the unique key CAN be changed by this method;
	these methods I'm creating put the responsibilty on the front end to ensure that
	bad things do not happen
	Return Success, MSg"""

	body = json.loads(request.body)

	if not body.get('key'):
		return False, "Required Arg Key not found"

	key = key_method(kind=body['key']['kind'], ID=body['key']['id'], body=None)

	model = key.get()

	if not model:
		return False, "Object Not Found in DB"
	else:
		del body['key']

		body = prepare_body(obj=body, key_method=key_method)

		for attr in body:
			setattr(model, attr, body[attr])

		model.put()

		return True, "Success"

def API_DELETE(model_type, key_method, request, kind):
	"Takes a 'key' in the query params"
	if request.get('key'):
		ID = request.get('key')

		key = key_method(kind=kind, ID=ID, body=None)

		key.delete()

		return True, "Successfully Deleted"
	else:
		return False, "Object not found"

"""For Now I'm not using Body"""
def Key_Method(kind, ID, body):
	if isinstance(ID, str):
		ID = int(str)
	if kind == 'Task':
		return ndb.Key('Task', ID)
	elif kind == 'Task_Entry':
		return ndb.Key('Task_Entry', ID)


import os
import time
import argparse

import torch
import numpy as np
from PIL import Image
from torch.autograd import Variable
from torchvision import transforms

from nets import basic, depth_predciton, basic_NL, DGNLNet
from config import test_raincityscapes_path
from misc import check_mkdir
from skimage.metrics import mean_squared_error
from skimage.metrics import peak_signal_noise_ratio
from skimage.metrics import structural_similarity

from dataset3 import ImageFolder, make_dataset
from skimage.measure import label, regionprops
from jittor_code.utils import reverse_mapping, visulize_mapping, edge_align, get_boundary_point
from hungarian_matching import caculate_tp_fp_fn

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

torch.manual_seed(2019)
torch.cuda.set_device(0)

ckpt_path = './ckpt'
args = {
    'snapshot': '40000',
    'depth_snapshot': ''
}

transform = transforms.Compose([
    transforms.Resize([272,480]),
    transforms.ToTensor() ])

to_pil = transforms.ToPILImage()

def predict(exp_name, root_input, threshold):
    net = DGNLNet().cuda()

    if len(args['snapshot']) > 0:
        print('load snapshot \'%s\' for testing' % args['snapshot'])
        net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, args['snapshot'] + '.pth'),
                                       map_location=lambda storage, loc: storage.cuda(0)))

    net.eval()
    avg_time = 0

    save_path = os.path.join(ckpt_path, exp_name, '(%s) prediction_%s_%s' % (exp_name, root_input.split("/")[-1], args['snapshot']))
    check_mkdir(save_path)

    with torch.no_grad():
        img_list = [img_name for img_name in os.listdir(root_input)]

        for idx, img_name in enumerate(img_list):
            img = Image.open(os.path.join(root_input, img_name)).convert('RGB')
            w, h = img.size
            img_var = Variable(transform(img).unsqueeze(0)).cuda()

            start_time = time.time()

            res, key_points = net(img_var)

            torch.cuda.synchronize()

            avg_time = avg_time + time.time() - start_time
            print('predicting: %d / %d, avg_time: %.5f' % (idx + 1, len(img_list), avg_time/(idx+1)))
            result = transforms.Resize((h, w))(to_pil(res.data.squeeze(0).cpu()))
            result.save(os.path.join(save_path, img_name))

            ################### DHT ##########################
            key_points = torch.sigmoid(key_points)
            binary_kmap = key_points.squeeze().cpu().numpy() > threshold
            kmap_label = label(binary_kmap, connectivity=1)
            props = regionprops(kmap_label)
            plist = []
            for prop in props:
                plist.append(prop.centroid)

            size = (272, 480)
            b_points = reverse_mapping(plist, numAngle=100, numRho=100, size=(272, 480))
            scale_w = size[1] / 480
            scale_h = size[0] / 272
            for i in range(len(b_points)):
                y1 = int(np.round(b_points[i][0] * scale_h))
                x1 = int(np.round(b_points[i][1] * scale_w))
                y2 = int(np.round(b_points[i][2] * scale_h))
                x2 = int(np.round(b_points[i][3] * scale_w))
                if x1 == x2:
                    angle = -np.pi / 2
                else:
                    angle = np.arctan((y1-y2) / (x1-x2))
                (x1, y1), (x2, y2) = get_boundary_point(y1, x1, angle, size[0], size[1])
                b_points[i] = (y1, x1, y2, x2)

            vis = visulize_mapping(b_points, size[::-1], result)
            vis.save(os.path.join(save_path, img_name).replace(".png", "_dht.png"))

def evaluate(exp_name, root_input, threshold):
    net = DGNLNet().cuda()

    if len(args['snapshot']) > 0:
        print('load snapshot \'%s\' for testing' % args['snapshot'])
        net.load_state_dict(torch.load(os.path.join(ckpt_path, exp_name, args['snapshot'] + '.pth'),
                                       map_location=lambda storage, loc: storage.cuda(0)))

    net.eval()
    avg_time = 0

    with torch.no_grad():

        img_list = make_dataset(root_input, is_train=False)
        MSE, PSNR, SSIM = 0, 0, 0
        total_tp = np.zeros(99)
        total_fp = np.zeros(99)
        total_fn = np.zeros(99)

        for idx, path in enumerate(img_list):
            save_path = os.path.join(ckpt_path, exp_name, '(%s) prediction_%s' % (exp_name, args['snapshot']))
            check_mkdir(save_path)

            img_path, gt_path, data_path = path
            data = np.load(data_path, allow_pickle=True).item()
            gt_coords = data["coords"]
            img = Image.open(img_path).convert('RGB')
            w, h = img.size
            img_var = Variable(transform(img).unsqueeze(0)).cuda()

            start_time = time.time()

            res, key_points = net(img_var)

            torch.cuda.synchronize()

            avg_time = avg_time + time.time() - start_time
            print('predicting: %d / %d, avg_time: %.5f' % (idx + 1, len(img_list), avg_time/(idx+1)))

            ##################### Dehaze ########################
            result = transforms.Resize((h, w))(to_pil(res.data.squeeze(0).cpu()))
            result.save(os.path.join(save_path, img_path.split("/")[-1]))

            gt = Image.open(gt_path).convert('RGB')
            result1 = np.array(result)
            gt = np.array(gt)
#            print(result.shape, gt.shape)
            MSE += mean_squared_error(gt, result1)
            PSNR += peak_signal_noise_ratio(gt, result1)
            SSIM += structural_similarity(gt, result1, multichannel=True)

            ################### DHT ##########################
            key_points = torch.sigmoid(key_points)
            binary_kmap = key_points.squeeze().cpu().numpy() > threshold
            kmap_label = label(binary_kmap, connectivity=1)
            props = regionprops(kmap_label)
            plist = []
            for prop in props:
                plist.append(prop.centroid)
            size = (272, 480)
            b_points = reverse_mapping(plist, numAngle=100, numRho=100, size=(272, 480))

            for i in range(1, 100):
                tp, fp, fn = caculate_tp_fp_fn(b_points, gt_coords, thresh=i*0.01)
                total_tp[i-1] += tp
                total_fp[i-1] += fp
                total_fn[i-1] += fn

            scale_w = size[1] / 480
            scale_h = size[0] / 272
            for i in range(len(b_points)):
                y1 = int(np.round(b_points[i][0] * scale_h))
                x1 = int(np.round(b_points[i][1] * scale_w))
                y2 = int(np.round(b_points[i][2] * scale_h))
                x2 = int(np.round(b_points[i][3] * scale_w))
                if x1 == x2:
                    angle = -np.pi / 2
                else:
                    angle = np.arctan((y1-y2) / (x1-x2))
                (x1, y1), (x2, y2) = get_boundary_point(y1, x1, angle, size[0], size[1])
                b_points[i] = (y1, x1, y2, x2)
            vis = visulize_mapping(b_points, size[::-1], result)
            vis.save(os.path.join(save_path, img_path.split("/")[-1].replace(".png", "_dht.png")))

        total_recall = total_tp / (total_tp + total_fn + 1e-8)
        total_precision = total_tp / (total_tp + total_fp + 1e-8)
        f = 2 * total_recall * total_precision / (total_recall + total_precision + 1e-8)

        print('%s_%s:' % (exp_name, args['snapshot']))
        print("MSE: " + str(MSE / len(img_list)))
        print("PSNR: " + str(PSNR / len(img_list)))
        print("SSIM: " + str(SSIM / len(img_list)))
        print('total_precison: ' + str(total_precision.mean()))
        print('total_recall" '+ str(total_recall.mean()))
        print('f-measure@0.95: ' + str(f[95 - 1]))

        f = open("result.txt", "a")
        f.write('%s_%s:\n' % (exp_name, args['snapshot']))
        f.write("MSE: " + str(MSE / len(img_list)) + "\n")
        f.write("PSNR: " + str(PSNR / len(img_list)) + "\n")
        f.write("SSIM: " + str(SSIM / len(img_list)) + "\n")
        f.write("total_precison: " + str(total_precision.mean()) + "\n")
        f.write("total_recall: "+ str(total_recall.mean()) + "\n")
        f.write("f-measure@0.95: " + str(f[95 - 1]) + "\n")

        f.close()

def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--exp_name', type=str, default='DGNLNet', help='model name')
    parser.add_argument('--root_input', type=str, default='dataset/sewer', help='input dir')
    parser.add_argument('--val', action='store_true', help='if val')
    parser.add_argument('--threshold', type=float, default=0.01, help='for dht')
    opt = parser.parse_args()
    return opt

if __name__ == '__main__':
    opt = parse_opt()
    print(opt.val, opt.exp_name)
    if opt.val:
        evaluate(opt.exp_name, opt.root_input, opt.threshold)
    else:
        predict(opt.exp_name, opt.root_input, opt.threshold)


import csv
import json
import logging
import pickle
from datetime import date

import pymysql
from flask import Flask, request, jsonify, \
    send_from_directory
from flask_cors import CORS
from pymysql import cursors

logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.DEBUG,
    datefmt='%Y-%m-%d %H:%M:%S',
    filename='myapp.log',
    filemode='w')

app = Flask(__name__)
CORS(app)

def load_from_file(filename):
    with open(filename, 'rb') as f:
        url_list_divided = pickle.load(f)
    return url_list_divided

def load_from_json_file(filename):
    with open(filename, 'r') as f:
        url_list_json = json.load(f)
    return url_list_json

# these 2 objects are image-caption data to be annotated, saved by pickle.dump
# Format:
# [
#   [(caption_1, url_1, source_1), (caption_2, url_2, source_2), ...] # annotator 1
#   [(caption_1, url_1, source_1), (caption_2, url_2, source_2), ...] # annotator 2
#   ...
# ]
#{'id':[(caption, url, source),(caption, url, source), ...]}
url_list = load_from_file('url_list.pkl')
test_url_list = load_from_file('url_list_divided.pkl')

@app.route('/', methods=('GET', 'POST',))
def hello_world():
    return 'Hello World!'

@app.route('/login', methods=['POST', 'OPTIONS'])
def login():
    id_annotator = request.args.get('id_annotator')
    passwd_annotator = request.args.get('passwd_annotator')
    req_json = request.get_json()
    app.logger.info(req_json)
    if req_json:
        id_annotator = req_json['id_annotator']
        passwd_annotator = req_json['passwd_annotator']
    app.logger.info('{} {} is trying to log in'.format(id_annotator, passwd_annotator))

    if id_annotator and passwd_annotator:

        db = pymysql.connect('localhost', 'merterm', '1234567890', 'google_annotation')

        cursor = db.cursor(cursors.DictCursor)
        sql = "SELECT * FROM `annotator` WHERE id_annotator='" + id_annotator + "'"
        # sql = "SELECT `id_annotator`, `passwd_annotator` FROM `annotator` WHERE id_annotator='" + id_annotator + "'"
        app.logger.debug('Database query: %s', sql)
        cursor.execute(sql)
        data = cursor.fetchone()
        app.logger.debug('Database query result: {}'.format(data))
        db.close()

        # logged in
        if data and data['passwd_annotator'] == passwd_annotator:
            return jsonify({'code': 200, 'msg': 'logged in', 'token': id_annotator})

    return jsonify({'code': 400, 'msg': 'error'})

@app.route('/index', methods=['POST', 'OPTIONS'])
def index():
    token = request.headers.get('token')
    if not token:
        app.logger.debug('{} is trying to get batches.'.format(token))
        return jsonify({'code': 400, 'msg': 'error'})
    app.logger.debug('{} is trying to get batches.'.format(token))
    batches = []

    batches = []
    if token.isdigit():
        id_annotator = int(token)
        batches = url_list[id_annotator - 1]
    elif token == 'test':
        batches = test_url_list[0]
    else:
        return jsonify({'code': 400, 'msg': 'id error'})

    ret = list(range(1, len(batches) + 1))
    return jsonify({'code': 200, 'id_batches': ret})

@app.route('/getone', methods=['POST', 'OPTIONS'])
def get_one():
    token = request.headers.get('token')
    id_batch = request.headers.get('id_batch')
    id_image = request.headers.get('id_image')
    if not token or id_batch is None or id_image is None:
        app.logger.debug('{} {} {} is trying to get one.'.format(token, id_image, id_batch))
        app.logger.debug(request.headers)
        return jsonify({'code': 400, 'msg': 'error'})
    id_batch = int(id_batch) - 1
    id_image = int(id_image) - 1
    app.logger.debug('{} is trying to get image {} in batch {}.'.format(token, id_image, id_batch))
    batches = []

    batches = []
    if token.isdigit():
        id_annotator = int(token)
        batches = url_list[id_annotator - 1]
    elif token == 'test':
        batches = test_url_list[0]
    else:
        return jsonify({'code': 400, 'msg': 'id error'})

    if id_batch >= len(batches) or id_batch < 0:
        return jsonify({'code': 400, 'msg': 'bad id_batch'})
    if id_image >= len(batches[id_batch]) or id_image < 0:
        return jsonify({'code': 400, 'msg': 'bad id_image'})
    app.logger.debug('size sanity check: {} {} {}'.format(id_image, id_batch, len(batches[id_batch])))
    ret = batches[id_batch][id_image]
    app.logger.debug('returning {}'.format(ret))
    source = 'test' if token == 'test' else ret[2]
    return jsonify({'code': 200, 'caption': ret[0], 'url': ret[1], 'source': source})

@app.route('/export', methods=['POST', 'OPTIONS'])
def export():
    token = request.headers.get('token')
    if not token or token != 'admin':
        app.logger.info('{} is trying to export.'.format(token))
        app.logger.debug(request.headers)
        return jsonify({'code': 400, 'msg': 'error'})

    db = pymysql.connect('localhost', 'merterm', '1234567890', 'google_annotation')

    cursor = db.cursor()
    sql = "SELECT * FROM `annotation`"
    app.logger.debug('Database query: %s', sql)
    cursor.execute(sql)
    data = cursor.fetchall()
    # app.logger.debug('Database query result: {}'.format(data))

    date_today = date.today().strftime('%m%d')

    with open('output_{}.tsv'.format(date_today), 'w') as fp:
        writer = csv.writer(fp, delimiter='\t', lineterminator='\n')
        col_names = [col[0] for col in cursor.description]
        writer.writerow(col_names)
        writer.writerows(data)
    db.close()

    return jsonify({'code': 200, 'msg': 'success'})

@app.route('/download')
def download():
    token = request.headers.get('token')
    app.logger.info('{} is trying to download.'.format(token))
    if not token or token != 'admin':
        app.logger.debug(request.headers)
        return jsonify({'code': 400, 'msg': 'error'})
    date_today = date.today().strftime('%m%d')

    return send_from_directory('.', 'output_{}.tsv'.format(date_today), as_attachment=True)

@app.route('/rmdb')
def rmdb():
    token = request.headers.get('token')
    app.logger.info('{} is trying to download.'.format(token))
    if not token or token != 'admin':
        app.logger.debug(request.headers)
        return jsonify({'code': 400, 'msg': 'error'})

    db = pymysql.connect('localhost', 'merterm', '1234567890', 'google_annotation')

    cursor = db.cursor()
    sql = "delete FROM `annotation`"
    app.logger.debug('Database deletion: %s', sql)
    cursor.execute(sql)
    data = cursor.fetchall()
    app.logger.debug('Database deletion result: {}'.format(data))
    db.commit()
    db.close()
    return jsonify({'code': 200, 'msg': 'error'})

@app.route('/annotate', methods=['POST', 'OPTIONS'])
def annotate():
    app.logger.debug(request.headers)
    token = request.headers.get('token')
    id_batch = request.headers.get('id_batch')
    id_image = request.headers.get('id_image')
    url = request.headers.get('url')
    caption = request.headers.get('caption')
    jsdata = request.get_json()
    if not token or id_batch is None or id_image is None or not url or not caption or not jsdata:
        app.logger.debug('{} {} {} {} {} is trying to annotate.'.format(token, id_image, id_batch, url, caption))
        app.logger.debug(jsdata)
        return jsonify({'code': 400, 'msg': 'error'})
    app.logger.info('{} {} {} {} {} is trying to annotate.'.format(token, id_image, id_batch, url, caption))
    app.logger.debug(jsdata)
    id_batch = int(id_batch) - 1
    id_image = int(id_image) - 1

    batches = []
    if token.isdigit():
        id_annotator = int(token)
        batches = url_list[id_annotator - 1]
    elif token == 'test':
        batches = test_url_list[0]
    else:
        return jsonify({'code': 400, 'msg': 'id error'})

    if id_batch >= len(batches) or id_batch < 0:
        return jsonify({'code': 400, 'msg': 'bad id_batch'})
    if id_image >= len(batches[id_batch]) or id_image < 0:
        return jsonify({'code': 400, 'msg': 'bad id_image'})

    db = pymysql.connect('localhost', 'merterm', '1234567890', 'google_annotation')

    cursor = db.cursor(cursors.DictCursor)
    caption = caption.replace("'", "''")
    url = url.replace("'", "''")

    other = jsdata['other']
    other = other.replace("'", "''")
    other = other.replace("\r", "|")
    other = other.replace("\n", "|")

    values = "NULL, \'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\'".format(
        token, caption,
        url,
        jsdata['cb0'],
        jsdata['cb1'],
        jsdata['cb2'],
        jsdata['cb3'],
        jsdata['cb4'],
        jsdata['cb5'],
        jsdata['cb6'],
        other,
        jsdata['cbWhen'],
        jsdata['cbHow'],
        jsdata['cbWhere'],
        jsdata['Identification'],
        jsdata['source'],
        jsdata['cbBrokenImg'],
        jsdata['cbMetaGood'],
        jsdata['cbMetaBad'],
        jsdata['cbVisible1'],
        jsdata['cbVisible2'],
        jsdata['cbVisible3'],
        jsdata['cbVisible4'],
        jsdata['cbVisible5'],
        jsdata['cbSubjectiveGood'],
        jsdata['cbSubjectiveBad'],
        jsdata['cbStory1'],
        jsdata['cbStory2'],
        jsdata['cbStory3'],
        jsdata['cbStory4'],
        jsdata['cbStory5'],
    )
    sql = "INSERT INTO annotation () VALUES ({})".format(values)
    app.logger.debug('Database insertion: %s', sql)
    cursor.execute(sql)
    db.commit()
    db.close()

    id_image += 1
    if id_image >= len(batches[id_batch]):
        return jsonify({'code': 200, 'id_image': -1})
    else:
        return jsonify({'code': 200, 'id_image': id_image + 1})

@app.after_request
def after_request(response):
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

if __name__ == '__main__':
    app.run(debug=True)


from django.urls import path

from rooms.views.view_v1_amenity import AmenitiesListAPI, RoomAmenitiesAPI
from rooms.views.view_v1_room import (
    RoomCreateAPI,
    RoomCreatePhotosAPI,
    RoomDeleteAPI,
    RoomDetailAPI,
    RoomsListAPI,
    RoomUpdateAPI,
)

urlpatterns = [
    path("v1/list", RoomsListAPI.as_view()),
    path("v1/create", RoomCreateAPI.as_view()),
    path("v1/detail/<int:room_id>", RoomDetailAPI.as_view()),
    path("v1/update/<int:room_id>", RoomUpdateAPI.as_view()),
    path("v1/delete/<int:room_id>", RoomDeleteAPI.as_view()),
    path("v1/<int:room_id>/amenities", RoomAmenitiesAPI.as_view()),
    path("v1/<int:room_id>/photos", RoomCreatePhotosAPI.as_view()),
    path("v1/amenities/list", AmenitiesListAPI.as_view()),
]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
import datetime

# Create your views here.

def greetings_info(request):

    date = datetime.datetime.now()
    hours = datetime.datetime.now().strftime('%H')

    msg = 'Hello Guest very Good '

    if hours < 12 :
        msg = msg + " MORNING"

    elif hours < 16:
        msg = msg + " AFTERNOON"

    elif hours < 21:
        msg = msg + " EVENING"

    else:
        msg = msg+ " NIGHT"

    return render(request, 'displayapp/result.html', {'msg':msg,'date':date})


from pandac.PandaModules import *
loadPrcFile('config/Config.prc')

from direct.showbase.ShowBase import ShowBase
import Globals
import ToontownLoader
from dna.DNAStorage import DNAStorage

class Window(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)

        cbm = CullBinManager.getGlobalPtr()
        cbm.addBin('ground', CullBinManager.BTUnsorted, 18)
        cbm.addBin('shadow', CullBinManager.BTBackToFront, 19)
        cbm.addBin('gui-popup', CullBinManager.BTFixed, 60)

        camera.setPosHpr(0, 0, 0, 0, 0, 0)
        self.camLens.setMinFov(Globals.DefaultCameraFov / (4. / 3.))
        self.camLens.setNearFar(Globals.DefaultCameraNear, Globals.DefaultCameraFar)
        self.cam2d.node().setCameraMask(BitMask32.bit(1))

        # Keep VFS here to use files
        self.vfs = VirtualFileSystem.getGlobalPtr()
        for mount in Globals.mounts:
            self.vfs.mount("multifiles/" + mount, "/", 0)

        oldLoader = self.loader
        self.loader = ToontownLoader.ToontownLoader(self)
        __builtins__.loader = self.loader
        oldLoader.destroy()

        self.loadDnaStore()

        from hood.places.ToontownCentral import TTC
        self.ttc = TTC(None)
        self.ttc.load()
        self.ttc.startSky()

    def loadDnaStore(self):
        if not hasattr(self, 'dnaStore'):
            self.dnaStore = DNAStorage()

            self.loader.loadDNA('phase_4/dna/storage.xml').store(self.dnaStore)

            '''self.dnaStore.storeFont(Globals.getInterfaceFont(), 'humanist')
            self.dnaStore.storeFont(Globals.getSignFont(), 'mickey')
            self.dnaStore.storeFont(ToontownGlobals.getSuitFont(), 'suit')'''

            self.loader.loadDNA('phase_3.5/dna/storage_interior.xml').store(self.dnaStore)

    def setCurrentZone(self, zone):
        self.currentZone = zone

    def setLastPlayground(self, zone):
        self.lastPlayground = zone

T = Window()
T.run()


from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

app_name = 'core'

urlpatterns = [
    # path('jet/', include('jet.urls', 'jet')),  # Django JET URLS
    # path('jet/dashboard/', include('jet.dashboard.urls', 'jet-dashboard')),
    path('admin/', admin.site.urls),
    path('djrichtextfield/', include('djrichtextfield.urls')),
    path('basket/', include('basket.urls', namespace='basket')),
    path('account/', include('account.urls', namespace='account')),
    path('payment/', include('payment.urls', namespace='payment')),
    path('orders/', include('orders.urls', namespace='orders')),
    path('', include('main_store.urls', namespace='main_store')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import sys
sys.path.append('..')
import numpy as np
import matplotlib.pyplot as plt

from dataset import sequence
from common.optimizer import Adam
from common.trainer import Trainer
from common.util import eval_seq2seq
from common.seq2seq_layer import Seq2Seq

# 데이터 로드
(x_train, t_train), (x_test, t_test) = sequence.load_data('addition.txt', seed=1984)
char_to_id, id_to_char = sequence.get_vocab()

# 하이퍼파라미터
vocab_size = len(char_to_id)
wordvec_size = 16
hidden_size = 128
batch_size = 128
max_epoch = 25
max_grad = 5.0

# model, optimizer, Trainer 클래스
model = Seq2Seq(vocab_size, wordvec_size, hidden_size)
optimizer = Adam()
trainer = Trainer(model, optimizer)

acc_list = []
for epoch in range(max_epoch):
    trainer.fit(x_train, t_train, max_epochs=1, batch_size=batch_size, max_grad=max_grad)

    correct_num = 0
    for i in range(len(x_test)):
        question, correct = x_test[[i]], t_test[[i]]  # 1d-array로 반환
        verbose = i < 10
        correct_num += eval_seq2seq(model, question, correct, id_to_char, verbose)

    acc = float(correct_num) / len(x_test)
    acc_list.append(acc)
    print('검증 정확도: %.3f%%' % (acc * 100))


import re
import requests
from bs4 import BeautifulSoup

def create_soup(url):
    res = requests.get(url)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, "lxml")
    return soup

def print_news(index, title, link):
    print("{}. {}".format(index+1, title))
    print(" (링크: {})".format(link))
    print()

def scrape_weather():
    print("[오늘의 날씨]")
    url = "https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%EC%84%9C%EC%9A%B8+%EB%82%A0%EC%94%A8"
    res = requests.get(url)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, "lxml")

    curr_temp = soup.find(
        "div", attrs={"class": "temperature_text"}).get_text()

    min_temp = soup.find("span", attrs={"class": "lowest"}).get_text()
    max_temp = soup.find("span", attrs={"class": "highest"}).get_text()

    rainfall = soup.find("div", attrs={"class": "cell_weather"})
    morning_rainfall = rainfall.find_all("span", attrs={"class": "rainfall"})[
        0].get_text()
    afternoon_rainfall = rainfall.find_all(
        "span", attrs={"class": "rainfall"})[1].get_text()

    # morning_rain_rate = soup.find(
    #     "span", attrs={"class": "weather_left"}, text).get_text().strip()
    # afternoon_rain_rate = soup.find(
    #     "span", attrs={"class": "point_time afternoon"}).get_text().strip()

    dust = soup.find("ul", attrs={"class": "today_chart_list"})
    pm10 = dust.find_all("li")[0].get_text()
    pm25 = dust.find_all("li")[1].get_text()

    print(" {} ({} / {})".format(curr_temp, min_temp, max_temp))
    print("  강수확률 오전 {} / 오후 {}".format(morning_rainfall, afternoon_rainfall))
    print()
    print("{}".format(pm10))
    print("{}".format(pm25))
    print()

def scrape_headline_news():
    print("[헤드라인 뉴스]")
    url = "https://news.daum.net/"
    soup = create_soup(url)

    news_list = soup.find(
        "ul", attrs={"class": "list_newsissue"}).find_all("div", attrs={"class": "cont_thumb"}, limit=5)

    for index, news in enumerate(news_list):
        title = news.find("a").get_text().strip()
        link = url + news.find("a")["href"]
        print_news(index, title, link)

def scrape_it_news():
    print("[IT 뉴스]")
    url = "https://news.daum.net/digital#1"
    soup = create_soup(url)
    itnews_list = soup.find(
        "ul", attrs={"class": "list_newsmajor"}).find_all("li", limit=5)
    for index, news in enumerate(itnews_list):

        # 만일 img 태그 등 중간에 삽입된 태그가 있으면 [0]이 아닌 [1]번째 a 태그 정보를 사용하려면
        #a_idx = 0
        #img = news.find("img")
        # if img:
        #a_idx = 1
        #a_tag = news.find_all("a")[a_idx]
        #title = a_tag.get_text().strip()
        #link = a_tag["href"]

        title = news.find("a").get_text().strip()
        link = url + news.find("a")["href"]
        print_news(index, title, link)
    print()

# def scrape_english():
#     print("[오늘의 영어 회화]")
#     url = "https://www.hackers.co.kr/?c=s_eng/eng_contents/I_others_english&keywd=haceng_submain_lnb_eng_I_others_english&logger_kw=haceng_submain_lnb_eng_I_others_english"
#     soup = create_soup(url)
#     sentences = soup.find_all("div", attrs={"id": re.compile("^conv_kor_t")})

#     print("<한글지문>")
#     for sentence in sentences[:len(sentences)//2]:
#         print(sentence.get_text().strip())
#     print()

#     print("<영어지문>")
#     for sentence in sentences[len(sentences)//2:]:
#         print(sentence.get_text().strip())
#     print()

# if __name__ == "__main__":
#     scrape_weather()
#     scrape_headline_news()
#     scrape_it_news()
#     scrape_english()


import names

def binser(name: "list", sernam):
    alphabets = "abcdefghijklmnopqrstuvwxyz"
    alphabetk = {}
    number = 0
    sernum = []
    for x in list(alphabets):
        alphabetk[number] = x
        number += 1
    # print(alphabetk)
    for x in sernam:
        for num, sim in alphabetk.items():
            if x == sim:
                sernum.append(str(num))
                # print(sernum)
    name.sort()
    low = 0
    high = len(name)-1
    cy = 0
    while low <= high:
        cy += 1
        mid = (low + high)
        guesna = name[mid]
        # print(guesna)
        guesnu = []
        for x in guesna:
            for num, sim in alphabetk.items():
                # print(x, sim)
                if str(x) == str(sim):
                    guesnu.append(str(num))
                    # print(guesnu, sernum)
        if guesnu == sernum:
            return mid, cy
        elif guesnu > sernum:
            high = mid - 1
        else:
            low = mid + 1

name = []
for x in range(0, 128):
    name.append(names.get_first_name().lower())
print(name)
sernam = input("Введите имя: ")
try:
    fina, cy = binser(name, sernam)
except TypeError:
    print(None)
    quit()
print(name[fina], fina, cy)


import os
import click
import responses
from cloudscale import Cloudscale, CloudscaleApiException, CloudscaleException, CLOUDSCALE_API_ENDPOINT
from cloudscale.cli import cli
from click.testing import CliRunner
from configparser import ConfigParser
from ..util import tags_to_dict

class TestClass():

    CLOUDSCALE_CONFIG = os.getenv('CLOUDSCALE_CONFIG')

    def setup_class(self):
        config = ConfigParser()
        config.update({
            'test': {
                'api_token': 'test_token',
            },
            'default': {
                'api_token': 'default_token',
            },
        })

        # TODO: better solution?
        if self.CLOUDSCALE_CONFIG and self.CLOUDSCALE_CONFIG.startswith('/tmp/'):
            with open(self.CLOUDSCALE_CONFIG, 'w') as configfile:    # save
                config.write(configfile)

    def test_config_ini(self):
        if self.CLOUDSCALE_CONFIG:

            cloudscale = Cloudscale(profile="test")
            assert cloudscale.api_token == "test_token"

            cloudscale = Cloudscale()
            assert cloudscale.api_token == "default_token"

            try:
                cloudscale = Cloudscale(profile="does-not-exist")
            except CloudscaleException as e:
                assert str(e).startswith("Profile 'does-not-exist' not found in config files:")

    def teardown_class(self):
        if os.path.exists(self.CLOUDSCALE_CONFIG):
            os.remove(self.CLOUDSCALE_CONFIG)

def test_missing_api_key():
    try:
        cloudscale = Cloudscale(api_token=None)
    except CloudscaleException as e:
        assert str(e) == "Missing API key: see -h for help"

def test_mutually_exclusvie_api_key_and_profile():
    try:
        cloudscale = Cloudscale(api_token="token", profile="test")
    except CloudscaleException as e:
        assert str(e) == "API token and profile are mutually exclusive"

@responses.activate
def test_exception_returns():
    responses.add(
        responses.GET,
        CLOUDSCALE_API_ENDPOINT + '/objects-users/unknown',
        json={
            "detail": "Not found."
        },
        status=404)
    responses.add(
        responses.GET,
        CLOUDSCALE_API_ENDPOINT + '/objects-users/unknown',
        json={},
        status=500)

    try:
        cloudscale = Cloudscale(api_token="token")
        cloudscale.objects_user.get_by_uuid(uuid="unknown")
    except CloudscaleApiException as e:
        assert e.status_code == 404
        assert str(e) == "API Response Error (404): Not found."
        assert e.response == {'data': {'detail': 'Not found.'}, 'status_code': 404}
    try:
        cloudscale = Cloudscale(api_token="token")
        cloudscale.objects_user.get_by_uuid(uuid="unknown")
    except CloudscaleApiException as e:
        assert e.status_code == 500
        assert e.response == {'data': {}, 'status_code': 500}
        assert str(e) == "API Response Error (500): {}"

def test_not_implemented():
    try:
        cloudscale = Cloudscale(api_token="token")
        cloudscale.foobar.get_by_uuid(uuid="unknown")
    except NotImplementedError as e:
        assert str(e) == "'foobar' not implemented"

def test_list_commands():
    runner = CliRunner()
    result = runner.invoke(cli, [
    ])
    assert result.exit_code == 0