import datetime
import json
import time

from datamanage import exceptions as dm_errors
from datamanage.lite.dmonitor.flow_models import (
    JobnaviExecuteLog,
    JobnaviScheduleInfo,
    ProcessingBatchInfo,
)
from datamanage.lite.dmonitor.mixins.dmonitor_mixins import DmonitorMixin
from datamanage.lite.dmonitor.serializers import (
    DmonitorBatchExecutionSerializer,
    DmonitorBatchMonitorSerializer,
    DmonitorBatchScheduleSerializer,
)
from datamanage.utils.time_tools import timetostr, tznow
from rest_framework.response import Response

from common.base_utils import model_to_dict
from common.decorators import list_route, params_valid
from common.log import logger
from common.views import APIViewSet

class BatchExecutionsViewSet(DmonitorMixin, APIViewSet):
    @params_valid(serializer=DmonitorBatchMonitorSerializer)
    def list(self, request, params):
        """
        @api {get} /datamanage/dmonitor/batch_executions/ 查询离线表的执行状态

        @apiVersion 1.0.0
        @apiGroup DmonitorBatch
        @apiName get_batch_executions

        @apiParam {string[]} result_table_ids 结果表列表
        @apiParam {string[]} processing_ids 数据处理ID列表
        @apiParam {string} [period] 调度次数, 周期为时的任务默认值为12, 周期为天的任务默认值为7

        @apiParamExample {json} 参数样例:
            {
                "result_table_ids": ["591_example_table1", "591_example_table2"],
                "period": 12
            }

        @apiSuccessExample Success-Response:
            HTTP/1.1 200 OK
            {
                "data": {
                    "591_example_table1": {
                        "interval": 3600,
                        "execute_history": [
                            {
                                "status": "fail",
                                "status_str": "失败",
                                "execute_id": "",
                                "err_msg": "",
                                "err_code": "0",
                                "start_time": "2018-11-11 11:11:11",
                                "end_time": "2018-11-11 12:11:11",
                                "period_start_time": "2018-11-11 11:11:11",
                                "period_end_time": "2018-11-11 12:11:11"
                            }
                        ]
                    },
                    "591_example_table2": {
                        "interval": 86400,
                        "execute_history": [
                            {
                                "status": "fail",
                                "status_str": "失败",
                                "execute_id": "",
                                "err_msg": "",
                                "err_code": "0",
                                "start_time": "2018-11-11 11:11:11",
                                "end_time": "2018-11-12 11:11:11",
                                "period_start_time": "2018-11-11 11:11:11",
                                "period_end_time": "2018-11-12 11:11:11"
                            }
                        ]
                    }
                },
                "message": "ok",
                "code": "1500200",
                "result": true,
                "errors": {}
            }
        """
        result_table_ids = params["result_table_ids"]
        processing_ids = params["processing_ids"]
        period = params.get("period")
        batch_executions = {}

        for result_table_id in result_table_ids:
            try:
                batch_executions[result_table_id] = self.get_batch_executions(
                    result_table_id=result_table_id, period=period
                )
            except Exception as e:
                batch_executions[result_table_id] = {}
                logger.error(e)

        for processing_id in processing_ids:
            try:
                batch_executions[processing_id] = self.get_batch_executions(processing_id=processing_id, period=period)
            except Exception as e:
                batch_executions[processing_id] = {}
                logger.error(e)

        return Response(batch_executions)

    @list_route(methods=["get"], url_path="by_time")
    @params_valid(serializer=DmonitorBatchExecutionSerializer)
    def by_time(self, request, params):
        """
        @api {get} /datamanage/dmonitor/batch_executions/by_time/ 查询离线表的执行状态

        @apiVersion 1.0.0
        @apiGroup DmonitorBatch
        @apiName get_batch_executions_by_time

        @apiParam {time} [start_time] 开始时间
        @apiParam {time} [end_time] 结束时间
        @apiParam {int} [recent_time] 最近时间(秒)

        @apiSuccessExample Success-Response:
            HTTP/1.1 200 OK
            {
                "errors": {},
                "message": "ok",
                "code": "1500200",
                "result": true,
                "data": {
                    "591_batch_table1": [
                        {
                            "exec_id": "",
                            "status": "fail",
                            "status_detail": "失败"
                            "err_msg": "",
                            "err_code": "0",
                            "schedule_time": "2018年11月11日11时"
                        }
                    ]
                }
            }
        """
        batch_executions = {}
        if "start_time" in params and "end_time" in params:
            start_time = timetostr(params["start_time"])
            end_time = timetostr(params["end_time"])
        elif "recent_time" in params:
            recent_time = params["recent_time"]
            end_time = tznow()
            start_time = end_time - datetime.timedelta(seconds=recent_time)
        else:
            raise dm_errors.ParamBlankError()

        queryset = JobnaviExecuteLog.objects.filter(updated_at__gte=start_time, updated_at__lte=end_time)

        for item in queryset:
            status, status_detail, err_msg, err_code = self.get_batch_detail(model_to_dict(item))
            schedule_time = self.get_schedule_time_display(item.schedule_time // 1000)
            if item.schedule_id not in batch_executions:
                batch_executions[item.schedule_id] = []
            batch_executions[item.schedule_id].append(
                {
                    "exec_id": item.exec_id,
                    "type_id": item.type_id,
                    "status": status,
                    "status_detail": status_detail,
                    "src_status": item.status,
                    "err_msg": err_msg,
                    "err_code": err_code,
                    "schedule_id": item.schedule_id,
                    "schedule_time": schedule_time,
                    "schedule_timestamp": item.schedule_time,
                    "started_at": item.started_at.strftime("%Y-%m-%d %H:%M:%S"),
                    "created_at": item.created_at.strftime("%Y-%m-%d %H:%M:%S"),
                    "updated_at": item.updated_at.strftime("%Y-%m-%d %H:%M:%S"),
                }
            )

        return Response(batch_executions)

    @list_route(methods=["get"], url_path="latest")
    @params_valid(serializer=DmonitorBatchScheduleSerializer)
    def latest(self, request, params):
        """
        @api {get} /datamanage/dmonitor/batch_executions/latest/ 查询离线表的执行状态

        @apiVersion 1.0.0
        @apiGroup DmonitorBatch
        @apiName get_latest_batch_executions

        @apiParam {string[]} processing_ids 数据处理ID列表

        @apiSuccessExample Success-Response:
            HTTP/1.1 200 OK
            {
                "errors": {},
                "message": "ok",
                "code": "1500200",
                "result": true,
                "data": {
                    "591_batch_table1": {
                        "exec_id": 1000,
                        "schedule_time": 1586848500000,
                        "status": "finished",
                        "type_id": "spark_sql",
                        "created_at": "2020-01-01 00:00:00",
                        "updated_at": "2020-01-01 00:01:00"
                    }
                }
            }
        """
        import gevent
        from gevent import monkey

        monkey.patch_all()

        processing_ids = params.get("processing_ids", [])
        batch_executions = {}

        index = 0
        step = 20
        max_index = len(processing_ids)
        while index < max_index:
            tasks = []
            for processing_id in processing_ids[index : min(index + step, max_index)]:
                tasks.append(
                    gevent.spawn(
                        self.fetch_latest_execution_by_procs_id,
                        batch_executions,
                        processing_id,
                    )
                )
            gevent.joinall(tasks)
            index += step

        return Response(batch_executions)

    def fetch_latest_execution_by_procs_id(self, batch_executions, processing_id):
        queryset = JobnaviExecuteLog.objects.filter(schedule_id=processing_id).order_by("-exec_id")
        try:
            execution = queryset[0]
            batch_executions[processing_id] = {
                "exec_id": execution.exec_id,
                "schedule_time": execution.schedule_time,
                "status": execution.status,
                "type_id": execution.type_id,
                "created_at": execution.created_at,
                "updated_at": execution.updated_at,
            }
        except Exception:
            pass

class BatchScheduleViewSet(DmonitorMixin, APIViewSet):
    @params_valid(serializer=DmonitorBatchScheduleSerializer)
    def list(self, request, params):
        """
        @api {get} /datamanage/dmonitor/batch_schedules/ 查询离线表的执行状态

        @apiVersion 1.0.0
        @apiGroup DmonitorBatch
        @apiName get_batch_processing_schedules

        @apiParam {string[]} processing_ids 数据处理ID列表

        @apiParamExample {json} 参数样例:
            {
                "processing_ids": ["processing1_id", "processing2_id"],
            }

        @apiSuccessExample Success-Response:
            HTTP/1.1 200 OK
            {
                "data": {
                    "processing1_id": {
                        "delay": 0,
                        "count_freq": 1,
                        "schedule_period": "hour",
                        "accumulate": false,
                        "data_start": -1,
                        "data_end": -1
                    },
                    "processing2_id": {
                        "delay": 1,
                        "count_freq": 1,
                        "schedule_period": "day",
                        "accumulate": true,
                        "data_start": 0,
                        "data_end": 23
                    }
                },
                "message": "ok",
                "code": "1500200",
                "result": true,
                "errors": {}
            }
        """
        processing_ids = params["processing_ids"]
        batch_info_queryset = ProcessingBatchInfo.objects.all()
        schedule_info_queryset = JobnaviScheduleInfo.objects.all()
        results = {}

        index = 0
        step = 100
        length = len(processing_ids)
        while index < length:
            ids = processing_ids[index : min(length, index + step)]
            batch_infos = batch_info_queryset.filter(processing_id__in=ids)
            self.attach_batch_info(results, batch_infos)

            schedule_infos = schedule_info_queryset.filter(schedule_id__in=ids)
            self.attach_schedule_info(results, schedule_infos)
            index += step

        return Response(results)

    def attach_batch_info(self, results, batch_infos):
        for item in batch_infos:
            submit_args = json.loads(item.submit_args)
            config_time = item.created_at or item.updated_at
            if config_time:
                config_time = int(time.mktime(config_time.timetuple()) * 1000)
            results[item.processing_id] = {
                "delay": item.delay,
                "count_freq": item.count_freq,
                "schedule_period": submit_args.get("schedule_period", "hour"),
                "accumulate": submit_args.get("accumulate", False),
                "data_start": submit_args.get("data_start", -1),
                "data_end": submit_args.get("data_end", -1),
                "start_time": submit_args.get("advanced", {}).get("start_time"),
                "config_time": config_time,
            }

    def attach_schedule_info(self, results, schedule_infos):
        for item in schedule_infos:
            if item.schedule_id in results:
                results[item.schedule_id]["first_schedule_time"] = item.first_schedule_time


from architectures.autoencoder import Generator as Autoencoder
from architectures.decoder import Generator as Decoder
from architectures.variational_autoencoder import Generator as Variational_Autoencoder
from dataset.TrackerDS_advanced import TrackerDS_advanced
from engine import *
from transforms import itk_transforms as itktransforms
from transforms import tensor_transforms as tensortransforms
from torchvision import transforms as torchtransforms
import torch
import numpy as np
import glob
from matplotlib import pyplot as plt
#import SimpleITK as sitk
from scipy.ndimage.filters import gaussian_filter

#TRANSFORMS
resample = itktransforms.Resample(new_spacing=[.5, .5, 1.])
tonumpy  = itktransforms.ToNumpy(outputtype='float')
totensor = torchtransforms.ToTensor()
crop     = tensortransforms.CropToRatio(outputaspect=1.)
resize   = tensortransforms.Resize(size=(256,256), interp='BILINEAR')
rescale  = tensortransforms.Rescale(interval=(0,1))
transform = torchtransforms.Compose([resample, tonumpy, totensor, crop, resize, rescale])

ConstantRescale = tensortransforms.ConstantRescale(scaling = np.array([1/250,1/250,1/500,1.,1.,1.,1.]))

#CUDA SETUP
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
tensortype = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor

#DATASET AND DATALOADER#########################################
root = "/Users/cesaremagnetti/Documents/BEng_project/data/phantom_04Mar2020"
#root = "/home/cm19/BEng_project/data/data"
ds = TrackerDS_advanced(root, mode = "infer", transform=transform, target_transform=ConstantRescale)
ds.remove_corrupted_indexes(ds.get_corrupted_indexes())
#############################################################

#load networks###############################################
roots = [
    "/users/cesaremagnetti/final_models/phantom/DECODER/last.tar",
    "/users/cesaremagnetti/final_models/phantom/PRETRAINED/last.tar",
    "/users/cesaremagnetti/final_models/phantom/VARIATIONAL_AUTOENCODER/last.tar",
    "/users/cesaremagnetti/final_models/phantom/AUTOENCODER/last.tar"
]
nets = ()
for idx, root in enumerate(roots):
    print(idx)
    checkpoint = torch.load(root, map_location='cpu')
    cfg = checkpoint['model_info']['model_configuration']
    # cfg['interpolation_scheme'] = 'nearest'
    for key in cfg.keys():
        print("{} --> {}".format(key, cfg[key]))
    if "DECODER" in cfg['net_name']:
        net = Decoder(cfg).to(device)
    elif "VARIATIONAL_AUTOENCODER" in cfg['net_name']:
        net = Variational_Autoencoder(cfg).to(device)
    elif "AUTOENCODER" in cfg['net_name']:
        net = Autoencoder(cfg).to(device)
    else:
        net = Decoder(cfg).to(device)
        print("WARNING: inconsistent name for root directory:\n{}\n, decoder architecture was instanciated by default,"\
              " rename the root directory accordingly if needed:\n i.e. ~/ANY PATH/ARCHITECTURE TYPE (egs: DECODER or"\
              " AUTOENCODER)/ANY PATH/model.tar")

    # 1. filter out unnecessary keys
    pretrained_dict = {}
    flag = True
    for k, v in checkpoint['model_state_dict'].items():
        if k not in net.state_dict():
            if flag:
                print("the following parameters will be discarded:\n")
                flag = False
            print("{}: {}".format(k, v))
        else:
            pretrained_dict[k] = v
    #net.print_info(verbose=True)
    #load state dict
    net.load_state_dict(pretrained_dict)

    #net.print_info(True)
    net.eval()
    #names.append(net.get_info('model_name'))
    nets+=(net,)
#############################################################

#get full trajectory#########################################
x, y, z = [], [], []
for i in range(len(ds)):
    _, tr = ds[i]
    x.append(tr[0])
    y.append(tr[1])
    z.append(tr[2])
    if i%100 ==0:
        print("{}%".format(i/len(ds)*100))
#############################################################

#get a trajectory of 10 samples##############################
N = 10
idx = np.random.randint(len(ds)-N)
idx = 852
imgs,outs = [],[]
for j in range(len(nets)):
    outs.append([])
for i in range(N):
    im,tr = ds[idx+i]
    imgs.append(im.detach().cpu().numpy().squeeze())
    tr = torch.tensor(tr).unsqueeze(0).to(device).type(tensortype)
    for j in range(len(nets)):
        outs[j].append(nets[j].decode(tr).detach().cpu().numpy().squeeze())

IM = np.hstack(imgs)
for j in range(len(outs)):
    outs[j] = np.hstack(outs[j])

IMAGE = np.vstack([IM,]+outs)
cmap = plt.cm.gray
norm = plt.Normalize(vmin=0, vmax=1)
fig = plt.figure(figsize=(15,5))
#plt.set_cmap("Set3")
grid = plt.GridSpec(5, 15, hspace=0., wspace=0.)

points = fig.add_subplot(grid[:,:5])
images = fig.add_subplot(grid[:,5:])

points.scatter(y,z, c = 'k', alpha = 0.5)
trajectory = points.scatter(y[idx:idx+N],z[idx:idx+N],c = np.arange(N),cmap=plt.cm.get_cmap('Set3', N))
fig.colorbar(trajectory, ax = points)
points.set_xlabel("Y (mm)")
points.set_ylabel("Z (mm)")
points.set_title("highlighted trajectory")
# norm = matplotlib.colors.Normalize(vmin=0, vmax=N-1)
# fig.colorbar(SM(norm=norm, cmap=plt.get_cmap("Set3")), ax = points)

images.imshow(cmap(norm(IMAGE)))
names = ["original", "decoder", "pretrained\ndecoder", "autoencoder", "variational\nautoencoder"]
images.set_xticks([128+idx*256 for idx in range(N)])
images.set_xticklabels([idx+1 for idx in range(N)])
images.set_yticks([128+idx*256 for idx in range(len(nets)+1)])
images.set_yticklabels([names[idx] for idx in range(len(names))])

print(idx)
plt.tight_layout()
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Thu Oct 24 11:58:13 2019

@author: Shinhyunjin
"""

#----------------------------------------#
###### Simulation Methods for Fiance ####
#----------------------------------------#

## Author : Shin Hyunjin (KAIST)
## Main Reference : Yuxing Yang(2017)

## Contents

# 1. American Option

# 2. Binary Option

# 3. Barrier Option
# 3-1 Down and out Call
# 3-2 Down and in Call
# 3-3 Up and in Call
# 3-4 Up and out Call
# 3-5 Down and out Put
# 3-6 Down and in Put
# 3-7 Up and in Put
# 3-8 Up and out Put

# 4. Double Rebate Option

# 5. Asian Option
# 5-1 Asian Call
# 5-2 Asian Put

# Further Study : Lookback Option ,

#----------------------------------------------------------------------------#

# Load Library

import random
import scipy as sp
from scipy import log, exp, sqrt, stats

#------------------------------------------------------------------------------#

# 2. Binary Option

def terminalStockPrice(S, T, r, sigma):

    tao = random.gauss(0, 1.0)
    terminalPrice = S * sp.exp((r-0.5*sigma**2)*T+sigma*sp.sqrt(T)*tao)
    return terminalPrice

def binaryCallPayoff(x, sT,payoff):

    if sT >=x:
        return payoff
    else:
        return 0.0

def binaryPutPayoff(x, sT,payoff):

    if sT >=x:
        return 0
    else:
        return payoff

# input var
S = 40
x = 41
T = 0.5
r = 0.01
sigma = 0.2
fixedPayoff = 10.0
nSimulations = 10000
payoffs = 0 #initial payoff

# binary Call Option Simulations
for i in range(nSimulations):

    sT = terminalStockPrice(S, T, r, sigma)
    payoffs += binaryCallPayoff(x, sT, fixedPayoff)

# result

price = sp.exp(-r*T) * (payoffs/float(nSimulations))
print('Binary options call = %.8f' % price)

#-----------------------------------------------------------------------------#

# 5. Asian Option #
# 쓸데없는 팁 : 평균가 옵션이 아시안 옵션인 이유
# 홍콩시장에선 선물과 옵션의 행사가가 평균가로 정해진다.
# 그래서 HSI, HSCEI 선물 옵션을 거래할때는 행사가를 평균가, VWAP 등을 활용해 접근한다.
# 선물 옵션 만기일에 종가를 조작하는 세력을 방지할 수 있어 진일보된 방법.

# input

s0 = 30
x = 32
T = 3/12
r = 0.015
sigma = 0.18
sp.random.seed(123)
nSimulations = 1000
nSteps = 500
dt = T/ nSteps

# 5-1 Asian Call

asianCall = sp.zeros([nSimulations], dtype = float)

for j in range(0, nSimulations):

    sT = s0
    total = 0
    for i in range(0, int(nSteps)):
        e = sp.random.normal()
        sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt))
        total += sT
        price_average = total / nSteps

    asianCall[j] = max(price_average - x, 0)

# result

asianCallPrice = sp.mean(asianCall)*sp.exp(-r*T)
print('Call price based on average price = ', round(asianCallPrice, 3))#

# 5-1 Asian Put

asianPut = sp.zeros([nSimulations], dtype = float)

for j in range(0, nSimulations):

    sT = s0
    total = 0
    for i in range(0, int(nSteps)):
        e = sp.random.normal()
        sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt))
        total += sT
        price_average = total / nSteps

    asianPut[j] = max(x-price_average, 0)

# result

asianPutPrice = sp.mean(asianPut)*sp.exp(-r*T)
print('Put price based on average price = ', round(asianPutPrice, 3))#

이색옵션 시뮬레이션.py


def rice(vetor):
	if ( len(vetor) <= 1 ):
		return null
	n_one = 0
	n_zero = 0
	for i in vetor: # Calcula o numero de 1 e 0
		if i == 0:
			n_zero += 1
		elif i == 1:
			n_one += 1
		else:
			continue
	rice = (n_one - n_zero)/(n_one + n_zero)
	return rice


import asyncio
import config
import discord
import owotrans

from discord.ext import commands
from utils.checks import is_approved_talker

class Fun(commands.Cog):
	def __init__(self, bot):
		self.bot = bot

	@commands.command()
	async def owo(self, ctx, *, arg):
		"""owoifies your message"""
		msg = owotrans.owo(arg)
		await ctx.send(msg)

	@commands.command(aliases=["say"])
	@is_approved_talker()
	@commands.bot_has_permissions(manage_messages=True)
	async def talk(self, ctx, *, arg):
		"""deletes your message and talks through the bot"""
		await ctx.message.delete()
		await ctx.send(arg)

	@commands.command()
	async def dice(self, ctx):
		""" Roll the dice until you die (roll 4 to 6). """
		from random import randint

		# Delete message which invoked this command
		await ctx.message.delete()

		game_in_progress = True
		round_number = 1
		accumulated_score = 0

		# Initialise the reactions, message
		roll_emoji = "\U0001F3B2"
		stop_emoji = "\U0000274E"
		embed = discord.Embed(title="Dice Game")
		embed.description = f"| Player : {ctx.author.name} | Round : {round_number} | Game In Progress |"
		embed.add_field(name="Score", value=str(accumulated_score), inline=False)
		embed.set_footer(text=f"{roll_emoji} to keep rolling, {stop_emoji} to end game and keep your score")

		# Show the message
		message = await ctx.send(embed=embed)
		await message.add_reaction(roll_emoji)
		await message.add_reaction(stop_emoji)

		# This function will handle user input
		def progress(reaction, user):
			nonlocal user_choice
			# If the user is not the player or the reaction is not on the correct message, ignore
			if (user is None) or (user.id != ctx.author.id) or (reaction.message.id != message.id):
				user_choice = None
				return False

			if reaction.emoji == roll_emoji:
				user_choice = roll_emoji

			elif reaction.emoji == stop_emoji:
				user_choice = stop_emoji

			return True

		# The game starts here
		user_choice = None			# Stores the user choice made in each iteration of the while loop
		while game_in_progress:
			try:
				curr_reaction, curr_user = await ctx.bot.wait_for('reaction_add', check=progress, timeout=60.0)
				await message.remove_reaction(curr_reaction, curr_user)
			except asyncio.TimeoutError:
				user_choice = None
				game_in_progress = False
				await message.delete()
				await ctx.send(f"`Dice Game: {ctx.author.name} timed out with a score of {accumulated_score} on Round {round_number}.`")
			except Exception as e:
				pass			# Choose to ignore this error

			# The player chooses to roll again
			if user_choice is roll_emoji:
				round_number += 1
				roll = randint(1, 6)

				if roll <= 3:		# The player rolls successfully, the score will be added
					accumulated_score += roll
					embed.description = f"| Player : {ctx.author.name} | Round : {round_number} | Game In Progress |"
					embed.set_field_at(0, name="Score", value=f"You rolled a {roll}. Your new score is {accumulated_score}.")
					await message.edit(embed=embed)
				else:				# The player rolls more than 3 and die
					game_in_progress = False
					await message.delete()
					await ctx.send(f"`Dice Game: {ctx.author.name} rolled a {roll} and lost with a score of {accumulated_score} on Round {round_number}`")

			# The player chooses to stop
			elif user_choice is stop_emoji:
				game_in_progress = False
				await message.delete()
				await ctx.send(f"`Dice Game: {ctx.author.name} kept a score of {accumulated_score} on Round {round_number}`")

	@commands.command()
	async def nword(self, ctx, member: discord.Member):
		nword1_counter = self.bot.nword1_counter.get(member.id, 0)
		nword2_counter = self.bot.nword2_counter.get(member.id, 0)
		nwords = nword2_counter + nword1_counter
		await ctx.send(f"Thank you for the request, comrade. I have looked through {member.mention}'s posting history and found {nwords} N-words, of which {nword1_counter} were hard-Rs since 27/10/19.")

def setup(bot):
	bot.add_cog(Fun(bot))


# from dolfin.cpp.generation import BoxMesh
# from dolfin.cpp.generation import Point
from dolfin import *

N = 60
lx, ly, lz = 0.2, 0.1, 1.0
mesh = BoxMesh(Point(0, 0, 0), Point(lx, ly, lz), int(lx*N), int(ly*N), int(lz*N))

# from dolfin.function.functionspace import FunctionSpace
# from dolfin.function.argument import TrialFunction
# from dolfin.function.argument import TestFunction

from ufl import inner
from ufl import grad
from ufl import dx

V = FunctionSpace(mesh, 'Lagrange', 1)

u_h = TrialFunction(V)
v_h = TestFunction(V)

a = inner(grad(u_h), grad(v_h)) * dx
b = inner(u_h, v_h) * dx

# from dolfin.cpp.la import PETScMatrix
# from dolfin.fem.assembling import assemble

A = PETScMatrix(V.mesh().mpi_comm())
assemble(a, tensor=A)

B = PETScMatrix(V.mesh().mpi_comm())
assemble(b, tensor=B)

bc_dofs = []
for bc in bcs:
    bc_dofs.extend(list(bc.get_boundary_values().keys()))

A.mat().zeroRowsColumnsLocal(bc_dofs, diag=diag_value)
B.mat().zeroRowsColumnsLocal(bc_dofs, diag=1.)

import scipy

A_array = scipy.sparse.csr_matrix(A.mat().getValuesCSR()[::-1])
B_array = scipy.sparse.csr_matrix(B.mat().getValuesCSR()[::-1])

k = 20  # number of eigenvalues
which = 'SM'  # smallest magnityde
w, v = scipy.linalg.eigh(A_array, B_array, k=k, which=which)

import numpy as np
from dolfin.cpp.la import SLEPcEigenSolver

# instantiate solver
solver = SLEPcEigenSolver(A, B)

# update parameters
solver.parameters['solver'] = 'krylov-schur'
solver.parameters['spectrum'] = 'smallest magnitude'
solver.parameters['problem_type'] = 'gen_hermitian'
solver.parameters['tolerance'] = 1e-4

# solve
n_eig = 20
solver.solve(n_eig)

# collect eigenpairs
w, v = [], []
for i in range(solver.get_number_converged()):
    r, _, rv, _ = solver.get_eigenpair(i)
    w.append(r)
    v.append(rv)

w = np.array(w)
v = np.array(v).T


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 12 17:54:14 2018

@author: temp
"""

import matplotlib.pyplot as plt
import numpy as np
import os.path as path
import glob
from obspy import read
from matplotlib.gridspec import GridSpec
import dread
import matplotlib as mpl

plt.rcParams["font.family"] = "serif"
plt.rcParams['axes.axisbelow'] = True
mpl.rcParams.update({'font.size': 18})

#all in SI units
#mag_ub = 3.9#2.77
#mag_lb = 2.8#2.75
beta = 3500. #3500m/s
stressdrop = 5e6 #pascals
U = 0.63#0.63
rho = 2750. #kg/m^3

top_dir = '/Volumes/USGS_Data/project'

box = 'all_paths'

boxpath = top_dir + '/boxes/' + box

#Joe_ev = top_dir + '/catalogs/Joe_evlist.txt'
#events = np.genfromtxt(Joe_ev, dtype = None, comments = '#', delimiter = None, names = True)
#ev_list = events['evid']

#velocity time series N,E
seismo_files = glob.glob(boxpath + '/corrected/Event_*/*HHN*.SAC')

#record_files = []
##record_files_dist = []
#for ev in ev_list:
#    record_files.extend(glob.glob(boxpath + '/record_spectra_meters/Event_' + ev + '/*.out'))

record_files = glob.glob(boxpath + '/record_spectra_meters/Event_*/*.out')

event_dir = boxpath + '/secondo_meters/'
#event_files = glob.glob(boxpath + '/secondo/*.out')

station_dir = boxpath + '/secondo_meters/'
#station_files = glob.glob(boxpath + '/secondo/*.out')

#print record_paths
print(len(record_files))

hardstart = boxpath + '/record_spectra_meters/Event_2010_02_06_09_12_27/AZ_BZN_HHNE__2010_02_06_09_12_27.out'
ind = record_files.index(hardstart)
#
#ind = 0
for i in range(ind, ind+20):  ##for every record
#for i in range(ind, ind+20):
#    #North component
    base = path.basename(record_files[i])
    box = record_files[i].split('/')[-4]
    network, station, channel, loc = base.split('_')[0:4]
    yyyy, month, day, hh, mm, ss = base.split('_')[4:]
    ss = ss.split('.')[0]
    eventid = yyyy + '_' + month + '_' + day + '_' + hh + '_' + mm + '_' + ss
#
#    #read in raw data for record info
#    #correct for distance
    raw_file_N = boxpath + '/corrected/Event_'+ eventid + '/' + network + '_' + station + '_HHN_' + loc + '_' + eventid + '.SAC'
    stream = read(raw_file_N)
    tr = stream[0]
    dataN = tr.data

    raw_file_E = boxpath + '/corrected/Event_'+ eventid + '/' + network + '_' + station + '_HHE_' + loc + '_' + eventid + '.SAC'
    stream = read(raw_file_E)
    tr = stream[0]
    dataE = tr.data
#    print(raw_file)
#
    evlon =  tr.stats.sac.evlo #deg
    evlat =  tr.stats.sac.evla #deg
    evdepth = tr.stats.sac.evdp #km
    stlon = tr.stats.sac.stlo #deg
    stlat = tr.stats.sac.stla #deg
    stdepth = tr.stats.sac.stdp #km

    ml = tr.stats.sac.mag
#
#    #find distance between event and station
    dist_km =  dread.compute_rrup(evlon, evlat, evdepth, stlon, stlat, stdepth) #in km
#    #km to m
    dist_m = dist_km*1000.

    record_data = np.genfromtxt(record_files[i], dtype = float, comments = '#', delimiter = None, usecols = (0,1))#only read in first two cols
    f_bins = record_data[:,0]

    #record spectra
    record_spec = record_data[:,1]

    #record spectra distance corrected
#    record_data_dist = np.genfromtxt(record_files_dist[i], dtype = float, comments = '#', delimiter = None, usecols = (0,1))#only read in first two cols

#    record_spec_dist = record_data_dist[:,1]
    #event spectra
    event_data = np.genfromtxt(event_dir + eventid + '.out', dtype = float, comments = '#', delimiter = None, usecols = (0,1))
    event_spec = event_data[:,1]
    #station spectra
    station_data = np.genfromtxt(station_dir + station + '.out', dtype = float, comments = '#', delimiter = None, usecols = (0,1))
    station_spec = station_data[:,1]

#    Bias_data = np.genfromtxt( boxpath + '/secondo_Bias/Bias.out', dtype = float, comments = '#', delimiter = None, usecols = (0,1))
#    Bias = Bias_data[:,1]

    ##calculate the Brune spectra
    #if less than 3, convert local magnitude to moment magnitude
    if ml < 3.0:
        M = 0.884 + 0.667*ml#0.884 + 0.667*ml
    else:
        M = ml
    #compute Brune in SI units
    #Moment from the moment magnitude
    M0 = 10.**((3./2.)*M + 9.1)
    #corner frequency
    fc = beta*(stressdrop/(8.47*M0))**(1./3.)
    omega0 = (M0*U)/(4.*rho*np.pi*(beta**(3.0)))
#   omega0 = M0
#    print 'local magnitude', ml, 'Moment magnitude', M, 'Moment ', M0, 'corner frequency (Hz)', fc, 'Omega0 ', omega0
    #brune spectra over all frequencies
    Brune = (2.*np.pi*(f_bins)*omega0)/(1.+((1./fc)*f_bins)**2.)
    #m^2
#    Brune = Brune*(10.**4.)

#    plot the station spectra
    fig = plt.figure(figsize = (22,16))
    plt.tight_layout(pad=0.01, w_pad=0.01, h_pad=0.01)
    plt.subplots_adjust(left=0.06, right=0.99, top=0.95, bottom=0.12)

    gs = GridSpec(3,2)

    plt.subplot(gs.new_subplotspec((0, 0), colspan=2))
    plt.title('event ' +  eventid, fontsize = 15)
    plt.plot(dataE, color='black', label = 'EW')
    plt.plot(dataN, color = 'red', label = 'NS')
    plt.legend(loc=1, borderaxespad=0.)
#    fig.yaxis.get_major_formatter().set_powerlimits((0, 1))
    plt.gca().yaxis.get_major_formatter().set_powerlimits((0, 1))
#    fig.text(0.04, 0.3, 'Velocity amplitude (cm)', va='center', rotation='vertical', fontsize = 20)
#    fig.text(0.5, 0.05, 'distance: (km) ' + str(round(dist_km,1)) + ' distance: (cm) ' + str(round(dist_cm,2), fontsize = 20)

    plt.ylabel('Velocity (m/s)')

    plt.subplot(gs.new_subplotspec((1, 0), colspan=1))
    plt.xlim(0.1,70)
    plt.grid()
#    plt.loglog(f_bins, record_spec, color = 'b', label = 'record')
    plt.loglog(f_bins, record_spec, label = 'record')
    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.tick_params(axis='both', which='both', length = 5, width = 1)
    plt.legend()
    fig.text(0.01, 0.4, 'Velocity amplitude (m)', va='center', rotation='vertical', fontsize = 12)

    fig.text(0.4, 0.01, 'distance: (km) ' + str(round(dist_km,1)) + ' distance: (m) ' + '{:.2e}'.format(dist_m), fontsize = 16)

    plt.subplot(gs.new_subplotspec((1, 1), colspan=1))
    plt.xlim(0.1,70)
    plt.grid()
#    plt.title('distance: (km) ' + str(round(dist_km,1)) + ' distance: (cm) ' + str(round(dist_cm,2)))

    plt.loglog(f_bins, record_spec*dist_m, color = 'b', label = 'record dist corr')
    plt.loglog(f_bins, station_spec*event_spec, color='black', label = 'event*site')
    plt.legend(loc=1, borderaxespad=0.)
#    plt.xlim(0.1, 50)
    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.tick_params(axis='both', which='both', length = 5, width = 1)

    plt.subplot(gs.new_subplotspec((2, 0), colspan=1))
    plt.xlim(0.1,70)
    plt.xlabel('event ' + eventid, fontsize = 16)
    plt.grid()
    plt.loglog(f_bins, event_spec, color = 'green')
    plt.loglog(f_bins, Brune, ls = '--', color = 'green')
#    plt.xlim(0.1, 50)
    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.tick_params(axis='both', which='both', length = 5, width = 1)

    plt.subplot(gs.new_subplotspec((2, 1), colspan=1))
    plt.xlim(0.1,70)
    plt.xlabel('station ' + station, fontsize = 16)
    plt.grid()
    plt.loglog(f_bins, station_spec, color='r')
#    plt.xlim(0.1, 50)
    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.tick_params(axis='both', which='both', length = 5, width = 1)
#    plt.title('Frequency (Hz)', fontsize = 15)
    plt.savefig(boxpath + '/plots_unconstrained/' + eventid + '.' + network + '.' +  station + '.png')
    plt.show()
#    plt.close()
#plt.close('all')


from django.urls import path
from . views import (
    order_add, place_order, orders_list,
    order_details, order_placed, my_orders,
    order_payments, order_update, render_pdf_view, order_pdf_view
)

app_name = 'orders'

urlpatterns = [
    path('list/', orders_list, name = 'orders-list'),

    path('invoice/', render_pdf_view, name = 'invoice'),
    path('invoice/<transaction_id>/', order_pdf_view, name = 'order_pdf_view'),

    path('order/add/', order_add, name = 'order-add'),
    path('order/placed/', order_placed, name = 'order-placed'),
    path('order<str:transaction_id>/edit/', order_update, name = 'order-edit'),
    path('order/<str:transaction_id>/', order_details, name = 'order-details'),
    path('my-orders/', my_orders, name = 'my-orders'),
    path('place-order/', place_order, name = 'place-order'),
    path('<str:transaction_id>/payments/', order_payments, name = 'order-payments'),

]


import serial
from selenium import webdriver
import os

caminho = "file:"+os.path.dirname(os.path.abspath(__file__))+ "/Site/testeSiteHtml.html"

porta = 'COM7'

ser = serial.Serial(porta, 9600)
a = True
maxRange = ser.read_until().decode('ascii').strip() #Maximo
while a:
    try:
        maxRange = ser.read_until().decode('ascii').strip() #Maximo
        print("Max "+maxRange)
    except:
        a=False
driver = webdriver.Chrome()

driver.get(caminho)

dataAnterior = ser.read_until().decode('ascii').strip()
aux = True
while True:
    data = ser.readline().decode().strip()  # leitura da porta serial
    print(f"Valor do encoder: {data}")  # exibição do valor do encode
    if(dataAnterior != data):
        driver.execute_script(f"moverHorizontalmente("+str(data)+","+str(maxRange)+");")
        dataAnterior = data
        aux = True
    elif (aux):
        driver.execute_script(f"pararAviao();")
        aux = False


import sys

from ariblib import tsopen
from ariblib.descriptors import ShortEventDescriptor
from ariblib.sections import EventInformationSection

def show_program(eit):
    event = iter(eit.events).__next__()
    program_title = event.descriptors[ShortEventDescriptor][0].event_name_char
    d_week = {'Sun': '日', 'Mon': '月', 'Tue': '火', 'Wed': '水', 'Thu': '木', 'Fri': '金', 'Sat': '土'}
    start = event.start_time.strftime('%Y%m%d(') + d_week[event.start_time.strftime('%a')] + event.start_time.strftime(')%H%M')
    return "{} {}".format(start, program_title)

with tsopen(sys.argv[1]) as ts:

    #番組を表示する
    int_i = 0
    while True:
        EventInformationSection._table_ids = [0x4E]
        try:
            current = next(table for table in ts.sections(EventInformationSection) if table.section_number == int_i)
            print(show_program(current))
            int_i += 1
        except:
            break


from matplotlib.pyplot import bar
import requests
from bs4 import BeautifulSoup
import json

bvid = 'BV1os411D7be'
# bvid = 'BV1Q3411Y7Z6'

def get_video_cid(bvid):
    video_info = requests.get("https://api.bilibili.com/x/player/pagelist?bvid="+bvid+"&jsonp=jsonp")
    print('get cid',video_info.status_code)
    video_info = BeautifulSoup(video_info.text, "html.parser")
    video_info_json = json.loads(video_info.text)
    cid = video_info_json['data'][0]['cid']
    return cid

def get_barrage(cid):
    barrage_info = requests.get("https://api.bilibili.com/x/v1/dm/list.so?oid="+str(cid))
    print('get barrage',barrage_info.status_code)
    barrage_info.encoding = 'utf-8'
    barrage_info = BeautifulSoup(barrage_info.text, "xml")
    # print(barrage_info)
    barrages = barrage_info.find_all('d')
    result = []
    for barrage in barrages:
        # print(barrage['p'],barrage.get_text())
        single_barrage_info = barrage['p'].split(',')
        single_barrage_text = barrage.get_text()
        result.append([single_barrage_info[0],single_barrage_text])
    return result

def save_barrage(bvid,barrage):
    with open(bvid+'_barrage.txt','w') as file:
        for single_barrage in barrage:
            file.write(single_barrage[0]+' "'+single_barrage[1]+'"\n')
    return

if __name__ == "__main__":
    cid = get_video_cid(bvid)
    print('cid : ',cid)
    barrage = get_barrage(cid)
    save_barrage(bvid,barrage)


from collections import defaultdict
all=[ s.strip() for s in open('3','r').readlines()]
row = tree = lambda: defaultdict(list)
cloth = defaultdict(row)
doub = 0

testdata = [
"#123 @ 1,3: 4x4",
"#2 @ 3,1: 4x4",
"#3 @ 5,5: 2x2"
]

ids = defaultdict(bool)
#all = testdata
for s in all:
	id,_,coord,grid = s.split()
	id = id[1:]
	x,y = [int(_) for _ in coord[:len(coord)-1].split(",")]
	sx,sy = [int(_) for _ in grid.split("x")]

	ids[id] = True
	for i in range(x, x+sx):
		for j in range(y, y+sy):
			cloth[j][i].append(id)

for y in range(len(cloth)):
	for x in range(len(cloth)):
		l = len(cloth[y][x])
		if l > 1:
			doub += 1
			for i in cloth[y][x]:
				ids[i] = False

print("doubles:", doub)

for i in ids:
	if ids[i]:
		print("Remains: ", i)


from collections import defaultdict

lat = input()
assert (lat[-1] == 'n' or lat[-1] == 's')
dir1 = None
if lat[-1] == 'n':
    dir1 = "North"
else:
    dir1 = "South"
lat = lat[:-1]
longitude = input()
assert (longitude[-1] == 'e' or longitude[-1] == 'w')
dir2 = None
if longitude[-1] == 'e':
    dir2 = "East"
else:
    dir2 = "West"
longitude = longitude[:-1]

l = defaultdict(lambda: 0)

for i in lat:
    l[i] += 1

keymax1 = max(l, key=l.get)
keymin1 = min(l, key=l.get)

m = defaultdict(lambda: 0)

for j in longitude:
    m[j] += 1

keymax2 = max(m, key=m.get)
keymin2 = min(m, key=m.get)
ans1 = l[keymax1] - l[keymin1]
ans2 = m[keymax2] - m[keymin2]
print(ans1, dir1, ans2, dir2)


#!/usr/bin/env python
import boto3
s3 = boto3.resource("s3")
bucket_name = "aamanrebellohack"
object_name = "readings.txt"
try:
 response = s3.Object(bucket_name, object_name).put(Body=open(object_name, 'rb'))
 print(response)
except Exception as error:
 print(error)

# NOTE: Above function essentially sends a file (which will always be called readings.txt) to
# an S3 bucket called aamanrebellohack.

# This is what was created for the purposes of the hackathon, although naturally there is a need
# to scale up the code with modules and functions if we wish to implement this idea on a larger scale.
# There may be more than one buckets and the file may not always be called readings.txt


#MarielaMoralesPerez
Es = [
	("Ballena","Mammalia"),
	("Delfin", "Mammalia"),
	("Gato", "Mammalia"),
	("Oso", "Mammalia"),
	("Tortuga", "Oviparos"),
	("Cocodrilo", "Oviparos"),
	("Iguana", "Oviparos"),
	("Gallo", "Oviparos"),
	("Mammalia","Viviparos"),
	("Viviparos","Tetrapodos"),
	("Oviparos", "Seuropsidos"),
	("Seuropsidos", "Tetrapodos"),
	("Tetrapodos", "Vertebrados")
]

Tiene = [
	("Ballena","G Mamarias"),
	("Ballena", "Piel queratina"),
	("Delfin","G Mamarias"),
	("Oso","G Mamarias"),
	("Oso","Pelo"),
	("Oso", "Garras"),
	("Gato","G Mamarias"),
	("Gato","Pelo"),
	("Gato", "Garras"),

	("Oviparos", "Piel queratina")
]

Vive = [
	("Ballena","Agua"),
	("Delfin", "Agua"),
	("Gato", "Tierra"),
	("Oso", "Tierra"),
	("Iguana", "Tierra"),
	("Gallo", "Tierra"),
	("Tortuga", "Agua"),
	("Tortuga", "Tierra"),
	("Cocodrilo", "Agua"),
	("Cocodrilo", "Tierra")
]

def Ser(A, B, C):
    c=0
    while c<len(C) :
        if C[c][0]==A:
            if C[c][1]==B:
                return True
            A = C[c][1]
        c=c+1
    return False

def Tener(A, B, C):
    e=0
    while e<len(C) :
        if C[e][0]==A:
            if C[e][1]==B:
                return True
        e=e+1
    return False

def Vivir(A, B, C):
    e=0
    while e<len(C) :
        if C[e][0]==A:
            if C[e][1]==B:
                return True
        e=e+1
    return False

print(Ser("Ballena","Vertebrados",Es))
print(Tener("Gato","Garras",Tiene))
print(Vivir("Gato","Agua",Vive))


def po(a,l):
    for i in range(0,l):
        if i==0:
            if sum(a[i+1::])==0:
                return "YES"
        elif i==l-1:
            continue
        else:
            x=a[i+1::]
            y=a[i-1::-1]
            if sum(x)==sum(y):
                return "YES"
    return "NO"

for _ in range(int(input())):
    f=int(input())
    d=list(map(int,input().split()))
    print(po(d,f))


import torch

from bench.core.executer import Executer

def intersect(box_a, box_b):
    """ We resize both tensors to [A,B,2] without new malloc:
    [A,2] -> [A,1,2] -> [A,B,2]
    [B,2] -> [1,B,2] -> [A,B,2]
    Then we compute the area of intersect between box_a and box_b.
    Args:
      box_a: (tensor) bounding boxes, Shape: [n,A,4].
      box_b: (tensor) bounding boxes, Shape: [n,B,4].
    Return:
      (tensor) intersection area, Shape: [n,A,B].
    """
    n = box_a.size(0)
    A = box_a.size(1)
    B = box_b.size(1)
    max_xy = torch.min(box_a[:, :, 2:].unsqueeze(2).expand(n, A, B, 2),
                       box_b[:, :, 2:].unsqueeze(1).expand(n, A, B, 2))
    min_xy = torch.max(box_a[:, :, :2].unsqueeze(2).expand(n, A, B, 2),
                       box_b[:, :, :2].unsqueeze(1).expand(n, A, B, 2))
    inter = torch.clamp((max_xy - min_xy), min=0)
    return inter[:, :, :, 0] * inter[:, :, :, 1]

def args_adaptor(np_args):
    box_a = torch.from_numpy(np_args[0]).cuda()
    box_b = torch.from_numpy(np_args[1]).cuda()
    return [box_a, box_b]

def executer_creator():
    return Executer(intersect, args_adaptor)


import sys
import os
import time
import itertools
import subprocess
import multiprocessing

from . import util, errors, space

#python3 support
PY3 = sys.version_info > (3,)

class Destination(object):
    type = filename = overwrite = value = config = limits = None
    opts = {}

    def set_final_filename(self, filename, overwrite):
        self.type = 'final'
        self.filename = filename
        self.overwrite = overwrite

    def set_final_options(self, opts):
        if opts is not False:
            self.opts = opts

    def set_limits(self, limits):
        self.limits = limits

    def set_config(self, conf):
        self.config = conf

    def set_tmp_filename(self, filename):
        self.type = 'tmp'
        self.filename = filename

    def set_memory(self):
        self.type = 'memory'

    def store(self, verse):
        self.value = None
        if verse.dimension == 0:
            raise ValueError('Empty output, Multiverse contains no spaces')
        if self.type == 'memory':
            self.value = verse
        elif self.type == 'tmp':
            verse.tofile(self.filename)
        elif self.type == 'final':
            for sp, fn in zip(verse.spaces, self.final_filenames()):
                sp.config = self.config
                sp.tofile(fn)

    def retrieve(self):
        if self.type == 'memory':
            return self.value

    def final_filenames(self):
        fns = []
        if not self.limits == None:
            base, ext = os.path.splitext(self.filename)
            for limlabel in util.limit_to_filelabel(self.limits):
                fn = (base + '_' + limlabel + ext).format(**self.opts)
                if not self.overwrite:
                    fn = util.find_unused_filename(fn)
                fns.append(fn)
        else:
            fn = self.filename.format(**self.opts)
            if not self.overwrite:
                fn = util.find_unused_filename(fn)
            fns.append(fn)
        return fns

class DispatcherBase(util.ConfigurableObject):
    def __init__(self, config, main):
        self.main = main
        super(DispatcherBase, self).__init__(config)

    def parse_config(self, config):
        super(DispatcherBase, self).parse_config(config)
        self.config.destination = Destination()
        destination = config.pop('destination', 'output.hdf5')  # optional 'output.hdf5' by default
        overwrite = util.parse_bool(config.pop('overwrite', 'false'))  #by default: numbered files in the form output_  # .hdf5:
        self.config.destination.set_final_filename(destination, overwrite)  # explicitly parsing the options first helps with the debugging
        self.config.host = config.pop('host', None)  # ip adress of the running gui awaiting the spaces
        self.config.port = config.pop('port', None)  # port of the running gui awaiting the spaces
        self.config.send_to_gui = util.parse_bool(config.pop('send_to_gui', 'false'))  # previewing the data, if true, also specify host and port

    def send(self, verses):  # provides the possiblity to send the results to the gui over the network
        if self.config.send_to_gui or (self.config.host is not None and self.config.host is not None):  # only continue of ip is specified and send_to_server is flagged
            for M in verses:
                if self.config.destination.limits is None:
                    sp = M.spaces[0]
                    if isinstance(sp, space.Space):
                        util.socket_send(self.config.host, int(self.config.port), util.serialize(sp, ','.join(self.main.config.command)))
                else:
                    for sp, label in zip(M.spaces, util.limit_to_filelabel(self.config.destination.limits)):
                        if isinstance(sp, space.Space):
                            util.socket_send(self.config.host, int(self.config.port), util.serialize(sp, '{0}_{1}'.format(','.join(self.main.config.command), label)))
                yield M
        else:
            for M in verses:
                yield M

    def has_specific_task(self):
        return False

    def process_jobs(self, jobs):
        raise NotImplementedError

    def sum(self, results):
        raise NotImplementedError

# The simplest possible dispatcher. Does the work all by itself on a single
# thread/core/node. 'Local' will most likely suit your needs better.
class SingleCore(DispatcherBase):
    def process_jobs(self, jobs):
        for job in jobs:
            yield self.main.process_job(job)

    def sum(self, results):
        return space.chunked_sum(self.send(results))

# Base class for Dispatchers using subprocesses to do some work.
class ReentrantBase(DispatcherBase):
    actions = 'user',

    def parse_config(self, config):
        super(ReentrantBase, self).parse_config(config)
        self.config.action = config.pop('action', 'user').lower()
        if self.config.action not in self.actions:
            raise errors.ConfigError('action {0} not recognized for {1}'.format(self.config.action, self.__class__.__name__))

    def has_specific_task(self):
        if self.config.action == 'user':
            return False
        else:
            return True

    def run_specific_task(self, command):
        raise NotImplementedError

# Dispatch multiple worker processes locally, while doing the summation in the main process
class Local(ReentrantBase):
    ### OFFICIAL API
    actions = 'user', 'job'

    def parse_config(self, config):
        super(Local, self).parse_config(config)
        self.config.ncores = int(config.pop('ncores', 0))  # optionally, specify number of cores (autodetect by default)
        if self.config.ncores <= 0:
            self.config.ncores = multiprocessing.cpu_count()

    def process_jobs(self, jobs):
        if self.config.ncores == 1 and not PY3:  # note: SingleCore will be marginally faster
            map = itertools.imap
        else:
            pool = multiprocessing.Pool(self.config.ncores)
            map = pool.imap_unordered

        configs = (self.prepare_config(job) for job in jobs)
        for result in map(self.main.get_reentrant(), configs):
            yield result

    def sum(self, results):
        return space.chunked_sum(self.send(results))

    def run_specific_task(self, command):
        if command:
            raise errors.SubprocessError("invalid command, too many parameters: '{0}'".format(command))
        if self.config.action == 'job':
            result = self.main.process_job(self.config.job)
            self.config.destination.store(result)

    ### UTILITY
    def prepare_config(self, job):
        config = self.main.clone_config()
        config.dispatcher.destination.set_memory()
        config.dispatcher.action = 'job'
        config.dispatcher.job = job
        return config, ()

# Dispatch many worker processes on an Oar cluster.

class Oar(ReentrantBase):
    ### OFFICIAL API
    actions = 'user', 'process'

    def parse_config(self, config):
        super(Oar, self).parse_config(config)
        self.config.tmpdir = config.pop('tmpdir', os.getcwd())  # Optional, current directory by default
        self.config.oarsub_options = config.pop('oarsub_options', 'walltime=0:15')  # optionally, tweak oarsub parameters
        self.config.executable = config.pop('executable', ' '.join(util.get_python_executable()))  # optionally, override default location of python and/or BINoculars installation

    def process_jobs(self, jobs):
        self.configfiles = []
        self.intermediates = []
        clusters = util.cluster_jobs2(jobs, self.main.input.config.target_weight)
        for jobscluster in clusters:
            uniq = util.uniqid()
            jobconfig = os.path.join(self.config.tmpdir, 'binoculars-{0}-jobcfg.zpi'.format(uniq))
            self.configfiles.append(jobconfig)

            config = self.main.clone_config()
            interm = os.path.join(self.config.tmpdir, 'binoculars-{0}-jobout.hdf5'.format(uniq))
            self.intermediates.append(interm)
            config.dispatcher.destination.set_tmp_filename(interm)
            config.dispatcher.sum = ()

            config.dispatcher.action = 'process'
            config.dispatcher.jobs = jobscluster
            util.zpi_save(config, jobconfig)
            yield self.oarsub(jobconfig)

        #if all jobs are sent to the cluster send the process that sums all other jobs
        uniq = util.uniqid()
        jobconfig = os.path.join(self.config.tmpdir, 'binoculars-{0}-jobcfg.zpi'.format(uniq))
        self.configfiles.append(jobconfig)
        config = self.main.clone_config()
        config.dispatcher.sum = self.intermediates
        config.dispatcher.action = 'process'
        config.dispatcher.jobs = ()
        util.zpi_save(config, jobconfig)
        yield self.oarsub(jobconfig)

    def sum(self, results):
        jobs = list(results)
        jobscopy = jobs[:]
        self.oarwait(jobs)
        self.oar_cleanup(jobscopy)
        return True

    def run_specific_task(self, command):
        if self.config.action != 'process' or (not self.config.jobs and not self.config.sum) or command:
            raise errors.SubprocessError("invalid command, too many parameters or no jobs/sum given")

        jobs = sum = space.EmptyVerse()
        if self.config.jobs:
            jobs = space.verse_sum(self.send(self.main.process_job(job) for job in self.config.jobs))
        if self.config.sum:
            sum = space.chunked_sum(space.Multiverse.fromfile(src) for src in util.yield_when_exists(self.config.sum))
        self.config.destination.store(jobs + sum)

    ### calling OAR
    @staticmethod
    def subprocess_run(*command):
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output, unused_err = process.communicate()
        retcode = process.poll()
        return retcode, output

    def oarsub(self, *args):
        command = '{0} process {1}'.format(self.config.executable, ' '.join(args))
        ret, output = self.subprocess_run('oarsub', '-l {0}'.format(self.config.oarsub_options), command)
        if ret == 0:
            lines = output.split('\n')
            for line in lines:
                if line.startswith('OAR_JOB_ID='):
                    void, jobid = line.split('=')
                    util.status('{0}: Launched job {1}'.format(time.ctime(), jobid))
                    return jobid.strip()
        return False

    def oarstat(self, jobid):
        # % oarstat -s -j 5651374
        # 5651374: Running
        # % oarstat -s -j 5651374
        # 5651374: Finishing
        ret, output = self.subprocess_run('oarstat', '-s', '-j', str(jobid))
        if ret == 0:
            for n in output.split('\n'):
                if n.startswith(str(jobid)):
                    job, status = n.split(':')
            return status.strip()
        else:
            return 'Unknown'

    def oarwait(self, jobs, remaining=0):
        linelen = 0
        if len(jobs) > remaining:
            util.status('{0}: getting status of {1} jobs...'.format(time.ctime(), len(jobs)))
        else:
            return

        delay = util.loop_delayer(30)
        while len(jobs) > remaining:
            next(delay)
            i = 0
            R = 0
            W = 0
            U = 0

            while i < len(jobs):
                state = self.oarstat(jobs[i])
                if state == 'Running':
                    R += 1
                elif state in ('Waiting', 'toLaunch', 'Launching'):
                    W += 1
                elif state == 'Unknown':
                    U += 1
                else:  # assume state == 'Finishing' or 'Terminated' but don't wait on something unknown
                    del jobs[i]
                    i -= 1  # otherwise it skips a job
                i += 1
            util.status('{0}: {1} jobs to go. {2} waiting, {3} running, {4} unknown.'.format(time.ctime(), len(jobs), W, R, U))
        util.statuseol()

    def oar_cleanup(self, jobs):
        # cleanup:
        for f in itertools.chain(self.configfiles, self.intermediates):
            try:
                os.remove(f)
            except Exception as e:
                print("unable to remove {0}: {1}".format(f, e))

        errorfn = []

        for jobid in jobs:
            errorfilename = 'OAR.{0}.stderr'.format(jobid)

            if os.path.exists(errorfilename):
                with open(errorfilename, 'r') as fp:
                    errormsg = fp.read()
                if len(errormsg) > 0:
                    errorfn.append(errorfilename)
                    print('Critical error: OAR Job {0} failed with the following error: \n{1}'.format(jobid, errormsg))

        if len(errorfn) > 0:
            print('Warning! {0} job(s) failed. See above for the details or the error log files: {1}'.format(len(errorfn), ', '.join(errorfn)))


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

import argparse
import cv2
import os
import sys

from detectron.datasets.json_dataset import JsonDataset
from detectron.utils.io import load_object
import detectron.utils.vis as vis_utils

# OpenCL may be enabled by default in OpenCV3; disable it because it's not
# thread safe and causes unwanted GPU memory allocations.
cv2.ocl.setUseOpenCL(False)

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--dataset',
        dest='dataset',
        help='dataset',
        default='coco_2014_minival',
        type=str
    )
    parser.add_argument(
        '--detections',
        dest='detections',
        help='detections pkl file',
        default='',
        type=str
    )
    parser.add_argument(
        '--thresh',
        dest='thresh',
        help='detection prob threshold',
        default=0.9,
        type=float
    )
    parser.add_argument(
        '--output-dir',
        dest='output_dir',
        help='output directory',
        default='./tmp/vis-output',
        type=str
    )
    parser.add_argument(
        '--first',
        dest='first',
        help='only visualize the first k images',
        default=0,
        type=int
    )
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    args = parser.parse_args()
    return args

def vis(dataset, detections_pkl, thresh, output_dir, limit=0):
    ds = JsonDataset(dataset)
    roidb = ds.get_roidb()

    dets = load_object(detections_pkl)

    assert all(k in dets for k in ['all_boxes', 'all_segms', 'all_keyps']), \
        'Expected detections pkl file in the format used by test_engine.py'

    all_boxes = dets['all_boxes']
    all_segms = dets['all_segms']
    all_keyps = dets['all_keyps']

    def id_or_index(ix, val):
        if len(val) == 0:
            return val
        else:
            return val[ix]

    for ix, entry in enumerate(roidb):
        if limit > 0 and ix >= limit:
            break
        if ix % 10 == 0:
            print('{:d}/{:d}'.format(ix + 1, len(roidb)))

        im = cv2.imread(entry['image'])
        im_name = os.path.splitext(os.path.basename(entry['image']))[0]

        cls_boxes_i = [
            id_or_index(ix, cls_k_boxes) for cls_k_boxes in all_boxes
        ]
        cls_segms_i = [
            id_or_index(ix, cls_k_segms) for cls_k_segms in all_segms
        ]
        cls_keyps_i = [
            id_or_index(ix, cls_k_keyps) for cls_k_keyps in all_keyps
        ]

        vis_utils.vis_one_image(
            im[:, :, ::-1],
            '{:d}_{:s}'.format(ix, im_name),
            os.path.join(output_dir, 'vis'),
            cls_boxes_i,
            segms=cls_segms_i,
            keypoints=cls_keyps_i,
            thresh=thresh,
            box_alpha=0.8,
            dataset=ds,
            show_class=True
        )

if __name__ == '__main__':
    opts = parse_args()
    vis(
        opts.dataset,
        opts.detections,
        opts.thresh,
        opts.output_dir,
        limit=opts.first
    )


import os
import json
import subprocess
import io

def make_request(method: str, params: object):
    obj = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
    string = json.dumps(obj)
    return f"Content-Length: {len(string)}\r\nContent-Type: application/vscode-jsonrpc; charset=utf8\r\n\r\n{string}"

def symbol_request(uri: str):
    return make_request(
        "textDocument/documentSymbol",
        {"textDocument": {"uri": uri}},
    )

def initialize_request(root_path: str):
    return make_request("initialize", {"rootPath": root_path})

def submit_request(formatted_req: str):
    """
    Submit the formatted request to fortls. Returns an array of responses, since formatted_req may contain multiple requests.
    """
    command = [
        "fortls",
        "--incl_suffixes",
        ".f90",
        "--enable_code_actions",
        "--incremental_sync",
    ]
    process = subprocess.Popen(
        command,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    stdout, stderr = process.communicate(input=formatted_req)

    if process.returncode == 0:
        buf = io.StringIO(stdout)
        n = len(buf.getvalue())

        arr = []

        while buf.tell() < n:
            content_length = int(buf.readline().split(":")[1])
            _content_type = buf.readline()
            _empty_line = buf.readline()
            j = buf.read(content_length)
            obj = json.loads(j)
            arr.append(obj)

        return arr
    else:
        raise ChildProcessError(stderr)

def get_document_symbols(root_path: str, uri: str):
    initialize = initialize_request(root_path)
    symbols = symbol_request(uri)
    req = initialize + symbols

    res = submit_request(req)
    symbol_response = res[1]

    return symbol_response['result']

if __name__ == "__main__":
    symbols = get_document_symbols(
        root_path="/Users/anthony/Documents/climate_code_conversion/dependency_graphs/source",
        uri="/Users/anthony/Documents/climate_code_conversion/dependency_graphs/source/client.f90",
    )

    import pprint

    pprint.pprint(symbols)


import pytest

from yargy.morph import (
    Grams,
    Form,

    CachedMorphAnalyzer
)

@pytest.fixture(scope='module')
def morph():
    return CachedMorphAnalyzer()

def test_morph(morph):
    forms = morph('сирота')
    assert forms == [
        Form('сирота', Grams({'ms-f', 'NOUN', 'anim', 'nomn', 'sing'}))
    ]

    grams = forms[0].grams
    assert grams.gender.bi
    assert grams.number.single
    assert not grams.case.fixed

    values = morph.normalized('стали')
    assert values == {'сталь', 'стать'}

def test_inflect(morph):
    forms = morph('Александру')
    form = forms[0]

    assert 'Name' in form.grams
    assert form.inflect() == 'александр'
    assert form.inflect({'nomn', 'plur'}) == 'александры'

def test_check_gram(morph):
    with pytest.raises(ValueError):
        morph.check_gram('verb')


# ------------------------------------------------------------------------------
# PBRT exporter - python  plugin for Maya by Vladimir (Volodymyr) Kazantsev
# 2013
#
# Based on a Python LuxMaya translation by Doug Hammond, in turn base on translation of the c++ luxmaya exporter,
# in turn based on original maya-pbrt c++ exporter by Mark Colbert
#
# This file is licensed under the GPL (the original exporter uses that license)
# http://www.gnu.org/licenses/gpl-3.0.txt
#
# $Id$
#
# ------------------------------------------------------------------------------
#
# lights export module
#
# ------------------------------------------------------------------------------

import math
from maya import OpenMaya

# uncomment for interactive development
# import ExportModule
# reload(ExportModule)
from ExportModule import ExportModule

class Light(ExportModule):
    """
    Light ExportModule base class. This is primarily a factory for returning
    various light types.
    """

    @staticmethod
    def LightFactory( fileHandle, dagPath ):
        """
        Detect the given light type and return a corresponding light object
        """

        nodeType = dagPath.node().apiType()

        if nodeType == OpenMaya.MFn.kSpotLight:
            #this is a spotlight
            return SpotLight( fileHandle, dagPath )
        elif nodeType == OpenMaya.MFn.kPointLight:
            #this is a pointlight
            return PointLight( fileHandle, dagPath )
        elif nodeType == OpenMaya.MFn.kDirectionalLight:
            #this is a pointlight
            return DistantLight( fileHandle, dagPath )

        else:
            OpenMaya.MGlobal.displayWarning("Light type %i not supported" % nodeType)
            return False

    @staticmethod
    def defaultLighting():
        return """
        AttributeBegin
        CoordSysTransform "camera"
        LightSource "distant" "color I" [ .7 .7 .5 ] "point from" [0 0 0] "point to" [1.0 -1.0 1.0]
        LightSource "distant" "color I" [ .2 .2 .35 ] "point from" [0 0 0] "point to" [0.0 0.0 1.0]
        AttributeEnd

        """

    def getOutput(self):
        """
        Nothing to do here, child classes output light type specific syntax.
        """
        pass

class DistantLight(Light):
    """
    Distant light type.
    """

    light = OpenMaya.MFnDirectionalLight()

    def __init__( self, fileHandle, dagPath ):
        """
        Constructor. Sets up this object's data.
        """

        self.fileHandle = fileHandle
        self.dagPath = dagPath
        self.light = OpenMaya.MFnDirectionalLight( dagPath )

    def getOutput(self):
        """
        Return LightSource "distant" from the given directional node.
        """

        color = self.light.color()
        intensity = self.light.intensity()

        colorR = self.rgcAndClamp( color.r * intensity )
        colorG = self.rgcAndClamp( color.g * intensity )
        colorB = self.rgcAndClamp( color.b * intensity )

        self.addToOutput( '# Directional Light ' + self.dagPath.fullPathName() )
        self.addToOutput( 'TransformBegin' )
        self.addToOutput( self.translationMatrix( self.dagPath ) )
        self.addToOutput( '\tLightSource "distant"' )
        self.addToOutput( '\t\t"color L" [%f %f %f]' % (colorR, colorG, colorB) )
        self.addToOutput( '\t\t"point from" [0 0 0]')
        self.addToOutput( '\t\t"point to" [0 0 -1]' )
        self.addToOutput( 'TransformEnd' )
        self.addToOutput( '' )

        self.fileHandle.flush()

class SpotLight(Light):
    """
    Spot light type.
    """

    light = OpenMaya.MFnSpotLight()

    def __init__( self, fileHandle, dagPath ):
        """
        Constructor. Sets up this object's data.
        """

        self.fileHandle = fileHandle
        self.dagPath = dagPath
        self.light = OpenMaya.MFnSpotLight( dagPath )

    def getOutput(self):
        """
        Return PBRT LightSource "spot" from the given spotlight node.
        """

        color = self.light.color()
        intensity = self.light.intensity()

        colorR = self.rgcAndClamp( color.r * intensity )
        colorG = self.rgcAndClamp( color.g * intensity )
        colorB = self.rgcAndClamp( color.b * intensity )

        self.addToOutput( '# Spot Light ' + self.dagPath.fullPathName() )
        self.addToOutput( 'TransformBegin' )
        self.addToOutput( self.translationMatrix( self.dagPath ) )
        self.addToOutput( '\tLightSource "spot"' )
        self.addToOutput( '\t\t"color I" [%f %f %f]' % (colorR, colorG, colorB) )
        self.addToOutput( '\t\t"point from" [0 0 0]')
        self.addToOutput( '\t\t"point to" [0 0 -1]' )
        self.addToOutput( '\t\t"float coneangle" [%f]' % ( self.light.coneAngle()*180/math.pi ) )
        self.addToOutput( '\t\t"float conedeltaangle" [%f]' % ( self.light.dropOff()*180/math.pi ) )
        self.addToOutput( 'TransformEnd' )
        self.addToOutput( '' )

        self.fileHandle.flush()

class PointLight(Light):
    """
    Point light type.
    """

    light = OpenMaya.MFnPointLight()

    def __init__( self, fileHandle, dagPath ):
        """
        Constructor. Sets up this object's data.
        """

        self.fileHandle = fileHandle
        self.dagPath = dagPath
        self.light = OpenMaya.MFnPointLight( dagPath )

    def getOutput(self):
        """
        Export Maya Point Light as pbrt's LightSource "point"
        """

        color = self.light.color()
        intensity = self.light.intensity()

        colorR = self.rgcAndClamp( color.r * intensity )
        colorG = self.rgcAndClamp( color.g * intensity )
        colorB = self.rgcAndClamp( color.b * intensity )

        self.addToOutput( '# Point Light ' + self.dagPath.fullPathName() )
        self.addToOutput( 'TransformBegin' )
        self.addToOutput( self.translationMatrix( self.dagPath ) )
        self.addToOutput( '\tLightSource "point"' )
        self.addToOutput( '\t\t"color I" [%f %f %f]' % (colorR, colorG, colorB) )
        self.addToOutput( 'TransformEnd' )
        self.addToOutput( '' )


import glob
import os
import shutil
from RunKit.sh_tools import sh_call
from RunKit.envToJson import get_cmsenv

def mk_genTuple(input, output, source, config, cmssw, verbose):
  if verbose > 0:
    print(f'Processing "{input}" into {output}')
  input_files = glob.glob(input)
  input_str = ','.join([ 'file:' + f for f in input_files])
  if len(input_str) == 0:
    raise RuntimeError("mk_genTuple: input not found.")
  out_dir, _ = os.path.split(output)
  os.makedirs(out_dir, exist_ok=True)
  if os.path.exists(output):
    os.remove(output)
  out_tmp = output + '.tmp.root'
  env = get_cmsenv(cmssw)
  sh_call(['cmsRun', config, f'inputFiles={input_str}', f'output={out_tmp}', f'source={source}'],
          env=env, verbose=verbose)
  shutil.move(out_tmp, output)
  print(f'GenTuple "{output}" successfully created.')

if __name__ == "__main__":
  import argparse
  parser = argparse.ArgumentParser(description='Create l1Tuples adding generator level info.')
  parser.add_argument('--input', required=True, type=str, help="input file or glob pattern")
  parser.add_argument('--output', required=True, type=str, help="output file")
  parser.add_argument('--source', required=False, type=str, default="genParticles",
                      help="collection of generated partilces. E.g. genParticles, prunedGenParticles etc.")
  parser.add_argument('--config', required=False, type=str,
                      default=os.path.join(os.environ['ANALYSIS_PATH'], 'MCProd', 'gentuple_cff.py'),
                      help="python file with CMSSW configuration")
  parser.add_argument('--cmssw', required=False, type=str, default=os.environ['DEFAULT_CMSSW_HOME'],
                      help="path to the CMSSW installation")
  parser.add_argument('--verbose', required=False, type=int, default=1, help="verbosity level")
  args = parser.parse_args()

  mk_genTuple(args.input, args.output, args.source, args.config, args.cmssw, args.verbose)


# 留言分析程式
# 学习点：
#       open file, 清单筛选

# 清单读取建立
data = []
file_name = 'reviews.txt'
with open(file_name,'r') as f: #开启档案，读取模式
    for line in f:
        data.append(line)
print('档案读取完毕，一共有',len(data),'笔留言')
# 清单筛选
new = []
for d in data:
    if len(d) < 100:
        new.append(d)
print('一共有',len(new),'笔留言长度小于100')
good = []
# for d in data:
#     if 'good' in d:
#         good.append(d)
good = [d for d in data if 'good' in d] #快写法 list comprehension
# output = [(number-1) for number in reference if number %2 == 0]
#              运算          变数        清单          筛选条件
bad = ['bad' in d for d in data]

print('一共有',len(good),'笔留言中提到good')


import requests
import re
from parsel import Selector

#//*[@id="js-w-content"]/div[3]/div[1]/div[1]/div[1]/div/div/pre

def getMusicLyr(music_name, music_singer):
    nw_music_name = re.sub('\s', '-', music_name)
    nw_music_singer = re.sub('\s', '-', music_singer)
    b_link = 'https://www.cifraclub.com.br/' + nw_music_singer + '/' + nw_music_name + '/'
    print(b_link)
    link = requests.get(b_link)
    sel = Selector(link.text)
    text_parts = sel.xpath('//*[@id="js-w-content"]/div[3]/div[1]/div[1]/div[1]/div/div/pre').extract()
    song = ' '.join(text_parts)

    song = re.sub('<b>', '', song)
    song = re.sub('</b>', '', song)

    if song == "": return "NSF" #No song was found
    else:
        return(music_name + " - " + music_singer + "\n" + "\n" + song + "\n" + "\n" + "Para tocar no Cifra club : " + b_link)


import cv2 as cv
import numpy as np
import time
from cv2 import COLOR_BGR2HSV
from itertools import product
import cvzone

# confidence and nms threshold to be applied on model
Conf_threshold = 0.4
NMS_threshold = 0.4

# colours to be used for labelling classes
COLORS = [(0, 255, 0), (0, 0, 255), (255, 0, 0),
          (255, 255, 0), (255, 0, 255), (0, 255, 255)]

#HSV colour ranges of maximum/minimum speed limit signs
lower_red = np.array([160,160,20])
upper_red = np.array([179,255,255])
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])

#png instructions to be overlaid
Min20Message = cv.imread("min20.png", cv.IMREAD_UNCHANGED)
Min20Message = cv.resize(Min20Message, (0,0), None, 0.3,0.3)
Min30Message = cv.imread("min30.png", cv.IMREAD_UNCHANGED)
Min30Message = cv.resize(Min30Message, (0,0), None, 0.3,0.3)
Min40Message = cv.imread("min40.png", cv.IMREAD_UNCHANGED)
Min40Message = cv.resize(Min40Message, (0,0), None, 0.3,0.3)
Min50Message = cv.imread("min50.png", cv.IMREAD_UNCHANGED)
Min50Message = cv.resize(Min50Message, (0,0), None, 0.3,0.3)
Min60Message = cv.imread("min60.png", cv.IMREAD_UNCHANGED)
Min60Message = cv.resize(Min60Message, (0,0), None, 0.3,0.3)
Min70Message = cv.imread("min70.png", cv.IMREAD_UNCHANGED)
Min70Message = cv.resize(Min70Message, (0,0), None, 0.3,0.3)
Min80Message = cv.imread("min80.png", cv.IMREAD_UNCHANGED)
Min80Message = cv.resize(Min80Message, (0,0), None, 0.3,0.3)
MinSpeedHUD = cv.imread("MinSpeedHUD.png", cv.IMREAD_UNCHANGED)
MinSpeedHUD = cv.resize(MinSpeedHUD, (0,0), None, 0.3,0.3)
Max20Message = cv.imread("max20.png", cv.IMREAD_UNCHANGED)
Max20Message = cv.resize(Max20Message, (0,0), None, 0.3,0.3)
Max30Message = cv.imread("max30.png", cv.IMREAD_UNCHANGED)
Max30Message = cv.resize(Max30Message, (0,0), None, 0.3,0.3)
Max40Message = cv.imread("max40.png", cv.IMREAD_UNCHANGED)
Max40Message = cv.resize(Max40Message, (0,0), None, 0.3,0.3)
Max50Message = cv.imread("max50.png", cv.IMREAD_UNCHANGED)
Max50Message = cv.resize(Max50Message, (0,0), None, 0.3,0.3)
Max60Message = cv.imread("max60.png", cv.IMREAD_UNCHANGED)
Max60Message = cv.resize(Max60Message, (0,0), None, 0.3,0.3)
Max70Message = cv.imread("max70.png", cv.IMREAD_UNCHANGED)
Max70Message = cv.resize(Max70Message, (0,0), None, 0.3,0.3)
Max80Message = cv.imread("max80.png", cv.IMREAD_UNCHANGED)
Max80Message = cv.resize(Max80Message, (0,0), None, 0.3,0.3)
MaxSpeedHUD = cv.imread("MaxSpeedHUD.png", cv.IMREAD_UNCHANGED)
MaxSpeedHUD = cv.resize(MaxSpeedHUD, (0,0), None, 0.3,0.3)

#model configuration
width = 416
height = 416
weights = "backup/yolov4-tiny-obj_final.weights"
cfg = "cfg/yolov4-tiny-obj.cfg"
class_name = []
with open('data/obj.names', 'r') as f:
    class_name = [cname.strip() for cname in f.readlines()]
print(class_name)
net = cv.dnn.readNet(weights, cfg)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA_FP16)

#set input parameters of model
model = cv.dnn_DetectionModel(net)
model.setInputParams(size=(416, 416), scale=1/255, swapRB=True)
HSVImage = []

# define speed sign labels
speedsigns = ['speed sign 20mph', 'speed sign 30mph', 'speed sign 40mph',
             'speed sign 50mph', 'speed sign 60mph', 'speed sign 70mph', 'speed sign 80mph']

#read in image
cap = cv.imread('data/ss12.jpg')

#overlay canvas for HUD
cap = cv.resize(cap,(1080,720),fx=0,fy=0, interpolation = cv.INTER_CUBIC)
cap = cvzone.overlayPNG(cap, MinSpeedHUD, [0,667])
cap = cvzone.overlayPNG(cap, MaxSpeedHUD, [792,667])
classes, scores, boxes = model.detect(cap, Conf_threshold, NMS_threshold)
for (classid, score, box) in zip(classes, scores, boxes):
    color = COLORS[int(classid) % len(COLORS)]
    label = "%s : %.2f" % (class_name[classid[0]], score)

#look for speed signs specifically with a confidence higher than 60%
    if class_name[classid[0]] in speedsigns and score > 0.6:
       # box_width = box[2]
        #box_height = box[3]
        #print(box_width)
        #print(box_height)
        box[2] = box[0] + box[2]
        box[3] = box[1] + box[3]

        # get rectangle surrounding speed sign and transform to 416x416 window
        srcSpeedSign  = np.float32([[box[0],box[1]],[box[2],box[1]],[box[0],box[3]],[box[2],box[3]]])
        dstSpeedSign = np.float32([[0,0], [width,0], [0,height], [width,height]])
        SpeedSignPT = cv.getPerspectiveTransform(srcSpeedSign, dstSpeedSign)
        warpedSpeedSign = cv.warpPerspective(cap, SpeedSignPT, (width,height))
        #count number of coloured pixels
        kernel = np.ones((5, 5), "uint8")
        #Max Speed (Red)
        HSVImage = cv.cvtColor(warpedSpeedSign, cv.COLOR_BGR2HSV)
        MaxSpeedMask = cv.inRange(HSVImage, lower_red, upper_red)
        MaxSpeedMask = cv.dilate(MaxSpeedMask, kernel)
        RedPixels = cv.countNonZero(MaxSpeedMask)
        print("Red Pixels = ", RedPixels)
        #Min Speed (Blue)
        MinSpeedMask = cv.inRange(HSVImage, lower_blue ,upper_blue)
        MinSpeedMask = cv.dilate(MinSpeedMask, kernel)
        BluePixels = cv.countNonZero(MinSpeedMask)
        print("Blue Pixels = ", BluePixels)

        # adjust labels based on state of label
        if class_name[classid[0]] == "speed sign 20mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max20Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min20Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 30mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max30Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min30Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 40mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max40Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min40Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 50mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max50Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min50Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 60mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max60Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min60Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 70mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max70Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min70Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        elif class_name[classid[0]] == "speed sign 80mph":
            if RedPixels > BluePixels:
                SpeedSignState = "(Max Speed)"
                MaxSpeedHUD = Max80Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
            else:
                SpeedSignState = "(Min Speed)"
                MinSpeedHUD = Min80Message
                cv.rectangle(cap, (box[0],box[1]), (box[2],box[3]), color, 1)
                cv.putText(cap, class_name[classid[0]] + ' ' + SpeedSignState + ' %.2f' % score, (box[0], box[1]-10),
                    cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
        cap = cvzone.overlayPNG(cap, MinSpeedHUD, [0,667])
        cap = cvzone.overlayPNG(cap, MaxSpeedHUD, [792,667])
cv.imshow("Test", cap)
cv.imshow("warpedTransform", warpedSpeedSign)
cv.waitKey(0)


import json
import requests
from bs4 import BeautifulSoup

URL_mubawab = "https://www.mubawab.ma/fr/a/7215667/appartement-%C3%A0-hay-chrifa-bd-al-qods"
URL_avito = "https://www.avito.ma/fr/la_ville_haute/appartements/Appartement_en_Vente_%C3%A0_K%C3%A9nitra_46812563.htm"
URL_sarouty = "https://www.sarouty.ma/louer/villa-a-louer-marrakech-agdal-819702.html"

def del_from_list(A,element):
    new_a = []
    for el in A :
        if element in el:
            continue
        new_a.append(el)
    return new_a

def mubawab(URL,output_name = 'mubawab.json'):
    output_scraped = {}
    page = requests.get(URL)

    soup = BeautifulSoup(page.content, "html.parser")

    # Request
    mydivs = soup.find_all("span", {"class": "tagProp"})
    names = ['Surface','N° de Pièces','N° de Chambres','N° de Salles de bain','Etat','Ancieneté','Etage']
    price_div = soup.find("h3", {"class": "orangeTit"})
    search_title = soup.find("h1", {"class": "searchTitle"})
    Characteristics = soup.find("div", {"class": "catNav"})
    li = Characteristics.text.strip().split('\n')
    # Output
    output_scraped['Titre'] = search_title.text.strip()
    output_scraped['Prix'] = price_div.text.strip()
    new_li = del_from_list(li,'\t\t')

    for el,name in zip(new_li,names):
        output_scraped[name] = el

    return output_scraped

def avito(URL):

    # charac_list = []
    names_char = ['Surface','N° de Chambres','N° de Salles de bain']
    names_char_2  = ['Type','Secteur','Salons','Étage']
    output_scraped = {}
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, "html.parser")

    # Requêtes
    mydivs = soup.find("div", {"class": "sc-1g3sn3w-7 eDUytn"})
    charac = soup.find("div", {"class": "sc-6p5md9-0 jTtIhf"})
    charac_2 = soup.find("ol", {"class": "qmn92k-2 kekZFG"})
    price_div = soup.find("p", {"class": "sc-1x0vz2r-0 eXteHH"})
    search_title = soup.find("h1", {"class": "sc-1x0vz2r-0 EJoJb"})

    # Output
    output_scraped['Titre'] = search_title.text.strip()
    output_scraped['Prix'] = price_div.text.strip()
    for el,name in zip(charac_2,names_char_2):
        output_scraped[name] = el.text.strip().split(name)[1]
    for el,name in zip(charac,names_char):
        output_scraped[name] = el.text.strip()

    return output_scraped #, charac_list

def sarouty(URL):

    output_scraped = {}
    names = ['Prix','Type','Référence','N° de Pièces','N° de Salles de bain','Mobiliers','Surface']
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, "html.parser")

    # Requêtes
    charac = soup.find_all("div", {"class": "facts__content"})
    search_title = soup.find("h1", {"class": "property-header__title--detail"})

    # Output
    output_scraped['Titre'] = search_title.text.strip()

    for el,name in zip(charac,names):
        output_scraped[name] = el.text.strip()

    return output_scraped

# print(avito(URL_avito))
# print(sarouty(URL_sarouty))


from google.appengine.ext import ndb

import logging
import datetime
import myutils

class WaterStore(ndb.Model):
    timesDrank = ndb.IntegerProperty()

def registerWaterCommands(commander):
    commander.register_command('/waterupd', update_water)
    commander.register_command(commander.KEY_OTHER, handle_water)

def getWaterKey(uid, date):
    date_got = datetime.datetime.fromtimestamp(int(date)).strftime("%Y-%m-%d")
    key = '{}:water:{}'.format(uid, date_got)
    return key

def getWaterAmount(key):
    return ndb.Key('WaterStore', key).get()

def handle_water(request_body, text):
    waters = text.count(u'\U0001f376')
    if waters == 0:
        return ''

    uid = myutils.extract_user_id(request_body)
    date = myutils.extract_date(request_body)

    key = getWaterKey(uid, date)
    queryWater = getWaterAmount(key)

    logging.info('key: ' + key)
    logging.info('queryWater.timesDrank: ' + str(queryWater))
    logging.info('Waters to add: {}'.format(waters))

    amount_drank = waters
    if queryWater is not None:
        amount_drank += queryWater.timesDrank

    waterDrank = WaterStore(key=ndb.Key('WaterStore', key),timesDrank=amount_drank)
    waterDrank.put()

    return 'Water drank today: {} cups'.format(amount_drank)

def update_water(request_body, params):
    logging.info('Inside update_water...')
    if len(params) != 1 or not myutils.is_number(params[0]):
        logging.info('Params: {}'.format(params))
        return 'Didn\'t fully understand. Should be like: /waterupd 2'

    name = myutils.extract_user_first_name(request_body)
    uid = myutils.extract_user_id(request_body)
    date = myutils.extract_date(request_body)
    amount = int(params[0])

    key = getWaterKey(uid, date)
    waterDrank = WaterStore(key=ndb.Key('WaterStore', key),timesDrank=amount)
    waterDrank.put()

    return '{}, the water amount was updated.'.format(name)