import json
import logging
from pprint import pprint

from etas import set_up_logger
from etas.inversion import ETASParameterCalculation

set_up_logger(level=logging.DEBUG)

logger = logging.getLogger(__name__)

if __name__ == '__main__':

    # reads configuration for example ETAS parameter inversion
    with open('../config/ch_forecast_config.json', 'r') as f:
        forecast_config = json.load(f)

    etas_invert = ETASParameterCalculation(forecast_config)

    etas_invert.prepare()

    theta = etas_invert.invert()

    etas_invert.store_results(forecast_config['data_path'], True, True)

    pprint(etas_invert.__dict__)

    with open('../output_data/parameters_ch.json', 'r') as f:
        forecast_config_reload = json.load(f)

    etas_reload = ETASParameterCalculation.load_calculation(
        forecast_config_reload)

    pprint(etas_reload.__dict__)


from datetime import date
atual = date.today().year
total_maior= 0
total_menor = 0
for i in range(1, 8):
    nasc = int(input('Em que ano a {}° pessoa nasceu? '.format(i)))
    idade = atual - nasc
    if idade >= 21:
        total_menor += 1
    else:
        total_maior += 1
print('Ao todo, temos {} maiores de idade'.format(total_maior))
print('Ao todo, temos {} menores de idade'.format(total_menor))


import pandas as pd

from utils.scrape import webscrape
from utils.general import data_structure_format

class privateproperty:
    '''
    This class is able to pull all relevant data from the privateproperty.co.za website
    '''

    def get_area_links(self):

        '''
        :return: dictionary with all area links of website
        '''
        area_dict = dict()
        provinces = {'eastern-cape': '7',
                     'free-state': '6',
                     'gauteng': '3',
                     'kwazulu-natal': '2',
                     'limpopo': '8',
                     'mpumalanga': '10',
                     'northern-cape': '5',
                     'north-west': '9',
                     'western-cape': '4'}

        area_container = {'div': 'contentHolder'}

        for province in provinces:
            province_url = f'https://www.privateproperty.co.za/for-sale/{province}/{provinces[province]}'
            province_object = webscrape(province_url, area_container)

            items = province_object.scraper_items()
            try:
                areas = items[1].find_all('a', href=True)
                area_list = ['https://www.privateproperty.co.za' + i['href'] for i in areas]
                area_dict[province] = area_list
            except:
                print(f'{province} is tikking')

        return area_dict

    def listing_aggregations(self, area_dict: dict):

        final_df = pd.DataFrame()

        container = {'a': "listingResult row"}
        data = {'title': 'div',
                'priceDescription': 'div',
                'propertyType': 'div',
                'suburb': 'div',
                'address': 'div'
                }
        index_container = {'div': 'pageNumbers'}

        for province in area_dict:
            for permutation in area_dict[province]:
                permutation_items = webscrape(permutation, index_container).scraper_items()

                if len(permutation_items) > 0:
                    links = permutation_items[0].find_all('a', href=True)
                    try:
                        if len(links) == 0:
                            max_index = 1
                        else:
                            max_index_link = links[-2]['href']
                            max_index = int(max_index_link.split('=')[1])

                        for index in range(1, max_index + 1):
                            print(province, permutation, index)
                            if index == 1:
                                url = permutation
                            else:
                                url = f'{permutation}?page={index}'
                            obj = webscrape(url, container)
                            obj_dict = obj.scraper_dict(data)
                            frame = data_structure_format().nest_dict_to_dataframe(obj_dict)
                            final_df = pd.concat([final_df, frame], ignore_index=True)
                            print(len(final_df))
                    except:
                        print(f'{permutation} is tikking')
                else:
                    continue

        return final_df


import requests
from bs4 import BeautifulSoup
import concurrent.futures
from requests.exceptions import ConnectTimeout, ReadTimeout, ProxyError
import csv
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print(os.getcwd())

# Check status code

time = []
list_up = []
list_timeout = []
list_down = []
timeout = 60

def read_file(file_name):
    results = []
    with open(file_name, 'r', encoding='utf-8') as file:
        content = file.readlines()
    for line in content:
        results.append(line.strip())
    return results

def check_status_code(list_url):
    for url in list_url:
        try:
            response = requests.get(url=url, timeout=timeout)
            if response.status_code in range(100, 400):
                list_up.append(url)
            elif response.status_code in range(400, 600):
                list_down.append(url)
        except (ConnectTimeout, ReadTimeout):
            list_timeout.append(url)

def show_state():
    for url in list_up:
        print('{} is up'.format(url))
    for url in list_down:
        print('{} is down'.format(url))
    for url in list_timeout:
        print('{} is timeout'.format(url))

# Check content tittle

def read_target_title():
    my_dict = {}
    with open('targets.txt', 'r', encoding='utf-8') as file:
        # Read each line of the file
        contents = file.readlines()
        for i in range(len(contents)):
            if i % 2 == 0:
                key = contents[i].strip()
            elif i%2 != 0:
                my_dict[key] = contents[i].strip()
    return my_dict

def check_content_title(targets):
    for url in targets.keys():
        response = requests.get(url=url, timeout=timeout)
        soup = BeautifulSoup(response.text, 'html.parser')
        title = soup.title.string
        if title == targets[url]:
            print("{} same title".format(url))
        elif title != targets[url]:
            print("{} change title".format(url))

def open_file_proxy():
    proxylist = []
    portlist = []
    with open('proxylist.csv', 'r') as file:
        reader = csv.reader(file)
        for row in reader:
            proxylist.append(row[0])
            portlist.append(row[1])
    return proxylist, portlist

def extract(proxy, port):
    try:
        response = requests.get(url='https://ipinfo.io', timeout=3, proxies={'http':proxy, 'https':proxy})
        print("{} is working".format(proxy))
    except (ProxyError):
        pass

    # print('debug end')

# Change IP

if __name__ == '__main__':
    proxylist, portlist = open_file_proxy()
    with concurrent.futures.ThreadPoolExecutor() as exector:
        exector.map(extract, proxylist, portlist)


import unittest
import flask_testing
import json
from Skills import Skill
from app import app, db

class TestApp(flask_testing.TestCase):
    app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite://"
    app.config['TESTING'] = True

    def create_app(self):
        return app

    def setUp(self):
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()

# tests for create_a_skill
class TestCreateSkill(TestApp):
    # test for create a skill work as intended
    def test_create_skill(self):
        request_body = {
            "Skill_ID": "LE01",
            "Skill_Name": "Leadership Skill",
            "Skill_Desc": "How to be a leader"
        }

        response = self.client.post("/skill/create",
                                    data=json.dumps(request_body),
                                    content_type='application/json')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json, {
            "code": 200,
            "data": {
                "Skill_ID": "LE01",
                "Skill_Name": "Leadership Skill",
                "Skill_Desc": "How to be a leader",
                "Skill_Status": None
            }
        })

    # test for creating skill with existing id will return error
    def test_create_invalid_skill_with_same_id(self):
        s1 = Skill("LE01", "Leadership Skill", "How to be a leader")
        db.session.add(s1)
        db.session.commit()

        request_body = {
            "Skill_ID": "LE01",
            "Skill_Name": "Testing Name",
            "Skill_Desc": "Testing Desc"
        }

        response = self.client.post("/skill/create",
                                    data=json.dumps(request_body),
                                    content_type='application/json')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json, {
            "code": 400,
            "data": {
                "skillId": "LE01"
            },
            "message": "Skill ID already exists."
        })

    # test for creating skill with existing name will return error
    def test_create_invalid_skill_with_same_name(self):
        s1 = Skill("LE01", "Leadership Skill", "How to be a leader")
        db.session.add(s1)
        db.session.commit()

        request_body = {
            "Skill_ID": "TT200",
            "Skill_Name": "Leadership Skill",
            "Skill_Desc": "Testing Desc"
        }

        response = self.client.post("/skill/create",
                                    data=json.dumps(request_body),
                                    content_type='application/json')
        self.assertEqual(response.status_code, 400)
        self.assertEqual(response.json, {
            "code": 400,
            "data": {
                "skillname": "Leadership Skill"
            },
            "message": "Skill Name already exists."
        })

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


#!/usr/bin/env python

import os

def sample(dir, max_id, fout_name):
		## Read the file and keep only ids lower than max_id
		graphfile = os.path.join(dir, "twitter" + ".csv")
		f_out = open(os.path.join(dir, fout_name), "w")
		with open(graphfile) as f:
			lines = f.readlines()
		for line in lines:
			node_fro = int(line.split(',')[0])
			node_to = int(line.split(',')[1])
			if node_fro <= max_id and node_to <= max_id:
				f_out.write(line)
		f_out.close()

if __name__ == "__main__":
	sample(".", 8000, "twitter_small.csv")
	sample(".", 80000, "twitter_large.csv")


from django.contrib.auth.decorators import login_required
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.shortcuts import get_object_or_404
from django.shortcuts import render

from deployments.models import Deployment, Profile
from servers.models import Server

@login_required
def deployment(request, pk, fqdn, profile):
    deployment = get_object_or_404(
        Deployment,
        pk=pk,
        server__fqdn=fqdn,
        profile__name=profile,
        is_active=True)

    return render(
        request,
        'webui/deployments/deployment.html.j2',
        context={
            'view': 'deployments',
            'subview': 'deployments',
            'deployment': deployment,
        })

@login_required
def deployments(request):
    deployments = Deployment.objects.filter(is_active=True)
    servers = Server.objects.filter(is_active=True)
    profiles = Profile.objects.filter(is_active=True)

    paginator = Paginator(deployments, 15)

    try:
        pages = paginator.page(request.GET.get('page'))
    except PageNotAnInteger:
        pages = paginator.page(1)
    except EmptyPage:
        pages = paginator.page(paginator.num_pages)

    return render(
        request,
        'webui/deployments/deployments.html.j2',
        context={
            'view': 'deployments',
            'subview': 'deployments',
            'deployments': pages,
            'servers': servers,
            'profiles': profiles,
        })

@login_required
def profile(request, name, version):
    profile = get_object_or_404(
        Profile,
        name=name,
        version=version,
        is_active=True)

    return render(
        request,
        'webui/deployments/profile.html.j2',
        context={
            'view': 'deployments',
            'subview': 'profiles',
            'profile': profile,
        })

@login_required
def profiles(request):
    profiles = Profile.objects.filter(is_active=True)

    return render(
        request,
        'webui/deployments/profiles.html.j2',
        context={
            'view': 'deployments',
            'subview': 'profiles',
            'profiles': profiles,
        })


import argparse
from sys import platform
import numpy as np
import os
import sys

from models import *  # set ONNX_EXPORT in models.py
root = os.getcwd()
yolo_path = os.path.join(root,'detector','yolov3')
sys.path.append(yolo_path)
print(root,yolo_path)
from utils.datasets import *
from utils.utils import *

def detect(save_txt=True, save_img=False, stream_img=False):
    img_size = (320, 192) if ONNX_EXPORT else opt.img_size  # (320, 192) or (416, 256) or (608, 352) for (height, width)
    out, source, weights, half = opt.output, opt.source, opt.weights, opt.half
    webcam = source == '0' or source.startswith('rtsp') or source.startswith('http')
    streams = 'streams' in source and source.endswith('.txt')
    sep_sign = os.sep
    #print('seperation sign is ',os.sep)
    save_res = []
    # Initialize
    device = torch_utils.select_device(device='cpu' if ONNX_EXPORT else opt.device)
    #device = torch_utils.select_device(device='cpu')

    # device = torch_utils.select_device(device='cpu')
    if os.path.exists(out):
        shutil.rmtree(out)  # delete output folder
    os.makedirs(out)  # make new output folder

    # Initialize model
    model = Darknet(opt.cfg, img_size)

    # Load weights
    if weights.endswith('.pt'):  # pytorch format
        model.load_state_dict(torch.load(weights, map_location=device)['model'])
    else:  # darknet format
        _ = load_darknet_weights(model, weights)

    # Fuse Conv2d + BatchNorm2d layers
    # model.fuse()

    # Eval mode
    model.to(device).eval()
    # Export mode
    if ONNX_EXPORT:
        img = torch.zeros((1, 3) + img_size)  # (1, 3, 320, 192)
        torch.onnx.export(model, img, 'weights/export.onnx', verbose=True)
        return

    # Half precision
    half = half and device.type != 'cpu'  # half precision only supported on CUDA
    if half:
        model.half()

    # Set Dataloader
    vid_path, vid_writer = None, None
    if streams:
        stream_img = False
        torch.backends.cudnn.benchmark = True  # set True to speed up constant image size inference
        dataset = LoadStreams(source, img_size=img_size, half=half)
    elif webcam:
        stream_img = True
        dataset = LoadWebcam(source, img_size=img_size, half=half)
    else:
        save_img = save_img
        print("if source exits",os.path.exists(source))
        dataset = LoadImages(source, img_size=img_size, half=half)

    # Get classes and colors
    classes = load_classes(parse_data_cfg(opt.data)['names'])
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(classes))]

    # Run inference
    t0 = time.time()
    for path, img, im0s, vid_cap in dataset:

        t = time.time()

        # Get detections
        img = torch.from_numpy(img).to(device)
        if img.ndimension() == 3:
            img = img.unsqueeze(0)
        pred, _ = model(img)

        for i, det in enumerate(non_max_suppression(pred, opt.conf_thres, opt.nms_thres)):  # detections per image
            if streams:  # batch_size > 1
                p, s, im0 = path[i], '%g: ' % i, im0s[i]
            else:
                p, s, im0 = path, '', im0s
                print('path',path)
                im_name,_ = path.split('.')
                print(im_name)
                _,_,video_name,_,im_num = im_name.split(sep_sign)
                print('im_number is {}'.format(im_num))
            print(p)

            save_path = str(Path(out) / Path(p).name)
            s += '%gx%g ' % img.shape[2:]  # print string
            if det is not None and len(det):
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()

                # Print results
                for c in det[:, -1].unique():
                    n = (det[:, -1] == c).sum()  # detections per class
                    s += '%g %ss, ' % (n, classes[int(c)])  # add to string

                # Write results
                for *xyxy, conf, _, cls in det:
                    if save_txt:  # Write to file
                        # save_res.append([int(im_num),-1,*xyxy,conf,-1,-1,-1])
                        #with open(save_path + '.txt', 'a') as file:
                            #file.write(('%g ' * 6 + '\n') % (*xyxy, cls, conf))
                        with open(os.path.join(out, 'det.txt'),'a') as f:
                            xyxy[2] = xyxy[2] - xyxy[0]
                            xyxy[3] = xyxy[3] - xyxy[1]
                            f.write(('%g ' * 10 + '\n') % (int(im_num),-1,*xyxy,conf,-1,-1,-1))
                            #f.write(('%g ' * 9 + '\n') % ( -1, *xyxy, conf, -1, -1, -1))

                    if save_img or stream_img:  # Add bbox to image
                        label = '%s %.2f' % (classes[int(cls)], conf)
                        plot_one_box(xyxy, im0, label=label, color=colors[int(cls)])
            print('%sDone. (%.3fs)' % (s, time.time() - t))

            #if save_txt:
            #    pass#np.savetxt(os.path.join(out, "det.txt"), save_res, fmt="%d,%d,%.3f,%.3f,%.3f,%.3f,%.4f,%d,%d,%d")
            # Stream results
            if stream_img:
                cv2.imshow(p, im0)

            # Save results (image with detections)
            if save_img:
                if dataset.mode == 'images':
                    cv2.imwrite(save_path, im0)
                else:
                    if vid_path != save_path:  # new video
                        vid_path = save_path
                        if isinstance(vid_writer, cv2.VideoWriter):
                            vid_writer.release()  # release previous video writer

                        fps = vid_cap.get(cv2.CAP_PROP_FPS)
                        w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
                        h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h))
                    vid_writer.write(im0)

    if save_txt or save_img:
        print('Results saved to %s' % os.getcwd() + os.sep + out)
        if platform == 'darwin':  # MacOS
            os.system('open ' + out + ' ' + save_path)

    print('Done. (%.3fs)' % (time.time() - t0))

    if save_txt:
        save_form = np.loadtxt(os.path.join(out, 'det.txt'))
        np.savetxt(os.path.join(out, 'det.txt'),save_form,fmt="%d,%d,%.3f,%.3f,%.3f,%.3f,%.4f,%d,%d,%d")

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--cfg', type=str, default=yolo_path+'/'+'cfg/yolov3-spp-1cls.cfg', help='cfg file path')
    parser.add_argument('--data', type=str, default=yolo_path+'/'+'citypersons.data', help='citypersons.data file path')
    parser.add_argument('--weights', type=str, default='weights/yolo_weights.pt', help='path to weights file')
    #parser.add_argument('--weights', type=str, default='weights/last.pt', help='path to weights file')

    parser.add_argument('--source', type=str, default='data/samples', help='source')  # input file/folder, 0 for webcam
    parser.add_argument('--output', type=str, default='output', help='output folder')  # output folder
    parser.add_argument('--img-size', type=int, default=1920, help='inference size (pixels)')
    parser.add_argument('--conf-thres', type=float, default=0.3, help='object confidence threshold')
    parser.add_argument('--nms-thres', type=float, default=0.2, help='iou threshold for non-maximum suppression')
    parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)')
    parser.add_argument('--half', action='store_true', help='half precision FP16 inference')
    parser.add_argument('--device', default='', help='device id (i.e. 0 or 0,1) or cpu')
    parser.add_argument('--save_txt',action='store_true',help='determine whether to save txt for det result')
    parser.add_argument('--save_img',action='store_true',help='determine whether to save txt for det result')
    opt = parser.parse_args()
    print(opt)

    with torch.no_grad():
        detect(opt.save_txt,opt.save_img)


from flask import Flask
app = Flask(__name__)
import config
from werkzeug.routing import BaseConverter

class LeagueConverter(BaseConverter):
    def __init__(self, url_map):
        super(LeagueConverter, self).__init__(url_map)
        self.regex = '(?:fleaflicker|myfantasyleague|yahoo)'

app.url_map.converters['league'] = LeagueConverter

app.debug = config.mode == "development"
app.config["SQLALCHEMY_DATABASE_URI"] = config.db
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SERVER_NAME"] = config.serverName
app.config["PREFERRED_URL_SCHEME"] = config.preferredScheme
app.config["UPLOAD_FOLDER"] = config.uploadFolder


# 一女子择偶,两个男士被选择
# 1.如果两男存款少于100万,都不考虑
# 2.如果两男存款大于1000万,跟存款多的走
# 3.男士
# 4.选择个子高的
# 5.若果个子都大于170,且相差不到5厘米,看颜值(0,10)
# 7.低于7分的直接pass,高于7分选择颜值高的,若果颜值相差不到2,看房子
# 8.房子不能低于100平且价值不能少于200万,都有的话,选择价值高的
#     价值相差少于20万,看车子
# 9.车子价值不能少于50万,相差不到10万,抛硬币
#
# 使用继承
# 传入两个对象
# def 抛硬币()

import random
class Human(object):
    def __init__(self,name='',sex=True,height = 0):
        self.name = name
        self.sex = sex
        self.height = height

    def sexCunkuan(self,person1,person2):
        if person1.sex == person2.sex ==False or (person1.cunkuan and person2.cunkuan) in range(1000000):
            return 0
        if person1.sex ==person2.sex ==True:
            if (person1.cunkuan and person2.cunkuan) in range(1000000,10000001):
                return 1
            if (person1.cunkuan and person2.cunkuan) in range(10000000,):
                if person1.cunkuan>person2.cunkuan:
                    print('选择{}',format(person1.name))
                    return 2
                if person1.cunkuan<person2.cunkuan:
                    print('选择{}',format(person2.name))
                else:
                    return 1
            if person1.cunkuan>1000000 and person2<100000:
                return 2
            if person1.cunkuan<1000000 and person2>1000000:
                return 3
        if person1.sex == True and person2.sex == False:
            if person1.cunkuan in range(1000000,):
                return 2
            else:
                return 0

        if person1.sex == False and person2.sex == True:
            if person2.cunkuan in range(1000000,):
                return 2
            else:
                return 0
        else:
            return 3

    def isHeight(self,person1,person2):
        if ((person1.height and person2.height)<=170 and person1.height == person2.height) or ((person1.height and person2.height)>170 and abs(person1.height-person2.height)<=5):
            return 1
        if person1.height> person2.height:
            return 0
        if person1.height < person2.height:
            return 2

    def isPerfect(self,person1,person2):
        if (person1.perfect and person2.perfect) <7:
            return 0  #pass
        else:
            if abs(person1.perfect-person2.perfect)<2:
                return 1  #接着判断
            if person1.perfect>person2.perfect:
                return 0 #选择person1
            if person1.perfect<person2.perfect:
                return 2 #选择person2

    def isHouse(self,person1,person2):
        if (person1.house and person2.house)>100 and (person1.house_price and person2.house_price)>=2000000:
            if abs(person1.house_price-person2.house_price)<200000:
                return 0 #接着走
            if person1.house_price>person2.house_price:
                return -1 #选择person1
            if person1.house_price<person2.house_price:
                return 1 #选择person2
        if person1.house<100 and person2.house<100:
            print('你们房子都不行')
            return 2 #pass
        if person1.house>100 and person2.house<100:
            if person1.house_price>=2000000:
                return -1
            if person1.house_price<2000000:
                return 2 #pass
        if person1.house<100 and person2.house>100:
            if person2.house_price>=2000000:
                return 1
            if person2.house_price<2000000:
                return 2 #pass

    def isCar(self,person1,person2):
        if person1.car_price<500000 and person2.car_price<500000:
            return 0  #车都不行
        if abs(person1.car_price - person2.car_price)<100000:
            return  3
        if person1.car_price>500000 and person2.car_price<500000:
            return 1  #选择1
        if person1.car_price<500000 and person2.car_price>500000:
            return 2  # 选择2

    def throwRmb(self):
        return random.randint(1,3)
class Man(Human):
    def __init__(self,name,sex,height,cunkuan,perfect,house,house_price,car_price):
        super(Man,self).__init__(name,sex,height)
        self.cunkuan = cunkuan
        self.perfect = perfect
        self.house = house
        self.house_price = house_price
        self.car_price = car_price

class Woman(Human):
    def chose(self,man1,man2):
        result = super(Woman,self).sexCunkuan(man1,man2)
        if result == 0:
            print('存款问题或者性别不对')
        if result == 2 :
            print('选择{}'.format(man1.name))
        if result ==3:
            print('选择{}'.format(man2.name))
        if result == 1:
            result = super(Woman,self).isHeight(man1,man2)
            if result == 0:
                print('选择{}'.format(man1.name))
            if result == 2:
                print('选择{}'.format(man2.name))
            if result == 1:
                result = super(Woman,self).isPerfect(man1,man2)
                if result == 0:
                    print('颜值都不行')
                if result == 0:
                    print('选择{}'.format(man1.name))
                if result ==2:
                    print('选择{}'.format(man2.name))
                if result ==1:
                    result = super(Woman,self).isHouse(man1,man2)
                    if result == 2:
                        print('你们房子都不行')
                    if result == -1:
                        print('选择{}'.format(man1.name))
                    if result == 1:
                        print('选择{}'.format(man2.name))
                    if result == 0:
                        result == super(Woman,self).isCar(man1,man2)
                        if result == 1:
                            print('车都不行')
                        if result == 0:
                            print('选择{}'.format(man1.name))
                        if result == 2:
                            print('选择{}'.format(man2.name))
                        if result == 3:
                            result = super(Woman,self).throwRmb()
                            if result == 1:
                                print('选择{}'.format(man1.name))
                            else:
                                print('选择{}'.format(man2.name))

man1 = Man('张三',True,160,1200000,8,110,3000000,600000)
man2 = Man('李四',True,172,1100000,9,120,3000000,700000)
woman = Woman('小红',False,170)

woman.chose(man1,man2)


from flask import request, abort

from .. import api_bp as api
from ...setup import db
from ..utils import json_response, InvalidAPIRequest
from ...models.clients import Client
from ...decorators import token_required, current_user

from .schemas import ClientSchema

@api.route("/clients", methods=["GET"])
@token_required()
def get_all_clients():
    return json_response({
        "clients": [c.to_json() for c in current_user.company.clients.all()]
    }, 200)

@api.route("/clients", methods=["POST"])
@token_required()
def add_client_by_name():
    json, errors = ClientSchema().load(request.json)
    if errors:
        raise InvalidAPIRequest(payload=errors)

    client = Client.get_or_create_by_name(json["name"], current_user.company_id)
    return json_response({"client": client.to_json()}, 201)

@api.route("/clients/<int:client_id>", methods=["DELETE"])
@token_required()
def delete(client_id):
    client = Client.query.get_or_404(client_id)
    if client.company_id != current_user.company_id:
        abort(403)

    client_id = client.id
    db.session.delete(client)
    return json_response({"id": client_id}, 200)

@api.route("/clients/<int:client_id>", methods=["PUT"])
@token_required()
def edit(client_id):
    json, errors = ClientSchema().load(request.json)
    if errors:
        raise InvalidAPIRequest(payload=errors)

    client = Client.query.get_or_404(client_id)
    if client.company_id != current_user.company_id:
        abort(403)

    client.name = json["name"]

    return json_response({"client": client.to_json()}, 200)


from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import Upload, batata,file_content
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.core.files.uploadedfile import UploadedFile
import csv
from django.conf import settings
from threading import Thread
import time,sys,os,signal

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

is_paused = False
stopped = False
pid= os.getpid()
def show(request):
    content =  {}
    if request.method =='POST':
        uploaded_file = request.FILES['file']
        uploaded_file_chunks = UploadedFile.chunks(uploaded_file,chunk_size=100)
        print(uploaded_file.name)
        print(uploaded_file.size)
        fs = FileSystemStorage()
        fs.save(uploaded_file.name,uploaded_file)
        global upload
        upload = Upload()
        upload.title = uploaded_file.name
        upload.file = uploaded_file
        upload.save()
        global filecontent
        filecontent = file_content()
        path = os.path.join(BASE_DIR, 'media/')+uploaded_file.name
        with open(path) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            next(csv_reader)
            for row in csv_reader:
                if not is_paused:
                    filecontent.seq = row[0]
                    filecontent.First_name =row[1]
                    filecontent.Last_name = row[2]
                    filecontent.age = row[3]
                    filecontent.street = row[4]
                    filecontent.city = row[5]
                    filecontent.state = row[6]
                    filecontent.zipcode = row[7]
                    filecontent.dollar = row[8]
                    filecontent.color = row[9]
                    filecontent.date = row[10]
                    filecontent.CSV_ID=upload.pk
                    filecontent.save()
                    time.sleep(1)
                    if stopped:
                        break
                while is_paused:
                    time.sleep(1)
                    print("paused")
    return render(request,'upload/upload.html', content)

def pause_upload(request):
    global is_paused
    if not is_paused:
        is_paused = True
    return render(request, 'upload/upload.html')

def resume(request):
    global is_paused
    if is_paused:
        is_paused = False
    print('thread continued')
    return render(request,'upload/upload.html')

def stop(request):
    global stopped
    if not stopped:
        stopped = True
    return HttpResponse('stopped')


import sys
prime_set = {2}
not_prime_set = {1}
def is_prime(x):
    if x in prime_set:
        return True
    elif x in not_prime_set:
        return False
    else:
        for i in range(2, x):
            if x % i == 0:
                not_prime_set.add(x)
                return False
    prime_set.add(x)
    return True
N = int(sys.stdin.readline())
il = map(int, sys.stdin.readline().split())
count = 0
for i in il:
    if is_prime(i):
        count += 1
print(count)


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
import torch.distributed.optim
import torchmetrics.functional as MF
import dgl
import dgl.nn as dglnn
import time
import numpy as np
from ogb.nodeproppred import DglNodePropPredDataset

USE_WRAPPER = False

class SAGE(nn.Module):
    def __init__(self, in_feats, n_hidden, n_classes):
        super().__init__()
        self.layers = nn.ModuleList()
        self.layers.append(dglnn.SAGEConv(in_feats, n_hidden, 'mean'))
        self.layers.append(dglnn.SAGEConv(n_hidden, n_hidden, 'mean'))
        self.layers.append(dglnn.SAGEConv(n_hidden, n_classes, 'mean'))
        self.dropout = nn.Dropout(0.5)

    def forward(self, blocks, x):
        h = x
        for l, (layer, block) in enumerate(zip(self.layers, blocks)):
            h = layer(block, h)
            if l != len(self.layers) - 1:
                h = F.relu(h)
                h = self.dropout(h)
        return h

def train(rank, world_size, graph, num_classes, split_idx):
    torch.cuda.set_device(rank)
    dist.init_process_group('nccl', 'tcp://127.0.0.1:12347', world_size=world_size, rank=rank)

    model = SAGE(graph.ndata['feat'].shape[1], 256, num_classes).cuda()
    model = nn.parallel.DistributedDataParallel(model, device_ids=[rank], output_device=rank)
    opt = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=5e-4)

    train_idx, valid_idx, test_idx = split_idx['train'], split_idx['valid'], split_idx['test']
    if USE_WRAPPER:
        import dglnew
        graph = dglnew.graph.wrapper.DGLGraphStorage(graph)

    sampler = dgl.dataloading.NeighborSampler(
            [5, 5, 5], output_device='cpu', prefetch_node_feats=['feat'],
            prefetch_labels=['label'])
    dataloader = dgl.dataloading.NodeDataLoader(
            graph,
            train_idx,
            sampler,
            device='cuda',
            batch_size=1000,
            shuffle=True,
            drop_last=False,
            pin_memory=True,
            num_workers=4,
            persistent_workers=True,
            use_ddp=True,
            use_prefetch_thread=True)       # TBD: could probably remove this argument

    durations = []
    for _ in range(10):
        t0 = time.time()
        for it, (input_nodes, output_nodes, blocks) in enumerate(dataloader):
            x = blocks[0].srcdata['feat']
            y = blocks[-1].dstdata['label'][:, 0]
            y_hat = model(blocks, x)
            loss = F.cross_entropy(y_hat, y)
            opt.zero_grad()
            loss.backward()
            opt.step()
            if it % 20 == 0:
                acc = MF.accuracy(y_hat, y)
                mem = torch.cuda.max_memory_allocated() / 1000000
                print('Loss', loss.item(), 'Acc', acc.item(), 'GPU Mem', mem, 'MB')
        tt = time.time()
        if rank == 0:
            print(tt - t0)
            durations.append(tt - t0)
    if rank == 0:
        print(np.mean(durations[4:]), np.std(durations[4:]))

if __name__ == '__main__':
    dataset = DglNodePropPredDataset('ogbn-products')
    graph, labels = dataset[0]
    graph.ndata['label'] = labels
    graph.create_formats_()
    split_idx = dataset.get_idx_split()
    num_classes = dataset.num_classes
    n_procs = 4

    # Tested with mp.spawn and fork.  Both worked and got 4s per epoch with 4 GPUs
    # and 3.86s per epoch with 8 GPUs on p2.8x, compared to 5.2s from official examples.
    #import torch.multiprocessing as mp
    #mp.spawn(train, args=(n_procs, graph, num_classes, split_idx), nprocs=n_procs)
    import dgl.multiprocessing as mp
    procs = []
    for i in range(n_procs):
        p = mp.Process(target=train, args=(i, n_procs, graph, num_classes, split_idx))
        p.start()
        procs.append(p)
    for p in procs:
        p.join()


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

from django.db import migrations, models
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('game', '0021_auto_20160225_1633'),
    ]

    operations = [
        migrations.CreateModel(
            name='AllianceMessage',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('text', models.CharField(max_length=200)),
                ('date_occurred', models.DateTimeField(default=django.utils.timezone.now, verbose_name=b'date occurred')),
                ('from_account', models.ForeignKey(related_name='from_account_alliance_messages', to='game.Account')),
                ('to_alliance', models.ForeignKey(related_name='to_alliance_messages', to='game.Alliance')),
            ],
        ),
    ]


import os
import sys

from .cmdbase import PushPull
from .zanatalib.logger import Logger

log = Logger()

class GenericPush(PushPull):

    def __init__(self, *args, **kargs):
        super(GenericPush, self).__init__(*args, **kargs)

    def run(self):
        pushtrans = None
        push_trans_only = False
        force = False
        project_type, deletefiles, tmlfolder, filelist = self.get_files()
        # Disable dir option for generic push command
        if 'dir' in self.context_data:
            log.warn("dir option is disabled in push command, please use --srcdir and --transdir, or specify value in zanata.xml")

        if 'pushtrans' in self.context_data:
            log.warn("--push-trans is deprecated, please use '--push-type both' instead")
            pushtrans = True

        if 'pushtype' in self.context_data:
            push_type = self.context_data.get('pushtype')
            if push_type == "source":
                pushtrans = False
            elif push_type == "target":
                push_trans_only = True
            elif push_type == "both":
                pushtrans = True

        if 'pushtransonly' in self.context_data:
            push_trans_only = True

        if push_trans_only:
            transfolder = self.process_transdir('')
            merge = self.process_merge()
            lang_list = self.get_lang_list()
            locale_map = self.context_data.get('locale_map')
            self.zanatacmd.push_trans_command(transfolder, self.project_id, self.version_id, lang_list, locale_map,
                                              project_type, merge, self.file_mapping_rules)
            sys.exit(0)

        if not os.path.isdir(tmlfolder):
            log.error("Can not find source folder, please specify the source folder with '--srcdir' or using zanata.xml")
            sys.exit(1)

        if 'force' in self.context_data:
            force = True

        folder = None
        if project_type == 'podir':
            log.info("POT directory (originals):%s" % tmlfolder)
        elif project_type == 'gettext':
            log.info("PO directory (originals):%s" % tmlfolder)
            folder = tmlfolder

        if pushtrans is None:
            pushtrans = self.get_pushtrans()

        if deletefiles:
            self.zanatacmd.del_server_content(tmlfolder, self.project_id, self.version_id, filelist, force, project_type)

        if pushtrans:
            log.info("Send local translation: True")
            import_param = self.get_importparam(project_type, folder)
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans,
                                        self.plural_support, import_param, self.file_mapping_rules)
        else:
            log.info("Send local translation: False")
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans, self.plural_support)

class PublicanPush(PushPull):
    def __init__(self, *args, **kargs):
        super(PublicanPush, self).__init__(*args, **kargs)

    def run(self):
        importpo = False
        force = False
        project_type, deletefiles, tmlfolder, filelist = self.get_files()

        log.info("Reuse previous translation on server:%s" % self.copytrans)

        if 'force' in self.context_data:
            force = True

        log.info("POT directory (originals):%s" % tmlfolder)

        importpo = self.get_importpo()

        if deletefiles:
            self.zanatacmd.del_server_content(tmlfolder, self.project_id, self.version_id, filelist, force, "podir")

        if importpo:
            import_param = self.get_importparam("podir", tmlfolder)
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans,
                                        self.plural_support, import_param, self.file_mapping_rules)
        else:
            log.info("Importing source documents only")
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans, self.plural_support)

class PoPush(PushPull):
    def __init__(self, *args, **kargs):
        super(PoPush, self).__init__(*args, **kargs)

    def run(self):
        force = False
        importpo = False
        import_param = None
        project_type, deletefiles, tmlfolder, filelist = self.get_files()
        log.info("Reuse previous translation on server: %s" % self.copytrans)
        importpo = self.get_importpo()

        if importpo:
            log.info("Importing translation")
            import_param = self.get_importparam("gettext", tmlfolder)
        else:
            log.info("Importing source documents only")

        if 'force' in self.context_data:
            force = True
        if deletefiles is True:
            self.zanatacmd.del_server_content(tmlfolder, self.project_id, self.version_id, filelist, force, "gettext")

        if importpo:
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans,
                                        self.plural_support, import_param, self.file_mapping_rules)
        else:
            self.zanatacmd.push_command(filelist, tmlfolder, self.project_id, self.version_id, self.copytrans, self.plural_support)


import tensorflow as tf

def build_loss(similarity_type, similarity_scales, regulariser_type, regulariser_weight,
               label_moving, label_fixed, network_type, ddf):
    label_similarity = multi_scale_loss(label_fixed, label_moving, similarity_type.lower(), similarity_scales)
    if network_type.lower() == 'global':
        ddf_regularisation = tf.constant(0.0)
    else:
        ddf_regularisation = tf.reduce_mean(local_displacement_energy(ddf, regulariser_type, regulariser_weight))
    return tf.reduce_mean(label_similarity), ddf_regularisation

def weighted_binary_cross_entropy(ts, ps, pw=1, eps=1e-6):
    ps = tf.clip_by_value(ps, eps, 1-eps)
    return -tf.reduce_sum(
        tf.concat([ts*pw, 1-ts], axis=4)*tf.log(tf.concat([ps, 1-ps], axis=4)),
        axis=4, keep_dims=True)

def dice_simple(ts, ps, eps_vol=1e-6):
    numerator = tf.reduce_sum(ts*ps, axis=[1, 2, 3, 4]) * 2
    denominator = tf.reduce_sum(ts, axis=[1, 2, 3, 4]) + tf.reduce_sum(ps, axis=[1, 2, 3, 4])+eps_vol
    return numerator/denominator

def dice_generalised(ts, ps, weights):
    ts2 = tf.concat([ts, 1-ts], axis=4)
    ps2 = tf.concat([ps, 1-ps], axis=4)
    numerator = 2 * tf.reduce_sum(tf.reduce_sum(ts2*ps2, axis=[1, 2, 3]) * weights, axis=1)
    denominator = tf.reduce_sum((tf.reduce_sum(ts2, axis=[1, 2, 3]) +
                                 tf.reduce_sum(ps2, axis=[1, 2, 3])) * weights, axis=1)
    return numerator/denominator

def jaccard_simple(ts, ps, eps_vol=1e-6):
    numerator = tf.reduce_sum(ts*ps, axis=[1, 2, 3, 4])
    denominator = tf.reduce_sum(tf.square(ts), axis=[1, 2, 3, 4]) + \
                  tf.reduce_sum(tf.square(ps), axis=[1, 2, 3, 4]) - numerator + eps_vol
    return numerator/denominator

def gauss_kernel1d(sigma):
    if sigma == 0:
        return 0
    else:
        tail = int(sigma*3)
        k = tf.exp([-0.5*x**2/sigma**2 for x in range(-tail, tail+1)])
        return k / tf.reduce_sum(k)

def cauchy_kernel1d(sigma):  # this is an approximation
    if sigma == 0:
        return 0
    else:
        tail = int(sigma*5)
        # k = tf.reciprocal(([((x/sigma)**2+1)*sigma*3.141592653589793 for x in range(-tail, tail+1)]))
        k = tf.reciprocal([((x/sigma)**2+1) for x in range(-tail, tail + 1)])
        return k / tf.reduce_sum(k)

def separable_filter3d(vol, kernel):
    if kernel == 0:
        return vol
    else:
        strides = [1, 1, 1, 1, 1]
        return tf.nn.conv3d(tf.nn.conv3d(tf.nn.conv3d(
            vol,
            tf.reshape(kernel, [-1, 1, 1, 1, 1]), strides, "SAME"),
            tf.reshape(kernel, [1, -1, 1, 1, 1]), strides, "SAME"),
            tf.reshape(kernel, [1, 1, -1, 1, 1]), strides, "SAME")

def single_scale_loss(label_fixed, label_moving, loss_type):
    if loss_type == 'cross-entropy':
        label_loss_batch = tf.reduce_mean(weighted_binary_cross_entropy(label_fixed, label_moving), axis=[1, 2, 3, 4])
    elif loss_type == 'mean-squared':
        label_loss_batch = tf.reduce_mean(tf.squared_difference(label_fixed, label_moving), axis=[1, 2, 3, 4])
    elif loss_type == 'dice':
        label_loss_batch = 1 - dice_simple(label_fixed, label_moving)
    elif loss_type == 'jaccard':
        label_loss_batch = 1 - jaccard_simple(label_fixed, label_moving)
    else:
        raise Exception('Not recognised label correspondence loss!')
    return label_loss_batch

def multi_scale_loss(label_fixed, label_moving, loss_type, loss_scales):
    label_loss_all = tf.stack(
        [single_scale_loss(
            separable_filter3d(label_fixed, gauss_kernel1d(s)),
            separable_filter3d(label_moving, gauss_kernel1d(s)), loss_type)
            for s in loss_scales],
        axis=1)
    return tf.reduce_mean(label_loss_all, axis=1)

def local_displacement_energy(ddf, energy_type, energy_weight):

    def gradient_dx(fv): return (fv[:, 2:, 1:-1, 1:-1] - fv[:, :-2, 1:-1, 1:-1]) / 2

    def gradient_dy(fv): return (fv[:, 1:-1, 2:, 1:-1] - fv[:, 1:-1, :-2, 1:-1]) / 2

    def gradient_dz(fv): return (fv[:, 1:-1, 1:-1, 2:] - fv[:, 1:-1, 1:-1, :-2]) / 2

    def gradient_txyz(Txyz, fn):
        return tf.stack([fn(Txyz[..., i]) for i in [0, 1, 2]], axis=4)

    def compute_gradient_norm(displacement, flag_l1=False):
        dTdx = gradient_txyz(displacement, gradient_dx)
        dTdy = gradient_txyz(displacement, gradient_dy)
        dTdz = gradient_txyz(displacement, gradient_dz)
        if flag_l1:
            norms = tf.abs(dTdx) + tf.abs(dTdy) + tf.abs(dTdz)
        else:
            norms = dTdx**2 + dTdy**2 + dTdz**2
        return tf.reduce_mean(norms, [1, 2, 3, 4])

    def compute_bending_energy(displacement):
        dTdx = gradient_txyz(displacement, gradient_dx)
        dTdy = gradient_txyz(displacement, gradient_dy)
        dTdz = gradient_txyz(displacement, gradient_dz)
        dTdxx = gradient_txyz(dTdx, gradient_dx)
        dTdyy = gradient_txyz(dTdy, gradient_dy)
        dTdzz = gradient_txyz(dTdz, gradient_dz)
        dTdxy = gradient_txyz(dTdx, gradient_dy)
        dTdyz = gradient_txyz(dTdy, gradient_dz)
        dTdxz = gradient_txyz(dTdx, gradient_dz)
        return tf.reduce_mean(dTdxx**2 + dTdyy**2 + dTdzz**2 + 2*dTdxy**2 + 2*dTdxz**2 + 2*dTdyz**2, [1, 2, 3, 4])

    if energy_weight:
        if energy_type == 'bending':
            energy = compute_bending_energy(ddf)
        elif energy_type == 'gradient-l2':
            energy = compute_gradient_norm(ddf)
        elif energy_type == 'gradient-l1':
            energy = compute_gradient_norm(ddf, flag_l1=True)
        else:
            raise Exception('Not recognised local regulariser!')
    else:
        energy = tf.constant(0.0)

    return energy*energy_weight


# Authors: Tomas Hodan (hodantom@cmp.felk.cvut.cz), Pavel Haluza
# Center for Machine Perception, Czech Technical University in Prague

# A script to download any subset of the T-LESS dataset, which is available at:
# http://cmp.felk.cvut.cz/t-less
# For instructions run: python t-less_download.py --help

# Note: The code at the beginning of this file was copied from wget:
# http://pypi.python.org/pypi/wget

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

# WGET

__version__ = "3.2"

import sys, shutil, os
import tempfile
import math

PY3K = sys.version_info >= (3, 0)
if PY3K:
  import urllib.request as ulib
  import urllib.parse as urlparse
else:
  import urllib as ulib
  import urlparse

# --- workarounds for Python misbehavior ---

# enable passing unicode arguments from command line in Python 2.x
# https://stackoverflow.com/questions/846850/read-unicode-characters
def win32_utf8_argv():
    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
    strings.

    Versions 2.x of Python don't support Unicode in sys.argv on
    Windows, with the underlying Windows API instead replacing multi-byte
    characters with '?'.
    """

    from ctypes import POINTER, byref, cdll, c_int, windll
    from ctypes.wintypes import LPCWSTR, LPWSTR

    GetCommandLineW = cdll.kernel32.GetCommandLineW
    GetCommandLineW.argtypes = []
    GetCommandLineW.restype = LPCWSTR

    CommandLineToArgvW = windll.shell32.CommandLineToArgvW
    CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
    CommandLineToArgvW.restype = POINTER(LPWSTR)

    cmd = GetCommandLineW()
    argc = c_int(0)
    argv = CommandLineToArgvW(cmd, byref(argc))
    argnum = argc.value
    sysnum = len(sys.argv)
    result = []
    if argnum > 0:
        # Remove Python executable and commands if present
        start = argnum - sysnum
        for i in range(start, argnum):
            result.append(argv[i].encode('utf-8'))
    return result

# enable unicode output to windows console
# https://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash
def win32_unicode_console():
    import codecs
    from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
    from ctypes.wintypes import BOOL, HANDLE, DWORD, LPWSTR, LPCWSTR, LPVOID

    original_stderr = sys.stderr

    # Output exceptions in this code to original_stderr, so that we can at least see them
    def _complain(message):
        original_stderr.write(message if isinstance(message, str) else repr(message))
        original_stderr.write('\n')

    codecs.register(lambda name: codecs.lookup('utf-8') if name == 'cp65001' else None)

    try:
        GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32))
        STD_OUTPUT_HANDLE = DWORD(-11)
        STD_ERROR_HANDLE = DWORD(-12)
        GetFileType = WINFUNCTYPE(DWORD, DWORD)(("GetFileType", windll.kernel32))
        FILE_TYPE_CHAR = 0x0002
        FILE_TYPE_REMOTE = 0x8000
        GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(("GetConsoleMode", windll.kernel32))
        INVALID_HANDLE_VALUE = DWORD(-1).value

        def not_a_console(handle):
            if handle == INVALID_HANDLE_VALUE or handle is None:
                return True
            return ((GetFileType(handle) & ~FILE_TYPE_REMOTE) != FILE_TYPE_CHAR
                    or GetConsoleMode(handle, byref(DWORD())) == 0)

        old_stdout_fileno = None
        old_stderr_fileno = None
        if hasattr(sys.stdout, 'fileno'):
            old_stdout_fileno = sys.stdout.fileno()
        if hasattr(sys.stderr, 'fileno'):
            old_stderr_fileno = sys.stderr.fileno()

        STDOUT_FILENO = 1
        STDERR_FILENO = 2
        real_stdout = (old_stdout_fileno == STDOUT_FILENO)
        real_stderr = (old_stderr_fileno == STDERR_FILENO)

        if real_stdout:
            hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
            if not_a_console(hStdout):
                real_stdout = False

        if real_stderr:
            hStderr = GetStdHandle(STD_ERROR_HANDLE)
            if not_a_console(hStderr):
                real_stderr = False

        if real_stdout or real_stderr:
            WriteConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPWSTR, DWORD, POINTER(DWORD), LPVOID)(("WriteConsoleW", windll.kernel32))

            class UnicodeOutput:
                def __init__(self, hConsole, stream, fileno, name):
                    self._hConsole = hConsole
                    self._stream = stream
                    self._fileno = fileno
                    self.closed = False
                    self.softspace = False
                    self.mode = 'w'
                    self.encoding = 'utf-8'
                    self.name = name
                    self.flush()

                def isatty(self):
                    return False

                def close(self):
                    # don't really close the handle, that would only cause problems
                    self.closed = True

                def fileno(self):
                    return self._fileno

                def flush(self):
                    if self._hConsole is None:
                        try:
                            self._stream.flush()
                        except Exception as e:
                            _complain("%s.flush: %r from %r" % (self.name, e, self._stream))
                            raise

                def write(self, text):
                    try:
                        if self._hConsole is None:
                            if not PY3K and isinstance(text, unicode):
                                text = text.encode('utf-8')
                            elif PY3K and isinstance(text, str):
                                text = text.encode('utf-8')
                            self._stream.write(text)
                        else:
                            if not PY3K and not isinstance(text, unicode):
                                text = str(text).decode('utf-8')
                            elif PY3K and not isinstance(text, str):
                                text = text.decode('utf-8')
                            remaining = len(text)
                            while remaining:
                                n = DWORD(0)
                                # There is a shorter-than-documented limitation on the
                                # length of the string passed to WriteConsoleW (see
                                # <http://tahoe-lafs.org/trac/tahoe-lafs/ticket/1232>.
                                retval = WriteConsoleW(self._hConsole, text, min(remaining, 10000), byref(n), None)
                                if retval == 0 or n.value == 0:
                                    raise IOError("WriteConsoleW returned %r, n.value = %r" % (retval, n.value))
                                remaining -= n.value
                                if not remaining:
                                    break
                                text = text[n.value:]
                    except Exception as e:
                        _complain("%s.write: %r" % (self.name, e))
                        raise

                def writelines(self, lines):
                    try:
                        for line in lines:
                            self.write(line)
                    except Exception as e:
                        _complain("%s.writelines: %r" % (self.name, e))
                        raise

            if real_stdout:
                sys.stdout = UnicodeOutput(hStdout, None, STDOUT_FILENO, '<Unicode console stdout>')
            else:
                sys.stdout = UnicodeOutput(None, sys.stdout, old_stdout_fileno, '<Unicode redirected stdout>')

            if real_stderr:
                sys.stderr = UnicodeOutput(hStderr, None, STDERR_FILENO, '<Unicode console stderr>')
            else:
                sys.stderr = UnicodeOutput(None, sys.stderr, old_stderr_fileno, '<Unicode redirected stderr>')
    except Exception as e:
        _complain("exception %r while fixing up sys.stdout and sys.stderr" % (e,))

# --- helpers ---

def to_unicode(filename):
    """:return: filename decoded from utf-8 to unicode"""
    #
    if PY3K:
        # [ ] test this on Python 3 + (Windows, Linux)
        # [ ] port filename_from_headers once this works
        # [ ] add test to repository / Travis
        return filename
    else:
        if isinstance(filename, unicode):
            return filename
        else:
            return unicode(filename, 'utf-8')

def filename_from_url(url):
    """:return: detected filename as unicode or None"""
    # [ ] test urlparse behavior with unicode url
    fname = os.path.basename(urlparse.urlparse(url).path)
    if len(fname.strip(" \n\t.")) == 0:
        return None
    return to_unicode(fname)

def filename_from_headers(headers):
    """Detect filename from Content-Disposition headers if present.
    http://greenbytes.de/tech/tc2231/

    :param: headers as dict, list or string
    :return: filename from content-disposition header or None
    """
    if type(headers) == str:
        headers = headers.splitlines()
    if type(headers) == list:
        headers = dict([x.split(':', 1) for x in headers])
    cdisp = headers.get("Content-Disposition")
    if not cdisp:
        return None
    cdtype = cdisp.split(';')
    if len(cdtype) == 1:
        return None
    if cdtype[0].strip().lower() not in ('inline', 'attachment'):
        return None
    # several filename params is illegal, but just in case
    fnames = [x for x in cdtype[1:] if x.strip().startswith('filename=')]
    if len(fnames) > 1:
        return None
    name = fnames[0].split('=')[1].strip(' \t"')
    name = os.path.basename(name)
    if not name:
        return None
    return name

def filename_fix_existing(filename):
    """Expands name portion of filename with numeric ' (x)' suffix to
    return filename that doesn't exist already.
    """
    dirname = u'.'
    name, ext = filename.rsplit('.', 1)
    names = [x for x in os.listdir(dirname) if x.startswith(name)]
    names = [x.rsplit('.', 1)[0] for x in names]
    suffixes = [x.replace(name, '') for x in names]
    # filter suffixes that match ' (x)' pattern
    suffixes = [x[2:-1] for x in suffixes
                   if x.startswith(' (') and x.endswith(')')]
    indexes  = [int(x) for x in suffixes
                   if set(x) <= set('0123456789')]
    idx = 1
    if indexes:
        idx += sorted(indexes)[-1]
    return '%s (%d).%s' % (name, idx, ext)

# --- terminal/console output helpers ---

def get_console_width():
    """Return width of available window area. Autodetection works for
       Windows and POSIX platforms. Returns 80 for others

       Code from http://bitbucket.org/techtonik/python-pager
    """

    if os.name == 'nt':
        STD_INPUT_HANDLE  = -10
        STD_OUTPUT_HANDLE = -11
        STD_ERROR_HANDLE  = -12

        # get console handle
        from ctypes import windll, Structure, byref
        try:
            from ctypes.wintypes import SHORT, WORD, DWORD
        except ImportError:
            # workaround for missing types in Python 2.5
            from ctypes import (
                c_short as SHORT, c_ushort as WORD, c_ulong as DWORD)
        console_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

        # CONSOLE_SCREEN_BUFFER_INFO Structure
        class COORD(Structure):
            _fields_ = [("X", SHORT), ("Y", SHORT)]

        class SMALL_RECT(Structure):
            _fields_ = [("Left", SHORT), ("Top", SHORT),
                        ("Right", SHORT), ("Bottom", SHORT)]

        class CONSOLE_SCREEN_BUFFER_INFO(Structure):
            _fields_ = [("dwSize", COORD),
                        ("dwCursorPosition", COORD),
                        ("wAttributes", WORD),
                        ("srWindow", SMALL_RECT),
                        ("dwMaximumWindowSize", DWORD)]

        sbi = CONSOLE_SCREEN_BUFFER_INFO()
        ret = windll.kernel32.GetConsoleScreenBufferInfo(
            console_handle, byref(sbi))
        if ret == 0:
            return 0
        return sbi.srWindow.Right+1

    elif os.name == 'posix':
        from fcntl import ioctl
        from termios import TIOCGWINSZ
        from array import array

        winsize = array("H", [0] * 4)
        try:
            ioctl(sys.stdout.fileno(), TIOCGWINSZ, winsize)
        except IOError:
            pass
        return (winsize[1], winsize[0])[0]

    return 80

def bar_thermometer(current, total, width=80):
    """Return thermometer style progress bar string. `total` argument
    can not be zero. The minimum size of bar returned is 3. Example:

        [..........            ]

    Control and trailing symbols (\r and spaces) are not included.
    See `bar_adaptive` for more information.
    """
    # number of dots on thermometer scale
    avail_dots = width-2
    shaded_dots = int(math.floor(float(current) / total * avail_dots))
    return '[' + '.'*shaded_dots + ' '*(avail_dots-shaded_dots) + ']'

def bar_adaptive(current, total, width=80):
    """Return progress bar string for given values in one of three
    styles depending on available width:

        [..  ] downloaded / total
        downloaded / total
        [.. ]

    if total value is unknown or <= 0, show bytes counter using two
    adaptive styles:

        %s / unknown
        %s

    if there is not enough space on the screen, do not display anything

    returned string doesn't include control characters like \r used to
    place cursor at the beginning of the line to erase previous content.

    this function leaves one free character at the end of string to
    avoid automatic linefeed on Windows.
    """

    # process special case when total size is unknown and return immediately
    if not total or total < 0:
        msg = "%s / unknown" % current
        if len(msg) < width:    # leaves one character to avoid linefeed
            return msg
        if len("%s" % current) < width:
            return "%s" % current

    # --- adaptive layout algorithm ---
    #
    # [x] describe the format of the progress bar
    # [x] describe min width for each data field
    # [x] set priorities for each element
    # [x] select elements to be shown
    #   [x] choose top priority element min_width < avail_width
    #   [x] lessen avail_width by value if min_width
    #   [x] exclude element from priority list and repeat

    #  10% [.. ]  10/100
    # pppp bbbbb sssssss

    min_width = {
      'percent': 4,  # 100%
      'bar': 3,      # [.]
      'size': len("%s" % total)*2 + 3, # 'xxxx / yyyy'
    }
    priority = ['percent', 'bar', 'size']

    # select elements to show
    selected = []
    avail = width
    for field in priority:
      if min_width[field] < avail:
        selected.append(field)
        avail -= min_width[field]+1   # +1 is for separator or for reserved space at
                                      # the end of line to avoid linefeed on Windows
    # render
    output = ''
    for field in selected:

      if field == 'percent':
        # fixed size width for percentage
        output += ('%s%%' % (100 * current // total)).rjust(min_width['percent'])
      elif field == 'bar':  # [. ]
        # bar takes its min width + all available space
        output += bar_thermometer(current, total, min_width['bar']+avail)
      elif field == 'size':
        # size field has a constant width (min == max)
        output += ("%s / %s" % (current, total)).rjust(min_width['size'])

      selected = selected[1:]
      if selected:
        output += ' '  # add field separator

    return output

# --/ console helpers

__current_size = 0  # global state variable, which exists solely as a
                    # workaround against Python 3.3.0 regression
                    # http://bugs.python.org/issue16409
                    # fixed in Python 3.3.1
def callback_progress(blocks, block_size, total_size, bar_function):
    """callback function for urlretrieve that is called when connection is
    created and when once for each block

    draws adaptive progress bar in terminal/console

    use sys.stdout.write() instead of "print,", because it allows one more
    symbol at the line end without linefeed on Windows

    :param blocks: number of blocks transferred so far
    :param block_size: in bytes
    :param total_size: in bytes, can be -1 if server doesn't return it
    :param bar_function: another callback function to visualize progress
    """
    global __current_size

    width = min(100, get_console_width())

    if sys.version_info[:3] == (3, 3, 0):  # regression workaround
        if blocks == 0:  # first call
            __current_size = 0
        else:
            __current_size += block_size
        current_size = __current_size
    else:
        current_size = min(blocks*block_size, total_size)
    progress = bar_function(current_size, total_size, width)
    if progress:
        sys.stdout.write("\r" + progress)

def detect_filename(url=None, out=None, headers=None, default="download.wget"):
    """Return filename for saving file. If no filename is detected from output
    argument, url or headers, return default (download.wget)
    """
    names = dict(out='', url='', headers='')
    if out:
        names["out"] = out or ''
    if url:
        names["url"] = filename_from_url(url) or ''
    if headers:
        names["headers"] = filename_from_headers(headers) or ''
    return names["out"] or names["headers"] or names["url"] or default

def download(url, out=None, bar=bar_adaptive):
    """High level function, which downloads URL into tmp file in current
    directory and then renames it to filename autodetected from either URL
    or HTTP headers.

    :param bar: function to track download progress (visualize etc.)
    :param out: output filename or directory
    :return:    filename where URL is downloaded to
    """
    # detect of out is a directory
    outdir = None
    if out and os.path.isdir(out):
        outdir = out
        out = None

    # get filename for temp file in current directory
    prefix = detect_filename(url, out)
    (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
    os.close(fd)
    os.unlink(tmpfile)

    # set progress monitoring callback
    def callback_charged(blocks, block_size, total_size):
        # 'closure' to set bar drawing function in callback
        callback_progress(blocks, block_size, total_size, bar_function=bar)
    if bar:
        callback = callback_charged
    else:
        callback = None

    if PY3K:
        # Python 3 can not quote URL as needed
        binurl = list(urlparse.urlsplit(url))
        binurl[2] = urlparse.quote(binurl[2])
        binurl = urlparse.urlunsplit(binurl)
    else:
        binurl = url
    (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
    filename = detect_filename(url, out, headers)
    if outdir:
        filename = outdir + "/" + filename

    # add numeric ' (x)' suffix if filename already exists
    if os.path.exists(filename):
        filename = filename_fix_existing(filename)
    shutil.move(tmpfile, filename)

    #print headers
    return filename

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

# T-LESS DOWNLOAD

import zipfile
import argparse

version = 2
url_root = "http://ptak.felk.cvut.cz/darwin/t-less/v" + str(version)
train_ids = list(range(1, 31))
test_ids = list(range(1, 21))
t_types = ["train", "test"]
sensor_types = ["primesense", "kinect", "canon"]
model_types = ["cad", "cad_subdivided", "reconst"]

def parse():
    parser = argparse.ArgumentParser(
        description="Downloads and unpacks the selected parts of the T-LESS dataset. "
                    "Example usage: "
                    "\"t-less_download.py --train --test --models --sensors primesense\" "
                    "- downloads and unpacks all models and all images from the Primesense sensor.")
    parser.add_argument("--destination", default=".",
                        help="destination folder inside which a new folder for the dataset is created"
                             " (default is the current folder)")
    parser.add_argument("--train", nargs="*", type=int, choices=train_ids, default=train_ids,
                        metavar="obj_id", help="IDs of objects (all are downloaded if no ID is specified)")
    parser.add_argument("--test", nargs="*", type=int, choices=test_ids, default=test_ids,
                        metavar="scene_id", help="IDs of scenes (all are downloaded if no ID is specified)")
    parser.add_argument("--sensors", nargs="+", default=sensor_types, choices=sensor_types,
                        help="sensors (images from all sensors are downloaded if none is specified)")
    parser.add_argument("--models", nargs="*", choices=model_types,default=model_types,
                        help="variants of 3D object models (all variants are downloaded if none is specified)")
    args = parser.parse_args()

    def reinterpret_arg(arg, all, none):
        if arg == none:
            return all
        elif arg is None:
            return none
        else:
            return arg

    dest = os.path.normpath(args.destination)
    ids = [args.train, args.test]
    if all(i is None for i in ids + [args.models]):
        #parser.error("What should be downloaded?")
        parser.print_usage()
        parser.exit()
    for i, id_list in enumerate(ids):
        ids[i] = reinterpret_arg(id_list, train_ids if i < 1 else test_ids, [])
    args.models = reinterpret_arg(args.models, model_types, [])
    return dest, ids, args.sensors, args.models

step_current = 0
step_total = 0
def down_unzip(url, dest, zip_download_dir):
    global step_current
    step_current += 1
    down_path = os.path.join(zip_download_dir, os.path.basename(url))

    print("Downloading {}/{}: {}".format(step_current, step_total, url))
    name_file = download(url, out=down_path)
    print("")

    print("Unpacking")
    unzip(name_file, dest)
    print("")

    print("")

def unzip(name_file, dest):
    zip_file = zipfile.ZipFile(name_file)
    # zip_file.extractall(dest)  # no progress bar
    size_uncompress = sum((f.file_size for f in zip_file.infolist()))
    size_extracted = 0
    for f in zip_file.infolist():
        size_extracted += f.file_size
        callback_progress_zip(size_extracted, size_uncompress)
        zip_file.extract(f, dest)
    zip_file.close()
    os.remove(name_file)

def callback_progress_zip(size_current, size_total, bar_function=bar_adaptive):
    width = min(100, get_console_width())
    progress = bar_function(size_current, size_total, width)
    if progress:
        sys.stdout.write("\r" + progress)

def main():
    global step_total
    path_dest_home, ids_lists, sensors, models = parse()
    step_total = len(sensors) * len(ids_lists) * sum((len(i) for i in ids_lists)) + len(models)

    down_sensors = ", ".join(sensors)
    if down_sensors == "": down_sensors = "-"
    down_obj_ids = ", ".join(map(str, ids_lists[0]))
    if down_obj_ids == "": down_obj_ids = "-"
    down_scene_ids = ", ".join(map(str, ids_lists[1]))
    if down_scene_ids == "": down_scene_ids = "-"
    down_model_types = ", ".join(models)
    if down_model_types == "": down_model_types = "-"

    print("\nData to be downloaded to \"{}\":\n"
          "------------------------------------\n"
          "Training images (from sensors: {}) for objects:\n{}\n\n"
          "Test images (from sensors: {}) for scenes:\n{}\n\n"
          "Models:\n{}\n"
          "------------------------------------\n".format(
        path_dest_home, down_sensors, down_obj_ids, down_sensors,
        down_scene_ids, down_model_types))
    print("Note: If you have already downloaded some of these dataset parts to "
          "the same destination folder, they will be downloaded again and overwritten.\n")
    inp = input if PY3K else raw_input
    inp("Press ENTER to continue.")
    print("")

    root = "t-less_v{:d}".format(version)
    zip_download_dir = os.path.join(path_dest_home, root)

    if not os.path.exists(zip_download_dir):
        os.makedirs(zip_download_dir)

    for sensor in sensors:
        for id_data_type, ids in enumerate(ids_lists):
            parent = "{}_{}".format(t_types[id_data_type], sensor)
            for id in ids:
                name = "{:02d}".format(id)
                path = os.path.join(path_dest_home, root, parent)
                url = "{}/{}.zip".format(url_root, "_".join([root, parent, name]))
                down_unzip(url, path, zip_download_dir)
    for model_type in models:
        name = "models_" + model_type
        path_base = os.path.join(root)
        url = "{}/{}.zip".format(url_root, "{}_{}".format(root, name))
        down_unzip(url, os.path.join(path_dest_home, path_base), zip_download_dir)

if __name__ == '__main__':
    main()


from os import path

import logging.config

from flask import Flask, Blueprint
from api.restplus import api
from api.geolocation_data_flaskapi.business.security import authenticate, identity
from flask_jwt import JWT, jwt_required, current_identity
from api.geolocation_data_flaskapi.endpoints.location_endpoint import ns as location_namespace

from database import db

def create_app():
    flask_app = Flask(__name__)
    return flask_app

def initialize_app(flask_app):
    blueprint = Blueprint('geolocation', __name__, url_prefix='/geolocation')
    api.init_app(blueprint)
    api.add_namespace(location_namespace)
    flask_app.register_blueprint(blueprint)

    db.init_app(flask_app)

    from database import create_database
    create_database(app=flask_app)

log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.conf')
logging.config.fileConfig(log_file_path)
log = logging.getLogger(__name__)

app = create_app()
app.config.from_object('config.DevelopmentConfig')
initialize_app(app)

jwt = JWT(app, authenticate, identity)

@app.route('/')
def index():
    return 'Computer says, "Hello."'

@app.route('/protected')
@jwt_required()
def protected():
    return '%s' % current_identity

def main():
    initialize_app(app)

    if settings.FLASK_MODE is 'DEV':
        log.info('>>>>> Starting development server at http://{host}/{context}/ <<<<<'.format(
            host=app.config['SERVER_NAME'],
            context='geolocation')
        )

if __name__ == "__main__":
    app.run()


import pygame
from pygame.locals import *

import constant_storage
from spliting import macgyver_class

class Display:
    """
    Management of all the graphic part of the program.
    This class has only one class method,
    because it is not intended to create instances.
    """

    @classmethod
    def display_routine(cls, global_map, practicable_zones, guard,
                        macgyver, items):
        """
        Here, we will use all the elements of the logical structure
        code to generate the correct display. We will therefore inject
        a lot of parameters in this class method. We
        will find a while function that will iterate 30 times per second as
        long as that the user does not close the pygame graphic interface.
        """

        pygame.init()
        window = pygame.display.set_mode((constant_storage.WINDOW_WIDTH,
                                          constant_storage.WINDOW_HEIGHT))
        display = True
        moving_functions = True

        while display:
            pygame.time.Clock().tick(30)
            for map_zone in global_map:
                if map_zone in practicable_zones:
                    window.blit(constant_storage.PATH_SPRITE, (map_zone[0] *
                                constant_storage.WIDTH_PPS, map_zone[1] *
                                constant_storage.HEIGHT_PPS))
                else:
                    window.blit(constant_storage.WALL_SPRITE, (map_zone[0] *
                                constant_storage.WIDTH_PPS,
                                map_zone[1]*constant_storage.HEIGHT_PPS))

            window.blit(constant_storage.GUARD_SPRITE, (guard.position[0] *
                        constant_storage.WIDTH_PPS,
                        guard.position[1]*constant_storage.HEIGHT_PPS))
            window.blit(constant_storage.MACGYVER_SPRITE,
                        (macgyver.position[0]*constant_storage.WIDTH_PPS,
                         macgyver.position[1]*constant_storage.HEIGHT_PPS))

            for event in pygame.event.get():
                if moving_functions:
                    if event.type == KEYDOWN and event.key == K_UP:
                        player_input = "front"
                        macgyver_class.MacGyver.move(macgyver, player_input,
                                      practicable_zones)
                    elif event.type == KEYDOWN and event.key == K_DOWN:
                        player_input = "back"
                        macgyver_class.MacGyver.move(macgyver, player_input,
                                      practicable_zones)
                    elif event.type == KEYDOWN and event.key == K_RIGHT:
                        player_input = "right"
                        macgyver_class.MacGyver.move(macgyver, player_input,
                                      practicable_zones)
                    elif event.type == KEYDOWN and event.key == K_LEFT:
                        player_input = "left"
                        macgyver_class.MacGyver.move(macgyver, player_input,
                                      practicable_zones)

                    elif event.type == QUIT:
                        display = False
                else:
                    if event.type == QUIT:
                        display = False

            constant_storage.items_auto_finder(items, macgyver)

            if "seringue" not in macgyver.stuff:
                macgyver_class.MacGyver.craft(macgyver, items)

            pygame.display.flip()

            if macgyver.position == guard.position and moving_functions:
                if "seringue" in macgyver.stuff:
                    print("You win the game")
                else:
                    print("Game Over")
                moving_functions = False


#Script de prueba para obtener la regresion del throughtput para un conjunto de celdas

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Reshape
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, TensorBoard
import numpy as np
import pandas as pd
from joblib import dump
from joblib import load
from math import floor
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.preprocessing import RobustScaler

#Cargar modelos
modelo_L_20=keras.models.load_model("/home/userbda/Documents/modelo_L_20.h5")
modelo_L_60=keras.models.load_model("/home/userbda/Documents/modelo_L_60.h5")
modelo_L_100=keras.models.load_model("/home/userbda/Documents/modelo_L_100.h5")

modelo_M_20=keras.models.load_model("/home/userbda/Documents/modelo_M_20.h5")
modelo_M_60=keras.models.load_model("/home/userbda/Documents/modelo_M_60.h5")
modelo_M_100=keras.models.load_model("/home/userbda/Documents/modelo_M_100.h5")

modelo_P_20=keras.models.load_model("/home/userbda/Documents/modelo_P_20.h5")
modelo_P_60=keras.models.load_model("/home/userbda/Documents/modelo_P_60.h5")
modelo_P_100=keras.models.load_model("/home/userbda/Documents/modelo_P_100.h5")

Datos_L=pd.read_csv('/home/userbda/Documents/celdas_L.csv')
Datos_M=pd.read_csv('/home/userbda/Documents/celdas_M.csv')
Datos_P=pd.read_csv('/home/userbda/Documents/celdas_P.csv')

#Creacion del tiempo como indice del dataframe
Datos_L["DATETIME_ID2"]=pd.to_datetime(Datos_L["DATETIME_ID2"])
Datos_L=Datos_L.set_index('DATETIME_ID2')

Datos_P["DATETIME_ID2"]=pd.to_datetime(Datos_P["DATETIME_ID2"])
Datos_P=Datos_P.set_index('DATETIME_ID2')

Datos_M["DATETIME_ID2"]=pd.to_datetime(Datos_M["DATETIME_ID2"])
Datos_M=Datos_M.set_index('DATETIME_ID2')

Datos=Datos_L
lista=Datos["EutrancellFDD"].unique().tolist()
celdas=[lista[1]]

print("Datos cargados")
#Prueba para celdas la primera celda de la banda L
def regresion(Datos,lista_celdas,prb_20,prb_60,prb_100):
    largo_dia=19  #Horas del dia
    n_dias_input=7  #Cantidad de dias (semana)
    for cell in lista_celdas:
        #Se obtienen los datos de una de las celdas
        datos_celda=(Datos[Datos["EutrancellFDD"]==cell])
        #Se filtra el horario de 6:00 am hasta 12:00 pm
        datos_celda=datos_celda.reindex(pd.date_range(min(datos_celda.index), max(datos_celda.index),freq='H')).fillna(np.nan)
        datos_celda=datos_celda.between_time(start_time='6:00', end_time='00:00')[1:]
        datos_celda=datos_celda.fillna(method='ffill',axis=0).fillna(method='bfill',axis=0)
        datos_celda=datos_celda.drop(columns=["EutrancellFDD"])

        #En este dataset desde el dia 84 se consideró conjunto test (no se usó para entrenar)
        #Se obtiene la regresion para el dia 85
        for numero_dia in list(range(84,85)):

            X=largo_dia*numero_dia
            entrada=datos_celda.iloc[X-largo_dia*n_dias_input:X]
            salida=datos_celda.iloc[X:X+largo_dia,0]
            variable=entrada.rolling(19).max()
            hora_v = variable.index.hour
            max_prb_mean=(variable.iloc[(hora_v == 0)]).iloc[:,8].mean()

            #Se selecciona modelo segun el uso de prb de los ultimos 7 dias
            if (max_prb_mean>0) &( max_prb_mean<20):
                model=prb_20
            if (max_prb_mean>20) &( max_prb_mean<60):
                model=prb_60
            if (max_prb_mean>60) &( max_prb_mean<100):
                model=prb_100

            #Normalizacion
            sca =RobustScaler(quantile_range=(0,95))
            sca.fit(entrada)
            entrada=sca.transform(entrada)

            #Prediccion
            predicciones=model.predict(np.array([entrada]))
            #Variable auxiliar
            a = np.zeros(shape=(largo_dia,datos_celda.shape[1]))
            prediccion = pd.DataFrame(a,columns=datos_celda.columns)
            prediccion["DL_User_Thp"]=predicciones[0].reshape(-1,1)
            prediccion_normed=prediccion.to_numpy()
            prediccion_normed=prediccion_normed[:,0]
            prediccion=sca.inverse_transform(prediccion)
            prediccion=prediccion[:,0]

            #Graficos de prediccion contra valor real
            #plt.plot(tiempo, prediccion/1000, label="predicción")
            #plt.plot(tiempo, salida/1000, label="valor real")
            #plt.xticks(rotation='vertical')
            #plt.ylabel("DL_User_Thp [Mbps]")
            #plt.xlabel("Hora")
            #plt.title(cell)
            #plt.legend()
            #plt.show()
    return salida/1000, prediccion/1000
print("Ejemplo de celdas banda 2600")
x_real,x_pred=regresion(Datos,celdas,modelo_L_20,modelo_L_60,modelo_L_100)

tiempo=np.arange(6,25,1)
plt.plot(tiempo, x_pred, label="predicción")
plt.plot(tiempo, x_real, label="valor real")
plt.xticks(rotation='vertical')
plt.ylabel("DL_User_Thp [Mbps]")
plt.xlabel("Hora")
plt.title(celdas)
plt.show()
print(" NUT prediccion ")
print(np.array(x_pred))
print(" NUT real")
print(np.array(x_real))


import codecs
import pandas as pd
import numpy as np
import random
from itertools import chain
from sklearn.model_selection import train_test_split
#from sklearn.cross_validation import train_test_split
np.set_printoptions(threshold=np.inf)
class DataLoad(object):
    """
    loading data from txt
    """
    def __init__(self):
        self._max_len = 0
        i=0
        self.time_steps=time_steps=24
        self.x_inputs = []
        self.y_inputs = []
        #with codecs.open(u"/home/xjj/lstm2med/train.in", 'r', "utf-8") as f:
        with codecs.open(u"train.in", 'r', "utf-8") as f:
            x, labes = [], []
            for line in f.readlines():
                #print(line)
                line_list = line.split()
                #print(line_list)
                if line_list != []:
                    #print(line_list)
                    if len(line_list) == 2:
                        x.append(line_list[0])
                        labes.append(line_list[1])
                    else:
                        x.append(" ")
                        labes.append(line_list[0])
 #                   x.append(line_list[0])
#                    if len(line_list) == 2 :
#                        labes.append(line_list[1])

                else:
                    self.x_inputs.append(x)
                    i+=1
                    self.y_inputs.append(labes)
                    if len(x) > self.max_len:
                        self._max_len = len(x)
                        if (self._max_len>1100):
                            print(self._max_len,i)
                           # print(x)
                    x, labes = [], []
                    #break
        df_data = pd.DataFrame({'words': self.x_inputs, 'tags': self.y_inputs}, index=range(len(self.x_inputs)))
        #print(df_data)
        #  句子长度
        df_data['sentence_len'] = df_data['words'].apply(lambda words: len(words))
        all_words = list(chain(*df_data['words'].values))
        #print(all_words)
        all_tags = list(chain(*df_data['tags'].values))
        # 2.统计所有 word
        sr_allwords = pd.Series(all_words)
        #print(sr_allwords)
        sr_allwords = sr_allwords.value_counts()
        #print(sr_allwords)
        self.set_words = sr_allwords.index
        #print(self.set_words)
        set_ids = range(1, len(self.set_words)+1)
        #print(set_ids)
        tags = pd.Series(all_tags).value_counts()
        self.set_tags = tags.index
        tag_ids = range(len(self.set_tags))
        # 3. 构建 words 和 tags 都转为数值 id 的映射（使用 Series 比 dict 更加方便）
        self.word2id = pd.Series(set_ids, index=self.set_words)
        self.id2word = pd.Series(self.set_words, index=set_ids)
        self.tag2id = pd.Series(tag_ids, index=self.set_tags)
        self.id2tag = pd.Series(self.set_tags, index=tag_ids)
        word2id = pd.Series(set_ids, index=self.set_words)
        max_len = self._max_len
        self.words = len(self.set_words), len(self.set_tags)
        print(self.words)

        def X_padding(words):
            """把 words 转为 id 形式，并自动补全位 max_len 长度。"""
            ids = list(self.word2id[words])
            if len(ids) <= time_steps:
                ids.extend([0]*(time_steps - len(ids)))
            else:
                ids.extend([0]*int((time_steps/3)-len(ids)%(time_steps/3)))
            #if len(ids) >= max_len:  # 长则弃掉
#                return ids[:max_len]
#            ids.extend([0] * (max_len - len(ids)))  # 短则补全
            return ids

        def y_padding(tags):
            """把 tags 转为 id 形式， 并自动补全位 max_len 长度。"""
            ids = list(self.tag2id[tags])
            if len(ids) <= time_steps:
                ids.extend([0]*(time_steps - len(ids)))
            else:
                ids.extend([0]*int((time_steps/3)-len(ids)%(time_steps/3)))
            #if len(ids) >= max_len:  # 长则弃掉
 #               return ids[:max_len]
 #           ids.extend([0] * (max_len - len(ids)))
            return ids
        df_data['X'] = df_data['words'].apply(X_padding)
        #print(df_data['X'])
        df_data['y'] = df_data['tags'].apply(y_padding)
       # for i in range (len(df_data['X'].values)):
#            for j in range (len(df_data['X'].values[i])):
#                df_data['X'].values[i][j]=df_data['X'].values[i][j]/len(self.set_words)
        self._x = np.asarray(list(df_data['X'].values))
        #print(len(self.set_words))
        #print(df_data['X'].values[0])
        #print(len(df_data['X'].values[0]))
        #print(self._x)
        self._y = np.asarray(list(df_data['y'].values))
        # 划分训练集和测试集
        self.x_train, self.x_test, self.y_train, self.y_test = train_test_split(self._x, self._y, test_size=0.2,random_state=42)

        #print(len(self.x_test[0]))
        self.x_train2lstm = self.split_train(self.x_train)
        self.y_train2lstm = self.split_train(self.y_train)
 #       print(len(self.x_train2lstm))
        #print(self.x_train2lstm[0:10])
        self.x_train2lstm,self.y_train2lstm = self.random_sort(self.x_train2lstm,self.y_train2lstm)
        self.x_train2lstm = np.asarray(self.x_train2lstm)
        self.y_train2lstm = np.asarray(self.y_train2lstm)
        #print(self.x_train2lstm)
       # print(self.y_train2lstm)
        #print(self.x_train2lstm[0:100])
        self.x_test2lstm = []
        self.y_test2lstm = []
        for i in range(len(self.x_test)):
            testdata = self.split_test(self.x_test[i])
            self.x_test2lstm.append(testdata)
        for i in range(len(self.y_test)):
            testlabel = self.split_test(self.y_test[i])
            self.y_test2lstm.append(testlabel)
        #print(self.x_test2lstm[0])
        #print(self.y_test2lstm)
        #print(self.y_test2lstm[0:1])
        #self.x_test2lstm = np.asarray(self.split_train(self.x_test))
        #self.y_test2lstm = np.asarray(self.split_train(self.y_test))
 #       aa=self.embedding_y(self.y_train)
        #print(self.x_train[2])
        #print(len(self.x_train[2]))
        #print(aa)
        #print(len(self.set_words))
        #print(self.x_train2lstm[0])
        #print(self.x_train2lstm,self.x_test2lstm)

    @property
    def length(self):
        return self.words

    @property
    def y(self):
        return self._y

    @property
    def max_len(self):
        return self._max_len

    @property
    def train(self):
        return self.x_train2lstm, self.y_train2lstm

    @property
    def test(self):
        return self.x_test2lstm, self.y_test2lstm

    def random_sort(self, inputs, labels):
        temp = list(range(len(inputs)))
        random.shuffle(temp)
        result_inputs, reslut_labels = [], []
        for index in temp:
            result_inputs.append(inputs[index])
            reslut_labels.append(labels[index])
        return result_inputs, reslut_labels

    def split_train(self,inputs):
        result =[]
        for ii in inputs:
            k = 0
            while k < len(ii)-self.time_steps/3:
                result.append(ii[k:k+self.time_steps])
                k +=int(self.time_steps/3)
            result.pop()
        return result
    def split_test(self,inputs):
        result = []
        k = 0
        while k<len(inputs)-self.time_steps/3:
            result.append(inputs[k:k+self.time_steps])
            k += int(self.time_steps/3)
        result.pop()
        return result

    def embedding_y(self,label):
        result = []
        z = np.zeros(len(self.set_tags))
        for i in label:
            for j in i:
                z1 = z
                z1[j]=1
                result.append(z1)
        result.pop()
        return result

    def test2ids(self, text):
        words = list(text)
        ids = list(self.word2id[words])
        if len(ids) >= self._max_len:  # 长则弃掉
            print(u'输出片段超过%d部分无法处理' % (self._max_len))
            return ids[:self._max_len]
        ids.extend([0] * (self._max_len - len(ids)))  # 短则补全
        ids = np.asarray(ids).reshape([-1, self._max_len])
        return ids
#a=DataLoad()


from django.shortcuts import render
from .models import History

def update_history(form, app_label, modelname, object, user):
    for fieldname in form.changed_data:
        try:
            old_value=str(form.initial[fieldname]),
        except KeyError:
            old_value=None

        history = History.objects.create(
            user=user,
            app_label=app_label,
            modelname=modelname,
            objectid=object.pk,
            fieldname=fieldname,
            old_value=old_value,
            new_value=str(form.cleaned_data[fieldname])
        )

        history.save()


"""
remote -> remote: create TE w/ start time, backup TD
remote -> local: TD source, remote TE ID [1]
local -> local: TE w/o backup, w/ remote TE id
local -> remote: local TE ID (i.e. RQ job ID) [2]
remote -> remote: TE update local TE ID

[1]
local -> local: TE is finished
local -> remote: POST TR w/ remote TE ID

[2]
remote -> local: by local TE ID, request a HTTP streaming for console
"""

import json

from django.views.decorators.http import require_POST, require_GET
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponse, StreamingHttpResponse

from . import tasks
from . models import ConsoleLine

@csrf_exempt
@require_POST
def execute_test(request):
    j = json.loads(request.body)
    #source = j['test_data']
    #command = f'pybot {source["filename"]}'
    #remote_id = j['remote_id']
    #host = j['host']
    #rq_job = tasks.execute_test.delay(td_src=source, cmd=command, rte_id=remote_id, taas=host)
    rq_job = tasks.execute_test.delay(td_dict=j)
    return JsonResponse({'rq_jid': rq_job.id})

@require_GET
def monitor_test_execution(request, rq_jid):
    from .models import TestExecution, TestResult
    from time import sleep

    def fetch_lines(te, last_id):
        cs = ConsoleLine.objects.filter(id__gt=last_id, test_execution=te,).order_by('id')
        lines = (c.output for c in cs)
        if cs:
            last_id = cs.last().id
        return last_id, lines

    def monitor(te, last_id=-1):
        while not TestResult.objects.filter(test_execution=te):
            last_id, lines = fetch_lines(te, last_id)
            yield from lines
            sleep(1)
        last_id, lines = fetch_lines(te, last_id)
        yield from lines

    te = TestExecution.objects.get(pk=rq_jid)
    trs = TestResult.objects.filter(test_execution=te)
    if trs:
        return HttpResponse(trs.first().console)
    else:
        return StreamingHttpResponse(monitor(te))

from django.utils.decorators import method_decorator
from django.views import View
from .models import Sut

@require_GET
def detail_sut(request, uuid):
    sut = Sut.objects.get(pk=uuid)
    return JsonResponse(sut.to_dict())

@csrf_exempt
@require_POST
def reserve_sut(request, uuid):
    j = json.loads(request.body)
    Sut.objects.get(pk=uuid).reserve(j['reserved_by'])
    return HttpResponse()

@csrf_exempt
@require_POST
def use_sut(request, uuid):
    j = json.loads(request.body)
    Sut.objects.get(pk=uuid).use(j['in_use'])
    return HttpResponse()

from .models import Taas

@method_decorator(csrf_exempt, name='dispatch')
class TaasView(View):
    def get(self, request):
        taas = Taas.objects.first()
        return JsonResponse({'ip': taas.ip, 'port': taas.port}) if taas else JsonResponse({})

    def put(self, request):
        """
        Mark test harness registered/unregistered
        """
        j = json.loads(request.body)
        if j:
            taas, created = Taas.objects.update_or_create(defaults=j)
        else:
            Taas.objects.all().delete()
            Sut.objects.filter(reserved_by__is_staff=False).update(reserved_by=None)
        return HttpResponse()

@require_GET
def list_suts(request):
    return JsonResponse(Sut.dump_all(), safe=False)

@require_GET
def test_report_page(requests, te_id, name):
    from .models import TestExecution, TestResult
    tr = TestExecution.objects.get(pk=te_id).test_result
    return HttpResponse(getattr(tr, name))


from contextlib import contextmanager

# class-based context manager with error handling (*exc -exceptions)

class PoemFiles:

  def __init__(self, poem_file, mode):
    print(' \n -- Starting up a poem context manager -- \n')
    self.file = poem_file
    self.mode = mode

  def __enter__(self):
    print(' \n --  Opening poem file -- \n')
    self.opened_poem_file = open(self.file, self.mode)
    return self.opened_poem_file

  def __exit__(self, exc_type, exc_value, traceback):
    print(exc_type, exc_value, traceback, '\n')
    # Write your code below:
    if isinstance(exc_value, AttributeError):
      self.opened_poem_file.close()
      return True

# with PoemFiles('poem.txt', 'r') as file:
#   print("---- Exception data below ---- \n ")
#   print(file.uppercasewords())

# with PoemFiles('poem.txt', 'r') as file2:
#   print(file2.read())
#   print(" \n ---- Exception data below ---- \n ")

#contextlib

@contextmanager
def poem_files(file, mode):
  print('Opening File')
  open_poem_file = open(file, mode)
  try:
    yield open_poem_file
  finally:
    print('Closing File')
    open_poem_file.close()

# with poem_files('poem.txt', 'a') as opened_file:
#  print('Inside yield')
#  opened_file.write('Rose is beautiful, Just like you.')


def rectangle():
    a = float(input('Введите длину прямоугольника: '))
    b = float(input('Введите ширину прямоугольника: '))
    s = a * b
    return s

def triangle():
    a = float(input('Введите основание треугольника: '))
    b = float(input('Введите высоту треугольника: '))
    s = (a * b) / 2
    return s

def circle():
    r = float(input('Введите радиус круга: '))
    s = 3.14 * (r ** 2)
    return s

print('Напишите функции нахождения площади фигур:', end='\n\n')

figure = input('1-Прямоугольник, 2-Треугольник, 3-Круг: ')
try:
    if figure == '1':
        print(f'Площадь: {rectangle():.2f}')
    elif figure == '2':
        print(f'Площадь: {triangle():.2f}')
    elif figure == '3':
        print(f'Площадь: {circle():.2f}')
    else:
        print('Ошибка.Нет такой фигуры')
except ValueError:
    print('Ошибка ввода данных')


import cv2 as cv

# Read in an image
img = cv.imread('images/park.jpg')
cv.imshow('Park', img)

# Converting to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #BGR to Gray scale
cv.imshow('Gray', gray)

# Blur
blur = cv.GaussianBlur(img, (3,3), cv.BORDER_DEFAULT) #Here (3,3) the kernal size is the intensity of blur, can use like (7,7).. for increasing or decreasing
cv.imshow('Blur', blur)

## Edge Cascade
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)

# Dilating the image
dilated = cv.dilate(canny, (3,3), iterations=1) #By default the structure passing to the cv.dilate is "canny", and the no.of iterations can be increased or decrese.
cv.imshow('Dilated', dilated)

# Eroding
eroded = cv.erode(dilated, (7,7), iterations=1) #dilated is passed.
cv.imshow('Eroded', eroded)

# Resize
resized = cv.resize(img, (500,500), interpolation=cv.INTER_CUBIC) #Resize either compresses or expand the image with in the specific bound. And the interpolation is used while shrinking the image.
cv.imshow('Resized', resized)

## Cropping
cropped = img[50:200, 200:400]
cv.imshow('Cropped', cropped)

cv.waitKey(0)