import math
from typing import Optional, Tuple, Union

import torch
import torch.nn.functional as F
from torch import Tensor
from torch_sparse import SparseTensor

from torch.nn import Sequential, ReLU, BatchNorm1d

from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn.dense.linear import Linear
from torch_geometric.typing import Adj, OptTensor, PairTensor
from torch_geometric.utils import softmax

class LinearBlock(torch.nn.Module):
    def __init__(self, in_node_num, out_node_num, activation=True):
        super().__init__()

        self.activation = activation

        self.linear = Linear(in_node_num, out_node_num, weight_initializer='kaiming_uniform')
        self.bn = BatchNorm1d(out_node_num)
        if self.activation:
            self.a = ReLU()

    def forward(self, x):

        x = self.linear(x)
        x = self.bn(x)
        if self.activation:
            x = self.a(x)

        return x

class UNet(torch.nn.Module):
    def __init__(self, in_node_num, out_node_num, latent_node_num):
        super().__init__()

        # self.encoder_layer1 = LinearBlock(in_node_num, int(latent_node_num//4))
        # self.encoder_layer2 = LinearBlock(int(latent_node_num//4), int(latent_node_num//2))
        # self.encoder_layer3 = LinearBlock(int(latent_node_num//2), latent_node_num)

        # self.decoder1_layer3 = LinearBlock(latent_node_num, int(in_node_num//2))
        # self.decoder1_layer2 = LinearBlock(int(in_node_num//2)+int(latent_node_num//2), int(in_node_num//4))
        # self.decoder1_layer1 = LinearBlock(int(in_node_num//4)+int(latent_node_num//4), in_node_num, activation=False)

        # self.decoder2_layer3 = LinearBlock(latent_node_num, int(out_node_num//2))
        # self.decoder2_layer2 = LinearBlock(int(out_node_num//2)+int(latent_node_num//2), int(out_node_num//4))
        # self.decoder2_layer1 = LinearBlock(int(out_node_num//4)+int(latent_node_num//4), out_node_num, activation=False)

        self.encoder_layer1 = LinearBlock(in_node_num, int(latent_node_num//2))
        self.encoder_layer2 = LinearBlock(int(latent_node_num//2), latent_node_num)

        self.decoder1_layer2 = LinearBlock(latent_node_num, int(in_node_num//2))
        self.decoder1_layer1 = LinearBlock(int(in_node_num//2)+int(latent_node_num//2), in_node_num, activation=False)

        self.decoder2_layer2 = LinearBlock(latent_node_num, int(out_node_num//2))
        self.decoder2_layer1 = LinearBlock(int(out_node_num//2)+int(latent_node_num//2), out_node_num, activation=False)

    def forward(self, x):

        # x1_e = self.encoder_layer1(x)
        # x2_e = self.encoder_layer2(x1_e)
        # x3_e = self.encoder_layer3(x2_e)

        # x3_d1 = self.decoder1_layer3(x3_e)
        # x3_d1 = torch.cat([x3_d1,x2_e], dim=-1)
        # x2_d1 = self.decoder1_layer2(x3_d1)
        # x2_d1 = torch.cat([x2_d1,x1_e], dim=-1)
        # x1_d1 = self.decoder1_layer1(x2_d1)

        # x3_d2 = self.decoder2_layer3(x3_e)
        # x3_d2 = torch.cat([x3_d2,x2_e], dim=-1)
        # x2_d2 = self.decoder2_layer2(x3_d2)
        # x2_d2 = torch.cat([x2_d2,x1_e], dim=-1)
        # x1_d2 = self.decoder2_layer1(x2_d2)

        x1_e = self.encoder_layer1(x)
        x2_e = self.encoder_layer2(x1_e)

        x2_d1 = self.decoder1_layer2(x2_e)
        x2_d1 = torch.cat([x2_d1,x1_e], dim=-1)
        x1_d1 = self.decoder1_layer1(x2_d1)

        x2_d2 = self.decoder2_layer2(x2_e)
        x2_d2 = torch.cat([x2_d2,x1_e], dim=-1)
        x1_d2 = self.decoder2_layer1(x2_d2)

        return x1_d1, x1_d2

class MyTransformerConv(MessagePassing):

    _alpha: OptTensor

    def __init__(
        self,
        in_channels: Union[int, Tuple[int, int]],
        out_channels: int,
        key_query_len: int = None,
        beta: bool = False,
        dropout: float = 0.,
        bias: bool = True,
        root_weight: bool = True,
        **kwargs,
    ):
        kwargs.setdefault('aggr', 'add')
        super(MyTransformerConv, self).__init__(node_dim=0, **kwargs)

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.beta = beta and root_weight
        self.root_weight = root_weight
        self.dropout = dropout
        self._alpha = None

        if isinstance(key_query_len, int) & key_query_len > 0:
            self.key_query_len = key_query_len
        else:
            self.key_query_len = out_channels

        self.unet = UNet(in_node_num=2*in_channels, out_node_num=out_channels, latent_node_num=self.key_query_len)
        self.query_bn = BatchNorm1d(2*in_channels)

        # self.unet = UNet(in_node_num=in_channels, out_node_num=out_channels, latent_node_num=self.key_query_len)
        # self.query_bn = BatchNorm1d(in_channels)

        # self.key = Linear(2*in_channels, self.key_query_len, weight_initializer='kaiming_uniform')
        # self.query = Linear(2*in_channels, self.key_query_len, weight_initializer='kaiming_uniform')
        # self.value = Linear(2*in_channels, out_channels, weight_initializer='kaiming_uniform')

        # self.encoder_key = Sequential(LinearBlock(2*in_channels, int(self.key_query_len//2)),
        #                               LinearBlock(int(self.key_query_len//2), self.key_query_len),)
        # self.decoder_query = Sequential(LinearBlock(self.key_query_len, int(self.key_query_len//2)),
        #                                 LinearBlock(int(self.key_query_len//2), 2*in_channels, activation=False),)
        # self.decoder_value = Sequential(LinearBlock(self.key_query_len, int(self.key_query_len//2)),
        #                                 LinearBlock(int(self.key_query_len//2), out_channels, activation=False))

        self.lin_skip = Linear(in_channels, out_channels, bias=bias)
        if self.beta:
            self.lin_beta = Linear(3 * out_channels, 1, bias=False)
        else:
            self.lin_beta = self.register_parameter('lin_beta', None)

    def forward(self, x: Union[Tensor, PairTensor], edge_index: Adj,
                return_attention_weights=None):

        if isinstance(x, Tensor):
            x: PairTensor = (x, x)

        # propagate_type: (query: Tensor, key:Tensor, value: Tensor, edge_attr: OptTensor) # noqa
        out = self.propagate(edge_index=edge_index, x=x, size=None)

        alpha = self._alpha
        # self._alpha = None

        if self.root_weight:
            x_r = self.lin_skip(x[1])
            if self.lin_beta is not None:
                beta = self.lin_beta(torch.cat([out, x_r, out - x_r], dim=-1))
                beta = beta.sigmoid()
                out = beta * x_r + (1 - beta) * out
            else:
                out += x_r

        if isinstance(return_attention_weights, bool):
            assert alpha is not None
            if isinstance(edge_index, Tensor):
                return out, (edge_index, alpha)
            elif isinstance(edge_index, SparseTensor):
                return out, edge_index.set_value(alpha, layout='coo')
        else:
            return out

    def message(self, x_i: Tensor, x_j: Tensor, index: Tensor, ptr: OptTensor,
                size_i: Optional[int]):

        x = torch.cat([x_i, x_j - x_i], dim=-1)
        query = self.query_bn(x)
        key, value = self.unet(x)

        # query = self.query_bn(x_i)
        # key, value = self.unet(x_j)

        # query = self.query(x)
        # key = self.key(x)
        # value = self.value(x)

        # latent_z = self.key(x)
        # key = self.decoder_query(latent_z)
        # value = self.decoder_value(latent_z)

        alpha = (query * key).sum(dim=-1) / math.sqrt(self.key_query_len)
        self._alpha_logits = alpha.clone()
        alpha = softmax(alpha, index, ptr, size_i)
        self._alpha = alpha
        alpha = F.dropout(alpha, p=self.dropout, training=self.training)

        out = value

        out *= alpha.view(-1, 1)
        return out

    def __repr__(self):
        return (f'{self.__class__.__name__}({self.in_channels}, '
                f'{self.out_channels})')


from KURVS import *
import matplotlib.pyplot as pyplot

def horizontalLinesErrors(data):
    # --- THIS CODE REMOVES THE HORIZONTAL ERRORS FROM THE DATA SET, CAUSED BY INSTRUMENT ERRORS ---
    errorRows = []
    slices = []
    for y in range(data.shape[0]): # this controlls the vertical, y, direction
        row = []
        for x in range(data.shape[1]): # this controlls the horizontal, x, direction
            row.append(data[y,x]) #appends data to rows for std
        if statistics.stdev(row) >= 10**-15: # start of working out if row should be removed
            errorRows.append(y)
    return errorRows

def splitSlices(data, errorRows):
    # --- THIS CODE SPLITS THE DATA INTO ITS DIFFERENT SLICES USING THE ERROR ROWS THAT WE HAVE PREVIOUSLY WORKED OUT ---
    cleanData = []
    slices = []
    for y in range(data.shape[0]): # this manovers through the y index of the tables
        if y not in errorRows:
            cleanData.append(y)  # this makes sure the data is not anomylous
    for row in cleanData: # this begins to create slices
        startOfSlice = cleanData[0]
        if row + 1 not in cleanData:
            endOfSlice = row
            if startOfSlice != endOfSlice:
                slice = data[startOfSlice: endOfSlice+1, :]
                slices.append(slice)
            cleanData =  cleanData[cleanData.index(row)+1 :]
    return slices

def sumEachSlice(slices):
    # --- THIS CODE SUMS EACH INDIVIDUAL SLICE AND RETURNS IT AS A LIST ---
    sliceSums = []
    for wavelengthSlice in slices:
        sliceSums.append(twoDtooneD(wavelengthSlice))
    sliceSums.reverse() # this reverses the list as the data seems to be read from the bottom up
    return sliceSums

def createGaussian(sliceSums):
    # --- THIS CODE CREATES GRAPHS FOR ALL OF THE SLICES AND ALSO WORKS OUT THE REDSHIFT OF EACH SLICE ---
    redshiftList = []
    lambda_peak_values = []
    for array in sliceSums:
        if 3 <= sliceSums.index(array) <= 10: # only takes the middle slices as outer slices are anomylous
            mean = writeToGaussian(array)
            redshiftList.append(findRedShift(mean))
            lambda_peak_values.append(findRedShift(mean)[1])

    return redshiftList, mean, lambda_peak_values

def findDistanceBetweenSlices(slices):
    sliceList = list(range(0, len(slices)))
    mValues = []
    for value in sliceList:
        if 3 <= sliceList.index(value)<= 10: # only takes the middle slices as outer slices are anomylous
            mValues.append(sliceList[value] * 1.72 * 3.086e19)
    return mValues

def rotationCurve(lambda_peak_values, mValues):
    velocityShifts = []
    referenceWavelength = lambda_peak_values[3] # a wavelength to compare the others to
    for lambda_peak_values in lambda_peak_values:
        velocityShift = 3*10**5*(lambda_peak_values/referenceWavelength - 1)
        velocityShifts.append(velocityShift)
    pyplot.scatter(mValues, velocityShifts, color = "b")
    pyplot.xlabel("Distance from centre of galaxy (km)")
    pyplot.ylabel("Velocity Shift (km/s)")
    pyplot.show()
    return velocityShifts

def correctingInclination(velocityShifts):
    velocityDifferenceMax = velocityShifts[-1] - velocityShifts[0]
    inclinationCorrection = 1.30
    V_rot = velocityDifferenceMax * (inclinationCorrection / 2)
    return V_rot

def findMass(V_rot, mValues):
    # THIS PIECE OF CODE FINDS THE MASS OF A GALAXY GIVEN ITS CORRECTED VELOCITY AND THE DISTANCES FROM THE CENTRE OF THE GALAXY
    R = (mValues[-1] - mValues[0])/2
    V_rot = V_rot * 1000
    Mass = (V_rot**2)*R/(6.67e-11)
    return Mass

def findDarkMatterFraction(Mass):
    luminousMass = (0.9*10**10)*(1.989*10**30) # this is the solar mass of the galaxy timesed by the number of kilograms in a solar mass
    darkMatter = Mass - luminousMass
    darkMatterPercentage = darkMatter/Mass * 100
    print(f"The percentage of dark matter in this galaxy is: {darkMatterPercentage} %")
    return darkMatterPercentage

def main():
    data = openData()
    errorRows = horizontalLinesErrors(data)
    slices = splitSlices(data, errorRows)
    sliceSums = sumEachSlice(slices)
    mValues = findDistanceBetweenSlices(slices)
    redshiftList, mean, lambda_peak_values = createGaussian(sliceSums)
    velocityShifts = rotationCurve(lambda_peak_values, mValues)
    V_rot = correctingInclination(velocityShifts)
    Mass = findMass(V_rot, mValues)
    findDarkMatterFraction(Mass)

if __name__ == "__main__":
    print("Running Programn")
    main()


from django.contrib import admin
from .models import *

# Register your models here.
class ProductAdmin(admin.ModelAdmin):
    list_display = ('name', 'category', 'priceh', 'pricef')
    list_filter = ('category',)
    search_fields = ('name', 'category')

class ItemAdmin(admin.ModelAdmin):
    list_display = ('product', 'quantityh', 'quantityf', 'order')
    list_filter = ('order',)

class ItemInline(admin.TabularInline):
    model = Item
    extra = 0

class OrderAdmin(admin.ModelAdmin):
    list_display = ('username', 'phone', 'type', 'amount', 'timestamp')
    list_filter = ('username', 'phone', 'type', 'timestamp')
    search_fields = ('username', 'phone', 'type', 'address', 'amount', 'timestamp')
    inlines = [ItemInline]
    readonly_fields = ('timestamp',)

admin.site.register(Category)
admin.site.register(Product, ProductAdmin)
admin.site.register(Item, ItemAdmin)
admin.site.register(Order, OrderAdmin)


# Bug with PyTorch source code makes torch.tensor as not callable for pylint.
# pylint: disable=not-callable

import pickle
from unittest import TestCase

import torch

class Seq2SeqIntegrationTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.verbose = False
        cls.a_torch_device = torch.device("cuda:0")
        cls.begin_of_sequence_idx = -1  # BOS
        cls.encoder_hidden_size = 1024
        cls.number_of_tags = 9  # tag space of our models
        cls.a_target_vector = torch.tensor([[0, 1, 1, 4, 5, 8], [1, 0, 3, 8, 0, 0]], device=cls.a_torch_device)

    def encoder_input_setUp(self, model_type: str):
        file = open(f"./tests/network/integration/to_predict_{model_type}.p", "rb")
        self.to_predict_tensor = pickle.load(file)
        self.to_predict_tensor = self.to_predict_tensor.to(self.a_torch_device)
        file.close()

        self.a_lengths_tensor = torch.tensor([6, 6], device=self.a_torch_device)
        self.a_batch_size = 2

    def encoder_output_setUp(self):
        self.decoder_input = torch.tensor([[[-1.], [-1.]]], device=self.a_torch_device)
        file = open("./tests/network/integration/decoder_hidden.p", "rb")
        self.decoder_hidden_tensor = pickle.load(file)
        self.decoder_hidden_tensor = (self.decoder_hidden_tensor[0].to(self.a_torch_device),
                                      self.decoder_hidden_tensor[1].to(self.a_torch_device))
        file.close()

    def decoder_input_setUp(self):
        self.max_length = self.a_lengths_tensor[0].item()

    def assert_output_is_valid_dim(self, actual_prediction):
        self.assertEqual(self.max_length + 1, actual_prediction.shape[0])  # + 1 since end-of-sequence (EOS)
        self.assertEqual(self.a_batch_size, actual_prediction.shape[1])
        self.assertEqual(self.number_of_tags, actual_prediction.shape[2])


"""股票实时报价"""

import pandas as pd
from tqdm import tqdm

from ..mongodb import get_db
from ..scripts.trading_calendar import is_trading_day
from ..utils import make_logger
from ..websource.wy import fetch_quote

DB_NAME = 'wy_quotes'
DATE_KEY = 'update'
# DT_FMT = r"%Y/%m/%d H:M:S"
logger = make_logger('实时报价')

def create_index(collection):
    collection.create_index([(DATE_KEY, -1)], name='dt_index')

def _to_timestamp(d):
    dt_keys = [DATE_KEY, 'time']
    for k in dt_keys:
        d[k] = pd.to_datetime(d[k])
    return d

def refresh():
    """刷新实时报价"""
    codes_db = get_db()
    codes = codes_db['股票列表'].find_one()['codes']
    db = get_db(DB_NAME)
    today = pd.Timestamp('today').floor('D')
    name = today.strftime(r"%Y-%m-%d")
    collection = db[name]
    if collection.estimated_document_count() == 0:
        create_index(collection)
    # 后台计划任务控制运行时间点。此处仅仅判断当天是否为交易日
    if not is_trading_day(today):
        logger.warning(f"{today} 非交易日")
        return
    docs = [_to_timestamp(doc) for doc in fetch_quote(codes)]
    docs = filter(lambda d: d[DATE_KEY].floor('D') == today, docs)
    r = collection.insert_many(list(docs))
    logger.info(f'Inserted {len(r.inserted_ids)} rows')

QUOTE_COL_MAPS = {
    '股票代码': 'code',
    '股票简称': 'name',
    '开盘': 'open',
    '前收盘': 'yestclose',
    '现价': 'price',
    '最高': 'high',
    '最低': 'low',
    '成交量': 'volume',
    '成交额': 'turnover',
    '买1量': 'bidvol1',
    '买1价': 'bid1',
    '买2量': 'bidvol2',
    '买3价': 'bid3',
    '买3量': 'bidvol3',
    '买4价': 'bid4',
    '买4量': 'bidvol4',
    '买5价': 'bid5',
    '买5量': 'bidvol5',
    '买1价': 'bid1',
    '卖1量': 'askvol1',
    '卖1价': 'ask1',
    '卖2量': 'askvol2',
    '卖2价': 'ask2',
    '卖3量': 'askvol3',
    '卖3价': 'ask3',
    '卖4量': 'askvol4',
    '卖4价': 'ask4',
    '卖5量': 'askvol5',
    '卖5价': 'ask5',
    '批次': 'update',
    '时间': 'time'}

def _to_wy_dict(doc):
    new_dict = {}
    for k, v in QUOTE_COL_MAPS.items():
        new_dict[v] = doc[k]
    new_dict['_id'] = doc['_id']
    return new_dict

def sina_to_wy():
    """转移数据"""
    src_db = get_db('quotes')
    tgt_db = get_db(DB_NAME)
    with tqdm(src_db.list_collection_names()) as it:
        for date_str in it:
            src_collection = src_db[date_str]
            docs = src_collection.find({})
            to_add = [_to_wy_dict(doc) for doc in docs]
            tgt_collection = tgt_db[date_str]
            create_index(tgt_collection)
            tgt_collection.insert_many(to_add)


from datetime import datetime
from editor import *

democtrl=None

def setup():
    global democtrl
    size(1280,720)
    background(0)
    stroke(0,255,0)
    noFill()
    democtrl=DemoCtrl()
    democtrl.renderall()

def draw():
    democtrl.blinkcursor()

def keyPressed():
    global democtrl
    print('[%d] %s'%(keyCode,key))
    if keyCode==18:
        democtrl.kmode='cmd'
        democtrl.stat('- command -')
    elif democtrl.kmode=='cmd':
        democtrl.cmdhandler(key,keyCode)
    elif democtrl.kmode=='edit':
        democtrl.currenteditor().edithandler(key,keyCode)
    else:
        democtrl.stat('unknown mode!')


import bmxobs as bmx
import matplotlib.pyplot as plt
import numpy as np
import statistics as stats

def bin_data(bmx_obj, channel, bin_fact, axis = "Time"):
    data = np.transpose(bmx_obj[int(channel)])
    data_shape = np.shape(data)

    if axis == "Time":
        ubin_len = float(data_shape[1])

    if axis == "Frequency":
        ubin_len = float(data_shape[0])

    if axis != "Time" and axis != "Frequency":
        print("ERROR: The allowed options for 'axis' are 'Time' and 'Frequency'")
        quit()

    bin_len = ubin_len/float(bin_fact)

    rem = (ubin_len-1.0)%(bin_len-1.0)
    n = (ubin_len-1.0-rem)/(bin_len-1.0)
    bin_len = int(((ubin_len-1.0)/n)+1)

    new_dat_list = np.zeros(bin_len)

    if axis == "Time":
        data_out = np.zeros(shape = (data_shape[0], bin_len))
        for row_index in np.arange(data_shape[0]):
            for element in np.arange(bin_len):
                if element == 0:
                    new_dat_list[element] = stats.mean(data[row_index, 0:int((n+1)/2)])

                if element == bin_len-1:
                    new_dat_list[element] = stats.mean(data[row_index, int(n*element - (n-1)/2):int(n*element + 1)])

                if element != 0 and element != bin_len-1:
                    new_dat_list[element] = stats.mean(data[row_index, int(n*element-(n-1)/2):int(n*element+(n+1)/2)])

            data_out[row_index, :] = new_dat_list

    if axis == "Frequency":
        data_out = np.zeros(shape = (bin_len, data_shape[1]))
        for col_index in np.arange(data_shape[1]):
            for element in np.arange(bin_len):
                if element == 0:
                    new_dat_list[element] = stats.mean(data[0:int((n+1)/2),col_index])

                if element == bin_len-1:
                    new_dat_list[element] = stats.mean(data[int(n*element - (n-1)/2):int(n*element + 1),col_index])

                if element != 0 and element != bin_len-1:
                    new_dat_list[element] = stats.mean(data[int(n*element-(n-1)/2):int(n*element+(n+1)/2),col_index])

            data_out[:, col_index] = new_dat_list

    return data_out

data = bmx.BMXObs("/home/chandrahas/Desktop/BMX/pas/210903_0000", channels="111")
data_out = bin_data(data,"111",40,"Frequency")
print(np.shape(data_out))

################### PLOT ##########################
extent = [0,1,data.freq[1][0],data.freq[1][-1]]
im = plt.imshow(data_out, cmap = "Blues", extent = extent, aspect = 'auto', interpolation = None)
plt.colorbar()
plt.show()


#coding: UTF-8
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
import random
import requests
import xlwt
from bs4 import BeautifulSoup

htmls = []
name = []
price = []
location = []
xiaoquname = []
time = []
other = []

page = 1
url = 'https://cq.lianjia.com/ershoufang/pg1/'
r = requests.get(url)
r.encoding= r.apparent_encoding
#r.encoding = "UTF-8"
soup = BeautifulSoup(r.text,'html.parser')
htmls.append(r.text)
for n in soup.find_all('a',class_="title"):
    name.append(n.text)

for p in soup.find_all(class_='price'):
    price.append(p.text)

for xqname in soup.find_all('a',attrs={'data-el':'region'}):
    xiaoquname.append(xqname.text)
#print str(name).decode('string_escape')

workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet')
num1=0
num2=0
num3=0
for n in name:
    num1 = num1+1
    worksheet.write(num1,0, label = n)

for p in price:
    num2 = num2+1
    worksheet.write(num2,1, label = p)

for a in xiaoquname:
    num3 = num3+1
    worksheet.write(num3,2, label = a)

workbook.save('Excel_test.xls')

#print("loading page" + url)

#print(soup.prettify())

#print (soup.select(""))

#prices = soup.find_all(class_="price")

#for pri in prices:
    #print(type(item))
 #   print(pri.text)

#infos = soup.find_all(class_="info")

#for inf in infos:
    #print(type(item))
 #   print(inf.text)


import os

import six

class SpaceService(object):
    def __init__(self, name, unit_file, environment=None, version=None):
        self.name = name
        self._unit_file = unit_file
        self.environment = environment or {}
        self.version = version

    @property
    def unit_file(self):
        if isinstance(self._unit_file, six.string_types):
            version = self.version or 'latest'
            return self._unit_file.replace('__VERSION__', version)
        return self._unit_file

    @unit_file.setter
    def unit_file(self, unit_file):
        self._unit_file = unit_file

class SpaceDockerService(SpaceService):
    def __init__(self, name, image, ports=None, volumes=None, environment=None,
                 version=None):
        super(SpaceDockerService, self).__init__(name, None, environment,
                                                 version)
        self.image = image
        self.ports = ports or {}
        self.volumes = volumes or {}

    @property
    def unit_file(self):
        name_base = os.path.splitext(self.name)[0]
        docker_run_flags = '--env-file /files/%s.env' % name_base
        docker_run_flags += self._dict_flags('p', self.ports)
        docker_run_flags += self._dict_flags('v', self.volumes)

        if ':' not in self.image:
            image = '%s:%s' % (self.image, self.version or 'latest')
        else:
            image = self.image

        return """[Unit]
Description={0}
Wants=spacel-agent.service

[Service]
User=space
TimeoutStartSec=0
Restart=always
StartLimitInterval=0
ExecStartPre=-/usr/bin/docker pull {1}
ExecStartPre=-/usr/bin/docker rm -f %n
ExecStart=/usr/bin/docker run --rm --name %n {2} {1}
ExecStop=/usr/bin/docker stop -t 2 %n
""".format(self.name, image, docker_run_flags)

    @staticmethod
    def _dict_flags(flag, items):
        if not items:
            return ''
        pad_flag = ' -%s ' % flag
        return pad_flag + pad_flag.join(['%s:%s' % (k, v)
                                         for k, v in items.items()])


import os
import re
from typing import Callable, Iterable

from lifecycle.config import Config
from lifecycle.monitor.base import JobMonitor
from lifecycle.monitor.health import check_until_job_is_operational, quick_check_job_condition
from lifecycle.monitor.metric_parser import read_last_call_timestamp_metric, scrape_metrics
from racetrack_client.log.exception import short_exception_details
from racetrack_client.utils.shell import shell_output
from racetrack_client.utils.time import datetime_to_timestamp, now
from racetrack_commons.deploy.resource import job_resource_name
from racetrack_commons.entities.dto import JobDto, JobStatus
from racetrack_client.log.logs import get_logger

from plugin_config import InfrastructureConfig

logger = get_logger(__name__)

class DockerDaemonMonitor(JobMonitor):
    """Discoverer listing job workloads deployed on a remote docker instance"""
    def __init__(self, infrastructure_target: str, infra_config: InfrastructureConfig) -> None:
        super().__init__()
        self.infra_config = infra_config
        self.infrastructure_target = infrastructure_target

    def list_jobs(self, config: Config) -> Iterable[JobDto]:
        # Heredoc to not mix quotes inside
        # Ports section needs to be last, because there were differences in outputs on developer systems:
        # One system had: 0.0.0.0:7020->7000/tcp
        # The other: 0.0.0.0:7000->7000/tcp, :::7000->7000/tcp
        cmd = f'DOCKER_HOST={self.infra_config.docker_host} ' + """
        docker ps -a --filter "name=^/job-" --format '{{.Names}} {{ .Label "job-name" }} {{ .Label "job-version" }} {{.Ports}}'
        """.strip()
        regex = r'(?P<resource_name>job-.+) (?P<job_name>.+) (?P<job_version>.+) 0\.0\.0\.0:(?P<job_port>\d+)->7000\/tcp'
        output = shell_output(cmd)

        assert self.infra_config.hostname, 'hostname of a docker daemon must be set'

        for line in output.splitlines():
            match = re.match(regex, line.strip())
            if match:
                resource_name = match.group('resource_name')
                job_name = match.group('job_name')
                job_version = match.group('job_version')
                job_port = match.group('job_port')

                internal_name = f'{self.infra_config.hostname}:{job_port}'

                job = JobDto(
                    name=job_name,
                    version=job_version,
                    status=JobStatus.RUNNING.value,
                    create_time=datetime_to_timestamp(now()),
                    update_time=datetime_to_timestamp(now()),
                    manifest=None,
                    internal_name=internal_name,
                    error=None,
                    infrastructure_target=self.infrastructure_target,
                )
                try:
                    job_url = f'http://{job.internal_name}'
                    quick_check_job_condition(job_url)
                    job_metrics = scrape_metrics(f'{job_url}/metrics')
                    job.last_call_time = read_last_call_timestamp_metric(job_metrics)
                except Exception as e:
                    error_details = short_exception_details(e)
                    job.error = error_details
                    job.status = JobStatus.ERROR.value
                    logger.warning(f'Job {job} is in bad condition: {error_details}')
                yield job

    def check_job_condition(self,
                               job: JobDto,
                               deployment_timestamp: int = 0,
                               on_job_alive: Callable = None,
                               logs_on_error: bool = True,
                               ):
        try:
            check_until_job_is_operational(f'http://{job.internal_name}', deployment_timestamp, on_job_alive)
        except Exception as e:
            if logs_on_error:
                logs = self.read_recent_logs(job)
                raise RuntimeError(f'{e}\nJob logs:\n{logs}')
            else:
                raise RuntimeError(str(e))

    def read_recent_logs(self, job: JobDto, tail: int = 20) -> str:
        container_name = job_resource_name(job.name, job.version)
        return shell_output(f'DOCKER_HOST={self.infra_config.docker_host} docker logs "{container_name}" --tail {tail}')


import csv
from pathlib import Path
# Some hacks to fix the directory and PATH so this can work from IDE or from console.
if Path.cwd().stem == 'joeyo':
    import os
    os.chdir('../..')
import sys
sys.path.insert(0, ".")
import data.utils as indl_du

if __name__ == "__main__":
    ROW_RANGE = [13, 19]  # Use [0, np.inf] to process all rows.
    # ROW_RANGE = [34, 35]  # From Ahmadi et al.

    data_dir = Path.cwd() / 'data' / 'joeyo'
    datasets_file = data_dir / 'datasets.csv'
    # Get the list of datasets to download
    datasets = []
    with open(datasets_file) as csvfile:
        datasetreader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
        for row in datasetreader:
            datasets.append(row)

    # Create a local folder to store the data
    local_dir = data_dir / 'download'
    if not local_dir.is_dir():
        local_dir.mkdir()

    base_url = 'https://zenodo.org/record/'
    file_grp = '583331'
    for row_ix, row in enumerate(datasets):
        if row_ix < ROW_RANGE[0] or row_ix > ROW_RANGE[1]:
            continue
        _fname = row['filename'] + '.mat'
        indl_du.download_from_web(base_url + file_grp + '/files/' + _fname,
                                  data_dir / 'download' / _fname, row['md5'])

        if len(row['supplemental']) > 0:
            _fname = row['filename'] + '.nwb'
            indl_du.download_from_web(base_url + row['supplemental'] + '/files/' + _fname,
                                      data_dir / 'download' / _fname, row['supp_md5'])


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
from xmind.core.markerref import MarkerId
xmind_name="java"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("分布式事务")
r2=s2.getRootTopic()
r2.setTitle("分布式事务")

content={
'分布式事务':[
    '为了保证不同数据库的数据一致性',
    {'过程':[
        '1.	基于XA协议的两阶段提交',
        '2.	消息事务+最终一致性',
        '3.	TCC编程模式'
    ]}
],
'分布式事务解决方案':[
    'https://www.jianshu.com/p/16b5900bb484'
]

}

#构建xmind
xmind.build(content,r2)
#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")


"""
This module provides plotting support in iPython.
"""
from functools import wraps

import matplotlib.pyplot as plt

__all__ = ['peek_show', "axis_labels_from_ctype"]

def peek_show(func):
    """
    A decorator to place on ``peek()`` methods to show the figure.

    The ``peek()`` method should return the figure then this method will
    attempt to show it in the correct way. This decorator will not return the
    figure to the user.
    """
    @wraps(func)
    def show_figure(*args, **kwargs):
        _ = func(*args, **kwargs)
        plt.show()

    return show_figure

def axis_labels_from_ctype(ctype, unit):
    """
    Returns axis labels for the given coordinate type and unit.

    Parameters
    ----------
    ctype: `str`
        Coordinate type.
    unit: `str`
        Required unit.

    Returns
    -------
    `str`
        "Axis Label [Unit]"
    """
    ctype_short = ctype[:4]

    labels = {'HGLN': 'Heliographic Longitude [{}]'.format(unit),
              'CRLN': 'Carrington Longitude [{}]'.format(unit),
              'HPLN': 'Helioprojective Longitude (Solar-X) [{}]'.format(unit),
              'SOLX': 'Heliocentric X [{}]'.format(unit),

              'HGLT': 'Latitude [{}]'.format(unit),
              'CRLT': 'Latitude [{}]'.format(unit),
              'HPLT': 'Helioprojective Latitude (Solar-Y) [{}]'.format(unit),
              'SOLY': 'Heliocentric Y [{}]'.format(unit)}

    return labels.get(ctype_short, "{} [{}]".format(ctype, unit))


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

import os
import sys
import requests
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, as_completed

def task_run(url):
    try:
        rep = requests.get(url)
        if rep.status_code != 200:
            print("%s http status %d".format(url, rep.status_code))
    except requests.exceptions.RequestException as e:
        print("%s except" % url)
        return e

def replay_request(req_file, domain):
    lines = []
    with open(req_file) as reader:
        lines = [line.rstrip() for line in reader]

    threads = []
    with ThreadPoolExecutor(max_workers=20) as executor:
        for line in lines:
            url = domain + line
            threads.append(executor.submit(task_run, url))

        finished = concurrent.futures.wait(threads)
        print(finished)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage : %s {filename}' % sys.argv[0])
        sys.exit(1)

    domain = "xxx"
    replay_request(sys.argv[1], domain)


# -*- coding: utf-8 -*-
"""
Created on Thu Jul  8 16:24:47 2021

@author: leow.weiqin
"""
#set flag for library
#os.environ["ENVIRONMENT_FLAG"] = 'production'

#script for data loading
import os
if os.environ["ENVIRONMENT_FLAG"] == 'development':
    from sql.python.PostgreSQL_ConnectionDev import pgupdateSQLTable_with_checking
elif os.environ["ENVIRONMENT_FLAG"] == 'production':
    from sql.python.PostgreSQL_ConnectionProd import pgupdateSQLTable_with_checking
from feature_engineering.partial_fe.data_prep_functions import *
from feature_engineering.partial_fe import *
from utils.aws_helper_python import upload_file_dev, upload_file_prod
import logging
import datetime
import sys

## logging
#create log folder if it does not exist
log_folder = os.getcwd()+"\logs\monthly_FE"
if not os.path.exists(log_folder):
    os.makedirs(log_folder)
log_filename = os.path.join(log_folder,"log_monthly_FE_{}.log".format(datetime.date.today()))
log_filename_s3 = "log_monthly_FE_{}.log".format(datetime.date.today())
#logging config
logging.basicConfig(level=logging.INFO,\
                    format='%(asctime)s %(levelname)-8s %(message)s',\
                    datefmt='%a, %d %b %Y %H:%M:%S',\
                    filename=log_filename,\
                    filemode='a')
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

## saving final dataframe to csv
data_folder = os.getcwd()+"\data"
if not os.path.exists(data_folder):
    os.makedirs(data_folder)
data_filename = os.path.join(data_folder,"monthly_features_phase2.csv")
data_filename_s3 = "monthly_features_phase2.csv"
data_filename_2 = os.path.join(data_folder,"monthly_features_ihs_supply_demand_all_binded_phase2.csv")
data_filename_2_s3 = "monthly_features_ihs_supply_demand_all_binded_phase2.csv"

try:
    #check pmi data availability
    logging.info('Checking for PMI and IHS Supply Demand data availability.')
    monthlyFE_flag = check_pmi_sd_availability()

    #check if monthly features already existing
    logging.info('Checking for PMI and IHS Supply Demand data availability.')
    monthlyFeatures_flag = check_for_monthly_features()

    if monthlyFE_flag == True and monthlyFeatures_flag == False:
        logging.info("PMI and IHS Supply Demand data is available and monthly features for the latest month is not produced yet, Thus performing FE.")
         #### data prep for monthly features
        logging.info("Running data prep for monthly features.")
        ##define start date (different for datasets) and end date (same accross dataset)
        #dates that can be updated monthly
        monthly_crack_start_date = datetime.date(year = 2004, month = 1, day = 1) ##TODO to detect max date from existing feature table
        fc_start_date = datetime.date(year = 2010, month = 1, day = 1)
        tradeflow_naphtha_start_date = datetime.date(year = 2011, month = 1, day = 1)
        fundamental_data_start_date = datetime.date(year = 2013, month = 3, day = 1)
        end_date = datetime.date.today().replace(day=1)
        trmi_end_date = end_date - datetime.timedelta(days=1) #required to be in date format instead of string

        #those that involve TA FE have to remain the same start date (cannot just pull recent months as it will affect the value)
        jetkero_stock_start_date = datetime.date(year = 2010, month = 1, day = 1)
        pmi_features_start_date = datetime.date(year = 2009, month = 12, day = 1)
        pmi_naphtha_features_start_date = datetime.date(year = 1992, month = 1, day = 1)
        pmi_expanding_features_start_date = datetime.date(year = 2002, month = 9, day = 1)
        pmi_transportation_features_start_date = datetime.date(year = 2009, month = 10, day = 1)
        trmi_start_date = datetime.date(year = 2010, month = 1, day = 1)
        trmi_start_date_2 = datetime.date(year = 1998, month = 1, day = 1)
        petchem_margin_start_date = datetime.date(year = 2014, month = 1, day = 1)

        ### crack price
        df_monthlyCrackPrice = MonthlyCrackPrice_data_prep(start_date = monthly_crack_start_date,
                                                           end_date = end_date)

        ### forward curve
        df_fc_final = FC_data_prep(df_monthlyCrackPrice= df_monthlyCrackPrice,
                                   start_date = fc_start_date,
                                   end_date = end_date)

        ### jet kero stock
        df_jetkero_stocks_final = jetkero_stocks_data_prep(start_date = jetkero_stock_start_date,
                                                           end_date = end_date)

        ### pmi
        df_pmi_final = pmi_data_prep(start_date = pmi_features_start_date,
                                     start_date_2 = pmi_naphtha_features_start_date,
                                     start_date_3 = pmi_expanding_features_start_date,
                                     start_date_4 = pmi_transportation_features_start_date,
                                     end_date = end_date)

        ### TRMI
        df_TRMI_final = TRMI_data_prep(start_date = trmi_start_date,
                                       start_date_2 = trmi_start_date_2,
                                       end_date = trmi_end_date)

        ### Tradeflow gasoil (require all data for seaonal feature, thus no start_date/end_date param
        df_tradeflow_gasoil = tradeflow_gasoil_data_prep()

        ### Tradeflow naphtha (used by gasoline95 model)
        df_tradeflow_naphtha =tradeflow_naphtha_data_prep(start_date = tradeflow_naphtha_start_date,
                                                          end_date = end_date)

        ### R2 features ((require all data for seaonal feature, thus no start_date/end_date param)
        df_fundamental_data_FE = fundamental_data_loading(end_date = end_date)

        ##push table to RDS DB, the updated table will be used by fundamental_data_prep
        logging.info("Pushing df_fundamental_data_FE to RDS DB.")

        ##TODO to change to prod db when productionalize
        pgupdateSQLTable_with_checking(latest_df = df_fundamental_data_FE,
                                           table_name = "monthly_data_merged_phase2",
                                           date_column = 'Date',
                                           keys = ['Date'])

        df_fundamental_data_final = fundamental_data_prep(start_date = fundamental_data_start_date,
                                                          end_date = end_date)

        ##petchem margin features
        df_petchem_margin_final = petchem_margin_data_prep(df_monthlyCrackPrice = df_monthlyCrackPrice,
                                                           start_date= petchem_margin_start_date,
                                                           end_date = end_date)

        ### IHS Supply Demand features (start and end date not applicable due to the all binded format)
        df_IHS_Supply_Demand_all_binded_final, df_IHS_Supply_Demand_appended_final = ihs_supply_demand_data_prep()

        ## merge all tables (monthly features)
        logging.info("Merging tables from relevant datasets.")
        df_merged = df_monthlyCrackPrice.merge(df_fc_final, how='outer', on='date_time')\
        .merge(df_jetkero_stocks_final, how='outer', on='date_time')\
        .merge(df_pmi_final, how='outer', on='date_time')\
        .merge(df_TRMI_final, how='outer', on='date_time')\
        .merge(df_tradeflow_gasoil, how='outer', on='date_time')\
        .merge(df_tradeflow_naphtha, how='outer', on='date_time')\
        .merge(df_fundamental_data_final, how='outer', on='date_time')\
        .merge(df_petchem_margin_final, how='outer', on='date_time')\
        .merge(df_IHS_Supply_Demand_appended_final, how='outer', on='date_time')
        logging.info("Monthly features (df_merged) data prep completed.")

        df_merged.sort_values(by="date_time",inplace = True)
        df_merged.reset_index(drop = True, inplace = True)

        ##push table to RDS DB
        logging.info("Pushing df_merged to RDS DB.")
        ##TODO to change to prod db when productionalize
        pgupdateSQLTable_with_checking(latest_df = df_merged,
                                           table_name = "monthly_features_phase2",
                                           keys = ['date_time'],
                                           date_column = "date_time")

        logging.info("Pushing df_IHS_Supply_Demand_all_binded_final to RDS DB.")
        ##TODO to change to prod db when productionalize
        pgupdateSQLTable_with_checking(latest_df = df_IHS_Supply_Demand_all_binded_final,
                                           table_name = "monthly_features_ihs_supply_demand_all_binded_phase2",
                                           keys = ['date_time','tag'],
                                           date_column = "date_time")

    #    logging.info("Saving final dataframe to csv.")
    #    df_merged.to_csv(data_filename)
    #    df_IHS_Supply_Demand_all_binded_final.to_csv(data_filename_2)

        #logging.info("Pushing final dataframe csv to S3.")
        #upload_file_dev(file_name = data_filename, object_name = 'monthly_features_phase2/{}'.format(data_filename_s3))
        #upload_file_dev(file_name = data_filename_2, object_name = 'monthly_features_phase2/{}'.format(data_filename_2_s3))

        logging.info("main_monthly_data_loading completed.")

    elif monthlyFE_flag == False or monthlyFeatures_flag == True:
        if monthlyFE_flag == False:
            logging.info("Latest PMI and IHS Supply Demand data is not available, hence not performing monthly FE.")
        elif monthlyFeatures_flag == True:
            logging.info("Monthly features already available hence not performing monthly FE.")

except: # catch *all* exceptions
    e = sys.exc_info()
    logging.error(e) # (Exception Type, Exception Value, TraceBack)

logging.info("Pushing log file to S3.")
if os.environ["ENVIRONMENT_FLAG"] == 'development':
    upload_file_dev(file_name = log_filename, object_name = 'log_files/{}'.format(log_filename_s3))
elif os.environ["ENVIRONMENT_FLAG"] == 'production':
    upload_file_prod(file_name = log_filename, object_name = 'log_files/{}'.format(log_filename_s3))

logging.shutdown()
os._exit(00)


import json
import time
from redis import Redis
from flask import Flask, request, jsonify, make_response, render_template
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from jinja2 import TemplateNotFound
from datetime import datetime

app = Flask(__name__)
limiter = Limiter(app,
                  key_func=get_remote_address)

redis = Redis()
app.config['REDIS_QUEUE_KEY'] = 'poltergeist'
qkey = app.config['REDIS_QUEUE_KEY']

@app.route('/cmd/<queue_name>', methods=['GET'])
def get_commands(queue_name):
    target_key = '%s:clip:%s' % (qkey, queue_name)
    clips = []
    start_time = time.time()
    while len(clips) == 0 and time.time() - start_time < 60:
        for key in redis.scan_iter(target_key+":*"):
            clip = redis.get(key).decode("utf-8")
            clips.append(clip)
            if clip.startswith("quiet:") or clip.startswith("quiet_all:"):
                clips = [clip]
                break
        time.sleep(0.25)
    return json.dumps({"clips": clips})

@app.route('/delete/<key>', methods=['GET'])
def delete(key):
    redis.delete(key)
    return "Success deleting key %s" % key

@app.route('/monitor', methods=['GET'])
def monitor():
    return "Success"

@app.route('/robots.txt', methods=['GET'])
def robots():
    return "User agent: * \n" + \
           "Disallow: /"

@app.route('/play/<clip_name>/<queue_name>', methods=['GET'])
@app.route('/play/<clip_name>', methods=['GET'])
@app.route('/', methods=['GET'])
@limiter.limit("1 per 2 second")
def submit_command(clip_name=None, queue_name=None):
    try:
        user_agent = request.headers['User-Agent'].lower()
        if "bot" in user_agent and not user_agent.startswith("slackbot"):
            return ""
    except KeyError:
        return ""

    if not queue_name:
        domain = request.headers['Host']
        if domain.endswith("jonheese.com") or domain.endswith('istolethis.com'):
            queue_name = "inetu-hdmi19"
        else:
            return jsonify(status="I couldn't find the queue_name for the " +
                           "URL you requested: %s" % domain), 404

    split_domain = domain.split(".")
    if not clip_name:
        clip_name = split_domain[0]

    if (clip_name.lower() == "friday" and datetime.today().weekday() != 4) or \
            ((clip_name.lower() == "lastchristmas" or
              clip_name.lower() == "jinglebell") and
             datetime.today().month != 12) or \
            (clip_name.lower() == "mondays" and
             datetime.today().weekday() != 0):
        return """

        <title>He need some milk!</title>
        <meta property="og:title" content="Oh no you didn't!"/>
        <meta property="og:image" content="/static/stahp.jpg"/>

"""
    if split_domain[1] != "sh":
        put_command(queue_name, clip_name)
        meta_url = "http://%s.sh.%s/" % (clip_name, ".".join(split_domain[1:]))
    else:
        meta_url = "http://%s/" % domain

    try:
        return render_template('%s/index.html' % clip_name, meta_url=meta_url)
    except TemplateNotFound:
        return render_template("notfound.html")

@app.route('/speech', methods=['POST'])
def queue_speech():
    text = request.form.get('text').replace(':', '@COLON@')
    if text is None:
        return render_template("notfound.html")
    put_command("inetu-hdmi19", "speech %s" % text)
    return render_template('speech/index.html')

def put_command(queue_name, clip_name):
    timestamp = time.time()
    target_key = '%s:clip:%s:%s' % (qkey, queue_name, timestamp)
    redis.set(target_key, "%s:%s" % (clip_name, str(timestamp)))

@app.route('/alexa', methods=['POST'])
def handle_alexa_request():
    print(json.dumps(request.json, indent=2))
    data = request.json
    request_type = data["request"]["type"]
    if request_type == "IntentRequest":
        clip_name = data["request"]["intent"]["slots"]["clip"]["value"]
        clip_name = clip_name.replace(" ", "").lower()
        put_command("inetu-hdmi19", clip_name)
        return generate_response("Okay")
    else:
        return generate_response("I couldn't find that clip")

@app.errorhandler(429)
def ratelimit_handler(e):
    return make_response("""
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
        <title>429 Too Many Requests</title>

Too Many Requests

    """, 429)

def generate_response(
        output_speech,
        card_title="",
        card_subtitle="",
        card_content="",
        endSession=True
):
    response = {
        "version": "1.0",
        "sessionAttributes": {
            "user": {
                "name": "Jon Heese"
            }
        },
        "response": {
            "outputSpeech": {
                "type": "PlainText",
                "text": output_speech
            },
            "card": {
                "type": "Simple",
                "title": card_title,
                "subtitle": card_subtitle,
                "content": card_content
            },
            "shouldEndSession": endSession
        }
    }
    return json.dumps(response)


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('project', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Employee',
            fields=[
                ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
                ('name', models.CharField(max_length=255)),
                ('address', models.CharField(max_length=255)),
                ('sex', models.CharField(max_length=20)),
                ('marital_status', models.BooleanField(default=False)),
                ('date_of_birth', models.DateField(null=True)),
            ],
        ),
        migrations.CreateModel(
            name='EmployeeRole',
            fields=[
                ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
                ('role', models.CharField(max_length=50)),
                ('description', models.CharField(max_length=255)),
            ],
        ),
        migrations.AddField(
            model_name='employee',
            name='role',
            field=models.ForeignKey(to='employee.EmployeeRole'),
        ),
        migrations.AddField(
            model_name='employee',
            name='site',
            field=models.ForeignKey(to='project.Project', default=2, related_name='employee'),
        ),
    ]


class Settings:
	""" Class to store all settings for the pinball game """
	def __init__(self):
		""" Initializes the settings """
		# Screen settings
		self.screen_size = (self.screen_width, self.screen_height) = (1200, 700)
		self.screen_color = (0,0,0) # color black
		self.screen_caption = "Pinball"

		# Ball settings
		self.ball_size = (self.ball_width, self.ball_height) = (10, 10)
		self.ball_color = (255,128,0) # color orange
		self.ball_speed = 1.0

		# Flipper settings
		self.flipper_size = (self.flipper_width, self.flipper_height) = (100, 20)
		self.flipper_color = (0,255,127) # color springgreen
		self.flipper_pos = 650
		self.flipper_speed = 2.5

		# Button settings
		self.button_width, self.button_height = 200, 50
		self.button_color = (100, 100, 100) # color
		self.text_color = (255, 255, 255)

		# Scoreboard settings
		self.score_font_color = (250,235,215) # color antiquewhite

		# Game settings
		self.game_active = False
		self.lives_left = 3


import sys, random
from locust import HttpLocust, TaskSet

def writePost(locust):
    postid = random.randint(1, 500)
    url_pre = '/editor/post?action=save&username=cs144&postid=';
    url_post = '&title=Loading%20Test&body=***Hello%20World!***';
    locust.client.post(url_pre + str(postid) + url_post, name='/editor/post?action=save')

class MyTaskSet(TaskSet):
    """ the class MyTaskSet inherits from the class TaskSet, defining the behavior of the user """
    tasks = {writePost: 1}

class MyLocust(HttpLocust):
    """ the class MyLocust inherits from the class HttpLocust, representing an HTTP user """
    task_set = MyTaskSet
    min_wait = 1000
    max_wait = 2000


#!/usr/bin/env python

import os, sys
import time
import numpy as np  # "pip install numpy" installs numpy
import random
import math
import csv
from numpy.linalg import inv

import de_R1

import gym
from gym import spaces
from gym.utils import seeding

from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam

#from callbacks import Callback
from rl.agents.dqn import DQNAgent

from rl.memory import SequentialMemory
from rl.util import *

import argparse
from inspect import currentframe, getframeinfo
from pathlib import Path

class PolicyDebug(object):
    """Abstract base class for all implemented policies.

        Each policy helps with selection of action to take on an environment.

        Do not use this abstract base class directly but instead use one of the concrete policies implemented.
        To implement your own policy, you have to implement the following methods:

        - `select_action`

        # Arguments
        agent (rl.core.Agent): Agent used
    """
    def _set_agent(self, agent):
        self.agent = agent

    @property
    def metrics_names(self):
        return []

    @property
    def metrics(self):
        return []

    def select_action(self, **kwargs):
        raise NotImplementedError()

    def get_config(self):
        """Return configuration of the policy

            # Returns
            Configuration as dict
        """
        return {}

class BoltzmannQPolicy(PolicyDebug):
    """Implement the Boltzmann Q Policy

        Boltzmann Q Policy builds a probability law on q values and returns
        an action selected randomly according to this law.
    """
    def __init__(self, tau=1., clip=(-500., 500.)):
        super(BoltzmannQPolicy, self).__init__()
        self.tau = tau
        self.clip = clip

    def select_action(self, q_values):
        """Return the selected action

            # Arguments
            q_values (np.ndarray): List of the estimations of Q for each action

            # Returns
            Selection action
        """
        assert q_values.ndim == 1
        q_values = q_values.astype('float64')
        nb_actions = q_values.shape[0]

        exp_values = np.exp(np.clip(q_values / self.tau, self.clip[0], self.clip[1]))
        probs = exp_values / np.sum(exp_values)
        action = np.random.choice(range(nb_actions), p=probs)
        return action

    def get_config(self):
        """Return configurations of BoltzmannQPolicy

            # Returns
            Dict of config
        """
        config = super(BoltzmannQPolicy, self).get_config()
        config['tau'] = self.tau
        config['clip'] = self.clip
        return config

class EpsGreedyQPolicy(PolicyDebug):
    """Implement the epsilon greedy policy

        Eps Greedy policy either:

        - takes a random action with probability epsilon
        - takes current best action with prob (1 - epsilon)
        """
    def __init__(self, eps=.1):
        super(EpsGreedyQPolicy, self).__init__()
        self.eps = eps

    def select_action(self, q_values):
        """Return the selected action

            # Arguments
            q_values (np.ndarray): List of the estimations of Q for each action

            # Returns
            Selection action
            """
        assert q_values.ndim == 1
        nb_actions = q_values.shape[0]

        if np.random.uniform() < self.eps:
            action = np.random.random_integers(0, nb_actions-1)
        else:
            action = np.argmax(q_values)
        return action

    def get_config(self):
        """Return configurations of EpsGreedyQPolicy

            # Returns
            Dict of config
            """
        config = super(EpsGreedyQPolicy, self).get_config()
        config['eps'] = self.eps
        return config

def DE(fun, lbounds, ubounds, budget):
    ENV_NAME = 'ea'

    env = de_R1.DEEnv(fun, lbounds, ubounds, budget)

    nb_actions = env.action_space.n

    # Build a sequential model.
    model = Sequential()
    model.add(Flatten(input_shape=(1,) + env.observation_space.shape))
    model.add(Dense(100, activation = 'relu'))
    model.add(Dense(100, activation = 'relu'))
    model.add(Dense(100, activation = 'relu'))
    model.add(Dense(100, activation = 'relu'))
    model.add(Dense(nb_actions, activation = 'linear'))
    print("Model Summary: ",model.summary())

    memory = SequentialMemory(limit=100000, window_length=1)#100000

    # Boltzmann Q Policy
    policy = EpsGreedyQPolicy()

    # DQN Agent: Finally, we configure and compile our agent. You can use every built-in Keras optimizer and even the metrics!
    dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=1e4, target_model_update=1e3, policy=policy, enable_double_dqn = True, batch_size = 64) # nb_steps_warmup >= nb_steps 2000
    # Neural Compilation
    dqn.compile(Adam(lr=1e-4), metrics=['mae'])
    dqn.load_weights('dqn_ea_weights.h5f')

    dqn.test(env, nb_episodes=1, visualize=False)
    return env.best_so_far


'''
Find the lowest common ancestor in an unordered binary tree given two values in the tree.

 Lowest common ancestor : the lowest common ancestor (LCA) of two nodes v and w in a tree or directed acyclic graph (DAG) is the lowest (i.e. deepest) node that has both v and w as descendants.
Example :

        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2_     0        8
         /   \
         7    4
For the above tree, the LCA of nodes 5 and 1 is 3.

 LCA = Lowest common ancestor
Please note that LCA for nodes 5 and 4 is 5.

You are given 2 values. Find the lowest common ancestor of the two nodes represented by val1 and val2
No guarantee that val1 and val2 exist in the tree. If one value doesn’t exist in the tree then return -1.
There are no duplicate values.
You can use extra memory, helper functions, and can modify the node struct but, you can’t add a parent pointer.
'''

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

class Solution:
    # @param A : root node of tree
    # @param B : integer
    # @param C : integer
    # @return an integer
    def lca(self, A, B, C):
        def findLCA(node, p1, p2):
            if node is None:
                return None, False, False
            isP1 = node.val == p1
            isP2 = node.val == p2
            ln, ll, lr = findLCA(node.left, p1, p2)
            rn, rl, rr = findLCA(node.right, p1, p2)
            if ln is not None:
                return ln, True, True
            elif rn is not None:
                return rn, True, True
            if (ll or rl or isP1) and (lr or rr or isP2):
                return node, True, True
            else:
                return None, (ll or rl or isP1), (lr or rr or isP2)

        n, l, r = findLCA(A, B, C)
        return n.val if n is not None else -1

Tree Data Structure /least-common-ancestor.py


"""
5. Faça um programa que imprima na tela os números de 1 a 20, um abaixo do outro.
Depois modifique o programa para que ele mostre os números um ao lado do outro.
"""

import time
import os

os.system('cls')

numeros = []

for i in range(1, 21):
    print(i)
    numeros.append(i)

time.sleep(8)
os.system('cls')

print (numeros)


class Queue(object):

    def __init__(self, capacity: int):
        self.__capacity__ = capacity
        self.arr = [None for _ in range(capacity)]
        self.head = 0
        self.tail = 0
        self.__size__ = 0

    def enqueue(self, value: int):

        if self.is_full():
            return

        self.arr[self.tail] = value

        self.tail += 1
        self.tail %= self.__capacity__
        self.__size__ += 1

    def dequeue(self) -> int:

        if self.is_empty():
            return -1

        output = self.arr[self.head]

        self.head += 1
        self.head %= self.__capacity__
        self.__size__ -= 1

        return output

    def values(self) -> list:

        output = []

        h = self.head
        t = self.tail

        while h != t:
            output.append(self.arr[h])
            h = (h+1)%self.__capacity__

        return output

    def capacity(self) -> int:
        return self.__capacity__

    def empty_space(self) -> int:
        return self.capacity() - len(self)

    def is_empty(self) -> bool:
        return (self.__size__ == 0)

    def is_full(self) -> bool:
        return (self.__size__ == self.__capacity__)

    def __len__(self) -> int:
        return self.__size__

class Ball(object):

    def __init__(self, numbers: list):
        self.numbers = numbers
        self.digit_index = 0

    def calculate_digit(self) -> int:
        if abs(self.digit_index) < len(self.numbers):
            return self.digit_index
        else:
            if self.digit_index >= 0:
                self.digit_index %= len(self.numbers)
            else:
                self.digit_index = (self.digit_index%len(self.numbers)) - len(self.numbers)

        return self.digit_index

if __name__ == "__main__":

    ins = input().split()
    n_balls, balls_len = int(ins[0]), int(ins[1])

    balls = Queue(n_balls)

    for _ in range(n_balls):
        ins = [int(num) for num in input().split()]
        balls.enqueue(Ball(ins))

    red_ball_n = int(input())
    red_ball_nums = [int(num) for num in input().split()]

    red_ball = Ball(red_ball_nums)

    repeation = int(input())

    for _ in range(repeation):

        balls.arr[balls.head].digit_index += red_ball.numbers[red_ball.calculate_digit()]

        hold = balls.dequeue()
        balls.enqueue(hold)

        red_ball.digit_index += 1

    while not balls.is_empty():
        hold = balls.dequeue()
        print(hold.numbers[hold.calculate_digit()], end='')

    print()

2nd series/question3_(رمز های حلقوی)/Q3.py


import datetime
import networkx as nx
import ifcopenshell.api
import ifcopenshell.util.date
import ifcopenshell.util.sequence

class Usecase:
    def __init__(self, file, work_schedule=None):
        """Calculate the critical path and floats for a work schedule

        This implements critical path analysis, using the forward pass and
        backward pass method. When run, any tasks that have no float will be
        marked as critical, and both the total and free floats will be
        populated for all task times.

        Cyclical relationships are detected and will result in a recursion
        error.

        :param work_schedule: The IfcWorkSchedule to perform the calculation on.
        :type work_schedule: ifcopenshell.entity_instance.entity_instance
        :return: None
        :rtype: None

        Example:

        .. code:: python

            # See the example for ifcopenshell.api.sequence.cascade_schedule for
            # details of how to set up a basic set of tasks and calculate the
            # critical path. Typically cascade_schedule is run prior to ensure
            # that dates are correct.
        """
        self.file = file
        self.settings = {"work_schedule": work_schedule}

    def execute(self):
        # The method implemented is the same as shown here:
        # https://www.youtube.com/watch?v=qTErIV6OqLg
        self.start_dates = []
        self.build_network_graph()

        if not self.start_dates:
            return

        is_cyclic = False
        attempts = 0
        self.pending_nodes = set(self.g.nodes)
        max_worst_case_attempts = pow(len(self.pending_nodes), 2)
        while self.pending_nodes:
            attempts += 1
            remaining_nodes = set()
            for pending_node in self.pending_nodes:
                if not self.forward_pass(pending_node):
                    remaining_nodes.add(pending_node)
            self.pending_nodes = remaining_nodes

            # As we parse nodes, the remaining attempts can drop dramatically, so we recalculate the upper limit
            max_remaining_attempts = pow(len(self.pending_nodes), 2)
            if max_remaining_attempts < max_worst_case_attempts:
                max_worst_case_attempts = max_remaining_attempts
                attempts = 0

            if attempts > max_worst_case_attempts:
                is_cyclic = True
                break  # We have an infinite loop due to a cyclic graph

        if is_cyclic:
            raise RecursionError(
                "Task graph is cyclic and so critical path method cannot be performed."
            )
            return

        self.pending_nodes = set(self.g.nodes)
        while self.pending_nodes:
            remaining_nodes = set()
            for pending_node in self.pending_nodes:
                if not self.backward_pass(pending_node):
                    remaining_nodes.add(pending_node)
            self.pending_nodes = remaining_nodes

        self.update_task_times()

    def build_network_graph(self):
        self.sequence_type_map = {
            None: "FS",
            "START_START": "SS",
            "START_FINISH": "SF",
            "FINISH_START": "FS",
            "FINISH_FINISH": "FF",
            "USERDEFINED": "FS",
            "NOTDEFINED": "FS",
        }
        self.g = nx.DiGraph()
        self.edges = []
        self.g.add_node("start", duration=0, duration_type="ELAPSEDTIME", calendar=None)
        self.g.add_node(
            "finish", duration=0, duration_type="ELAPSEDTIME", calendar=None
        )
        for rel in self.settings["work_schedule"].Controls:
            for related_object in rel.RelatedObjects:
                if not related_object.is_a("IfcTask"):
                    continue
                self.add_node(related_object)
        self.g.add_edges_from(self.edges)

    def add_node(self, task):
        if task.IsNestedBy:
            for rel in task.IsNestedBy:
                [self.add_node(o) for o in rel.RelatedObjects]
            return

        if task.TaskTime and task.TaskTime.ScheduleDuration:
            duration = ifcopenshell.util.date.ifc2datetime(
                task.TaskTime.ScheduleDuration
            ).days
            duration_type = task.TaskTime.DurationType
        else:
            duration = 0
            duration_type = "ELAPSEDTIME"

        self.g.add_node(
            task.id(),
            duration=duration,
            duration_type=duration_type,
            calendar=ifcopenshell.util.sequence.derive_calendar(task),
        )

        self.edges.extend(
            [
                (
                    rel.RelatingProcess.id(),
                    task.id(),
                    {
                        "lag_time": 0
                        if not rel.TimeLag
                        else ifcopenshell.util.date.ifc2datetime(
                            rel.TimeLag.LagValue.wrappedValue
                        ).days,
                        "type": self.sequence_type_map[rel.SequenceType],
                    },
                )
                for rel in ifcopenshell.util.sequence.get_sequence_assignment(task, sequence="predecessor")
            ]
        )

        predecessor_types = [rel.SequenceType for rel in ifcopenshell.util.sequence.get_sequence_assignment(task, "predecessor")]
        successor_types = [rel.SequenceType for rel in ifcopenshell.util.sequence.get_sequence_assignment(task, "successor")]

        if not predecessor_types:
            self.edges.append(("start", task.id(), {"lag_time": 0, "type": "FS"}))
            if task.TaskTime and task.TaskTime.ScheduleStart:
                self.start_dates.append(
                    ifcopenshell.util.date.ifc2datetime(task.TaskTime.ScheduleStart)
                )
                self.g.nodes[task.id()]["early_start"] = ifcopenshell.util.date.ifc2datetime(task.TaskTime.ScheduleStart) # we assume this task is constrained to start on this date
        if not successor_types:
            self.edges.append((task.id(), "finish", {"lag_time": 0, "type": "FF"}))

    def update_task_times(self):
        for ifc_definition_id in self.g.nodes:
            if ifc_definition_id in ("start", "finish"):
                continue
            data = self.g.nodes[ifc_definition_id]
            task = self.file.by_id(ifc_definition_id)
            if not task.TaskTime:
                continue
            ifcopenshell.api.run(
                "sequence.edit_task_time",
                self.file,
                task_time=task.TaskTime,
                attributes={
                    "FreeFloat": ifcopenshell.util.date.datetime2ifc(
                        data["free_float"], "IfcDuration"
                    ),
                    "TotalFloat": ifcopenshell.util.date.datetime2ifc(
                        data["total_float"], "IfcDuration"
                    ),
                    "IsCritical": data["total_float"].days == 0,
                    "EarlyStart": ifcopenshell.util.date.datetime2ifc(
                        data["early_start"], "IfcDateTime"
                    ),
                    "EarlyFinish": ifcopenshell.util.date.datetime2ifc(
                        data["early_finish"], "IfcDateTime"
                    ),
                    "LateStart": ifcopenshell.util.date.datetime2ifc(
                        data["late_start"], "IfcDateTime"
                    ),
                    "LateFinish": ifcopenshell.util.date.datetime2ifc(
                        data["late_finish"], "IfcDateTime"
                    ),
                },
            )

    def offset_date(self, date, days, node):
        return ifcopenshell.util.sequence.offset_date(
            date, datetime.timedelta(days=days), node["duration_type"], node["calendar"]
        )

    def forward_pass(self, node):
        successors = self.g.successors(node)
        predecessors = list(self.g.predecessors(node))
        data = self.g.nodes[node]

        if node == "start":
            data["early_start"] = min(self.start_dates)
        else:
            finishes = []
            starts = []
            if data.get("early_start") is not None:
                data["early_finish"] = ifcopenshell.util.sequence.get_start_or_finish_date(
                    data["early_start"],
                    datetime.timedelta(days=data["duration"]),
                    data["duration_type"],
                    data["calendar"],
                    date_type="FINISH",
                )
                return True  # we're done! We assume this task is constrained and finish processing it

            for predecessor in predecessors:
                predecessor_data = self.g.nodes[predecessor]
                edge = self.g[predecessor][node]
                if edge["type"] == "FS":
                    finish = predecessor_data.get("early_finish")
                    if finish is None:
                        return
                    days = 0 if predecessor_data["duration"] == 0 else 1
                    if edge["lag_time"]:
                        days += edge["lag_time"]
                    if days:
                        starts.append(
                            datetime.datetime.combine(
                                self.offset_date(finish, days, data), datetime.time(9)
                            )
                        )
                        starts.append(
                            datetime.datetime.combine(
                                self.offset_date(finish, days, predecessor_data),
                                datetime.time(9),
                            )
                        )
                    else:
                        starts.append(finish)
                elif edge["type"] == "SS":
                    start = predecessor_data.get("early_start")
                    if start is None:
                        return
                    if edge["lag_time"]:
                        starts.append(self.offset_date(start, edge["lag_time"], data))
                        starts.append(
                            self.offset_date(start, edge["lag_time"], predecessor_data)
                        )
                    else:
                        starts.append(start)
                elif edge["type"] == "FF":
                    finish = predecessor_data.get("early_finish")
                    if finish is None:
                        return
                    if edge["lag_time"]:
                        finishes.append(
                            self.offset_date(finish, edge["lag_time"], data)
                        )
                        finishes.append(
                            self.offset_date(finish, edge["lag_time"], predecessor_data)
                        )
                    else:
                        finishes.append(finish)
                elif edge["type"] == "SF":
                    start = predecessor_data.get("early_start")
                    if start is None:
                        return
                    days = -1
                    if edge["lag_time"]:
                        days += edge["lag_time"]
                    if days or edge["lag_time"]:
                        finishes.append(
                            datetime.datetime.combine(
                                self.offset_date(start, days, data), datetime.time(17)
                            )
                        )
                        finishes.append(
                            datetime.datetime.combine(
                                self.offset_date(start, days, predecessor_data),
                                datetime.time(17),
                            )
                        )
                    else:
                        finishes.append(start)
            if starts and finishes:
                data["early_start"] = max(starts)
                data["early_finish"] = max(finishes)
                potential_finish = ifcopenshell.util.sequence.get_start_or_finish_date(
                    data["early_start"],
                    datetime.timedelta(days=data["duration"]),
                    data["duration_type"],
                    data["calendar"],
                    date_type="FINISH",
                )
                if potential_finish > data["early_finish"]:
                    data["early_finish"] = potential_finish
                else:
                    data[
                        "early_start"
                    ] = ifcopenshell.util.sequence.get_start_or_finish_date(
                        data["early_finish"],
                        datetime.timedelta(days=data["duration"]),
                        data["duration_type"],
                        data["calendar"],
                        date_type="START",
                    )
            elif finishes:
                data["early_finish"] = max(finishes)
            elif starts:
                data["early_start"] = max(starts)
            else:
                print("How did this happen?")

        if data.get("early_finish") is None:
            data["early_finish"] = ifcopenshell.util.sequence.get_start_or_finish_date(
                data["early_start"],
                datetime.timedelta(days=data["duration"]),
                data["duration_type"],
                data["calendar"],
                date_type="FINISH",
            )
        elif data.get("early_start") is None:
            data["early_start"] = ifcopenshell.util.sequence.get_start_or_finish_date(
                data["early_finish"],
                datetime.timedelta(days=data["duration"]),
                data["duration_type"],
                data["calendar"],
                date_type="START",
            )

        return True

    def backward_pass(self, node):
        successors = list(self.g.successors(node))
        predecessors = self.g.predecessors(node)
        data = self.g.nodes[node]
        free_floats = []

        if node == "finish":
            data["late_finish"] = data["early_finish"]
        else:
            finishes = []
            starts = []
            for successor in successors:
                successor_data = self.g.nodes[successor]
                edge = self.g[node][successor]
                if edge["type"] == "FS":
                    start = successor_data.get("late_start")
                    if start is None:
                        return
                    days = 1
                    if edge["lag_time"]:
                        days += edge["lag_time"]
                    if days or edge["lag_time"]:
                        finishes.append(
                            datetime.datetime.combine(
                                self.offset_date(start, -days, data), datetime.time(17)
                            )
                        )
                        finishes.append(
                            datetime.datetime.combine(
                                self.offset_date(start, -days, successor_data),
                                datetime.time(17),
                            )
                        )
                    else:
                        finishes.append(start)
                    free_floats.append(
                        self.calculate_free_float(
                            data["early_finish"].date() + datetime.timedelta(days=1),
                            successor_data["early_start"].date(),
                            edge["lag_time"],
                            data,
                            successor_data,
                        )
                    )
                elif edge["type"] == "SS":
                    start = successor_data.get("late_start")
                    if start is None:
                        return
                    if edge["lag_time"]:
                        starts.append(self.offset_date(start, -edge["lag_time"], data))
                        starts.append(
                            self.offset_date(start, -edge["lag_time"], successor_data)
                        )
                    else:
                        starts.append(start)
                    free_floats.append(
                        self.calculate_free_float(
                            data["early_start"],
                            successor_data["early_start"],
                            edge["lag_time"],
                            data,
                            successor_data,
                        )
                    )
                elif edge["type"] == "FF":
                    finish = successor_data.get("late_finish")
                    if finish is None:
                        return
                    if edge["lag_time"]:
                        finishes.append(
                            self.offset_date(finish, -edge["lag_time"], data)
                        )
                        finishes.append(
                            self.offset_date(finish, -edge["lag_time"], successor_data)
                        )
                    else:
                        finishes.append(finish)
                    free_floats.append(
                        self.calculate_free_float(
                            data["early_finish"],
                            successor_data["early_finish"],
                            edge["lag_time"],
                            data,
                            successor_data,
                        )
                    )
                elif edge["type"] == "SF":
                    finish = successor_data.get("late_finish")
                    if finish is None:
                        return
                    days = 0 if successor_data["duration"] == 0 else -1
                    if edge["lag_time"]:
                        days += edge["lag_time"]
                    if days:
                        starts.append(
                            datetime.datetime.combine(
                                self.offset_date(finish, -days, data), datetime.time(9)
                            )
                        )
                        starts.append(
                            datetime.datetime.combine(
                                self.offset_date(finish, -days, successor_data),
                                datetime.time(9),
                            )
                        )
                    else:
                        starts.append(finish)
                    free_floats.append(
                        self.calculate_free_float(
                            data["early_start"],
                            successor_data["early_finish"],
                            edge["lag_time"],
                            data,
                            successor_data,
                        )
                    )
            if starts and finishes:
                data["late_start"] = min(starts)
                data["late_finish"] = min(finishes)
                if (
                    self.offset_date(data["late_start"], data["duration"], data)
                    < data["late_finish"]
                ):
                    data[
                        "late_finish"
                    ] = ifcopenshell.util.sequence.get_start_or_finish_date(
                        data["late_start"],
                        datetime.timedelta(days=data["duration"]),
                        data["duration_type"],
                        data["calendar"],
                        date_type="FINISH",
                    )
                else:
                    data[
                        "late_start"
                    ] = ifcopenshell.util.sequence.get_start_or_finish_date(
                        data["late_finish"],
                        datetime.timedelta(days=data["duration"]),
                        data["duration_type"],
                        data["calendar"],
                        date_type="START",
                    )
            elif finishes:
                data["late_finish"] = min(finishes)
            elif starts:
                data["late_start"] = min(starts)
            else:
                print("How did this happen?")

        if data.get("late_finish") is None:
            data["late_finish"] = ifcopenshell.util.sequence.get_start_or_finish_date(
                data["late_start"],
                datetime.timedelta(days=data["duration"]),
                data["duration_type"],
                data["calendar"],
                date_type="FINISH",
            )
        elif data.get("late_start") is None:
            data["late_start"] = ifcopenshell.util.sequence.get_start_or_finish_date(
                data["late_finish"],
                datetime.timedelta(days=data["duration"]),
                data["duration_type"],
                data["calendar"],
                date_type="START",
            )

        if data["duration_type"] == "WORKTIME":
            data["total_float"] = datetime.timedelta(
                days=ifcopenshell.util.sequence.count_working_days(
                    data["early_finish"], data["late_finish"], data["calendar"]
                )
            )
        else:
            data["total_float"] = data["late_finish"] - data["early_finish"]
            # If the float is within the span of a single day, it may show as a 8 hours
            if data["total_float"].seconds == 60 * 60 * 8:
                data["total_float"] = datetime.timedelta(
                    days=data["total_float"].days + 1
                )

        data["free_float"] = min(free_floats) if free_floats else None
        # If the float is within the span of a single day, it may show as a 8 hours
        if data["free_float"] and data["free_float"].seconds == 60 * 60 * 8:
            data["free_float"] = datetime.timedelta(days=data["free_float"].days + 1)

        return True

    def calculate_free_float(
        self,
        predecessor_date,
        successor_date,
        lag_time,
        predecessor_data,
        successor_data,
    ):
        if not lag_time:
            min_successor_date = successor_date
        else:
            min_successor_date = min(
                (
                    self.offset_date(successor_date, -lag_time, predecessor_data),
                    self.offset_date(successor_date, -lag_time, successor_data),
                )
            )
        if predecessor_data["duration_type"] == "WORKTIME":
            return datetime.timedelta(
                days=ifcopenshell.util.sequence.count_working_days(
                    predecessor_date, min_successor_date, predecessor_data["calendar"]
                )
            )
        return min_successor_date - predecessor_date


import numpy as np
import matplotlib.pyplot as plt
import _4_8_minkowski_config as local_config
import bib_config as global_config
from bib import *

# Create plot
fig, ax = plt.subplots()
setup_plot(ax, local_config.PLOT_BOTTOM_LEFT_CORNER,
           local_config.PLOT_TOP_RIGHT_CORNER)

lattice_points = plot_lattice(ax, local_config.BASIS_MATRIX, local_config.PLOT_BOTTOM_LEFT_CORNER,
                              local_config.PLOT_TOP_RIGHT_CORNER, False, **local_config.LATTICE_PROPERTIES)

# threshold for plotting parallelepiped
threshold = 5
for lattice_point in lattice_points:
    if np.linalg.norm(lattice_point) < threshold and np.linalg.norm(lattice_point + local_config.b_1 + local_config.b_2) < threshold:
        plot_fundamental_parallelepiped_from_point(
            ax, lattice_point, local_config.BASIS_MATRIX, **local_config.FUNDAMENTAL_PARALLELEPIPED_PROPERTIES)

# Get the lattice determinant
lattice_determinant = np.abs(np.linalg.det(local_config.BASIS_MATRIX))

# Get lower bound from the theorem.
bound = 4 * lattice_determinant
# Scale the volume by 1.5
bound *= 1.5

# Define a matrix defining an ellipsoid.
positive_definite_matrix = np.array([[1.0, 0.0], [0.0, 2.0]])

# Scale the ellipsoid until its volume exceeds the bounds.
while get_ellipsoid_volume(positive_definite_matrix) < bound:
    positive_definite_matrix *= 0.5

plot_ellipsoid(ax, local_config.ORIGIN,
               positive_definite_matrix, -np.pi / 4, False, True)
print(positive_definite_matrix)
# Get K
k_matrix = positive_definite_matrix * 4
plot_ellipsoid(ax, local_config.ORIGIN, k_matrix, -np.pi / 4, True, True)

# Check that vol(K) > det
assert (get_ellipsoid_volume(k_matrix) > lattice_determinant)

# Pick point x
x = np.array([-0.5, 0.5])
y = x - local_config.b_1 + local_config.b_2

plot_point(ax, x, '$x$', True)
plot_point(ax, y, '$y$', True)

# Get z
z = x - y
plot_point(ax, z, '$z$', True)

# Get poitns 2x, 2y
x_two = x * 2
y_two = y * 2
plot_point(ax, x_two, '$2x$', True)
plot_point(ax, y_two, '$2y$', True)
plot_point(ax, -y_two, '$-2y$', True)

# Visualize the equality
plot_vector(ax, local_config.ORIGIN, x, **local_config.PATH_ONE_PROPERTIES)
plot_vector(ax, x, z - x, **local_config.PATH_ONE_PROPERTIES)

plot_vector(ax, local_config.ORIGIN, x_two, **local_config.PATH_TWO_PROPERTIES)
plot_vector(ax, x_two, -y_two - x_two, **local_config.PATH_TWO_PROPERTIES)
# Show the plot
plt.show()


#!/usr/bin/python
import subprocess, sys
cmd = "snort -A console -i eth0 -c /etc/snort/snort.conf"
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()


"""add publish column to meta station

Revision ID: 879f0efa125f
Revises: 7b139906ac46
Create Date: 2022-01-13 09:33:05.636350

"""
from alembic import op
import sqlalchemy as sa
from pycds.context import get_schema_name

# revision identifiers, used by Alembic.
revision = "879f0efa125f"
down_revision = "7b139906ac46"
branch_labels = None
depends_on = None

schema_name = get_schema_name()

def upgrade():
    op.add_column(
        "meta_station",
        sa.Column(
            "publish", sa.Boolean(), default=True, server_default="true", nullable=False
        ),
        schema=schema_name,
    )

def downgrade():
    op.drop_column("meta_station", "publish", schema=schema_name)


import pygame

class Button():
	def __init__(self,x, y, image, scale, text=None, xoff=None):
		self.width = int(image.get_width() * scale)
		self.height = int(image.get_height() * scale)
		self.image = pygame.transform.scale(image, (self.width, self.height))
		self.rect = self.image.get_rect()
		self.rect.topleft = (x, y)

		self.text = None
		if text:
			self.text = text
			if xoff:
				self.xoff = xoff
			else:
				self.xoff = self.text.get_width() // 2
			self.yoff = self.text.get_height() // 2

		self.clicked = False

	def draw(self, surface):
		action = False

		#get mouse position
		pos = pygame.mouse.get_pos()

		#check mouseover and clicked conditions
		if self.rect.collidepoint(pos):
			if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
				action = True
				self.clicked = True

		if pygame.mouse.get_pressed()[0] == 0:
			self.clicked = False

		#draw button
		surface.blit(self.image, (self.rect.x, self.rect.y))
		if self.text:
			self.image.blit(self.text, (self.width//2 - self.xoff, self.height//2 - self.yoff))

		return action


class Solution(object):
    def XXX(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """

        matrix.reverse()
        res = list(map(list, zip(*matrix)))
        matrix[:] = res


# From: Bristol, UK
# To: Prague, CZ
# Time: within 5 days prior to 24.12
# No changes in flight
# cheapest
#%%
import requests
import pandas as pd
import json
import datetime
from pandas.io.json import json_normalize

baseUrl = 'https://api.skypicker.com/'
def get_data(url, params, headers):
  raw_data = requests.get(url, params=params, headers = headers)
  return json.loads(raw_data.text)

def get_city(city, country):
  url = baseUrl + 'locations?'
  headers = { 'Content-Type': 'application/json' }
  params = {'term': city, 'locale': country, 'location_types': 'airport','limit': 10, 'active_only': True, 'sort': 'name' }
  json_data = get_data(url, params, headers)
  df = json_normalize(json_data)
  result = {'id': df.iloc[0]['locations'][0]['id'],
  'locale_code': df.iloc[0]['meta.locale.code'] }
  return result

def get_flights(from_fly, to_fly, date_from, date_to):
    url = baseUrl + 'aggregation_flights?'
    headers = {'X-API-Version': '2'}
    params = {
      'fly_from':from_fly['id'],
      'fly_to': to_fly['id'],
      'v': 3,
      'date_from': date_from,
      'date_to': date_to,
      # 'max_fly_duration': 6,
      'flight_type': 'oneway',
      'one_for_city': 0,
      'one_per_date': 1,
      'adults': 1,
      'children':0,
      'infants': 0,
      'fly_days': [0,1,2,3,4,5,6],
      'fly_days_type': 'departure',
      # 'only_working_days': 0,
      # 'only_weekends': 0,
      # 'partner':'picky',
      # 'partner_market':'us',
      'curr':'EUR',
      'locale': 'en',
      # 'price_from':1,
      # 'price_to':10000,
      'dtime_from': '00:00',
      'dtime_to':'24:00',
      'atime_from': '00:00',
      'atime_to': '24:00',
      # 'stopover_from': '00:00',
      # 'stopover_to': '00:00',
      'max_stopovers': 0,
      # 'conn_on_diff_airport':1,
      # 'select_airlines': 'FR,AA',
      # 'select_airlines_exclude': False,
      # 'select_stop_airport': 'BCN,FRA',
      # 'select_stop_airport_exclude':False,
      # 'vehicle_type': 'aircraft',
      'limit': 30,
      'sort': 'price',
      'asc': 0,
      'xml':0
    }
    json_data = get_data(url, params, headers)
    return json_normalize(json_data['data'])

#%%

def get_cheapest(from_city, from_country, to_city, to_country, start_date, end_date):
  from_fly = get_city(from_city, from_country)
  to_fly = get_city(to_city, to_country)
  flights = get_flights(from_fly, to_fly, start_date, end_date)
  flights.sort_values(by=['price'], axis=0, inplace=True)
  cheapest_price = flights.iloc[0]['price']
  return cheapest_price

result = get_cheapest('Bristol', 'UK', 'Prague', 'CZ', '19/12/2019', '24/12/2019')
print(f'Cheapest price: {result} EUR')

#%%


'''
Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
'''

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

from queue import PriorityQueue
class Solution(object):
    def mergeKLists(self, lists):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not lists:
            return None
        head = ListNode(None)
        prev = head
        q = PriorityQueue()
        for node in lists:
            if node:
                q.put((node.val, node))
        while q.qsize():
            prev.next = q.get()[1]
            prev = prev.next
            if prev.next:
                q.put((prev.next.val, prev.next))
        return head.next

l1 =ListNode(0)
l1.next = ListNode(2)
l1.next.next = ListNode(5)
# l2 = None
# l3 = ListNode(-1)
s = Solution()
l = s.mergeKLists([l1])
if not l: print(l)
while l:
    print(l.val)
    l = l.next


from scipy.sparse import csr_matrix

from recpack.metrics.base import Metric
from recpack.util import get_top_K_ranks
from recpack.matrix import to_binary

class PercentileRanking(Metric):
    """Expected Percentile Ranking.

    Metric as described in Hu, Yifan, Yehuda Koren, and Chris Volinsky.
    "Collaborative filtering for implicit feedback datasets."
    2008 Eighth IEEE International Conference on Data Mining. Ieee, 2008.
    With a change to account for items that receive no recommendation score for a user.

    Percentile ranking is calculated according to the following formula:

    .. math::

        \\text{perc_rank} = \\frac{\\sum\\limits_{u \\in U,i \\in I} y^{true}_{u,i} * \\overline{\\text{rank}}_{u,i}}{\\sum\\limits_{u \\in U,i \\in I} y^{true}_{u,i}}

    where

    .. math::

        \\overline{rank}_{u,i} =
        \\begin{cases}
            \\frac{\\text{rank}_{u,i} - 1}{|I|} & \\text{if } i \\in y^{pred}(u) \\\\
            \\frac{\\max\\limits_{j} (\\text{rank}_{uj}) + |I|}{2|I|} & \\text{otherwise}
        \\end{cases}

    Non predicted items in the :math:`y^{true}` matrix,
    get the average rank from all remaining items per user.
    As if these remaining items would be ordered randomly.

    Lower values of this percentile-ranking are desirable,
    because that indicates relevant items are shown at higher positions.
    """

    def __init__(self):
        super().__init__()

    def _calculate(self, y_true: csr_matrix, y_pred: csr_matrix) -> None:
        """Calculate the percentile ranking score for the particular
        ``y_true`` and ``y_pred`` matrices.

        Assumes a binary ``y_true`` matrix.

        :param y_true: User-item matrix with the actual true rating values.
        :param y_pred: User-item matrix with all prediction rating scores.
        :return: None: The result will be saved in self.value.
        """

        # Get ranks for all recommended items.
        # Items with 0 score, will get no rank.
        # This transformation can be quite expensive if the y_pred matrix is dense.
        K = self.num_items
        ranking = get_top_K_ranks(y_pred, K)

        # Ranking starts at 0 for this metric
        # ranking.data -= 1
        rank_values = ranking / self.num_items  # to get a percentile ranking
        rank_values.data = rank_values.data - (1 / self.num_items)  # Ranking starts at 0

        # Compute the percentile rankings of hits in the topK
        hit_mat = y_true.multiply(rank_values)

        # Account for items that were expected, but not recommended
        # These items will get the average between max recommended rank,
        # and max possible rank
        # This means they are randomly distributed in the group of all 0 scores,
        # but we use the expected value rather than giving each a random rank
        # to improve computation speed.
        max_rank_per_user = rank_values.max(axis=1)

        rank_for_misses_per_user = csr_matrix((max_rank_per_user.toarray() + 1) / 2)

        # Add the average rank for all non matches.
        pure_hit = y_true.multiply(y_pred)
        ranking_mat = (y_true - to_binary(pure_hit)).multiply(rank_for_misses_per_user) + hit_mat

        # Multiply with 100 to get percents io fractions
        ranking_mat = ranking_mat * 100

        # Numerator is the sum of the ranks of y_true values.
        numerator = ranking_mat.sum()
        denominator = y_true.sum()

        self.value_ = numerator / denominator


import torch
import numpy as np
import tenseal as ts
from PIL import Image
import copy
import pdb

def ServerInference(enc_model, x_enc, windows_nb, kernel_shape, stride):
    # Encrypted evaluation
    enc_output = enc_model(x_enc, windows_nb)
    return enc_output

def train_acc(output, target):
    class_correct = list(0. for i in range(10))
    class_total = list(0. for i in range(10))
    # convert output probabilities to predicted class
    _, pred = torch.max(output, 1)
    # compare predictions to true label
    correct = np.squeeze(pred.eq(target.data.view_as(pred)))

    # calculate train accuracy for each object class
    for i in range(len(target)):
        label = target.data[i]
        class_correct[label] += correct[i].item()
        class_total[label] += 1

    print(
        f'Train Accuracy (Overall): {np.sum(class_correct) / np.sum(class_total)} \n'
        # f'Train Accuracy (Overall): {int(100 * np.sum(class_correct) / np.sum(class_total))}% \n'
        # f'({int(np.sum(class_correct))}/{int(np.sum(class_total))})'
        )
    return np.sum(class_correct) / np.sum(class_total)

def GenCiph(data, context, kernel_shape, stride):
    x_enc, windows_nb = ts.im2col_encoding(
            context, data.view(28, 28).tolist(), kernel_shape[0],
            kernel_shape[1], stride
        )
    return x_enc, windows_nb

def TestSampGen(data, distribution):
    class_counts = torch.bincount(torch.Tensor(data.targets).int()).cuda()
    class_weights = 1.0 / class_counts
    for i in range(len(distribution)):
        skew_weights =  distribution[i]*class_weights
        sample_weights = skew_weights[data.targets]
        distribution[i] = sample_weights
    return distribution

def aggregation(client_models):
    global_model = copy.deepcopy(client_models[0])
    for i in range(1, len(client_models)):
        for global_param, client_param in zip(global_model.parameters(), client_models[i].parameters()):
            global_param.data += client_param.data

    for global_param in global_model.parameters():
        global_param.data /= len(client_models)
    return global_model

def save_image(image_tensor, filename='/home/dev/workspace/Homomorphic-HalfFed/checkimage.png'):

    image_np = np.uint8(image_tensor.squeeze().permute(1, 2, 0).cpu().numpy() * 255)

    image_pil = Image.fromarray(image_np)

    if filename.lower().endswith('.png'):
        image_pil.save(filename)
    elif filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'):
        image_pil.save(filename, quality=95)
    else:
        raise ValueError("Unsupported file format: %s" % filename)