import h5py
from PIL import Image
import numpy as np
from pathlib import Path
import urllib.request
import pandas as pd
import logging

import torch.utils.data as data
from torch.utils.data import Dataset

from .convert import convert
from .transforms import DataTransform
from .utils import _is_pil_image

logger = logging.getLogger('mylogger')

class DepthDataset(Dataset):
    def __init__(self, cfg, train=True):
        if train:
            self.data_root = Path(cfg['data_root']) / 'train'
        else:
            self.data_root = Path(cfg['data_root']) / 'test'

        self.datafile = cfg['train'] if train else cfg['test']
        # self.transform = DataTransform(cfg)
        self.dataset = cfg['name']
        self.train = train
        self.cfg = cfg

        if self.dataset == 'KITTI':
            self.angle_range = (-1, 1)
            self.depth_scale = 256.0
            self.max_depth = 80.0
        elif self.dataset == 'NYU':
            self.angle_range = (-2.5, 2.5)
            self.depth_scale = 1000.0
            self.max_depth = 10.0

        self.fileset = pd.read_csv(self.datafile, sep=' ', header=None)

    def __getitem__(self, index):
        rgb_file = self.data_root / self.fileset.iloc[index, 0]
        gt_file = self.data_root / self.fileset.iloc[index, 1]
        gt_dense_file = gt_file.parent / 'dense' / gt_file.name.replace('depth', 'depth_dense')

        rgb = Image.open(rgb_file).convert('RGB')
        gt = Image.open(gt_file)
        gt_dense = Image.open(gt_dense_file)

        if self.train:
            angle = np.random.uniform(self.angle_range[0], self.angle_range[1])
            rgb = rgb.rotate(angle, resample=Image.BILINEAR)
            gt = gt.rotate(angle, resample=Image.NEAREST)
            gt_dense = gt_dense.rotate(angle, resample=Image.NEAREST)

        # cropping in size that can be divided by 16
        if self.dataset == 'KITTI':
            h = rgb.height
            w = rgb.width
            bound_left = (w - 1216) // 2
            bound_right = bound_left + 1216
            bound_top = h - 352
            bound_bottom = bound_top + 352

        elif self.dataset == 'NYU':
            if self.train:
                bound_left = 43
                bound_right = 608
                bound_top = 45
                bound_bottom = 472
            else:
                bound_left = 60
                bound_right = 604
                bound_top = 56
                bound_bottom = 472

        # crop and normalize 0 to 1 ==>  rgb range:(0,1),  depth range: (0, max_depth)
        rgb = rgb.crop((bound_left, bound_top, bound_right, bound_bottom))
        rgb = np.asarray(rgb, dtype=np.float32) / 255.0

        if _is_pil_image(gt):
            gt = gt.crop((bound_left, bound_top, bound_right, bound_bottom))
            gt = (np.asarray(gt, dtype=np.float32)) / self.depth_scale
            gt = np.expand_dims(gt, axis=2)
            gt = np.clip(gt, 0, self.max_depth)

        if _is_pil_image(gt_dense):
            gt_dense = gt_dense.crop((bound_left, bound_top, bound_right, bound_bottom))
            gt_dense = (np.asarray(gt_dense, dtype=np.float32)) / self.depth_scale
            gt_dense = np.expand_dims(gt_dense, axis=2)
            gt_dense = np.clip(gt_dense, 0, self.max_depth)

        rgb, gt, gt_dense = np.array(rgb), np.array(gt), np.array(gt_dense)
        # rgb, gt, gt_dense = self.transform([rgb] + [gt] + [gt_dense], self.train)
        transform = DataTransform(self.cfg)
        rgb, gt, gt_dense = transform([rgb] + [gt] + [gt_dense], self.train)

        return rgb, gt, gt_dense

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

def download_NYU():
    download_url = {
        'data': 'http://horatio.cs.nyu.edu/mit/silberman/nyu_depth_v2/nyu_depth_v2_labeled.mat',
        'splits': 'http://horatio.cs.nyu.edu/mit/silberman/indoor_seg_sup/splits.mat'
    }
    data_root = Path('loader/data/NYU')
    data = data_root / 'nyu_depth_v2_labeled.mat'
    splits = data_root / 'splits.mat'
    if not data.is_file():
        url = download_url['data']
        logger.info(f'Downloading data from {url}')
        urllib.request.urlretrieve(url, data)

    if not splits.is_file():
        url = download_url['splits']
        logger.info(f'Downloading train/test splits from {url}')
        urllib.request.urlretrieve(url, splits)

    convert(data, splits, data_root)

def DepthDataLoader(cfg, splits):
    """Function to build data loader(s) for the specified splits given the parameters.
    """
    data_root = Path(cfg.get('data_root', '/path/to/dataset'))
    if not data_root.is_dir():
        raise Exception(f'{data_root} does not exist')

    train = data_root / 'train'
    if not train.is_dir():
        download_NYU()

    num_workers = cfg.get('n_workers', 4)
    batch_size = cfg.get('batch_size', 1)

    # data_loader = dict()
    train_loader = None
    test_loader = None
    for split in splits:

        data_list = Path(cfg.get(split, None))
        if not data_list.is_file():
            raise Exception(f'{data_list} not available')

        train = True if split == 'train' else False
        dataset = DepthDataset(cfg, train)

        if train:
            train_loader = data.DataLoader(
                dataset, batch_size=batch_size, shuffle=True, drop_last=False,
                pin_memory=True, num_workers=num_workers
            )
        else:
            test_loader = data.DataLoader(
                dataset, batch_size=batch_size, shuffle=False, drop_last=False,
                pin_memory=True, num_workers=num_workers
            )

        logger.info("{split}: {size}".format(split=split, size=len(dataset)))

    logger.info("Building data loader with {} workers".format(num_workers))

    return train_loader, test_loader


import datetime
import csv
import getLocation
import math

class Crime:
    def __init__(self,crimetype,title,url,datet,coords,location,streetname):
        self.crimetype = crimetype
        self.title = title
        self.url = url
        self.unixtime = datet
        self.datet = datetime.datetime.fromtimestamp(int(datet)).strftime('%Y-%m-%d %H:%M:%S')
        self.hour = self.datet[-8:-6]
        self.coords = coords
        self.street = streetname
        self.location = location
        self.datesec = datet % 86400

    def getDateTime(self):
        return self.datet
    def getTimeOnly(self):
        return self.datet[-8:-3]
    def getType(self):
        return self.crimetype
    def getMonth(self):
        return int(float(self.datet[5:7]))
    def getYear(self):
        return int(float(self.datet[:4]))
    def getTitle(self):
        return self.title
    def getUrl(self):
        return self.url
    def getLocationnonUrl(self):
        return self.location
    def getCoords(self):
        return self.coords

class Crimes:

    def __init__(self,datapath,typepath, optionalCsvStreetNameCoordinate):
        temp_data = open(datapath,'r')
        data = temp_data.read()
        self.streetscoord = list()
        tempdata = open(optionalCsvStreetNameCoordinate).read()

        while (tempdata.find("\n")!=-1):
            elem = tempdata[:tempdata.find("\n")]
            x = tempdata[:tempdata.find("\t")]
            tempdata = tempdata[tempdata.find("\t")+1:]
            y = tempdata[:tempdata.find("\t")]
            tempdata = tempdata[tempdata.find("\t")+1:]
            streettmp = tempdata[:tempdata.find("\n")]
            tempdata = tempdata[tempdata.find("\n")+1:]
            item=((x,y),streettmp)
            self.streetscoord.append(item)

        type_data = csv.reader(open(typepath,'r'))
        self.types = list()
        for row in type_data:
            for item in row:
                self.types.append(item)
        self.result = list()
        while (data.find(",\n,\n" )!= (-1)):
            data=data.replace(",\n,\n",",\n")

        while (data.find("}") != (-1)):
            if data.find("\n") != (-1):
                tempdata = self.toCrime(data[data.find("{")+1:data.find("\n")],self.streetscoord)
                if tempdata!=None:
                    self.result.append(tempdata)
                data = data[data.find("\n")+1:]
            else:
                tempdata =self.toCrime(data,self.streetscoord)
                if tempdata != None:
                    self.result.append(tempdata)
                data = ""
        #fileout = open("output.csv",'w')
        #out = ""
       # for item in self.streetscoord:
        #    out+= str(item[0][0]) +'\t'+str(item[0][1])+'\t'+str(item[1])+'\n'
       # fileout.write(out)
       # fileout.close()
    def printCrimeData(self):
        fileout = open("usefuldata.csv",'w')
        out = ""
        for crime in self.result:
            coords = crime.coords
            lat = coords[0][1:]
            lon = coords[1][1:]
            out += '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n'.format(crime.title, crime.crimetype, crime.hour, lat, lon, crime.street)
        fileout.write(out)
        fileout.close()

    def findofTypes (self, crimeTypes):
        finalres= list()
        for crimetype in crimeTypes:
            for crime in self.result:
               if (crimetype.lower() in crime.crimetype.lower()):
                    finalres.append(crime)
        return finalres

    def findWithinTime(self, targetTimeHour,targetTimeMinute, timeRangeMinutes):
       finalres= list()
       for crime in self.result:
            if ((crime.datesec >(targetTimeHour*3600+targetTimeMinute*60-timeRangeMinutes*60)) & (crime.datesec<(targetTimeHour*3600+targetTimeMinute*60+timeRangeMinutes*60))):
                finalres.append(crime)

       return finalres
    def findWithinDate(self,targetYear,targetMonth):
       finalres = list()
       for crime in self.result:
           if ((crime.getMonth()==int(float(targetMonth))) & (int(float(targetYear))== crime.getYear())):
               finalres.append(crime)
       return finalres

    def crimelisttoGoogle(self,listofCrimes):
        result="heatmapData = [\n"
        for crime in listofCrimes:
            if 'none' not in crime.coords[0].lower():
                result =result + "	new google.maps.LatLng(" + crime.coords[0]+", "+crime.coords[1]+"),\n"
        result = result+ "];"
        return result

    def streetCrimed(self, listofStreets):
        result = list()
        for street in listofStreets:
            tempres = 0
            for crime in self.result:
                if crime.street!=None:
                    if street.lower() in crime.street.lower():
                        tempres=tempres+1
            if tempres !=0:
                result.append((street,tempres))
        return result

    def toCrime (self,data,streetscoordtmp):
        try:
            crimetype = data[data.find(":")+1:data.find(",")]
            data = data[data.find(",")+1:]
            data = data[data.find("'")+1:]
            data = data[data.find("'")+1:]
            data = data[data.find("'")+1:]
            title = data[:data.find("'")]
            data = data[data.find("'"):]
            data = data[data.find("'")+1:]
            data = data[data.find("'")+1:]
            data = data[data.find("'")+1:]
            data = data[data.find("'")+1:]
            url = data[:data.find("'")]
            data = data[data.find("'"):]
            data = data[data.find(":")+2:]
            datetimesec = float(data[:data.find(",")])
            data = data[data.find(",")+1:]
            data = data[data.find(":")+1:]
            if (data.find("None)")!=-1):
                coords = ["None","None"]
            else:
                coordx = data[data.find(":")+1:data.find(",")]
                data = data[data.find(",")+1:]
                coordy = data[data.find(":")+1:data.find("}")]
                coords = [coordx,coordy]
            data = data[data.find("}")+1:]
            data = data[data.find(":")+1:]
            data = data[data.find("'")+1:]
            location = data[:data.find("'")]

            street = None
            if ('none'.lower() not in coords[0].lower()):
                for streetname in self.streetscoord:
                    if (round(float(coords[0]),7),round(float(coords[1]),7)) == (round(float(streetname[0][0]),7),round(float(streetname[0][1]),7)):
                        street = streetname[1]
#                        print("found")
                        break
                if street== None:
                    print("search")
                    street = str(getLocation.getName((float(coords[0]),float(coords[1]))))
                    self.streetscoord.append(((float(coords[0]),float(coords[1])),street))

            if 'none' in crimetype.lower():
                for item in self.types:
                    if item.lower() in title.lower():
                            crimetype = item
                            break
            if (coords!= ["None","None"]) :
                if ( (math.sqrt((float(coordx)-55.954198)**2+(float(coordy) + 3.188782)**2)<2)):
                    return Crime(crimetype,title,url,datetimesec,coords,location,street)
                return None
        except:
                return None


# Search a 2D Matrix
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        if not matrix:
            return False

        left = 0
        right = len(matrix)-1
        mid = (left+right)//2

        col = 0
        while left < right:
            if matrix[left][-1] >= target:
                col = left
                break
            if matrix[right][0] <= target:
                col = right
                break

            mid = (left+right)//2
            if matrix[mid][0] <= target and  matrix[mid][-1] >= target:
                col = mid
                break

            if matrix[mid][0] > target:
                right = mid-1
                continue
            if matrix[mid][-1] < target:
                left = mid +1
                continue

        if target in matrix[col]:
            return True
        return False

# # 신박!?
# # 제일 큰값을 확인해가며, 해당 row 에 있는지 확인
# class Solution:
#     def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:

#         if(matrix==[]):
#             return 0
#         n=len(matrix)
#         m=len(matrix[0])
#         i=0
#         j=m-1
#         while(i<n and j>=0):
#             if(matrix[i][j]==target):
#                 return True
#             elif(target>matrix[i][j]):
#                 i+=1
#             else:
#                 j-=1
#         return False

# #  nums=matrix[mid//n][mid%n] !?!?
# # 아!? r=m*n-1 로 정의했기에 !
# # Awesome
# # 동작방식 : 마치 하나의 arr 로 만들어서 동작하는 방식과 동일!!
# class Solution:
#     def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
#         if not matrix:
#             return 0

#         m=len(matrix)
#         n=len(matrix[0])
#         l=0
#         r=m*n-1

#         while l<=r:
#             mid=(l+r)//2
#             nums=matrix[mid//n][mid%n]
#             if nums==target:
#                 return True
#             elif nums<target:
#                 l=mid+1
#             else:
#                 r=mid-1
#         return False

# # 동작방식 : 마치 하나의 arr 로 만들어서 동작하는 방식과 동일!!
# class Solution:
#     def searchMatrix(self, ma: List[List[int]], target: int) -> bool:
#         if len(ma)>0:
#             n=len(ma)
#             m=len(ma[0])

#             low=0
#             high=n*m-1
#             while low<=high:
#                 mid=low+(high-low)//2
#                 if ma[mid//m][mid%m]==target:
#                     return True
#                 elif ma[mid//m][mid%m]>target:
#                     high=mid-1
#                 elif ma[mid//m][mid%m]<target:
#                     low=mid+1
#             else:
#                 return False
#         else:
#             return False


# -*- coding: utf-8 -*-
from utils.util import AttrDict, merge_cfg_from_args, get_arguments
import os

args = get_arguments()
cfg = AttrDict()

# 待预测图像所在路径
cfg.data_dir = "data"
# 待预测图像名称列表
cfg.data_list_file = os.path.join("data", "test.txt")
# 模型加载路径
cfg.model_path = 'SpatialEmbeddings_kitti'
# 预测结果保存路径
cfg.vis_dir = "result"

# 待预测图像输入尺寸
cfg.input_size = (384, 1248)
# sigma值
cfg.n_sigma = 2
# 中心点阈值
cfg.threshold = 0.94
# 点集数阈值
cfg.min_pixel = 160

merge_cfg_from_args(args, cfg)


from django.http import HttpResponse
from django.shortcuts import render
from datetime import date, datetime
from .create_zip import Create_Zip
from .models import *
from home.views import SetMoneda
import json
from wallet.models import Wallet

def Inbox(request):
	message = Message_Email.objects.filter(to=request.session['pk'],trash = False).order_by('-time')
	return render(request,'chat/inbox.html',{'message':message})

def Trash(request):
	message = Message_Email.objects.filter(to=request.session['pk'],trash = True).order_by('-time')
	return render(request,'chat/trash.html',{'message':message})

def Email_Sent(request):
	message = Message_Email.objects.filter(From=request.session['pk']).order_by('-time')
	return render(request,'chat/send.html',{'message':message})

def Compose_Email(request,pk):
	if request.is_ajax():
		# print(request.GET.get("message"))
		request.session['data_message'] = str(request.GET.get("message")).strip()
		return HttpResponse(True)
	if request.method == 'POST':
		try:
			user_from = Teacher.objects.get(pk = request.session['pk'])
		except  Teacher.DoesNotExist:
			user_from = Student.objects.get(pk = request.session['pk'])
		now = datetime.now()
		for i in range(len(request.POST.getlist('email'))):
			try:
				user_to = Teacher.objects.get(email = request.POST.getlist('email')[i])
			except  Teacher.DoesNotExist:
				user_to = Student.objects.get(email = request.POST.getlist('email')[i])
			Message_Email(
				From = request.session['pk'],
				to = user_to.pk,
				date = now.today().strftime('%d-%m-%Y %H:%M'),
				time = datetime.now(),
				message = request.session['data_message'],
				from_name = user_from.first_name+' '+user_from.surname,
				to_name = user_to.first_name+' '+user_to.surname,
				subject = request.POST.getlist('subject')[0]
			).save()
		del request.session['data_message']
		if len(request.FILES.getlist('files')) > 0:
			for i in range(len(request.FILES.getlist('files'))):
				Attachments(
					file = request.FILES.getlist('files')[i],
					message_email = Message_Email.objects.filter(pk = request.session['pk']).last()
				).save()
		n = int(request.session['sent']) + 1
		request.session['sent'] = n
	email = ""
	try:
		user_from = Teacher.objects.get(pk = pk)
		if pk != 0:
			email = user_from.email
	except  Teacher.DoesNotExist:
		user_from = None

	try:
		user_from = Student.objects.get(pk = pk)
		if pk != 0:
			email = user_from.email
	except Student.DoesNotExist:
		user_from = None

	return render(request,'chat/compose_email.html',{'email':email})

def View_Email(request,pk):
	message = Message_Email.objects.get(pk=pk)
	message.unread = True
	message.save()

	try:
		attachments = Attachments.objects.filter(message_email =pk)
	except Attachments.DoesNotExist:
		attachments = None

	if request.is_ajax():
		list_data = [url.file.url for url in attachments ]
		create_z = Create_Zip()
		return HttpResponse(create_z.Run(list_data))

	totals = len(attachments) if attachments is not None else 0

	if attachments is not None:
		list_attachments = []
		for i in attachments:
			list_attachments.append({
					'url':i.file.url,
					'name_file' : str(i.file).split('/')[1]
				})

	try:
		user = Teacher.objects.get(pk=message.From)
	except Teacher.DoesNotExist:
		user = Student.objects.get(pk=message.From)
	date = Values_Dates(message.date)
	if len(Values_Dates(message.date)) == 1:
		date = Values_Dates(message.date)[0][:len(Values_Dates(message.date)[0]) - 3]

	return render(request,'chat/view_email.html',{'message':message,'user':user,'date':date,'attachments':list_attachments,
													'totals':totals
												})

def Values_Dates(date_message):
	now = datetime.now()
	fecha_cad1 = now.today().strftime('%d-%m-%Y %H:%M')
	fecha_cad2 = date_message
	fecha1 = datetime.strptime(fecha_cad1, '%d-%m-%Y %H:%M')
	fecha2 = datetime.strptime(fecha_cad2, '%d-%m-%Y %H:%M')
	if str(fecha2 - fecha1).split()[0] == '-1':
		return str(fecha1 - fecha2).split()
	return str(fecha2 - fecha1).split()[0]

def Hours():
	import time
	hours = time.ctime().split()[3].split(':')[0]
	minutos = time.ctime().split()[3].split(':')[1]
	return str(hours)+':'+str(minutos)

def Trash_Email(request):
	message = Message_Email.objects.get(pk=request.GET.get('pk'))
	message.trash = True
	message.save()
	return HttpResponse("Hola")

def Delete_Email(request):
	Message_Email.objects.get(pk=request.GET.get('pk')).delete()
	return HttpResponse("Hola")

def Chat_Private(request):
	try:
		with open('/home/hayprofe/hay_profe/data_user.json','r') as file:
			print(json.loads(file.read()))
	except Exception as e:
		pass

	obj = Obj(request)[0]
	try:
		user = Teacher.objects.get(pk=request.session['pk'])
	except Teacher.DoesNotExist:
		user = Student.objects.get(pk=request.session['pk'])

	with open('/home/hayprofe/hay_profe/data_user.json','w') as file:
		x = {
			'id': user.pk,
			'name': str(obj.first_name)+' '+str(obj.surname),
			'number': str(obj.phone),
			'pic': 'http://localhost:8000'+str(obj.img.url)
		}
		json.dump(x,file)

	if request.is_ajax():
		if request.GET.get('pk'):
			data = json.loads(request.GET.get('pk'))
			Chat_Privates(
				sender = data['sender'],
				body = data['body'],
				time = data['time'],
				status = data['status'],
				recvId = data['recvId'],
				_id = data['id'],
				_from= 1,
				_to = 2
			).save()
			with open("/home/hayprofe/hay_profe/message.json",'r') as file:
				data_message = json.loads(file.read())
			x = data_message
			consec = len(Chat_Privates.objects.all())
			data = {
						"id":consec,
						"sender": data['sender'],
						"body": data['body'],
						"time": data['time'],
						"status": data['status'],
						"recvId": data['recvId'],
						"recvIsGroup": False
					}
			z = x.append(data)
			with open("/home/hayprofe/hay_profe/message.json",'w') as file:
				json.dump(x,file)

			return HttpResponse("Hola bebe")
		elif request.GET.get("recibe"):
			n = int(request.session['time_chat']) + 1
			print(request.GET)
			if request.GET.get("recibe"):

				try:
					user = Teacher.objects.get(pk=request.session['pk'])
				except Exception:
					user = Student.objects.get(pk=request.session['pk'])
				print(user.type)
				if str(user.type) != 'Teacher':
					#RECIBE EL DINERO EL PROFESOR
					wallet_recvi = Wallet.objects.get(user = Teacher.objects.get(pk=request.GET.get('recibe')))
					value_money = Teacher.objects.get(pk=request.GET.get('recibe'))
					print(float( str(wallet_recvi.amount).replace(',','') ))
					add = float( str(wallet_recvi.amount).replace(',','') ) + float(value_money.money)
					wallet_recvi.amount = SetMoneda(str(add), simbolo="US$", n_decimales=0)
					wallet_recvi.save()
					print(value_money)

					#PAGA EL ESTUDIANTE
					wallet_sender = Wallet.objects.get(user = Student.objects.get(email = request.session['student_email']))
					less = float( str(wallet_sender.amount).replace(',','') ) - float(value_money.money)
					discount = less
					wallet_sender.amount = SetMoneda(str(less), simbolo="US$", n_decimales=0)
					wallet_sender.save()
					request.session['amount_wallet'] = discount

			else:
				print("Error")

			request.session['time_chat'] = n

			return HttpResponse(request.session['time_chat'])

	try:
		user = Teacher.objects.get(pk=request.session['pk'])
	except Teacher.DoesNotExist:
		user = Student.objects.get(pk=request.session['pk'])
	return render(request,'chat/chat_private.html',{'user':user,'x':{"Hola":'Hola'}})

def Obj(request):
	_type = ""
	try:
		user = Teacher.objects.get(pk=request.session['pk'])
		_type = "teacher"
	except Teacher.DoesNotExist:
		user = Student.objects.get(pk=request.session['pk'])
		_type = "student"
	return [user,_type]


from tkinter import filedialog, messagebox, END, Frame, Label, Toplevel, Text, Button, Menu, WORD, Scrollbar, \
    Listbox, LabelFrame
from tkinter.tix import Tk
from PIL import Image
from PIL import ImageTk
from nltk import word_tokenize
from pymorphy2 import MorphAnalyzer
from help import HELPTEXT
import win32api
import pickle
import time

vocabulary = []

class Lexeme:
    def __init__(self, lemma, tags, endings):
        self.lemma = lemma
        self.tags = tags
        self.endings = endings

    def print_args(self):
        return self.lemma, self.tags, self.endings

def newFile():
    global vocabulary
    vocabulary = []
    outputText.delete(0, END)

def saveFile():
    file_path = filedialog.asksaveasfilename()
    if file_path != "":
        f = open(file_path, 'wb')
        pickle.dump(outputText.get(0, END), f)
        f.close()

def openFile():
    global vocabulary
    file_path = filedialog.askopenfilename()
    if file_path != "":
        f = open(file_path, 'rb')
        words = pickle.load(f)
        for i in range(len(words)):
            outputText.insert(0, str(words[i]))
        f.close()

def printFile():
    file_path = filedialog.askopenfilename()
    print(file_path)
    if file_path:
        win32api.ShellExecute(0, 'print', file_path, None, '.', 0)

def cleanWord():
    global outputText
    if outputText:
        answer = messagebox.askyesno(
            title="Вопрос",
            message="Вы уверены, что хотите очистить словарь?")
        if answer:
            outputText.delete(0, END)

def addWord():
    global vocabulary
    start_time = time.time()
    words = {}
    analyzer = MorphAnalyzer()
    vocabulary.append(inputText.get(1.0, END))
    tokenize_sentence = word_tokenize(vocabulary[0])
    for word in tokenize_sentence:
        parse_word = analyzer.parse(word)[0]
        word_word = parse_word.word
        word_lemma = parse_word.normal_form
        word_tags = parse_word.tag.cyr_repr
        word_ending = list(set(word_word) - set(word_lemma))
        if word_word is not word_lemma:
            words.update({word_word: {'lemma': word_lemma, 'tag': word_tags, 'ending': word_ending}})
    sorted_words = sorted(words)
    for key in sorted_words:
        lexeme = Lexeme((words[key]['lemma']), (words[key]['tag']), (words[key]['ending']))
        outputText.insert(0, str(lexeme.lemma) + '      ' + str(lexeme.tags) + '      ' \
                          + str(lexeme.endings))
    end_time = time.time()
    result_time = end_time - start_time
    print(str(result_time) + " seconds")
    vocabulary.clear()

def helpMenu():
    children = Toplevel()
    children.title('Helper')
    children.geometry("450x300+300+300")
    outputHelpText = Text(children, height=15, width=54)
    scrollb = Scrollbar(children, command=outputHelpText.yview)
    scrollb.grid(row=4, column=4, sticky='nsew')
    outputHelpText.grid(row=4, column=0, sticky='nsew', columnspan=3)
    outputHelpText.configure(yscrollcommand=scrollb.set)
    outputHelpText.insert('end', HELPTEXT)
    outputHelpText.configure(state='disabled')

def aboutProgramMenu():
    children = Toplevel()
    children.title('About program')
    lemmaLabel = Label(children, text='Я после этой ночи')
    lemmaLabel.pack()
    photo = ImageTk.PhotoImage(Image.open("about.jpg"))
    imageLabel = Label(children, image=photo)
    imageLabel.image = photo
    imageLabel.pack()

def generateForm():
    global generated_form
    analyzer = MorphAnalyzer()
    lemma_text = lemmaText.get(1.0, END).replace('\n', "")
    lemma_for_generate = analyzer.parse(lemma_text)[0]
    tags_text = tagsText.get(1.0, END).replace('\n', "")
    s = tags_text
    tags_for_generate = s.replace(',', '').split()
    if lemma_text or tags_text:
        children = Toplevel()
        children.title('Generated word')
        children.geometry("300x150+300+300")
        started_temporary_generated_form = lemma_for_generate.inflect({tags_for_generate[0]})
        for i in range(len(tags_for_generate)):
            over_temporary_generated_form = started_temporary_generated_form.inflect({tags_for_generate[i]})
            generated_form = over_temporary_generated_form
        lemmaLabel = Label(children, text=generated_form.word)
        lemmaLabel.pack(padx=10, pady=30)
    else:
        messagebox.showerror(
            "Ошибка",
            "Заполните поля")

def generateWord():
    children = Toplevel()
    children.geometry("420x200+300+300")
    global lemmaText
    lemmaFrame = Frame(children, bd=10)
    lemmaLabel = Label(lemmaFrame, text='Лексема', width=7, height=2)
    lemmaLabel.pack(side='left')
    lemmaText = Text(lemmaFrame, height=1, width=20)
    lemmaText.pack(side='right')
    global tagsText
    tagsFrame = Frame(children, bd=10)
    tagsLabel = Label(tagsFrame, text='Тэги', width=7, height=2)
    tagsLabel.pack(side='left')
    tagsText = Text(tagsFrame, height=1, width=20)
    tagsText.pack(side='right')
    childrenFrame = Frame(children, bd=10)
    generateFormButton = Button(childrenFrame, text='Сгенерировать', width=25, height=2)
    generateFormButton.config(command=generateForm)
    generateFormButton.pack(side='right')
    children.title('Генерация слова')
    lemmaFrame.pack(side='top')
    tagsFrame.pack(side='top')
    childrenFrame.pack(side='bottom')

class App(Tk):
    def __init__(self):
        super().__init__()
        # -----------------------------------------INPUT--------------------------------------------------
        global inputText
        inputFrame = LabelFrame(self, bd=0, text='Ввод текста')
        inputText = Text(inputFrame, height=8, width=80, wrap=WORD)
        # -------------------------------------------OUTPUT------------------------------------------------
        global outputText
        outputFrame = LabelFrame(self, bd=0, text='Вывод словаря')
        outputText = Listbox(outputFrame, height=10, width=120)
        scrollb = Scrollbar(outputFrame, command=outputText.yview)
        scrollb.grid(row=4, column=5, sticky='nsew')
        outputText.grid(row=4, column=0, sticky='nsew', columnspan=5)
        outputText.configure(yscrollcommand=scrollb.set)
        # ------------------------------------------MENU---------------------------------------------------
        mainMenu = Menu(self)
        fileSubMenu = Menu(mainMenu, tearoff=0)
        fileSubMenu.add_command(label="Новый файл", command=newFile)
        fileSubMenu.add_command(label="Открыть...", command=openFile)
        fileSubMenu.add_command(label="Сохранить...", command=saveFile)
        fileSubMenu.add_command(label="Печать...", command=printFile)
        fileSubMenu.add_command(label="Выход", command=self.exitFile)

        helpSubMenu = Menu(mainMenu, tearoff=0)
        helpSubMenu.add_command(label="Помощь", command=helpMenu)
        helpSubMenu.add_command(label="О программе", command=aboutProgramMenu)

        mainMenu.add_cascade(label="Файл", menu=fileSubMenu)
        mainMenu.add_cascade(label="Справка", menu=helpSubMenu)
        self.config(menu=mainMenu)
        # ------------------------------------------Buttons---------------------------------------------------
        buttonsFrame = Frame(self, bd=5)
        addWordsButton = Button(buttonsFrame, text='Добавить', width=25, height=2)
        addWordsButton.config(command=addWord)
        addWordsButton.pack(side='left')
        spaceLabel1 = Label(buttonsFrame, width=7, height=2)
        spaceLabel1.pack(side='left')
        addWordsButton = Button(buttonsFrame, text='Очистить', width=25, height=2)
        addWordsButton.config(command=cleanWord)
        addWordsButton.pack(side='left')
        spaceLabel2 = Label(buttonsFrame, width=7, height=2)
        spaceLabel2.pack(side='left')
        generateNewWordsButton = Button(buttonsFrame, text='Сгенерировать', width=25, height=2)
        generateNewWordsButton.config(command=generateWord)
        generateNewWordsButton.pack(side='left')
        self.title('Lab 1')
        outputFrame.pack()
        inputFrame.pack()
        inputText.pack()
        buttonsFrame.pack()
        self.geometry('800x400')

    def exitFile(self):
        if outputText.get(0, END) != ():
            answer = messagebox.askyesno(
                title="Вопрос",
                message="Сохранить словарь?")
            if answer:
                saveFile()
                self.destroy()
            else:
                self.destroy()
        else:
            self.destroy()

if __name__ == "__main__":
    app = App()
    app.mainloop()


# coding: utf-8

# # Installation

# In[ ]:

#conda install matplotlib
#pip install matplotlib

# # Importing matplotlib

# In[56]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

#basic code:
from matplotlib import pyplot as plt

#Plotting to our canvas
plt.plot([1,2,3],[4,5,1])

#Showing what we plotted
plt.show()

# In[57]:

from matplotlib import pyplot as plt

x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

# In[62]:

from matplotlib import pyplot as plt
from matplotlib import style

style.use('ggplot')
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.plot(x,y,'g',label='line one', linewidth=9)
plt.plot(x2,y2,'r',label='line two',linewidth=9)
plt.title('Chart Information')
plt.ylabel('Based on Y axis')
plt.xlabel('Based on X axis')
plt.legend()
plt.grid(True,color='k')
plt.show()

# In[64]:

from matplotlib import pyplot as plt

plt.bar([0.25,1.25,2.25,3.25,4.25],[50,40,70,80,20],
label="Maruti",width=.5)
plt.bar([.75,1.75,2.75,3.75,4.75],[80,20,20,50,60],
label="Jaguar", color='g',width=.5)
plt.legend()
plt.xlabel('Days')
plt.ylabel('Distance (kms)')
plt.title('Car Information')
plt.show()

# In[65]:

import matplotlib.pyplot as plt
population_age = [22,55,62,45,21,22,34,42,42,4,2,102,95,85,55,110,120,70,65,55,111,115,80,75,65,54,44,43,42,48]
bins = [0,10,20,30,40,50,60,70,80,90,100]
plt.hist(population_age, bins, histtype='bar', rwidth=0.9)
plt.xlabel('Age Groups')
plt.ylabel('Total Number of people')
plt.title('Histogram Diagram')
plt.show()

# In[68]:

import matplotlib.pyplot as plt
x = [1,1.7,2,3.5,3,3.5,3.8]
y = [7.9,8,8.4,9,9.3,10,10.2]

x1=[8,8.2,9,9.7,10,10.3,11]
y1=[3,3.2,4.7,4,4.5,5,5.7]

plt.scatter(x,y, label='high income low saving',color='r')
plt.scatter(x1,y1,label='low income high savings',color='g')
plt.xlabel('saving*100')
plt.ylabel('income*1000')
plt.title('Scatter Plot Example')
plt.legend()
plt.show()

# In[47]:

import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping =[7,8,6,11,7]
eating = [2,3,4,3,2]
working =[7,8,7,2,2]
playing = [8,5,7,8,13]

plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.plot([],[],color='c', label='Eating', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Example for Stack Plot')
plt.legend()
plt.show()

# In[50]:

import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleeping =[7,8,6,11,7]
eating = [2,3,4,3,2]
working =[7,8,7,2,2]
playing = [8,5,7,8,13]
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
colrs = ['c','m','r','b']

plt.pie(slices,
  labels=activities,
  colors=colrs,
  startangle=90,
  shadow= True,
  explode=(0,0.1,0,0),
  autopct='%1.1f%%')

plt.title('Example for Pie Plot')
plt.show()

# In[53]:

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2))
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2))
plt.show()

# In[55]:

import pandas as pd
import matplotlib.transforms as mtransforms

url = 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=VIXCLS'
vix = pd.read_csv(url, index_col=0, parse_dates=True, na_values='.',
                  infer_datetime_format=True,
                  squeeze=True).dropna()
ma = vix.rolling('90d').mean()
state = pd.cut(ma, bins=[-np.inf, 14, 18, 24, np.inf],
               labels=range(4))

cmap = plt.get_cmap('RdYlGn_r')
ma.plot(color='black', linewidth=1.5, marker='', figsize=(8, 4),
        label='VIX 90d MA')
ax = plt.gca()  # Get the current Axes that ma.plot() references
ax.set_xlabel('')
ax.set_ylabel('90d moving average: CBOE VIX')
ax.set_title('Volatility Regime State')
ax.grid(False)
ax.legend(loc='upper center')
ax.set_xlim(xmin=ma.index[0], xmax=ma.index[-1])

trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes)
for i, color in enumerate(cmap([0.2, 0.4, 0.6, 0.8])):
    ax.fill_between(ma.index, 0, 1, where=state==i,
                    facecolor=color, transform=trans)
ax.axhline(vix.mean(), linestyle='dashed', color='xkcd:dark grey',
           alpha=0.6, label='Full-period mean', marker='')

# # Basic Plotting, Plotting terminology

# In[3]:

x = np.linspace(0,10,100)
y = np.sin(x)
z = np.cos(x)

# In[4]:

x

# In[16]:

from matplotlib import pyplot as plt

x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

# In[15]:

from matplotlib import pyplot as plt

x = [5,2,7]
y = [2,16,4]
plt.plot(x,y)
plt.title('Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

# In[9]:

# Let’s illustrate with a stacked area graph of three time series:
rng = np.arange(50)
rnd = np.random.randint(0, 10, size=(3, rng.size))
yrs = 1950 + rng

fig, ax = plt.subplots(figsize=(5, 3))
ax.stackplot(yrs, rng + rnd, labels=['Eastasia', 'Eurasia', 'Oceania'])
ax.set_title('Combined debt growth over time')
ax.legend(loc='upper left')
ax.set_ylabel('Total debt')
ax.set_xlim(xmin=yrs[0], xmax=yrs[-1])
fig.tight_layout()

# In[10]:

x = np.random.randint(low=1, high=11, size=50)
y = x + np.random.randint(1, 5, size=x.size)
data = np.column_stack((x, y))

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2,
                               figsize=(8, 4))

ax1.scatter(x=x, y=y, marker='o', c='r', edgecolor='b')
ax1.set_title('Scatter: $x$ versus $y$')
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')

ax2.hist(data, bins=np.arange(data.min(), data.max()),
         label=('x', 'y'))
ax2.legend(loc=(0.65, 0.8))
ax2.set_title('Frequencies of $x$ and $y$')
ax2.yaxis.tick_right()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import math
import logging
import numpy as np
from torch.nn.utils.rnn import pack_padded_sequence
from torch.nn.utils.rnn import pad_packed_sequence

def from_numpy(states):
    return [Variable(torch.from_numpy(state)).cuda() for state in states]

def get_GloVe(text, size, vectors, aggregation='mean'):
    vec = np.zeros(size).reshape((1, size))  # create for size of glove embedding and assign all values 0
    count = 0
    for word in text.split():
        print('\n word:   ', word)
        try:
            print('glove vect', vectors[word])
            vec += vectors[word].reshape((1, size))  # update vector with new word
            count += 1  # counts every word in sentence
        except KeyError:
            continue
    if aggregation == 'mean':
        if count != 0:
            vec /= count  # get average of vector to create embedding for sentence
        return vec
    elif aggregation == 'sum':
        return vec

class CNNRewardModel(nn.Module):
    def __init__(self, vocab_size, feat_size, dropout, activation):
        super(CNNRewardModel, self).__init__()
        self.vocab_size = vocab_size
        self.word_embed_dim = 300
        self.feat_size = feat_size
        self.kernel_num = 512
        self.kernels = [2, 3, 4, 5]
        self.out_dim = len(self.kernels) * self.kernel_num + self.word_embed_dim

        self.emb = nn.Embedding(self.vocab_size, self.word_embed_dim)
        self.emb.weight.data.copy_(torch.from_numpy(np.load('./data/pkls/glove_vocab_embedding.npy')))

        self.proj = nn.Linear(36*feat_size, self.word_embed_dim)

        self.convs = nn.ModuleList([nn.Conv2d(1, self.kernel_num, (k, self.word_embed_dim)) for k in self.kernels])

        self.dropout = nn.Dropout(dropout)

        self.fc = nn.Linear(self.out_dim, 1, bias=True)

        if activation.lower() == "linear":
            self.activation = None
        elif activation.lower() == "sign":
            self.activation = nn.Softsign()
        elif activation.lower() == "tahn":
            self.activation = nn.Tanh()
        elif activation.lower() == "sm":
            self.activation = nn.Softmax()
        elif activation.lower() == 'sigmoid':
            self.activation = nn.Sigmoid()

    def forward(self, instruction, img_feats, instruction_lens=None):

        embedding = Variable(self.emb(instruction).data)  # (batch_size, seq_length, embed_dim)
        # batch x seq_len x emb_dim -> batch x 1 x seq_len x emb_dim
        # embedding = embedding.unsqueeze(1)
        x = [F.relu(conv(embedding)).squeeze(3) for conv in self.convs]
        x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x]
        x = torch.cat(x, 1)

        # combine with image feature
        img = self.proj(img_feats)
        combined = torch.cat([x, img], 1)
        combined = self.dropout(combined)

        prob = self.fc(combined).view(-1)
        return self.activation(prob)

class GRURewardModel(nn.Module):

    def __init__(self, vocab_size, feat_size, dropout, activation):

        super(GRURewardModel, self).__init__()

        self.vocab_size = vocab_size
        self.word_embed_dim = 300
        self.gru_units = 2
        self.feat_size = feat_size
        self.kernel_num = 512
        self.kernels = [2, 3, 4, 5]
        self.out_dim = len(self.kernels) * self.kernel_num + self.word_embed_dim

        self.emb = nn.Embedding(self.vocab_size, self.word_embed_dim)
        self.emb.weight.data.copy_(torch.from_numpy(np.load('./data/pkls/glove_vocab_embedding.npy')))

        self.gru = nn.GRU(input_size=self.word_embed_dim, hidden_size=self.gru_units, batch_first=True)

        self.fc1 = nn.Linear(36 * feat_size, self.word_embed_dim)
        self.fc2 = nn.Linear(self.gru_units, 1)
        self.dropout = nn.Dropout(dropout)
        if activation.lower() == "linear":
            self.activation = None
        elif activation.lower() == "sign":
            self.activation = nn.Softsign()
        elif activation.lower() == "tahn":
            self.activation = nn.Tanh()
        elif activation.lower() == 'sigmoid':
            self.activation = nn.Sigmoid()

    def forward(self, instruction, img_feats, instruction_lens=None):
        img_feats = self.fc1(img_feats)
        embeddings = Variable(self.emb(instruction).data)
        inputs = torch.cat((img_feats.unsqueeze(1), embeddings.squeeze(1)), 1)
        outputs, _ = self.gru(inputs)
        outputs = self.dropout(outputs)
        pred = self.activation(self.fc2(outputs))
        return pred.squeeze(-1)


t = int(input())

for _ in range(t):
    n = int(input())
    arr = [int(input()) for _ in range(n)]
    max_val = max(arr)

    if arr.count(max_val) > 1:
        print("no winner")
        continue

    idx = arr.index(max_val) + 1
    print('majority winner', idx) if sum(arr) - max_val < max_val else print('minority winner', idx)


#coding:utf8

import json, time
from tornado import ioloop
import sqlutil

import logging
logger = logging.getLogger('hell')

MapSize = 20
MaxBuild = 20

buildinglevelcfg = {
	1: {
		0: {'timeinterval': 0, 'generate': 0, 'upgrade': 100, 'timedisabled': 600},
		1: {'timeinterval': 15, 'generate': 10, 'upgrade': 100, 'timedisabled': 600},
		2: {'timeinterval': 15, 'generate': 20, 'upgrade': 200, 'timedisabled': 600},
		3: {'timeinterval': 15, 'generate': 30, 'upgrade': 300, 'timedisabled': 600},
		4: {'timeinterval': 15, 'generate': 40, 'upgrade': 400, 'timedisabled': 600},
		5: {'timeinterval': 15, 'generate': 50, 'upgrade': 400, 'timedisabled': 600},
		'maxlevel': 5,
	},
	2: {
		0: {'timeinterval': 0, 'generate': 0, 'upgrade': 100, 'timedisabled': 600},
		1: {'timeinterval': 15, 'generate': 10, 'upgrade': 100, 'timedisabled': 600},
		2: {'timeinterval': 15, 'generate': 20, 'upgrade': 200, 'timedisabled': 600},
		3: {'timeinterval': 15, 'generate': 30, 'upgrade': 300, 'timedisabled': 600},
		4: {'timeinterval': 15, 'generate': 40, 'upgrade': 400, 'timedisabled': 600},
		5: {'timeinterval': 15, 'generate': 50, 'upgrade': 500, 'timedisabled': 600},
		'maxlevel': 5,
	},
	3: {
		0: {'timeinterval': 0, 'generate': 0, 'upgrade': 100, 'timedisabled': 600},
		1: {'timeinterval': 10, 'generate': 11, 'upgrade': 500, 'timedisabled': 600},
		2: {'timeinterval': 10, 'generate': 22, 'upgrade': 600, 'timedisabled': 600},
		3: {'timeinterval': 10, 'generate': 33, 'upgrade': 70, 'timedisabled': 600},
		4: {'timeinterval': 10, 'generate': 44, 'upgrade': 800, 'timedisabled': 600},
		5: {'timeinterval': 15, 'generate': 50, 'upgrade': 900, 'timedisabled': 600},
		'maxlevel': 5,
	},
	4: {
		0: {'timeinterval': 0, 'generate': 0, 'upgrade': 100, 'timedisabled': 600},
		1: {'timeinterval': 10, 'generate': 11, 'upgrade': 500, 'timedisabled': 600},
		2: {'timeinterval': 10, 'generate': 22, 'upgrade': 600, 'timedisabled': 600},
		3: {'timeinterval': 10, 'generate': 33, 'upgrade': 700, 'timedisabled': 600},
		4: {'timeinterval': 10, 'generate': 44, 'upgrade': 800, 'timedisabled': 600},
		5: {'timeinterval': 15, 'generate': 50, 'upgrade': 900, 'timedisabled': 600},
		'maxlevel': 5,
	},
	5: {
		0: {'timeinterval': 0, 'generate': 0, 'upgrade': 100, 'timedisabled': 600},
		1: {'timeinterval': 8, 'generate': 13, 'upgrade': 600, 'timedisabled': 600},
		2: {'timeinterval': 8, 'generate': 24, 'upgrade': 700, 'timedisabled': 600},
		3: {'timeinterval': 8, 'generate': 35, 'upgrade': 800, 'timedisabled': 600},
		4: {'timeinterval': 8, 'generate': 46, 'upgrade': 900, 'timedisabled': 600},
		5: {'timeinterval': 8, 'generate': 50, 'upgrade': 1000, 'timedisabled': 600},
		'maxlevel': 5,
	},
}

attackcfg = {
	1:{'attackid': 1, 'attackname': u'断水', 'cost': 100,  'attacktime': 60 * 5, 'attackpoint': 2,},
	2:{'attackid': 2, 'attackname': u'断网', 'cost': 120,  'attacktime': 60 * 5, 'attackpoint': 3,},
	3:{'attackid': 3, 'attackname': u'断电', 'cost': 200,  'attacktime': 60 * 5, 'attackpoint': 4,},
	4:{'attackid': 4, 'attackname': u'扔垃圾', 'cost': 150,  'attacktime': 60 * 5, 'attackpoint': 5,},
}
guardcfg = {}

def ontimeReadDb():
	global attackcfg, buildinglevelcfg, guardcfg

	tmp = sqlutil.ReadDataFromDB('attackconfig')
	attackcfg = {}
	for p in tmp:
		attackcfg[p['attackid']] = p

	tmp = sqlutil.ReadDataFromDB('guardconfig')
	guardcfg = {}
	for p in tmp:
		guardcfg[p['guardid']] = p

	txt = json.dumps(attackcfg, encoding='utf-8', ensure_ascii=False, indent=2)
	#print(txt)
	logger.info(txt)

	bname = sqlutil.ReadDataFromDB('buildingconfig')
	name = {}
	for p in bname:
		name[p['buildid']] = p['name']

	logger.info(name)

	tmp = sqlutil.ReadDataFromDB('buildinglevelconfig')
	buildinglevelcfg = {}
	for p in tmp:
		buildinglevelcfg[p['buildid']] = {}
	for p in tmp:
		buildinglevelcfg[p['buildid']][p['level']] = p
	for p in buildinglevelcfg:
		buildinglevelcfg[p]['maxlevel'] = len(buildinglevelcfg[p]) - 1
		buildinglevelcfg[p]['name'] = name.get(p, '')

	txt = json.dumps(buildinglevelcfg, encoding='utf-8', ensure_ascii=False, indent=2)
	#print(txt)
	logger.info(txt)

ioloop.PeriodicCallback(ontimeReadDb, 1000 * 60 * 30).start()
ontimeReadDb()

def getAttacPoint(aid):
	if aid in attackcfg:
		return attackcfg[aid]['cost']
	return 0

def getGuardPoint(gid):
	if gid in guardcfg:
		return guardcfg[gid]['cost']
	return 0

class AttackItem:
	def __init__(self):
		self.attackid = 0
		self.begintime = 0
		self.attacktime = 0

	def todict(self):
		d = {
		'attackid': self.attackid,
		'attacktime': self.attacktime,
		'begintime': self.begintime
		}
		return d

	def isValid(self):
		return time.time() <= self.begintime + self.attacktime

	def updatedict(self, d):
		if d.get('attackid'): self.attackid = d.get('attackid')
		if d.get('attacktime'): self.attacktime = d.get('attacktime')
		if d.get('begintime'): self.begintime = d.get('begintime')

	def guard(self, num):
		self.attacktime -= num
		self.attacktime = max(0, self.attacktime)

	def __repr__(self):
		return str([self.attackid, self.begintime, self.attacktime])

class EffectItem:
	def __init__(self):
		self.begintime = 0
		self.validtime = 0

	def valid(self):
		return self.begintime + self.validtime > time.time()

	def todict(self):
		d = {
		'validtime': self.validtime,
		'begintime': self.begintime,
		'valid': self.valid(),
		}
		return d

	def updatedict(self, d):
		if d.get('begintime'): self.begintime = d.get('begintime')
		if d.get('validtime'): self.validtime = d.get('validtime')

	def __repr__(self):
		return str([self.validtime, self.begintime])

class Builder:
	def __init__(self):
		self.bid = 0
		self.level = 0
		self.state = 1
		self.updatetime = 0
		self.attacks = []

	def todict(self):
		atmp = []
		for p in self.attacks:
			arr = []
			for q in p:
				arr.append(q)
			atmp.append(arr)

		d = {
			'bid': self.bid,
			'level': self.level,
			'state': self.state,
			'updatetime': self.updatetime,
			'attacks': atmp,
		}
		return d

	def updatedict(self, d):
		if d.get('bid'): self.bid = d.get('bid')
		if d.get('level'): self.level = d.get('level')
		if d.get('state'): self.updatetime = d.get('state')
		if d.get('updatetime'): self.buildstate = d.get('updatetime')
		if d.get('attacks'):
			self.attacks = []
			for p in d.get('attacks'):
				a = AttackItem()
				a.updatedict(p)
				self.attacks.append(a)

	def getGen(self):
		ret = 0
		retflag = False

		b = self.bid
		l = self.level
		u = self.updatetime
		if b > 0 and l > 0:
			cfg = buildinglevelcfg[b][l]
			t = time.time() - u
			if t > cfg['timeinterval'] + cfg['timedisabled']:
				self.state = 0
			elif t >= cfg['timeinterval']:
				ret = cfg['generate']
				retflag = True
			else:
				ret = int(t / cfg['timeinterval'] * cfg['generate'])

		return {'gen':ret, 'genflag':retflag, 'buildstate': self.state}

class MapInfo(object):
	"""docstring for MapInfo"""
	def __init__(self):
		self.map = [0 for _ in range(MapSize)]
		self.buildlevel = [0 for _ in range(MapSize)]
		self.updatetime = [time.time() for _ in range(MapSize)]
		self.buildstate = [1 for _ in range(MapSize)]

		self.map = [0 for _ in range(MapSize)]
		for i in range(5):
			self.map[i] = i + 1

		self.buildlevel = [0 for _ in range(MapSize)]
		self.attacks = [[] for _ in range(MapSize)]
		self.doubleinfo = [EffectItem() for _ in range(MapSize)]

		self.protectinfo = [EffectItem() for _ in range(MapSize)]

	def __repr__(self):
		return str(self.__dict__)

	def getAttackDict(self, index = -1):
		if index == -1:
			atmp = []
			for p in self.attacks:
				arr = []
				for q in p:
					arr.append(q.todict())
				atmp.append(arr)
			return atmp
		else:
			arr = []
			for q in self.attacks[index]:
				arr.append(q.todict())
			return arr

	def todict(self):
		atmp = self.getAttackDict()

		d = {
			'map': self.map,
			'buildlevel': self.buildlevel,
			'updatetime': self.updatetime,
			'buildstate': self.buildstate,
			'attacks': atmp,
			'doubleinfo': [p.todict() for p in self.doubleinfo],
			'protectinfo': [p.todict() for p in self.protectinfo],
		}
		return d

	def tojson(self):
		return json.dumps(self.todict())

	def updatejson(self, d):
		#logger.debug("updatejson: %s", d)
		if d != '':
			d = json.loads(d)
			if d.get('map'): self.map = d.get('map')
			if d.get('buildlevel'): self.buildlevel = d.get('buildlevel')
			if d.get('updatetime'): self.updatetime = d.get('updatetime')
			if d.get('buildstate'): self.buildstate = d.get('buildstate')
			if d.get('attacks'):
				self.attacks = []
				for p in d.get('attacks'):
					arr = []
					for q in p:
						#logger.debug("item: %s", q)
						a = AttackItem()
						a.updatedict(q)
						arr.append(a)
					self.attacks.append(arr)
				for i in range(MapSize):
					self.delAttack(i)

			if d.get('doubleinfo'):
				dinfo = d.get('doubleinfo')
				for i in range(len(dinfo)):
					self.doubleinfo[i].updatedict(dinfo[i])

			if d.get('protectinfo'):
				dinfo = d.get('protectinfo')
				for i in range(len(dinfo)):
					self.protectinfo[i].updatedict(dinfo[i])

	def getAttackNum(self, i, tm):
		ret = 0
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]

			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				for p in self.attacks[i]:
					aendtm = p.begintime + p.attacktime
					b = max(self.updatetime[i], p.begintime)
					e = min(aendtm, tm)
					if b < e:
						ret += int(e - b) * attackcfg[p.attackid]['attackpoint']
		return ret

	def addAttack(self, i, aid):
		if 0 <= i < MapSize and aid in attackcfg:
			b = self.map[i]
			l = self.buildlevel[i]
			if b <= 0 and l <= 0:
				return False

			if self.protectinfo[i].valid():
				return False

			ishas = False
			self.delAttack(i)
			for j in range(len(self.attacks[i])):
				if self.attacks[i][j].attackid == aid:
					self.attacks[i][j].attacktime += attackcfg[aid]['attacktime']
					ishas = True
					break
			if not ishas:
				a = AttackItem()
				a.attackid = aid
				a.begintime = time.time()
				a.attacktime = attackcfg[aid]['attacktime']
				self.attacks[i].append(a)
			return True
		return False

	def guard(self, i, gid):
		if 0 <= i < MapSize and gid in guardcfg:
			self.delAttack(i)
			gcfg = guardcfg[gid]

			logger.debug("%s, %s", gcfg, self.attacks[i])

			if gcfg['guardtype'] == 1:
				for j, p in enumerate(self.attacks[i]):
					if p.attackid == gcfg['attackid']:
						self.attacks[i][j].guard(gcfg['guardtime'])
						return True

			elif gcfg['guardtype'] == 2:
				if len(self.attacks[i]) <= 0:
					if self.protectinfo[i].valid():
						self.protectinfo[i].validtime += gcfg['guardtime']
					else:
						self.protectinfo[i].begintime = time.time()
						self.protectinfo[i].validtime = gcfg['guardtime']
					return True

		return False

	def delAttack(self, i):
		ret = False
		if 0 <= i < MapSize:
			arr = []
			for p in self.attacks[i]:
				aid = p.attackid
				if p.isValid():
					arr.append(p)
			ret = len(arr) == len(self.attacks[i])
			self.attacks[i] = arr
		return ret

	def createBuild(self, i, bid):
		if self.map[i] == 0 and bid in buildinglevelcfg:
			self.map[i] = bid
			self.buildlevel[i] = 1
			self.buildstate[i] = 1
			self.updatetime[i] = time.time()
			return True
		return False

	def getSecAttack(self, i, tmnow):
		ret = 0
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]

			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				for p in self.attacks[i]:
					aendtm = p.begintime + p.attacktime
					b = max(u, p.begintime)
					e = min(aendtm, tmnow)
					if b <= tmnow <= e:
						ret += attackcfg[p.attackid]['attackpoint']
		return ret

	def getSecDouble(self, i, tmnow):
		ret = 0
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]
			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				p = self.doubleinfo[i]
				if p.valid():
					aendtm = p.begintime + p.validtime
					if aendtm > self.updatetime[i]:
						b = max(self.updatetime[i], p.begintime)
						e = min(aendtm, tmnow)
						ret = cfg['generate']
		return ret

	def getGenAll(self):
		ret = [0 for _ in range(MapSize)]
		retflag = [False for _ in range(MapSize)]
		attackpoint = [0 for _ in range(MapSize)]

		gensec = 0

		for i in range(MapSize):
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]

			#self.delAttack(i)

			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				tmnow = time.time()
				t = tmnow - u
				if self.buildstate[i] == 0:
					pass
				elif t > cfg['timeinterval'] + cfg['timedisabled']:
					if self.buildstate[i] != 0:
						self.buildstate[i] = 2
						attackpoint[i] = self.getAttackNum(i, u + cfg['timeinterval'])
						ret[i] = cfg['generate'] * cfg['timeinterval'] - attackpoint[i] + self.getDoubleNum(i, u + cfg['timeinterval'])
						ret[i] = max(0, ret[i])
						retflag[i] = True

				elif t >= cfg['timeinterval']:
					attackpoint[i] = self.getAttackNum(i, u + cfg['timeinterval'])
					ret[i] = cfg['generate'] * cfg['timeinterval'] - attackpoint[i] + self.getDoubleNum(i, u + cfg['timeinterval'])
					ret[i] = max(0, ret[i])
					retflag[i] = True
				else:
					attackpoint[i] = self.getAttackNum(i, tmnow)
					ret[i] = int(t) * cfg['generate'] - attackpoint[i]  + self.getDoubleNum(i, tmnow)
					ret[i] = max(0, ret[i])

					tmpsec = cfg['generate'] + self.getSecDouble(i, tmnow) - self.getSecAttack(i, tmnow)
					tmpsec = max(0, tmpsec)
					gensec += tmpsec

		return {'gen':ret, 'genflag':retflag, 'attackpoint':attackpoint, 'buildstate': self.buildstate,
			'updatetime': self.updatetime,
			'attacks':self.getAttackDict(),
			'doubleinfo': [p.todict() for p in self.doubleinfo],
			'protectinfo': [p.todict() for p in self.protectinfo],
			'servertime': time.time(), 'gensec':gensec,
		}

	def getUpgrade(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			return buildinglevelcfg[b][l]['upgrade']
		return 0

	def getCreateBuild(self, bid, level):
		if bid in buildinglevelcfg:
			item = buildinglevelcfg[bid].get(level)
			if item:
				return item['upgrade']
		return 0

	def upgrade(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if l < buildinglevelcfg[b]['maxlevel']:
				self.buildlevel[i] += 1
				if self.buildstate[i] == 1:
					self.updatetime[i] = time.time()
				return True
		return False

	def getRecycle(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if b > 0 and l > 0:
				return buildinglevelcfg[b][l]['recycle']
		return 0

	def recycle(self, i):
		if 0 <= i < MapSize:
			self.buildlevel[i] = 0
			self.updatetime[i] = 0
			self.buildstate[i] = 1
			self.attacks[i] = []
			self.doubleinfo[i] = EffectItem()
			self.protectinfo[i] = EffectItem()
			return True
		return False

	def activate(self, i):

		if 0 <= i < MapSize:
			self.getGen(i)

			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]
			cfg = buildinglevelcfg[b][l]
			#if u + cfg['timeinterval'] + cfg['timedisabled'] < time.time():
			#	self.updatetime[i] = time.time()
			if self.buildstate[i] == 0:
				self.buildstate[i] = 1
				self.updatetime[i] = time.time()
				return True
			elif self.buildstate[i] == 2:
				self.buildstate[i] = 1
				return True

		return False

	def getDouble(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if b > 0 and l > 0:
				return buildinglevelcfg[b][l]['doublecost']
		return 0

	def double(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				#logger.info("aaaaaaaaaaaaaa:%s", self.doubleinfo[i].valid())
				if self.doubleinfo[i].valid():
					self.doubleinfo[i].validtime += cfg['doubletime']
				else:
					self.doubleinfo[i].begintime = time.time()
					self.doubleinfo[i].validtime = cfg['doubletime']
				return True
		return False

	def getProtect(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if b > 0 and l > 0:
				self.delAttack(i)
				logger.info("aaaaaaaaaaaaaa:%s", self.attacks[i])
				if len(self.attacks[i]) <= 0:
					return buildinglevelcfg[b][l]['protectcost']
		return 0

	def protect(self, i):
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				#logger.info("aaaaaaaaaaaaaa:%s", self.doubleinfo[i].valid())
				if self.protectinfo[i].valid():
					self.protectinfo[i].validtime += cfg['protecttime']
				else:
					self.protectinfo[i].begintime = time.time()
					self.protectinfo[i].validtime = cfg['protecttime']
				return True
		return False

	def getDoubleNum(self, i, tm):
		ret = 0
		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]
			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				p = self.doubleinfo[i]
				if p.valid():
					aendtm = p.begintime + p.validtime
					if aendtm > self.updatetime[i]:
						b = max(self.updatetime[i], p.begintime)
						e = min(aendtm, tm)
						if b < e:
							ret = int(e - b) * cfg['generate']
		return ret

	def getGen(self, i):
		ret = 0
		retflag = False
		attackpoint  = 0

		if 0 <= i < MapSize:
			b = self.map[i]
			l = self.buildlevel[i]
			u = self.updatetime[i]

			if b > 0 and l > 0:
				cfg = buildinglevelcfg[b][l]
				tmnow = time.time()
				t = tmnow - u

				if self.buildstate[i] == 0:
					pass
				elif t > cfg['timeinterval'] + cfg['timedisabled']:
					if self.buildstate[i] != 0:
						attackpoint = self.getAttackNum(i, u + cfg['timeinterval'])
						ret = cfg['generate'] * cfg['timeinterval'] - attackpoint + self.getDoubleNum(i, u + cfg['timeinterval'])
						ret = max(0, ret)
						retflag = True
						self.buildstate[i] = 2
				elif t >= cfg['timeinterval']:
					attackpoint = self.getAttackNum(i, u + cfg['timeinterval'])
					ret = cfg['generate'] * cfg['timeinterval'] - attackpoint + self.getDoubleNum(i, u + cfg['timeinterval'])
					ret = max(0, ret)
					retflag = True
				else:
					attackpoint = self.getAttackNum(i, tmnow)
					ret = int(t) * cfg['generate'] - attackpoint + self.getDoubleNum(i, tmnow)
					ret = max(0, ret)

		return {'gen':ret, 'genflag':retflag, 'attackpoint': attackpoint, 'buildstate': self.buildstate[i],
			'updatetime': self.updatetime[i],
			'attacks':self.getAttackDict(i),
			'doubleinfo': self.doubleinfo[i].todict(),
			'protectinfo': self.protectinfo[i].todict(),
			'servertime': time.time(),
		}


import os
from biocluster.config import Config
from multiprocessing import Pool
import subprocess

# def run_script(codes):
#     subprocess.call("Rscript {}".format(codes), shell=True)

class nomogram(object):
    def __init__(self, surv_table, factor_list, time_list, output):
        self.surv_table = surv_table
        self.factor_list = factor_list.strip().split(';')
        self.time = time_list.strip().split(';')
        self.output = output
        software_dir = Config().SOFTWARE_DIR
        self.rscript = software_dir + "/program/R-3.5.1/bin/Rscript"

    # def run(self, script_list):
    #     pool = Pool(self.pool_size)
    #     pool.map(run_script, script_list)
    #     pool.close()
    #     pool.join()

    def nomogram(self, output=None):
        if output is None:
            output = os.getcwd()
        script_name = os.path.join(output, 'nomogram.r')
        f = open(script_name, 'w')
        f.write('library(survival)\n')
        f.write('library(rms)\n')
        f.write('surv_cox <- read.table("{}", sep="\t", header=TRUE)\n'.format(self.surv_table))
        f.write('dd <- datadist(surv_cox)\n')
        f.write('options(datadist="dd")\n')
        factor = '+'.join(self.factor_list)
        f.write('res.cox <- cph(Surv(time, status) ~ {}, data =  lung, surv = T, x=T, y=T)\n'.format(factor))
        f.write('survival <- Survival(res.cox)\n')
        sur = list()
        for i in self.time:
            f.write('survival{} <- function(x)survival({}, x)\n'.format(i, i))
            sur.append('survival{}'.format(i))
        sur_str = ','.join(sur)
        f.write('nom = nomogram(res.cox, fun=list({}), fun.at = c(0.05, seq(0.1,0.9,by=0.05), 0.95), funlabel = c("1 year", "2 year"))\n'.format(sur_str))
        # f.write('write.table(nom, file = "{}", sep="\t")\n'.format(self.output))
        f.write('pdf(file="nomogram.pdf")\n')
        f.write('a = plot(nom)\n')
        f.write('dev.off()\n')
        f.close()
        os.system('{} nomogram.r'.format(self.rscript))

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description='This script is used to identify pirna from piRbase database.')
    parser.add_argument('-i', type=str, help='surv file')
    parser.add_argument('-fl', type=str, help='factor list')
    parser.add_argument('-tl', type=str, help='time list')
    parser.add_argument('-o', type=str, help='output path')
    args = parser.parse_args()
    nomogram = nomogram(args.i, args.fl, args.tl, args.o)
    nomogram.nomogram()


import os
import pandas as pd
from Services.Db.DbContext import DbContext

class DbService:
    @staticmethod
    def execute(query):
        context = DbContext()

        context.cursor.execute(query)
        context.db.commit()
        context.db.close()

    @staticmethod
    def select(query):
        context = DbContext()

        context.cursor.execute(query)
        result = context.cursor.fetchall()

        return result

    @staticmethod
    def get_table_csv(table_name):
        context = DbContext()
        connection = context.db
        supplier_folder = table_name.upper()
        path = os.path.join('../TemporaryStorage', supplier_folder, 'export')
        out_file_path = os.path.join(path, 'export.csv')
        if not os.path.exists(path):
            os.makedirs(path)

        table_df = pd.read_sql_query('SELECT manufacturer, supplier_part_number, '
                                     'part_number, CAST(quantity AS INTEGER) as quantity, '
                                     'ROUND(price, 2) as price FROM {} '
                                     'WHERE quantity > 0 AND price > 0'.format(table_name),
                                     connection)
        table_df.to_csv(out_file_path, sep=';', index=False)

        return out_file_path


import rclpy
from . import change_suffix
from time import sleep
import os
from std_msgs.msg import String

open_objname = 'test'

def change_text_string(file_name):
    obj_dir = os.getcwd() + '/src/object_communication/data/'
    change_suffix.change_obj_txt(file_name)
    f = open(obj_dir + file_name + '.txt', 'r', encoding='UTF-8')

    data = f.read()
    f.close()
    change_suffix.change_txt_obj(file_name)

    return data

def main(args=None):
    rclpy.init(args=args)
    node = rclpy.create_node('minimal_publisher')
    publisher = node.create_publisher(String, 'topic', 10)

    msg = String()
    senden = True
    i = 0
    while rclpy.ok():
        msg.data = change_text_string(open_objname)
        i += 1
        # node.get_logger().info('Publishing: "%s"' % msg.data)
        if senden:
            publisher.publish(msg)
            senden = False
        sleep(0.5)  # seconds

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()


from django.shortcuts import render
from datetime import datetime
# Create your views here.
def index(request):
    time = datetime.now()
    context = {
        'date': time.strftime("%b %d, %Y"),
        'time': time.strftime("%I:%M %p")
    }
    return render(request, 'timeDis/index.html', context)


import scenario
from scenario.scenariologging import ScenarioLogging
import scenario.test

# Steps:
from steps.common import ExecScenario
from steps.common import LogVerificationStep

class Logging410(scenario.test.TestCase):

    def __init__(self):  # type: (...) -> None
        scenario.test.TestCase.__init__(
            self,
            title="Scenario stack indentation",
            objective="Check that indentation is shifted for each sub-scenario being executed.",
            features=[scenario.test.features.LOGGING],
        )

        self.addstep(ExecScenario(
            scenario.test.paths.SUPERSCENARIO_SCENARIO, subscenario=scenario.test.paths.SCENARIO_LOGGING_SCENARIO,
            config_values={scenario.ConfigKey.LOG_COLOR_ENABLED: False},
        ))
        self.addstep(CheckMainScenario(ExecScenario.getinstance()))
        self.addstep(CheckSubScenario(CheckMainScenario.getinstance()))

class CheckMainScenario(LogVerificationStep):

    def __init__(
            self,
            exec_step,  # type: ExecScenario
    ):  # type: (...) -> None
        LogVerificationStep.__init__(self, exec_step)

        self.action_indentation = 0  # type: int
        self.result_indentation = 0  # type: int

    def step(self):  # type: (...) -> None
        self.STEP("Main scenario indentation")

        if self.RESULT("The beginning of the main scenario is displayed without indentation."):
            _main_beginning_line = self.assertline(f"SCENARIO '{scenario.test.paths.SUPERSCENARIO_SCENARIO}'")  # type: str
            self.assertregex("^SCENARIO ", _main_beginning_line, evidence=True)
        if self.RESULT("The steps of the main scenario are displayed without indentation."):
            for _main_step_text in (
                "STEP#1: Sub-scenario execution",
            ):  # type: str
                for _main_step_line in self.assertlines(_main_step_text):  # type: str
                    self.assertregex(
                        r"^STEP#", _main_step_line,
                        evidence=True,
                    )
        if self.RESULT("The 'ACTION: ' patterns of the main scenario actions of are right-aligned "
                       f"with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _main_action_text in (
                f"Execute the '{scenario.test.paths.SCENARIO_LOGGING_SCENARIO}' scenario.",
            ):  # type: str
                for _main_action_line in self.assertlines(_main_action_text):  # type: str
                    self.assertregex(
                        r"".join([
                            r"^",
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("ACTION: ")),
                            r"ACTION: ",
                        ]),
                        _main_action_line,
                        evidence=True,
                    )
                    self.action_indentation = _main_action_line.find("ACTION: ")
        if self.RESULT("The 'RESULT: ' patterns of the main scenario expected results are right-aligned "
                       f"with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _main_result_text in (
                "No exception is thrown.",
            ):  # type: str
                for _main_result_line in self.assertlines(_main_result_text):  # type: str
                    self.assertregex(
                        r"".join([
                            r"^",
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("RESULT: ")),
                            r"RESULT: ",
                        ]),
                        _main_result_line,
                        evidence=True,
                    )
                    self.result_indentation = _main_result_line.find("RESULT: ")
        if self.RESULT("The 'EVIDENCE: ' patterns the the main scenario evidence are right-aligned "
                       f"with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _main_evidence_text in (
                "Sub-scenario executed successfully",
            ):  # type: str
                for _main_evidence_line in self.assertlines(_main_evidence_text):  # type: str
                    self.assertregex(
                        r"".join([
                            r"^",
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("EVIDENCE: ")),
                            r"EVIDENCE: ",
                        ]),
                        _main_evidence_line,
                        evidence=True,
                    )

class CheckSubScenario(LogVerificationStep):

    def step(self):  # type: (...) -> None
        self.STEP("Sub-scenario indentation")

        assert isinstance(self.exec_step, CheckMainScenario)

        _subscenario_regex_start = r"^" + ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN.replace("|", r"\|")  # type: str
        if self.RESULT(f"The '{ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN}' indentation pattern "
                       "makes the '|' characters be indented constantly, and below the 'ACTION: ' and 'RESULT: ' patterns."):
            self.evidence(f"Main action indentation: {self.exec_step.action_indentation}")
            self.assertgreater(self.exec_step.action_indentation, 0)
            self.evidence(f"Main expected result indentation: {self.exec_step.result_indentation}")
            self.assertgreater(self.exec_step.result_indentation, 0)
            _main_action_result_indentation = max(
                self.exec_step.action_indentation,
                self.exec_step.result_indentation,
            )  # type: int
            _subscenario_line_indentation = -1  # type: int
            for _subscenario_line in self.assertlines(ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN):  # type: str
                self.evidence(f"Sub-scenario line: {_subscenario_line!r}")
                if _subscenario_line_indentation < 0:
                    _subscenario_line_indentation = _subscenario_line.find("|")
                    self.assertgreater(
                        _subscenario_line_indentation, _main_action_result_indentation,
                        evidence="'|' character position",
                    )
                else:
                    self.assertequal(
                        _subscenario_line.find("|"), _subscenario_line_indentation,
                        evidence="'|' character position",
                    )
            # Check we have actually processed sub-scenario lines.
            self.assertgreater(_subscenario_line_indentation, 0)
        if self.RESULT("The beginning of the sub-scenario is displayed "
                       f"with the {ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN!r} indentation pattern."):
            for _subscenario_beginning_line in self.assertlines(f"SCENARIO '{scenario.test.paths.SCENARIO_LOGGING_SCENARIO}'"):  # type: str
                self.assertregex(
                    _subscenario_regex_start + r"SCENARIO ", _subscenario_beginning_line,
                    evidence=True,
                )
        if self.RESULT("The steps of the sub-scenario are displayed "
                       f"with the {ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN!r} indentation pattern."):
            for _subscenario_step_text in (
                "STEP#1: Scenario logging",
            ):  # type: str
                for _subscenario_step_line in self.assertlines(_subscenario_step_text):  # type: str
                    self.assertregex(
                        _subscenario_regex_start + r"STEP#[0-9]+", _subscenario_step_line,
                        evidence=True,
                    )
        if self.RESULT("The 'ACTION: ' patterns of the sub-scenario actions are prefixed "
                       f"with the {ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN!r} indentation pattern, "
                       f"then right-aligned with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _subscenario_action_text in (
                "This is a sample action.",
            ):  # type: str
                for _subscenario_action_line in self.assertlines(_subscenario_action_text):  # type: str
                    self.assertregex(
                        r"".join([
                            _subscenario_regex_start,
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("ACTION: ")),
                            r"ACTION: ",
                        ]),
                        _subscenario_action_line,
                        evidence=True,
                    )
        if self.RESULT("The 'RESULT: ' patterns of the of the sub-scenario expected results are prefixed "
                       f"with the {ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN!r} indentation pattern, "
                       f"then right-aligned with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _subscenario_result_text in (
                "This is a sample expected result.",
            ):  # type: str
                for _subscenario_result_line in self.assertlines(_subscenario_result_text):  # type: str
                    self.assertregex(
                        r"".join([
                            _subscenario_regex_start,
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("RESULT: ")),
                            r"RESULT: ",
                        ]),
                        _subscenario_result_line,
                        evidence=True,
                    )
        if self.RESULT("The 'EVIDENCE: ' patterns of of the sub-scenario evidence are prefixed "
                       f"with the {ScenarioLogging.SCENARIO_STACK_INDENTATION_PATTERN!r} indentation pattern, "
                       f"then right-aligned with {ScenarioLogging.ACTION_RESULT_MARGIN} characters."):
            for _subscenario_evidence_text in (
                "Action evidence.",
                "Expected result evidence.",
            ):  # type: str
                for _subscenario_evidence_line in self.assertlines(_subscenario_evidence_text):  # type: str
                    self.assertregex(
                        r"".join([
                            _subscenario_regex_start,
                            r" " * (ScenarioLogging.ACTION_RESULT_MARGIN - len("EVIDENCE: ")),
                            r"EVIDENCE: ",
                        ]),
                        _subscenario_evidence_line,
                        evidence=True,
                    )


import re

#상수
IS_NUMERIC = "^[-+]?\\d+(\\.\\d+)?$"
#변수
l_int=[] #입력 정수 리스트
r_int=[] #5로 시작하는 세자리 정수리스트
f_num=1  #최종결과값

#1.정수만 5개 입력받는데, 숫자 아니면 계속 반복시켜서 5개의 정수만 받도록 한다.
for i in range(5):
    while True:
        inp =input(str(i+1)+"번째 정수입력하시요:")
        if re.match(IS_NUMERIC,inp):
            break
        else:
            print("아놔 숫자를 입력하라고  (--+):")
    l_int.append(int(inp))

#2.입력한 값중에 5로 시작하는 세자리 숫자 골라내기
for i in range(len(l_int)):
    tmp_num=str(l_int[i])
    #print(tmp_num[0:1])
    if tmp_num[0:1]=='5' and len(tmp_num)==3:
        r_int.append(int(tmp_num))

#3. 입력한 값을 계산하여 출력한다.
for i in range(len(r_int)):
    f_num *=r_int[i]

#4.최종결과값
print("5로 시작하는 세자리 숫자는 :",r_int)
print("곱한 최종결과는 :",str(f_num))


from GEMEditor.rw.units import add_unit_definitions
from lxml.etree import Element
from GEMEditor.rw import *

class TestAddUnitsDefinition:

    def test_node_addition(self):
        root = Element("root")
        add_unit_definitions(root)

        list_of_unitdefinitions_node = root.find(sbml3_listOfUnitDefinitions)
        assert list_of_unitdefinitions_node is not None
        assert len(list_of_unitdefinitions_node) == 1

        unit_definition_node = list_of_unitdefinitions_node.find(sbml3_unitDefinition)
        assert unit_definition_node is not None
        assert unit_definition_node.get("id") == "mmol_per_gDW_per_hr"

        list_of_units_node = unit_definition_node.find(sbml3_listOfUnits)
        assert list_of_units_node is not None
        assert len(list_of_units_node) == 3

        expected_values = set([("1", "mole", "1", "-3"),
                               ("-1", "gram", "1", "0"),
                               ("-1", "second", "3600", "0")])

        found_set = set()
        for child in list_of_units_node.iterfind(sbml3_unit):
            found_set.add((child.get("exponent"), child.get("kind"), child.get("multiplier"), child.get("scale")))

        assert expected_values == found_set


from __future__ import unicode_literals

import os
import unicodecsv
import termcolor
import re

from django.utils.translation import ugettext as _

from search.models.utils import is_isbn
from search.datasources.bookshops.all.discogs import discogsScraper as discogs  # noqa: F401
from search.datasources.bookshops.all.momox import momox  # noqa: F401
from search.datasources.bookshops.deDE.buchlentner import \
    buchlentnerScraper as buchlentner  # noqa: F401
from search.datasources.bookshops.esES.casadellibro import \
    casadellibroScraper as casadellibro  # noqa: F401
from search.datasources.bookshops.frFR.librairiedeparis import \
    librairiedeparisScraper as librairiedeparis  # noqa: F401
from search.datasources.bookshops.frFR.dilicom import \
    dilicomScraper as dilicom  # noqa: F401
from search.datasources.bookshops.frFR.lelivre import \
    lelivreScraper as lelivre  # noqa: F401
from search.datasources.bookshops.frFR.filigranes import \
    filigranesScraper as filigranes  # noqa: F401
from search.datasources.bookshops.all.bookdepository import \
    bookdepositoryScraper as bookdepository  # noqa: F401
from search.models import Card

from abelujo import settings

from search.models import utils

try:
    # The Electre API connector is installed separetely.
    import pyelectre
except:
    pass

#: Default datasource to be used when searching isbn, if source not supplied.
DEFAULT_DATASOURCE = "librairiedeparis"
if os.getenv('DILICOM_PASSWORD') and os.getenv('DILICOM_PASSWORD').strip():
    DEFAULT_DATASOURCE = 'dilicom'
    print(termcolor.colored('default datasource is DILICOM', 'blue'))

def electre_enabled():
    return os.getenv('ELECTRE_PASSWORD') is not None \
        and os.getenv('ELECTRE_USER') is not None

if electre_enabled():
    DEFAULT_DATASOURCE = 'electre'
    print(termcolor.colored('default datasource is ELECTRE', 'blue'))

if os.getenv('DEFAULT_DATASOURCE') and os.getenv('DEFAULT_DATASOURCE').strip():
    DEFAULT_DATASOURCE = os.getenv('DEFAULT_DATASOURCE')

def get_datasource_from_lang(lang):
    """From a lang (str), return the name (str) of the datasource module.

    And for CDs ? The client should be in "recordsop" mode.
    """
    if os.getenv('DEFAULT_DATASOURCE'):
        # Required to set the swiss source by default over the french one.
        return os.getenv('DEFAULT_DATASOURCE')

    if not lang:
        return DEFAULT_DATASOURCE

    if lang.startswith("fr") and not os.getenv('DILICOM_PASSWORD'):
        return "librairiedeparis"
    elif lang.startswith("fr") and os.getenv('DILICOM_PASSWORD'):
        return "dilicom"
    elif lang.startswith('ch'):
        return 'lelivre'
    elif lang.startswith("de"):
        return "buchlentner"
    elif lang.startswith("es"):
        return "casadellibro"
    elif lang in ["discogs", "momox"]:
        return lang
    else:
        return DEFAULT_DATASOURCE

def search_on_data_source(data_source, search_terms, PAGE=1):
    """
    Search with the appropriate scraper.

    - data_source is the name of an imported module.  Electre takes
      priority (it supports EAN search and free search, but not search of
      many EANs).
    - search_terms: list of strings.

    Return: a tuple (search results, stacktraces). Search result is a
    list of dicts.
    """
    if data_source == 'electre':
        res, traces = pyelectre.search_on_electre(search_terms)
    else:
        # get the imported module by name.
        # They all must have a class Scraper.
        scraper = getattr(globals()[data_source], "Scraper")
        # res, traces = scraper(*search_terms).search()
        res, traces = scraper(search_terms, PAGE=PAGE).search()

    # Check if we already have these cards in stock (with isbn).
    res = Card.is_in_stock(res)

    return res, traces

def dilicom_enabled():
    """
    Return a boolean indicating if the connection to Dilicom is possible.
    Currently, check we have the required environment variables.
    """
    return os.getenv('DILICOM_PASSWORD') is not None \
        and os.getenv('DILICOM_USER') is not None

def update_from_dilicom(card):
    """
    Update data: price, price excl vat, vat, availability, distributor,
    dimensions, weight.
    - card: card object.

    Return:
    - card,
    - list of messages (str).
    """
    if not card or not card.isbn or not (is_isbn(card.isbn)):
        return card, []

    scraper = getattr(globals()['dilicom'], "Scraper")
    res, traces = scraper(card.isbn).search()

    if res and res[0]:
        to_save = False
        res = res[0]
        if card.price != res['price']:
            card.price = res['price']
            to_save = True

        if res['theme'] and card.theme != res['theme']:
            card.theme = res['theme']
            to_save = True

        if res.get('width') and card.width != res.get('width'):
            card.width = res.get('width')
            to_save = True
        if res.get('weight') and card.weight != res.get('weight'):
            card.weight = res.get('weight')
            to_save = True
        if res.get('height') and card.height != res.get('height'):
            card.height = res.get('height')
            to_save = True
        if res.get('thickness') and card.thickness != res.get('thickness'):
            card.thickness = res.get('thickness')
            to_save = True
        if res.get('details_url') and card.width != res.get('details_url'):
            card.details_url = res.get('details_url')
            to_save = True
        if res.get('data_source') and card.width != res.get('data_source'):
            card.data_source = res.get('data_source')
            to_save = True

        # # "price_excl_vat" is already a model property.
        # # We'll use the "fmt" one.
        # if card.price_excl_vat != res.get('price_excl_vat'):
        #     card.price_excl_vat = res.get('price_excl_vat')
        #     to_save = True

        if to_save:
            card.save()

        # trick: add object fields, not saved to the DB, only for display.
        card.vat1 = res.get('vat1')
        card.price_excl_vat_fmt = res.get('price_excl_vat_fmt')
        card.dilicom_price_excl_vat = res.get('dilicom_price_excl_vat')

        card.availability = res['availability']
        card.availability_fmt = res['availability_fmt']

    else:
        res = card
    return card, traces

def update_from_electre(card):
    """
    Update this card from Electre's API (using the pyelectre Electre).
    Search by ISBN and update the important fields.
    """
    if not card or not card.isbn or not (is_isbn(card.isbn)):
        return card, []

    res, traces = pyelectre.search_on_electre(card.isbn)
    if res and res[0]:
        res = res[0]
        newcard = Card.update_from_dict(card, res,
                                        distributor_name=res.get('distributor_name'),
                                        distributor_gln=res.get('distributor_gln'),
            )
        return newcard

    return card, traces

def _check_csv_format(rows):
    """
    For all these rows, check we have an ISBN and an integer.

    Return: boolean, messages.
    """
    for row in rows:
        if not row and not row[0]:
            return None, ["rows missing data. They are expected to have an ISBN and a number."]
        if not is_isbn(row[0]):
            return False, ["Not all lines contain an ISBN."]
        try:
            int(row[1])
        except Exception as e:
            print(e)
            return False, ["Not all lines contain a valid number."]

    return True, []

def extract_all_isbns_quantities(inputrows):
    """
    From a list of lines, find the ISBNs and an associated quantity.
    Ignore the other columns.

    If we have only lines of ISBNs, OK, associate 1 quantity by default.
    If we have a comma-separated CSV with an ISBN and a number, OK.
    If we have a space-separated CSV with an ISBN and a number, OK.

    If we have a CSV with more rows… find the quantity. Not implemented.

    Ignore other optional columns.

    Ignore the first line(s) starting by // (form embedded comment).

    Return: a tuple:
    - list of tuples ISBN/quantity,
    - list of messages (strings)
    """
    msgs = utils.Messages()
    lines = []  # inputrows, cleaned up.
    rows = []   # final list of tuples.
    res = []

    def falsy_col(col):
        return col in [None, '', u'']

    def is_comment(line):
        return line.startswith('//')

    def contains_spacing(line):
        """
        - space or tab \t
        """
        return ' ' in line or '\t' in line

    def find_space_in_line(line):
        if ' ' in line:
            return ' '
        if '\t' in line:
            return '\t'

    # Quick cleanup.
    for row in inputrows:
        if row and not is_comment(row) and not row in [u'']:
            lines.append(row.strip())

    # ;-separated CSV?
    if ';' in lines[0]:
        for row in lines:
            isbn, quantity = row.split(';')
            if isbn and not falsy_col(isbn):
                try:
                    quantity = int(quantity)
                    if not quantity:
                        quantity = 1  # just in case
                    rows.append([isbn.strip(), quantity])
                except Exception as e:
                    # logging.warning("extract_all_isbns_quantities: could not parse quantity {} to int: {}".format(quantity, e))
                    msgs.add_warning("Could not parse quantity for ISBN {}".format(isbn))

    # We have a space-separated ISBN_qty
    elif contains_spacing(lines[0]):
        thespace = find_space_in_line(lines[0])
        if thespace and is_isbn(lines[0].split(thespace)[0]):
            for row in lines:
                isbn, quantity = re.split(thespace, row, maxsplit=1)
                if isbn and not falsy_col(isbn):
                    try:
                        quantity = int(quantity.strip())
                        if not quantity:
                            quantity = 1
                        rows.append([isbn.strip(), quantity])
                    except Exception as e:
                        msgs.add_warning("Could not parse quantity for ISBN {}".format(isbn))

        else:
            msgs.add_warning("Could not parse the line {}".format(row))

    # We simply have a list of ISBNs:
    elif is_isbn(lines[0]):
        for line in lines:
            rows.append([line, 1])  # default quantity
        return rows, []
    else:
        print("extract_all_isbns_quantities: dead end.")
        msgs.add_error("Could not parse input. Bad lines format.")

    # Collect ISBNs (and numbers).
    if rows and rows[-1] and len(rows[-1]) > 1:
        # Find where is the ISBNs (should be usually 1st position).
        first_col = rows[0][0]
        second_col = rows[0][1]
        try:
            int(second_col)
            is_number = True
        except Exception as e:
            print(e)
            is_number = False
        if is_isbn(first_col) and is_number:
            # Check all first columns are ISBNs, all second are numbers
            valid_csv, messages = _check_csv_format(rows)
            if not valid_csv:
                return None, messages
            res = rows

    else:
        msgs.add_warning("We couldn't recognize data. Please check the first rows are valid ISBNs and numbers. They can be CSV rows separated by ';' or only an ISBN on each line.")
        return [], msgs.msgs

    return res, msgs.msgs

def bulk_import_from_dilicom(isbns):
    """
    Import this list of ISBNs by batches of 100.

    Only Dilicom provides this batch request for now.
    Its search() methods handles the batch requests.

    - isbns: list of strings, all verified to be valid isbns.

    Return:
    """
    if not dilicom_enabled():
        return [], ["Dilicom is not enabled."]
    fel = dilicom.Scraper(*isbns)
    dicts, messages = fel.search()
    return dicts, messages

class Echo(object):
    """
    An object that implements just the write method of the file-like
    interface.
    """
    # taken from Django docs
    def write(self, value):
        """
        Write the value by returning it, instead of storing in a buffer.
        """
        return value

def cards2csv(cards):
    """
    Cards: as dicts.
    """
    pseudo_buffer = Echo()
    writer = unicodecsv.writer(pseudo_buffer, delimiter=b';')
    content = writer.writerow(b"")

    rows = [(it['title'],
             it['authors_repr'],
             it['price'],
             it['quantity'],
             it['shelf'],
             it['isbn'],
            )
            for it in cards]

    header = (_("title"),
              _("authors"),
              _("price"),
              _("quantity"),
              _("shelf"),
              _("isbn"),
    )
    rows.insert(0, header)
    content = b"".join([writer.writerow(row) for row in rows])
    return content

def format_price_for_locale(price):
    """
    For french-ish locales, a comma should be the decimal separator.
    """
    try:
        if settings.LOCALE_FOR_EXPORTS == "fr":
            return str(price).replace('.', ',')
    except:  # noqa: E722
        pass
    try:
        return str(price)
    except:  # noqa: E722
        return price


from collections import defaultdict

def make_hashable_list(lst: list) -> tuple:
    """
    Recursively converts lists to tuples.

    >>> make_hashable_list([1, [2, [3, [4]]]])
    (1, (2, (3, (4,))))

    """

    for n, index in enumerate(lst):
        if isinstance(index, list):
            lst[n] = make_hashable_list(index)

    return tuple(lst)

def make_dict_vals_hashable(dct: dict) -> dict:
    """
    Recursively converts values of a dict (a type list to a tuple).

    >>> make_dict_vals_hashable({'a': [1, [2]], 'b': {'x': [1, [2]]}})
    {'a': (1, (2,)), 'b': (('x', (1, (2,))),)}

    """

    for k, v in dct.items():
        if isinstance(v, list):
            dct[k] = make_hashable_list(v)
        elif isinstance(v, dict):
            dct[k] = tuple(make_dict_vals_hashable(dct[k]).items())

    return dct

def find_diff(old: dict, new: dict) -> defaultdict:
    """
    Find a difference between two dicts.

    >>> find_diff({'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3})
    defaultdict(<class 'list'>, {'c': [3]})

    """

    diff_mapping = defaultdict(list)

    old_hashable = make_dict_vals_hashable(old)
    new_hashable = make_dict_vals_hashable(new)

    diff = set(old_hashable.items()) ^ set(new_hashable.items())

    for k, v in diff:
        diff_mapping[k].append(v)

    return diff_mapping


from __future__ import division
import os
from dials.array_family import flex
dirpath = "/global/cscratch1/sd/nksauter/adse13_187/bernina/split2b"
from dxtbx.model.experiment_list import ExperimentListFactory

def from_files():
  for x in range(7534):
    if x%500==0: print(x)
    expt_file = "split_%04d.expt"%x
    expts = ExperimentListFactory.from_json_file(os.path.join(dirpath,expt_file),
                                              check_format=False)
    refl_file = "split_%04d.refl"%x
    refl_table = flex.reflection_table.from_file(os.path.join(dirpath,refl_file))

    yield refl_table,expts[0]

def run_job():
  from cctbx.crystal import symmetry
  from cctbx.uctbx import unit_cell
  from cctbx.sgtbx import space_group
  sym = symmetry(unit_cell=unit_cell((78.68, 78.68, 265.33, 90, 90, 120)),
                 space_group="P6522")
  millers = flex.miller_index()
  for ref,exp in from_files():
    idx = ref["miller_index"]
    millers.extend(idx)
  from cctbx.miller import set as mset, array
  MM = mset(crystal_symmetry=sym,anomalous_flag=True,indices=millers) # make miller set from all obs
  MA = MM.map_to_asu() # map to asu
  #dval = MA.unit_cell().d(MA.indices())
  #order = flex.sort_permutation(dval, reverse=True)
  #MAsort = MA.select(order)
  #for item in MAsort.indices(): print(item)
  #print (MAsort.indices().size(), len(set(MAsort.indices())))
  #ZZ = MAsort.array(data=flex.double(MAsort.size(),1.))
  MAobs = MA.array(data=flex.double(MA.size(),1.))
  ME = MAobs.merge_equivalents()
  new_red = ME.redundancies() # all observations with multiplicity of measurement
  z = new_red.complete_array(d_max=100000.,new_data_value=0) # fill in missing observations
  dval = z.unit_cell().d(z.indices())
  order = flex.sort_permutation(dval, reverse=True)
  MAsort = z.select(order)
  print("d_min is %.3f"%(flex.min(MA.d_spacings().data())))
  print("there are altogether %d measurements"%MA.indices().size())
  print("this is uniqueness : %d measurements"%new_red.indices().size())
  print("total in asymmetric unit %d"%MAsort.indices().size())
  print("maximum multiplicity is %d"%(flex.max(MAsort.data())))
  print("the mean multiplicity is %.2f"%(flex.mean(MAsort.data().as_double())))
  zeroes = (MAsort.data()==0).count(True)
  print("unmeasured %d or %.2f%% of total"%(zeroes, 100.*zeroes/MAsort.indices().size()))

  MAd = MA.select(MA.d_spacings().data()>1.63)
  new_redd = new_red.select(new_red.d_spacings().data()>1.63)
  MAsortd = MAsort.select(MAsort.d_spacings().data()>1.63)
  print("Cut, there are altogether %d measurements"%MAd.indices().size())
  print("Cut, this is uniqueness : %d measurements"%new_redd.indices().size())
  print("Cut, total in asymmetric unit %d"%MAsortd.indices().size())
  print("Cut, maximum multiplicity is %d"%(flex.max(MAsortd.data())))
  print("Cut, the mean multiplicity is %.2f"%(flex.mean(MAsortd.data().as_double())))
  zeroes = (MAsortd.data()==0).count(True)
  print("Cut, unmeasured %d or %.2f%% of total"%(zeroes, 100.*zeroes/MAsortd.indices().size()))

  if True:
      from matplotlib import pyplot as plt
      plt.plot(range(MAsort.indices().size()),MAsort.data(),"r.")
      plt.plot(range(MAsortd.indices().size()),MAsortd.data().as_double()-0.1,"b.")
      plt.title("Redundant measurement vs. Bragg spot position")
      plt.xlabel("Spots ordered by increasing Bragg angle →")
      plt.ylabel("Multiplicity of strong spot obs (Group A)")
      plt.show()

if __name__=="__main__":
  run_job()