import os
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# add function to import mazes
mazePath = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../Mazes/")
for file in os.listdir(mazePath):
    print(file)

# ask for input
mazeInput = str(input("\nWhich maze would you like solved?\n(type the name without the extension)\n"))
image = Image.open(str(mazePath)+'/'+mazeInput+'.png')
width, height = image.size
maze = np.array(image)

# calculate the dimensions of the maze

# define start and end nodes
def endNodes(maze, width):
    for i in range(width):
            if maze[0][i] == 1:
                maze[0][i] = 3
            if maze[-1][i] == 1:
                maze[-1][i] = 2
    return maze

# create function for dead end block algorithm
def deadEndBlocking(maze, width, height):
    changes = 0
    for i in range(1, width-1):
        for j in range(1, height-1):
            if maze[j][i] == 1:
                counter = 0

                if maze[j+1][i] > 0.75:
                    counter += 1
                if maze[j-1][i] > 0.75:
                    counter += 1
                if maze[j][i+1] > 0.75:
                    counter += 1
                if maze[j][i-1] > 0.75:
                    counter += 1

                if counter  == 1:
                    maze[j][i] = 0.75
                    changes += 1

    return maze, changes

# define the start and end points clearly in the maze
mazeNodes = endNodes(maze, width)

# search the original dead ends
solution, changes = deadEndBlocking(mazeNodes, width, height)

loop = 1
while True:
    solution, changes = deadEndBlocking(solution, width, height)
    loop += 1
    if changes == 0:
        print(loop)
        break

###
# can choose to apply djikstras algorithm here, knowing the possible solutions

# display the solved maze
plt.figure('maze solution')
plt.clf()
plt.imshow(solution)
plt.colorbar()
plt.show()


import matplotlib.pyplot as plt

from die import Die

# Create two D6 dice.
die_1 = Die()
die_2 = Die()

# Make some rolls, and store results in a list.
results = []
for roll_num in range(1000):
    result = die_1.roll() + die_2.roll()
    results.append(result)

# Analyze the results.
frequencies = []
min_result = die_1.num_of_die + die_2.num_of_die
max_result = die_1.num_sides + die_2.num_sides
for value in range(min_result, max_result+1):
    frequency = results.count(value)
    frequencies.append(frequency)

# Visualize the results.
x_values = list(range(min_result, max_result+1))

# Set the size of the plotting window.
plt.figure(figsize=(10, 6))

hist = plt.bar(x_values, frequencies)

plt.title("Results of rolling two D6 1000 times.", fontsize=24)
plt.xlabel("Side Number", fontsize=14)
plt.ylabel("Number of Times Rolled", fontsize=14)

# Set size of tick labels.
plt.tick_params(axis='both', which='major', labelsize=14)

plt.show()


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

"""
icScanner - Программа запуска сканирования документов.
            Пакетной обработки сканированных документов.

Параметры коммандной строки:

    python icscanner.py <Параметры запуска>

Параметры запуска:

    [Помощь и отладка]
        --help|-h|-?        Напечатать строки помощи
        --version|-v        Напечатать версию программы
        --debug|-d          Режим отладки
        --log|-l            Режим журналирования

   [Режимы запуска]
        --scanner=          Явное указание имени сканера
        --source=           Источник сканирования:
                            Flatbed     (Планшетный)
                            ADF_Front   (Фронтальная сторона)
                            ADF_Back    (Обратная сторона)
                            ADF_Duplex  (Дуплекс/Обе стороны)
        --mode=             Режим сканирования:
                            Lineart     (Штриховой)
                            Halftone    (Полутоновой)
                            Grey        (Черно-белый)
                            Color       (Цветной)
        --depth=            Глубина. По умолчанию 8
        --multi_scan        Вкл. режим многостраничного сканирования
        --preview           Вкл. просмотр результата сканирования
        --page_size=        Размер страницы:
                            A4 или A3
        --area=             Область сканирования
        --scan_dir=         Папка сканирования
        --file_name=        Имя файла сканирования (без расширения)
                            В случае пакетной обработки, необходимо
                            указать имена файлов через <;>
        --file_type=        Тип файла сканирования:
                            PDF, JPEG, JPG, TIF, BMP
        --ext_cmd=          Команда запуска внешнего
                            инструмента сканирования
        --pack_mode         Включение режима пакетной обработки.
                            Пакетный режим подразумевает запуск программы
                            без диалогового окна.
        --pack_pages=       Количество страниц в каждом документе
                            Если --pack_pages не указан, то считаем,
                            что все документы одностраничные.
                            В указании страниц можно указать через /
                            есть 2-х стороннее сканирование или нет.
                            Например: 2/1 - 2 листа с двух сторон
                            3/0 - 3 листа с одной стороны
        --max_sheets=       Ограничение количества листов в лотке сканнера
"""

import sys
import getopt
import wx

from ic import config
from ic.std.log import log
from scanner import scanner_dlg
from scanner import scan_manager

__version__ = (1, 1, 1, 2)

def main(argv):
    """
    Основная запускающая функция.
    @param argv: Список параметров коммандной строки.
    """
    # Разбираем аргументы командной строки
    try:
        options, args = getopt.getopt(argv, 'h?vdl',
                                      ['help', 'version', 'debug', 'log',
                                       'scanner=', 'source=',
                                       'mode=', 'multi_scan', 'preview',
                                       'page_size=', 'area=',
                                       'scan_dir=',
                                       'file_name=', 'file_type=',
                                       'ext_cmd=',
                                       'pack_mode', 'pack_pages=',
                                       'glue', 'max_sheets='])
    except getopt.error as msg:
        log.error(str(msg), bForcePrint=True)
        log.info(__doc__)
        sys.exit(2)

    # Инициализоция системы журналирования
    log.init(config)

    txt_version = '.'.join([str(ver) for ver in __version__])
    cmd_options = dict()
    for option, arg in options:
        if option in ('-h', '--help', '-?'):
            print(__doc__)
            sys.exit(0)
        elif option in ('-v', '--version'):
            print('icScanner version: %s' % txt_version)
            sys.exit(0)
        elif option in ('-d', '--debug'):
            config.set_glob_var('DEBUG_MODE', True)
        elif option in ('-l', '--log'):
            config.set_glob_var('LOG_MODE', True)
        elif option in ('--scanner', ):
            cmd_options['scanner'] = arg
        elif option in ('--source',):
            cmd_options['scan_source'] = arg
        elif option in ('--mode',):
            cmd_options['scan_mode'] = arg
        elif option in ('--multi_scan',):
            cmd_options['is_multi_scan'] = True
        elif option in ('--preview',):
            cmd_options['is_preview'] = True
        elif option in ('--page_size',):
            if arg in ('A4', 'a4'):
                cmd_options['page_size'] = scan_manager.A4_PORTRAIT_PAGE_SIZE
            elif arg in ('A3', 'a3'):
                cmd_options['page_size'] = scan_manager.A3_PORTRAIT_PAGE_SIZE
            else:
                log.warning(u'Не обрабатываемый размер страницы <%s>' % arg)
        elif option in ('--area',):
            try:
                area = tuple([float(x.strip()) for x in arg.split(',')])
                cmd_options['scan_area'] = area
            except:
                log.fatal(u'Ошибка парсинга параметра области сканирования <%s>' % arg)
        elif option in ('--scan_dir',):
            cmd_options['scan_dir'] = arg
        elif option in ('--file_name',):
            cmd_options['scan_filename'] = arg
        elif option in ('--file_type',):
            cmd_options['scan_filetype'] = arg.lower()
        elif option in ('--depth',):
            cmd_options['depth'] = int(arg)
        elif option in ('--ext_cmd',):
            cmd_options['ext_scan_cmd'] = arg
        elif option in ('--pack_mode',):
            cmd_options['pack_mode'] = True
        elif option in ('--pack_pages',):
            cmd_options['pack_pages'] = arg
        elif option in ('--max_sheets',):
            config.set_glob_var('DEFAULT_SCANNER_MAX_SHEETS', int(arg))
        else:
            log.warning(u'Не обрабатываемый параметр коммандной строки <%s>' % option)

    # По умолчанию пакетный режим отключен+
    #                                     v
    if not cmd_options.get('pack_mode', False):
        # Внимание! Приложение создается для
        # управления диалоговыми окнами
        app = wx.PySimpleApp()
        # ВНИМАНИЕ! Выставить русскую локаль
        # Это необходимо для корректного отображения календарей,
        # форматов дат, времени, данных и т.п.
        locale = wx.Locale()
        locale.Init(wx.LANGUAGE_RUSSIAN)

        scanner_dlg.do_scan_dlg(options=cmd_options,
                                title=u'Сканирование. icScanner ' + txt_version)
        app.MainLoop()
    else:
        # В пакетном режиме не используем диалоговое окно
        # Но в случае режима склеивания документа по частям диалоговые окна используются
        filenames = cmd_options.get('scan_filename', u'').split(';')
        pack_page_list = cmd_options.get('pack_pages', u'').split(';')
        n_pages = [int(pack_page.split('/')[0]) if '/' in pack_page else int(pack_page) for pack_page in pack_page_list]
        duplexes = [bool(int(pack_page.split('/')[1])) if '/' in pack_page else False for pack_page in pack_page_list]
        scan_filenames = [(filename, n_pages[i] if i < len(n_pages) else 1,
                           duplexes[i] if i < len(duplexes) else False) for i, filename in enumerate(filenames)]
        scan_admin = scanner_dlg.icScanAdministrator()
        # Установить дополнительные опции из коммандной строки
        scan_admin.setExtOptions(scan_dir=cmd_options['scan_dir'])
        scan_admin.runScanPack(*scan_filenames)

if __name__ == '__main__':
    main(sys.argv[1:])


from driftpy.constants.numeric_constants import (
    AMM_RESERVE_PRECISION,
    PRICE_DIV_PEG,
    PEG_PRECISION,
    AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO,
    QUOTE_PRECISION,
)
from driftpy.sdk_types import AssetType
from driftpy.types import PositionDirection, SwapDirection, AMM

def calculate_peg_from_target_price(
    target_price: int, base_asset_reserves: int, quote_asset_reserves: int
) -> int:
    return max(
        (
            target_price * base_asset_reserves / quote_asset_reserves
            + (PRICE_DIV_PEG / 2)
        )
        / PRICE_DIV_PEG,
        1,
    )

def calculate_mark_price_amm(amm: AMM):
    return calculate_price(
        amm.base_asset_reserve,
        amm.quote_asset_reserve,
        amm.peg_multiplier,
    )

def calculate_mark_price_amm(amm, oracle_price=None):
    dynamic_peg = "PrePeg" in amm.strategies

    if dynamic_peg:
        peg = calculate_peg_multiplier(amm, oracle_price)
    else:
        peg = amm.peg_multiplier

    return calculate_price(
        amm.base_asset_reserve,
        amm.quote_asset_reserve,
        peg,
    )

def calculate_bid_price_amm(amm: AMM, oracle_price=None):
    base_asset_reserves, quote_asset_reserves = calculate_spread_reserves(
        amm, PositionDirection.SHORT, oracle_price=oracle_price
    )
    return calculate_price(
        base_asset_reserves, quote_asset_reserves, amm.peg_multiplier
    )

def calculate_ask_price_amm(amm: AMM, oracle_price=None):
    base_asset_reserves, quote_asset_reserves = calculate_spread_reserves(
        amm, PositionDirection.LONG, oracle_price=oracle_price
    )
    return calculate_price(
        base_asset_reserves, quote_asset_reserves, amm.peg_multiplier
    )

def calculate_price(base_asset_amount, quote_asset_amount, peg_multiplier):
    if abs(base_asset_amount) <= 0:
        return 0
    else:
        return (quote_asset_amount / base_asset_amount) * peg_multiplier / PEG_PRECISION

def calculate_terminal_price(market):
    swap_direction = (
        SwapDirection.ADD if market.base_asset_amount > 0 else SwapDirection.REMOVE
    )

    new_quote_asset_amount, new_base_asset_amount = calculate_swap_output(
        abs(market.base_asset_amount),
        market.amm.base_asset_reserve,
        swap_direction,
        market.amm.sqrt_k,
    )
    # print(new_quote_asset_amount/new_base_asset_amount)

    terminal_price = calculate_price(
        new_base_asset_amount,
        new_quote_asset_amount,
        market.amm.peg_multiplier,
    )

    return terminal_price

def calculate_swap_output(
    swap_amount, input_asset_reserve, swap_direction: SwapDirection, invariant_sqrt
):
    invariant = invariant_sqrt * invariant_sqrt
    assert swap_direction in [
        SwapDirection.ADD,
        SwapDirection.REMOVE,
    ], "invalid swap direction parameter"
    assert swap_amount >= 0
    if swap_direction == SwapDirection.ADD:
        new_input_asset_reserve = input_asset_reserve + swap_amount
    else:
        assert input_asset_reserve > swap_amount, "%i > %i" % (
            input_asset_reserve,
            swap_amount,
        )
        new_input_asset_reserve = input_asset_reserve - swap_amount

    new_output_asset_reserve = invariant / new_input_asset_reserve
    return [new_output_asset_reserve, new_input_asset_reserve]

def calculate_amm_reserves_after_swap(
    amm,
    input_asset_type: AssetType,
    swap_amount,
    swap_direction: SwapDirection,
):
    if input_asset_type == AssetType.QUOTE:
        swap_amount = (
            swap_amount * AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO / amm.peg_multiplier
        )

        [new_base_asset_reserve, new_quote_asset_reserve] = calculate_swap_output(
            swap_amount,
            amm.quote_asset_reserve,
            swap_direction,
            amm.sqrt_k,
        )
    else:
        [new_quote_asset_reserve, new_base_asset_reserve] = calculate_swap_output(
            swap_amount,
            amm.base_asset_reserve,
            swap_direction,
            amm.sqrt_k,
        )

    return [new_quote_asset_reserve, new_base_asset_reserve]

def get_swap_direction(
    input_asset_type: AssetType, position_direction: PositionDirection
) -> SwapDirection:
    assert input_asset_type in [
        AssetType.BASE,
        AssetType.QUOTE,
    ], "invalid input_asset_type: " + str(input_asset_type)
    assert position_direction in [
        PositionDirection.LONG,
        PositionDirection.SHORT,
    ], "invalid position_direction: " + str(position_direction)
    if (
        position_direction == PositionDirection.LONG
        and input_asset_type == AssetType.BASE
    ):
        return SwapDirection.REMOVE

    if (
        position_direction == PositionDirection.SHORT
        and input_asset_type == AssetType.QUOTE
    ):
        return SwapDirection.REMOVE

    return SwapDirection.ADD

def calculate_budgeted_repeg(amm, cost, target_px=None, pay_only=False):
    if target_px == None:
        target_px = amm.last_oracle_price  # / 1e10
        assert amm.last_oracle_price != 0

    C = cost
    x = amm.base_asset_reserve / AMM_RESERVE_PRECISION
    y = amm.quote_asset_reserve / AMM_RESERVE_PRECISION
    d = amm.base_asset_amount_with_amm / AMM_RESERVE_PRECISION
    Q = amm.peg_multiplier / PEG_PRECISION
    k = (amm.sqrt_k / AMM_RESERVE_PRECISION) ** 2

    dqar = y - (k / (x + d))

    # print('dqar', dqar)
    cur_px = y / x * Q
    target_peg = target_px * x / y

    peg_change_direction = target_peg - Q

    use_target_peg = (dqar < 0 and peg_change_direction > 0) or (
        dqar > 0 and peg_change_direction < 0
    )

    if dqar != 0 and not use_target_peg:
        new_peg = Q + (C / dqar)
    else:
        new_peg = target_peg

    # print(cur_px, target_px, Q, '->', new_peg, '||', target_peg, '(budget:', C,')', d, dqar)
    if cur_px > target_px and new_peg < target_peg:
        new_peg = target_peg
    if cur_px < target_px and new_peg > target_peg:
        new_peg = target_peg

    if pay_only:
        if new_peg < Q and d > 0:
            new_peg = Q
        if new_peg > Q and d < 0:
            new_peg = Q

    # print("new_peg", new_peg, target_peg)
    return new_peg

def calculate_peg_multiplier(
    amm, oracle_price=None, now=None, delay=None, budget_cost=None
):
    # todo: make amm have all the vars needed
    if "PrePeg" in amm.strategies:
        if oracle_price is None:
            oracle_price = amm.last_oracle_price
            if delay is None:
                if now is not None:
                    delay = now - amm.last_oracle_price_twap_ts
                else:
                    delay = 100
            # delay_discount = 1/(delay*delay/2)
            # last_mark = calculate_mark_price_amm(market.amm)
            # target_px = last_mark + ((oracle_price-last_mark)*delay_discount)
            target_px = oracle_price
        else:
            target_px = oracle_price

        if budget_cost is None:
            fee_pool = (amm.total_fee_minus_distributions / QUOTE_PRECISION) - (
                amm.total_fee / QUOTE_PRECISION
            ) / 2
            budget_cost = max(0, fee_pool)
            # print('budget to repeg:', budget_cost, 'to target_price', target_px)

        new_peg = int(
            calculate_budgeted_repeg(amm, budget_cost, target_px=target_px)
            * PEG_PRECISION
        )
        return new_peg
    elif "PreFreePeg" in amm.strategies:
        target_px = oracle_price

        if budget_cost is None:
            fee_pool = (amm.total_fee_minus_distributions / QUOTE_PRECISION) - (
                amm.total_fee / QUOTE_PRECISION
            ) / 2
            budget_cost = max(0, fee_pool)

        new_peg = int(
            calculate_budgeted_repeg(amm, budget_cost, target_px=target_px)
            * PEG_PRECISION
        )
        return new_peg
    else:
        return amm.peg_multiplier

def calculate_spread_reserves(
    amm, position_direction: PositionDirection, oracle_price=None
):
    BID_ASK_SPREAD_PRECISION = 1_000_000  # this is 100% (thus 1_000 = .1%)
    mark_price = calculate_mark_price_amm(amm, oracle_price=oracle_price)
    spread = amm.base_spread
    # print('sprad reserve calc:', mark_price, spread, oracle_price)

    if "OracleRetreat" in amm.strategies:
        if oracle_price is None:
            oracle_price = amm.last_oracle_price
        pct_delta = float(oracle_price - mark_price) / mark_price
        # print(amm.last_oracle_price, mark_price, pct_delta, spread)
        if (pct_delta > 0 and position_direction == PositionDirection.LONG) or (
            pct_delta < 0 and position_direction == PositionDirection.SHORT
        ):
            oracle_spread = abs(pct_delta) * QUOTE_PRECISION * 2
            if oracle_spread > spread:
                spread = oracle_spread * 2
        else:
            # no retreat
            pass

    if "VolatilityScale" in amm.strategies:
        spread *= min(2, max(1, amm.mark_std))

    if "InventorySkew" in amm.strategies:
        max_scale = 5  # if 'OracleRetreat' not in amm.strategies else 20

        effective_position = (
            amm.base_asset_amount_with_amm
        )  # (amm.sqrt_k - amm.base_asset_reserve)

        net_cost_basis = (
            amm.quote_asset_amount_long - (amm.quote_asset_amount_short)
        ) / QUOTE_PRECISION
        net_base_asset_value = (
            (amm.quote_asset_reserve - amm.terminal_quote_asset_reserve)
            / AMM_RESERVE_PRECISION
            * amm.peg_multiplier
            / PEG_PRECISION
        )
        local_base_asset_value = mark_price * (
            effective_position / AMM_RESERVE_PRECISION
        )

        local_pnl = local_base_asset_value - net_cost_basis
        net_pnl = net_base_asset_value - net_cost_basis
        # print("local pnl: ", local_pnl, "net pnl:", net_pnl)
        if amm.total_fee_minus_distributions > 0:
            effective_leverage = (local_pnl - net_pnl) / (
                amm.total_fee_minus_distributions / QUOTE_PRECISION
            )
            print("effective_leverage:", effective_leverage)
            if position_direction == PositionDirection.LONG:
                # print((1 + (effective_position/(amm.sqrt_k/10000))))
                spread *= min(max_scale, max(1, (1 + effective_leverage)))
            else:
                spread *= min(max_scale, max(1, (1 - effective_leverage)))
        else:
            spread *= max_scale

    amm.last_spread = spread

    quote_asset_reserve_delta = 0
    if spread > 0:
        quote_asset_reserve_delta = amm.quote_asset_reserve / (
            BID_ASK_SPREAD_PRECISION / (spread / 4)
        )
    # print(quote_asset_reserve_delta)

    if position_direction == PositionDirection.LONG:
        quote_asset_reserve = amm.quote_asset_reserve + quote_asset_reserve_delta
    else:
        quote_asset_reserve = amm.quote_asset_reserve - quote_asset_reserve_delta

    base_asset_reserve = (amm.sqrt_k * amm.sqrt_k) / quote_asset_reserve
    # print(base_asset_reserve, quote_asset_reserve, amm.sqrt_k)
    return base_asset_reserve, quote_asset_reserve

# async def main():
#     # Try out  the functions here

#     # Initiate ClearingHouse
#     drift_acct = await ClearingHouse.create(program)
#     drift_user_acct = await drift_acct.get_user_account()

#     # Get the total value of your collateral
#     balance = drift_user_acct.collateral / 1e6
#     print(f"Total Collateral: {balance}")

#     asset = "SOL"  # Select which asset you want to use here

#     drift_assets = [
#         "SOL",
#         "BTC",
#         "ETH",
#         "LUNA",
#         "AVAX",
#         "BNB",
#         "MATIC",
#         "ATOM",
#         "DOT",
#         "ALGO",
#     ]
#     idx = drift_assets.index(asset)

#     markets = await drift_acct.get_markets_account()
#     market = markets.markets[idx]

#     markets_summary = calculate_market_summary(markets)

#     # Get the predicted funding rates of each market
#     print(calculate_predicted_funding(markets, markets_summary))

#     # Liquidity required to move AMM price of SOL to 95
#     print(calculate_target_price_trade(market, 95, output_asset_type="quote"))

#     # Slippage of a $5000 long trade
#     print(calculate_trade_slippage("LONG", 5000, market, input_asset_type="quote"))

# asyncio.run(main())


from django.shortcuts import render,redirect
from .models import *
from .forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

# Create your views here.

@login_required
def section(request):
    u=User.objects.all()
    s=Section.objects.all()
    t=None
    if request.method == 'POST':
        x=request.POST.get('list')
        y = request.POST
        l=None
        for key,value in y.items():
                if value== 'Add':
                    l=key

        t= Section.objects.get(name=l)
        m=list.objects.create(name=x,user=request.user,section=t)
    p=[]
    q=[]
    r=[]
    w=[]
    passion=set()
    mission=set()
    profession=set()
    vocation=set()

    for i in list.objects.filter(user=request.user,section=s[0]):
        p.append(i.name)

    for i in list.objects.filter(user=request.user,section=s[1]):
        q.append(i.name)
    for i in list.objects.filter(user=request.user,section=s[2]):
        r.append(i.name)
    for i in list.objects.filter(user=request.user,section=s[3]):
        w.append(i.name)

    passion=set(p).intersection(set(q))
    mission=set(p).intersection(set(w))
    profession=set(r).intersection(set(q))
    vocation=set(w).intersection(set(r))

    print(passion)
    print(mission)
    print(profession)
    print(vocation)
    purpose = []
    for i in passion:
        purpose.append(i)
    for i in mission:
        purpose.append(i)
    for i in profession:
        purpose.append(i)
    for i in vocation:
        purpose.append(i)

    t=list.objects.all().filter(user=request.user).order_by('-id')
    return render(request,'purpose/section.html',{'s':s,'t':t,'u':u,'purpose':purpose})
@login_required
def edit(request,pk):

    l=list.objects.get(pk=pk)
    form=UpdateList(instance=l)
    if request.method=="POST":
        form=UpdateList(request.POST,instance=l)
        if form.is_valid():
            instance=form.save(commit=False)
            instance.user=request.user

            instance.save()
            return redirect('purpose:home')
        else:
            form=UpdateList(instance=pk)
    return render(request,'purpose/update.html',{'form':form})
@login_required
def users(request):
    u=User.objects.exclude(email=request.user.email)
    return render(request,'purpose/users.html',{'u':u})
@login_required
def retrive_by_user(request,pk):

    l=list.objects.all().filter(user=request.user)

    return render(request,'purpose/retrive.html',{'l':l,'k':k})
@login_required
def retrieve_user_list(request,pk):

    s=Section.objects.all()

    u=User.objects.get(pk=pk)
    t=list.objects.filter(user=u)
    return render(request,'purpose/retrive.html',{'s':s,'t':t})

@login_required
def delete(request,pk):
    l=list.objects.get(pk=pk).delete()
    return redirect('purpose:home')


import json
import datetime
import os

from py_agent.utils import listen_for

@listen_for({'event_type': ('in', ['owm_onecall', 'brighton_busses'])})
def dashboard(event=None, handler=None):
    dashboard_file = os.environ.get('PY_AGENT_DASHBOARD_FILE')
    if not dashboard_file:
        dashboard_file = os.path.join(os.path.dirname(__file__), 'dashboard.json')

    current_data = {}
    try:
        with open(dashboard_file, 'r') as f:
            current_data = json.load(f)
    except:
        current_data = {}

    if event.event_type == 'owm_onecall':
        current_data['weather'] = update_weather(event)
    elif event.event_type == 'brighton_busses':
        current_data['bus_schedule'] = event.data

    with open(dashboard_file, 'w') as f:
        json.dump(current_data, f, indent=2)

def update_weather(event):
    data = event.data
    hourly = data['hourly']

    weather = {}
    for h in hourly:
        from_now = hours_from_now(h)
        if 0 < from_now < 9:
            weather[from_now] = analyze_conditions(h)

    weather['now'] = analyze_conditions(data['current'])
    return weather

def hours_from_now(hourly):
    dt = datetime.datetime.fromtimestamp(hourly['dt'])
    diff = dt - datetime.datetime.now()
    return int(diff.total_seconds() // 3600)

def analyze_conditions(data):
    profile = {
        'legs': {
            'light': (65, 1000),
            'medium': (35, 65),
            'heavy': (-200, 65)
        },
        'jacket': {
            'none': (65, 1000),
            'light': (35, 65),
            'medium': (-200, 35),
        },
        'head': {
            'none': (40, 1000),
            'light': (-200, 40)
        },
        'gloves': {
            'none': (65, 1000),
            'light': (50, 65),
            'heavy': (-200, 50)
        },
        'shoes': {
            'light': (45, 1000),
            'heavy': (-200, 45)
        }
    }

    tempf = k_to_f(data['feels_like'])
    profile = get_temp_profile(tempf, profile)

    is_rain = False
    for w in data['weather']:
        if w['main'].lower() in ['thunderstorm', 'drizzle', 'rain']:
            is_rain = True
            break

    if is_rain:
        profile['rain_gear'] = 'light'
        profile['shoes'] = 'heavy'
    else:
        profile['rain_gear'] = 'none'

    return {
        'profile': profile,
        'temperature': tempf,
        'weather': data['weather'],
        'humidity': data['humidity']
    }

def get_temp_profile(temp, profile):
    prof = {}
    for item in profile:
        for weight in profile[item]:
            low, high = profile[item][weight]
            if low < temp <= high:
                prof[item] = weight

    return prof

def k_to_f(kelvin):
    return int(kelvin * 1.8 - 459.67)


# 57 - Altere o programa anterior para mostrar no final a soma dos números.

num1 = int(input('Informe o primeiro número inteiro: '))
num2 = num1
soma = 0
while num2 <= num1:
    num2 = int(input('Informe o segundo número inteiro: '))
    if num2 <= num1:
        print('Nùmero inválido! Deve ser maior que o primeiro número!')

for i in range(num1, num2 + 1):
    soma += i
    print(i, end = ' ')

print(f'A soma dos números é {soma}')


import json
from climate_model.constants import OTHER_CONSTANTS
minutos = 60
PARAMS_ENV = {'STEP': minutos/(24*60), \
              'TIME_MAX': 90, #Dias simulados\
              'FRECUENCY': 60,
              'SEASON':1, \
              'MINUTOS':minutos}

#Para entrenamiento SEASON puede ser 1,2 o 'RANDOM'
#Para benchmark y tournament es recomendable que sea 'RANDOM', pero no absolutamente necesario.

#El min de STEP  no es 1/24, pero el min de FRECUENCY SÍ es 60
PARAMS_TRAIN = {'EPISODES': 1000, \
                'STEPS': int(PARAMS_ENV['TIME_MAX']/PARAMS_ENV['STEP']), \
                'BATCH_SIZE': 128, \
                'SHOW': False, \
                'SERVER':True, \
                'INDICE': 0, # Se usa en la simulacion al terminar el entrenamiento
                'SAVE_FREQ': 200
                }

PARAMS_SIM = {'anio':2017,\
            'mes':8,\
            'dia': 15,\
            'hora': 1
            }

CONTROLS = {'u_1': 1,
            'u_2': 1,
            'u_3': 1,
            'u_4': 0,
            'u_5': 0, #No hace nada por los parametros
            'u_6': 0,
            'u_7': 1,
            'u_8': 1,
            'u_9': 1,
            'u_10': 1,
            'u_11': 1,
            }
f = open ('ddpg/parametros.json', "r")
parametros_ddpg = json.loads(f.read())

all_params = {
            'PARAMS_DDPG':parametros_ddpg['PARAMS_DDPG'], \
            'PARAMS_UTILS':parametros_ddpg['PARAMS_UTILS'], \
            'PARAMS_ENV': PARAMS_ENV, \
            'PARAMS_TRAIN':PARAMS_TRAIN,\
            'PARAMS_SIM':PARAMS_SIM, \
            'MODEL_NOISE':OTHER_CONSTANTS['model_noise'].val,\
            'CONTROLS': CONTROLS}

def save_params(dicc,path):
    with open(path + '/parametros.json', 'w') as outfile:
        json.dump(dicc, outfile,indent=4)


print('Apple EFI Package Extractor v2.0_Linux_a1')

import os
import sys
import zlib
import shutil
import subprocess

if len(sys.argv) >= 2 :
	pkg = sys.argv[1:]
else :
	pkg = []
	in_path = input('\nEnter the full folder path: ')
	print('\nWorking...')
	for root, dirs, files in os.walk(in_path):
		for name in files :
			pkg.append(os.path.join(root, name))

final_path = os.path.join(os.getcwd(), 'AppleEFI')
if os.path.exists(final_path) : shutil.rmtree(final_path)

for input_file in pkg :
	file_path = os.path.abspath(input_file)
	file_name = os.path.basename(input_file)
	file_dir = os.path.dirname(file_path)
	file_ext = os.path.splitext(file_path)[1]

	print('\nFile: %s\n' % file_name)

	with open(input_file, 'rb') as in_buff : file_adler = zlib.adler32(in_buff.read()) & 0xFFFFFFFF

	pkg_payload = os.path.join(final_path, '%s_%0.8X' % (file_name, file_adler))
	pkg_temp = os.path.join(final_path, '__TEMP_%s_%0.8X' % (file_name, file_adler))
	os.makedirs(pkg_temp)

	subprocess.run(['bsdtar', '-xf', file_path, '-C', pkg_temp], check = True)
	#subprocess.run(['7z', 'x', '-aou', '-bso0', '-bse0', '-bsp0', '-o' + pkg_temp, file_path])

	if os.path.isfile(os.path.join(pkg_temp, 'Scripts')) :
		scripts_init = os.path.join(pkg_temp, 'Scripts')
		scripts_cpgz = os.path.join(pkg_temp, 'Scripts.gz')
		efi_path = os.path.join(pkg_temp, 'Tools', 'EFIPayloads', '')

		os.replace(scripts_init, scripts_cpgz)

		os.system('gunzip -k -q %s' % scripts_cpgz)

		os.system('(cd %s && cpio --quiet -id < %s)' % (pkg_temp, scripts_init))

		shutil.copytree(efi_path, pkg_payload)

	elif os.path.isfile(os.path.join(pkg_temp, 'Payload')) :
		payload_init = os.path.join(pkg_temp, 'Payload')
		payload_pbzx = os.path.join(pkg_temp, 'Payload.pbzx')
		payload_cpio = os.path.join(pkg_temp, 'Payload.cpio')
		zip_path = os.path.join(pkg_temp, 'usr', 'standalone', 'firmware', 'bridgeOSCustomer.bundle', 'Contents', 'Resources', 'UpdateBundle')
		efi_path = os.path.join(zip_path, 'boot', 'Firmware', 'MacEFI', '')

		os.replace(payload_init, payload_pbzx)

		subprocess.run(['python', 'parse_pbzx_fix.py', payload_pbzx, payload_cpio], check = True, stdout=subprocess.DEVNULL)

		os.system('(cd %s && cpio --quiet -id < %s)' % (pkg_temp, payload_cpio))

		shutil.unpack_archive(zip_path + '.zip', zip_path)

		if os.path.exists(efi_path) : shutil.copytree(efi_path, pkg_payload)

	shutil.rmtree(pkg_temp)

	im4p_files = []
	for root, dirs, files in os.walk(pkg_payload):
		for name in files :
			if name.endswith('.im4p') :
				im4p_files.append(os.path.join(root, name))

	if im4p_files : subprocess.run(['python', 'Apple_EFI_Split.py', '-skip', *im4p_files], check = True, stdout=subprocess.DEVNULL)
	for im4p in im4p_files : os.remove(im4p)

	final_files = []
	for root, dirs, files in os.walk(pkg_payload):
		for name in files :
			final_files.append(os.path.join(root, name))

	if final_files : subprocess.run(['python', 'Apple_EFI_Rename.py', '-skip', *final_files], check = True, stdout=subprocess.DEVNULL)

	for root, dirs, files in os.walk(pkg_payload):
		for name in files :
			if not os.path.isfile(os.path.join(final_path, name)) :
				shutil.copy2(os.path.join(root, name), os.path.join(final_path, name))

	shutil.rmtree(pkg_payload)

print('\nDone!')

Apple EFI Package Extractor/Linux_Pre-Alpha/Apple_EFI_Package.py


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

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if not head or k == 0:
            return head

        length = 1
        current = head
        while current.next:
            length += 1
            current = current.next

        k %= length

        if k == 0:
            return head

        new_head_index = length - k

        current.next = head

        current = head
        for _ in range(new_head_index - 1):
            current = current.next

        new_head = current.next
        current.next = None

        return new_head


import json
import numpy as np
from tree import head_to_tree, tree_to_adj

filename="/data0/zhanglonghui/HATT-Proto/data/fewrel/val_fewrel_stf.json"
save_adj_path="/data0/zhanglonghui/HATT-Proto/data/fewrel/val_fewrel_adj.npy"
maxlen=47
prune=-1 #保留距离最短依存路径为prune以内的路径，-1表示保留所有路径

with open(filename) as infile:
    ori_data = json.load(infile)

def inputs_to_tree_reps(head, l, prune, subj_pos, obj_pos):
    trees = [head_to_tree(head[i], l[i], prune, subj_pos[i], obj_pos[i]) for i in range(len(l))]
    adj = [tree_to_adj(maxlen, tree, directed=False, self_loop=False).reshape(1, maxlen, maxlen) for tree in trees]
    adj = np.concatenate(adj, axis=0)
    return adj

def get_positions(start_idx, end_idx, length):
    """ Get subj/obj position sequence. """
    return list(range(-start_idx, 0)) + [0]*(end_idx - start_idx + 1) + \
            list(range(1, length-end_idx))

head, l ,subj_pos, obj_pos=[],[],[],[]
for relation in ori_data:
        for ex in ori_data[relation]:  # 遍历某个关系中的样本
            head.append([int(i) for i in ex["stanford_head"]]+[0]*(maxlen-len(ex["stanford_head"])))
            l.append(len(ex["tokens"]))
            subj_pos.append(get_positions(ex['t'][2][0][0],ex['t'][2][0][-1],len(ex["tokens"]))+[0]*(maxlen-len(ex["stanford_head"])))
            obj_pos.append(get_positions(ex['h'][2][0][0],ex['h'][2][0][-1], len(ex["tokens"])) + [0] * (maxlen - len(ex["stanford_head"])))

head, l ,subj_pos, obj_pos=[np.array(i) for i in [head, l ,subj_pos, obj_pos]]
adj = inputs_to_tree_reps(head, l, prune, subj_pos, obj_pos)

np.save(save_adj_path, adj)


#!/usr/bin/env python
# -*- coding: GB2312 -*-
# Last modified:

"""docstring
"""

__revision__ = '0.1'

import sys
from log import *
from enrollment import *
from Object import *
from label import *
from weekend import *
from common import *
week = Week()
enrollment = Enrollment("../data/merge/enrollment.csv")
obj = Obj()
label = Label()
fout = open("conf/course.time.info","w")

count = {}
for line in open("../data/merge/log.csv"):
    #enrollment_id,username,course_id,time,source,event,object
    id,time,source,event,object = line.strip().split(",")
    username, course_id = enrollment.enrollment_info.get(id)
    if time.find("T") < 0:
        continue
    times = week.times(time)
    if course_id not in count:
        count[course_id] = []
    else:
        count[course_id].append(times)

for (k,v) in count.items():
    v = sorted(v)
    buf = []
    for i in range(CIDX_VEC_NUM):
        buf.append(v[i * len(v) / CIDX_VEC_NUM])
    fout.write("%s\t%s\n" % (k, ",".join(["%s" % k for k in buf])))


from math import log10
from math import sin
from math import fabs
TolX = 1e-12
TolCon = 1e-12

x1 = .2
x2 = 1

#y = lambda x: x + log10(x) - (sin(x) * sin(x))
y = lambda x: (x * x) + log10(x) - sin(x)

fx1 = y(x1)
fx2 = y(x2)
a = x1
b = x2

iter = 0

if (fx1 * fx2) > 0:
    print('Założenie nie spełnione\n')
else:
    while fabs(a - b) > TolX:
        a = b
        b = x1 - fx1 * (x2 - x1) / (fx2 - fx1)
        f0 = y(b)
        iter += 1

        if fabs(f0) < TolCon:
            break
        elif fx1 * f0 < 0:
            x2 = b
            fx2 = f0
    else:
        x1 = b
        fx1 = f0
print('Wynik: ', b, '\nLiczba iteracji: ', iter, '\n')
input()


#!/usr/bin/env python
#-*- encoding: utf8 -*-

from __future__ import print_function
from collections import namedtuple
from pyelftools.elftools.elf.elffile import ELFFile as ELFFile_
from pyelftools.elftools.elf.sections import Section
from pyelftools.elftools.common.py3compat import bytes2str

Func = namedtuple("Func", ["name", "mangled_name", "offset", "size"])

if __package__ is None:
    from os import sys, path
    sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

class ELFFileError(Exception):
    def __init__(self, what):
        Exception.__init__(self, what)

class ELFFile(ELFFile_):
    def __init__(self, filepath):
        self._fd = open(filepath, 'rb')
        ELFFile_.__init__(self, self._fd)
        self._symbol_interval_tree = self._build_symbol_tree()

    def __del__(self):
        self._fd.close()

    def get_offset_for_virtual_address(self, vaddr):
        correct_segment = None
        for segment in self.iter_segments():
            if (segment['p_vaddr'] >= vaddr and
                vaddr < segment['p_vaddr'] + segment['p_filesz']):
                correct_segment = segment
                break

        if correct_segment is not None:
            correct_section = None
            for section in self.iter_sections():
                if correct_segment.section_in_segment(section):
                    correct_section = section
                    break
            if correct_section is not None:
                offset_in_segment = vaddr - correct_segment['p_vaddr']
                return correct_section['sh_offset'] + offset_in_segment
            else:
                return None
        else:
            return None

    def get_virtual_address_for_offset(self, offset):
        section = self.get_section_by_offset(offset)
        offset_from_section_start = offset - section['sh_offset']
        section_vaddr = self.get_section_virtual_address(section)
        return section_vaddr + offset_from_section_start

    def get_section_by_offset(self, offset):
        for section in self.iter_sections():
            if offset >= section['sh_offset'] and offset < section['sh_offset'] + section['sh_size']:
                return section
        return None

    def get_segment_by_virtual_address(self, vaddr):
        for segment in self.iter_segments():
            if segment['p_vaddr'] >= vaddr and vaddr < segment['p_vaddr'] + segment['p_filesz']:
                return segment
        return None

    def get_section_by_virtual_address(self, vaddr):
        segment = self.get_segment_by_virtual_address(vaddr)
        for section in self.iter_section():
            if segment.section_in_segment(section):
                return section
        return None

    def get_section_virtual_address(self, section):
        assert isinstance(section, Section)
        matching_segments = []
        for segment in self.iter_segments():
            if segment.section_in_segment(section):
                matching_segments.append(segment)
        return max(matching_segments, key=lambda s: s['p_filesz'])

    def get_symbol_by_name(self, name):
        for symbol in self.iter_symbols():
            if symbol.name == name:
                return symbol
        # if we reached this point, then there is no such symbol in the file
        raise ELFFileError("The file has no %s symbol" % name)

    def get_function_by_mangled_name(self, mangled_name):
        for func in self.iter_functions():
            if func.mangled_name == mangled_name:
                return func
        raise ELFFileError("There is no function with mangled name %s" % mangled_name)

    def get_function_by_name(self, name):
        for func in self.iter_functions():
            if func.name == name:
                return func
        raise ELFFileError("There is no %s function" % name)

    def iter_symbols(self):
        sym_name = b".symtab"
        symtab = self.get_section_by_name(sym_name)
        if symtab is None:
            raise ELFFileError('The file has no %s section' % bytes2str(sym_name))
        return symtab.iter_symbols()

    def iter_functions(self):
        symtab = self._get_symbol_table()
        return self._iter_func(symtab.iter_symbols())

    def get_symbol_text(self, name):
        if name not in self.get_symbol_names():
            raise ELFFileError('The file has no % symbol' % name)
        text_section_offset = self._get_text_offset()
        symbol = self.get_symbol_by_name(name)
        print(symbol.name)

        # rewind the file to be at the symbol position
        symbol_size = symbol.entry["st_size"]
        symbol_offset = symbol.entry["st_value"]

        # remember the current position in order to go back afterwards
        cur = self._fd.tell()
        self._fd.seek(symbol_offset-text_section_offset)
        text = self._fd.read(symbol_size)
        self._fd.seek(cur)
        return text

    def get_function_names(self):
    # print('Processing file:', filename)
        sym_name = b'.symtab'
        symtab = self.get_section_by_name(sym_name)
        if symtab is None:
            raise ELFFileError('The file has no %s section' % bytes2str(sym_name))
        return [func.name for func in self._iter_func(symtab.iter_symbols())]

    def get_symbol_names(self):
        return [symbol.name for symbol in self.iter_symbols()]

    def get_section_names(self):
        return [symbol.name for symbol in self.iter_sections()]

    def get_text(self):
        text = self.get_section_by_name('.text')

        if text is None:
            raise ELFFileError("ELF file has no .text section")
        return text.data()

    def get_section_by_offset(self, offset):
        for section in  self.iter_sections():
            if self._offset_inside_section(offset, section):
                return section
        raise ELFFileError("There is no section that contains given offset: %d" % offset)

    def get_symbol_by_offset(self, offset):
        """
        Figures out name of the symbol for the given offset.

        offset -- a number expressing the offset with substracted base

        Returns None or a string. None is returned when there is no corresponding symbol
        (e.g. the binary is stripped or the offset is malformed)
        Otherwise returns demangled symbol name
        """
        symbol = self._symbol_interval_tree.search(offset) # returns a set
        if not symbol: #an empty set
            return None
        else:
            #assert len(symbol) == 1
            return list(symbol)[0].data # sets are not indexed, must be converted to a list

    def get_text_by_offset(self, offset):
        pass

### private functions ###

    def _get_text_offset(self):
        text_section = self.get_section_by_name('.text')
        for segment in self.iter_segments():
            if segment.section_in_segment(text_section):
                return segment['p_paddr']
        raise ELFFileError("Section .text is not present in the ELF file")

    def _get_symbol_table(self):
        sym_name = b'.symtab'
        symtab = self.get_section_by_name(sym_name)
        return symtab

    def _iter_func(self, symbols_iter=None):
        from ..demangle import demangle
        if symbols_iter is None:
            symtab = self._get_symbol_table()
            symbols_iter = symtab.iter_symbols()

        for sym in symbols_iter:
            if sym.entry['st_info']['type'] == 'STT_FUNC':
                demangled_name = demangle.cplus_demangle(sym.name, 1)
                if demangled_name is not None:
                    name = demangled_name
                else:
                    name = sym.name
                yield Func(name=name,
                           mangled_name=sym.name,
                           offset=sym.entry['st_value'],
                           size=sym.entry['st_size'])
        raise StopIteration()

    def _offset_inside_section(self, offset, section):
        sh = section.header
        return (offset >= sh['sh_offset']) and (offset < sh['sh_offset']+sh['sh_size'])

    def _build_symbol_tree(self):
        from intervaltree import IntervalTree, Interval
        base = self._get_binary_base()
        intervals = [Interval(func.offset-base, func.offset+func.size-base, func.name) for func in self._iter_func() if func.size != 0]
        return IntervalTree(intervals)

    def _get_binary_base(self):
        for segment in self.iter_segments():
            if segment['p_type'] == "PT_LOAD" and segment['p_offset'] == 0:
                return segment['p_vaddr']
        raise ELFFileError("Can't find base for the .text segment")

  #  def _offset_inside_segment(self, offset, segment):
  #      sh = segment.header
  #      return (offset >= sh['p_paddr']) and (offset < sh['p_paddr']+sh['p_filesz'])

if __name__ == "__main__":
    elf = ELFFile("/afs/cern.ch/user/p/paszoste/cp/simple-binary/a.out")
    print(elf.get_symbol_by_offset(int('731', 16)))
    print(elf.get_symbol_by_offset(int('982', 16)))
    print(elf.get_symbol_by_offset(int('9cd', 16)))
    print(elf.get_symbol_by_offset(int('9f5', 16)))
    print(elf.get_symbol_by_offset(int('a42', 16)))
    # for symbol in elf.iter_symbols():
    #     print(symbol.name, symbol.entry["st_size"], symbol.entry["st_value"])
    textsec = elf.get_section_by_name('.text')
    # print("SECTION")
    # for sec in elf.iter_sections():
    #     print(sec.header)
    #     if sec['sh_type'] == 'SHT_STRTAB':
    #         print(sec.get_string(49))
    # print("SEGMENT")
    # for seg in elf.iter_segments():
    #     print(seg.section_in_segment(textsec), seg.header)
    # print(elf._get_text_offset())
    text = elf.get_symbol_text("main")
    main_file = open("main_text", "wb")
    for segment in elf.iter_sections():
        print(segment.header)
    main_file.write(text)
    print(len(text))
    import xed
    inst_list = xed.disassemble(1, text, len(text))
    print(len(inst_list))
    for inst in inst_list:
        print(inst)
        print(xed.xed_decoded_inst_dump_intel_format(inst, 1024, 0))


""" Tests for custom django template tags. """
from __future__ import absolute_import

import unittest

import ddt
import six

from django.template import Context, Template

@ddt.ddt
class OAExtrasTests(unittest.TestCase):
    """ Tests for custom django template tags oa_extras. """

    template = Template(
        u"{% load oa_extras %}"
        u"{{ text|link_and_linebreak }}"
    )

    @ddt.data(
        ("", ""),
        ('check this https://dummy-url.com', 'https://dummy-url.com'),
        ('Visit this URL http://dummy-url.com', 'http://dummy-url.com'),
        ('dummy-text http://dummy-url.org', 'http://dummy-url.org'),
        ('dummy-url.com dummy-text', 'dummy-url.com')
    )
    @ddt.unpack
    def test_link_and_linebreak(self, text, link_text):
        rendered_template = self.template.render(Context({'text': text}))
        self.assertIn(link_text, rendered_template)
        if text:
            six.assertRegex(
                self,
                rendered_template,
                r'{link_text}'.format(link_text=link_text),
            )

    @ddt.data(
        ("hello ", "script"),
        ("http://dummy-url.com <applet></applet>", "applet"),
        ("", "iframe"),
        ("<link></link>", "link"),
    )
    @ddt.unpack
    def test_html_tags(self, text, tag):
        rendered_template = self.template.render(Context({'text': text}))
        escaped_tag = "<{tag}>".format(tag=tag)
        self.assertIn(escaped_tag, rendered_template)


#while (沒有限定次數迴圈)

# total = n = 0

# while n < 10:
#     n+=1
#     total+=n
#     print(total,end =" ")

total = person = source = 0

while (source != -1):
    person += 1
    total += source
    source = eval(input("請輸入第 %d 位學生的成績:"%(person)))
average = total / (source-1)
print("本班總成績: %d 分，平均成績:%5.2f分"%(total,average))


from django.shortcuts import render, redirect
from django.http import HttpResponse
from django import forms
from random import randint
from . import util

def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })

def get_title(request, title):
    """
    Get content of the encyclopedia entry per requierment 2
    """
    return render(request, "encyclopedia/title.html", {
        "title_name": title,
        "title": util.get_entry(title)
    })

class EditForm(forms.Form):
    """
    Define form used to edit entries per requierment 16 of readme
    """
    content = forms.CharField(widget=forms.Textarea,
                              max_length=1000, required=True)

def edit(request, title_name):
    """
    Edit the contents of a title per requierments 16 of readme
    """
    # Listen for a POST request and process data when detected
    if request.method == "POST":
        form = EditForm(request.POST)
        if form.is_valid():
            edited_content = form.cleaned_data['content']
            util.save_entry(title_name, edited_content)
            return redirect(f"/wiki/{ title_name }")

    # get content from file
    else:
        content = util.get_entry(title_name, return_html=False)
        return render(request, "encyclopedia/edit.html", {
            "form": EditForm({"content": content}),
            "title_name": title_name
            })

class NewEntryForm(forms.Form):
    """
    Define form used to create new entry per requierment 11 of readme
    """
    title = forms.CharField(max_length=50, required=True)
    content = forms.CharField(widget=forms.Textarea,
                              max_length=1000, required=True)

def new(request):
    """
    Handle a new entry per requierment 11 of readme
    """
    # Listen for a POST request and process data when detected
    if request.method == "POST":
        form = NewEntryForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']

            # standardize content with title header
            content = f"#{title}\n\n{content}"
            # standardize title for file name
            title = util.clean_title(form.cleaned_data['title'])

            # If the title is unique per requierment 14 in readme
            if title not in util.list_entries():
                util.save_entry(title, content)
                # open newly created page per requierment 15 of readme
                return get_title(request, title)
            else:
                return render(request, "encyclopedia/error.html")

    # Else create a blank form
    else:
        return render(request, "encyclopedia/new.html", {
            "form": NewEntryForm()
            })

def random(request):
    """
    Get a random page per requierment 20 of readme
    """
    entries = util.list_entries()
    n = randint(0, len(entries)-1)
    target_entry = entries[n]
    return redirect(f"/wiki/{ target_entry }")

def search(request):
    """
    Get contents of the search per requierments 7, 8, 9, 10
    """
    query = request.GET.get('q')
    results = util.search(query)

    # if an exact match open that entry
    if len(results) == 1:
        return redirect(f"/wiki/{ results[0] }")

    # if multiple partial matches open list of options
    elif len(results) > 1:
        return render(request, "encyclopedia/search_results.html", {
            "entries": results
        })
    # else prompt user for new input
    else:
        return render(request, "encyclopedia/search_empty.html")


import os
import music21 as m21
import torch
import torch.nn as nn
from collections import defaultdict
from torch.nn.utils.rnn import pad_sequence
import random
import numpy as np

def tokenize_score(score, time_step=0.25):
    tokens = []
    for part in score.parts:
        part_stream = part.flatten().notesAndRests
        prev_event = None
        symbol = -1  # Default value for 'symbol' variable

        for event in part_stream:
            if isinstance(event, m21.note.Note):
                symbol = event.pitch.midi
            elif isinstance(event, m21.note.Rest):
                symbol = -1  # Use -1 to represent rests

            steps = int(event.duration.quarterLength / time_step)
            tokens.extend([symbol] + [-1] * (steps - 1))

    return tokens

def load_tokenized_songs(dataset_path, segment_length=100, step_size=10, max_neg_ones=30):
    songs = []
    for path, _, files in os.walk(dataset_path):
        for file in files:
            song = m21.converter.parse(os.path.join(path, file))
            tokens = tokenize_score(song)
            num_segments = (len(tokens) - segment_length) // step_size + 1
            for i in range(num_segments):
                segment = tokens[i * step_size : i * step_size + segment_length]
                if segment.count(-1) <= max_neg_ones:
                    songs.append(segment)
    return songs

def process_files(folder_path):
    element_dict = defaultdict(int)

    files = os.listdir(folder_path)
    files.sort()

    for file in files:
        file_path = os.path.join(folder_path, file)

        with open(file_path, 'r') as f:
            content = f.read().split()

        for element in content:
            element_dict[element] += 1

    element_mapping = {element: i for i, element in enumerate(element_dict.keys())}
    reversed_mapping = {v: k for k, v in element_mapping.items()}  # Reverse the mapping

    return reversed_mapping

def one_hot_encoding(folder_path, element_mapping, desired_length):
    encoded_tensors = []  # List to store encoded tensors

    files = os.listdir(folder_path)
    files.sort()

    all_elements = set(element_mapping.values())

    for file in files:
        file_path = os.path.join(folder_path, file)

        with open(file_path, 'r') as f:
            content = f.read().split()

        encoded_content = [element_mapping.get(element, 0) for element in content]
        encoded_content = torch.tensor(encoded_content)  # Convert to PyTorch tensor

        # Pad or truncate the tensor to the desired length
        if len(encoded_content) < desired_length:
            padded_content = nn.functional.pad(encoded_content, (0, desired_length - len(encoded_content)))
        else:
            padded_content = encoded_content[:desired_length]

        encoded_tensors.append(padded_content)

    # Pad sequences to the same length
    padded_sequences = pad_sequence(encoded_tensors, batch_first=True)

    encoded_arrays = []
    for tensor, file in zip(padded_sequences, files):

        # Convert tensor to numpy array
        onehot_encoded = np.zeros((tensor.size(0), len(all_elements)))
        for i, element in enumerate(tensor):
            if element.item() in element_mapping.values():
                onehot_encoded[i, element.item()] = 1

        encoded_arrays.append((onehot_encoded, file))

    return encoded_arrays


import numpy as np
import torch
from torch.utils.data import TensorDataset, DataLoader
import torch.nn
import torch.nn.functional
import torch.optim
from torchvision import models #just for debugging
from  sklearn.model_selection import train_test_split, StratifiedShuffleSplit, ShuffleSplit, StratifiedKFold, KFold # KFold is added by Atif
from sklearn.utils import shuffle

def to_categorical(y, num_classes):
    """ 1-hot encodes a tensor """
    return np.eye(num_classes, dtype='uint8')[y]

# manaul training
def train_one_epoch(training_loader, validation_loader,
                    num_training_samples, num_validation_samples,
                    input_shape=(351, 246, 3), num_classes=100,
                    model_parameters = {'model_type': "Encoder+Classifier",
                                     'model': None, 'num_filters': 8,
                                     'optimizer': None,
                                     'loss_fn': torch.nn.CrossEntropyLoss(),
                                     'loss_fn2': torch.nn.MSELoss(),
                                     'lambda1': 0.5, 'lambda2': 0.5},
                    train_device='cuda'):
    num_filters = model_parameters['num_filters']
    model_type = model_parameters['model_type']
    model = model_parameters['model']
    optimizer = model_parameters['optimizer']
    loss_fn = model_parameters['loss_fn']
    loss_fn2 = model_parameters['loss_fn2']
    lambda1 = model_parameters['lambda1']
    lambda2 = model_parameters['lambda1']

    # training metrics
    train_loss = 0
    train_correct = 0

    # validation metrics
    valid_loss = 0
    valid_correct = 0

    # Here, we use enumerate(training_loader) instead of
    # iter(training_loader) so that we can track the batch
    # index and do some intra-epoch reporting
    model.train(True)
    if(model_type=='Classifier'):
      lambda1=1;
      lambda2=0;
      ct = 0
      for child in model.children():
        ct += 1
        if ct == 2: # turn off weigth update for Encoder and Decoder modules
            for param in child.parameters():
                param.requires_grad = True
        else:
            for param in child.parameters():
                param.requires_grad = False
    elif(model_type=='Encoder+Classifier'):
      lambda1=1;
      lambda2=0;
      ct = 0
      for child in model.children():
        ct += 1
        if ct == 3: # turn off weigth update for Decoder module only
            for param in child.parameters():
                param.requires_grad = False
        else:
            for param in child.parameters():
                param.requires_grad = True
    elif(model_type=='AutoEncoder'):
      lambda1=0;
      lambda2=1;
      ct = 0
      for child in model.children():
        ct += 1
        if ct == 2: # turn off weigth update for Classifier module
            for param in child.parameters():
                param.requires_grad = False
        else:
            for param in child.parameters():
                param.requires_grad = True
    elif(model_type=='DeepLSE'): # train full-network
      for child in model.children():
        for param in child.parameters():
            param.requires_grad = True
    else:
      print('Incorrect choice for model configuration')

    for i, data in enumerate(training_loader,0):
        # Every data instance is an input + label pair
        train_input, train_label = data
        # train_input = train_input.unsqueeze(dim=1).float()
        train_label= torch.tensor(to_categorical(y=train_label, num_classes=num_classes)).float()
        # train_label = train_label[:,None]
        if len(train_label.shape)==1:
          train_label = train_label.unsqueeze(dim=0)

        train_input = train_input.to(torch.device(train_device))
        train_label = train_label.to(torch.device(train_device))

        # print('train_input:',train_input.shape, 'train_label:',train_label.shape)

        # Zero your gradients for every batch!
        optimizer.zero_grad()
        # optimizer_classifier.zero_grad()

        # Make predictions for this batch
        # train_features_output = feature_extraction_module(train_input)
        # train_output = classifier_module(train_features_output)
        # train_output = pytorch_model_c1(train_input)
        train_output, decoded_input = model(train_input)

        # print('train_input:',train_input.shape, 'train_label:',train_label.shape, 'train_output:',train_output.shape)
        # print('train_label:',train_label)
        # print('train_output:',train_output)

        # Compute the loss and its gradients
        loss = 2*(lambda1)*loss_fn(train_output, train_label)
        loss = loss + 2*(lambda2)*loss_fn2(train_input, decoded_input)

        loss.backward()

        # Adjust learning weights
        optimizer.step()
        # optimizer_classifier.step()

        # Gather data and report
        train_loss += loss.item()
        for batch_count in range(train_output.shape[0]):
          if(torch.argmax(train_output[batch_count,:]) == torch.argmax(train_label[batch_count,:])):
            train_correct += 1

    # print('training epoch complete')
    # Here, we use enumerate(validation_loader) instead of
    # iter(validation_loader) so that we can track the batch
    # index and do some intra-epoch reporting
    model.train(False)
    for i, data in enumerate(validation_loader,0):
        # Every data instance is an input + label pair
        valid_input, valid_label = data

        # valid_input = valid_input.unsqueeze(dim=1).float()
        valid_label= torch.tensor(to_categorical(y=valid_label, num_classes=num_classes)).float()
        if len(valid_label.shape)==1:
          valid_label = valid_label.unsqueeze(dim=0)

        valid_input = valid_input.to(torch.device(train_device))
        valid_label = valid_label.to(torch.device(train_device))

        # Make predictions for this batch
        valid_output, temp = model(valid_input)

        # print('valid_input:',valid_input.shape, 'valid_label:',valid_label.shape, 'valid_output:',valid_output.shape)

        # Gather data and report
        valid_loss += loss_fn(valid_output, valid_label).item() + loss_fn2(valid_input, temp).item()
        for batch_count in range(valid_output.shape[0]):
          if(torch.argmax(valid_output[batch_count,:]) == torch.argmax(valid_label[batch_count,:])):
            valid_correct += 1

    training_accuracy = 100*train_correct/num_training_samples
    validation_accuracy = 100*valid_correct/num_validation_samples

    return train_loss, training_accuracy, valid_loss, validation_accuracy

def train_epochs(X_train, y_train, X_test, y_test,
                 model_parameters = {'model_type': "Encoder+Classifier",
                                     'model': None, 'num_filters': 8,
                                     'optimizer': None,
                                     'loss_fn': torch.nn.CrossEntropyLoss(),
                                     'loss_fn2': torch.nn.MSELoss(),
                                     'lambda1': 0.5, 'lambda2': 0.5},
                 max_state = {'ntrails': 0, 'kfolds': 0, 'epochs': 1},
                 current_state = {'trail': 0, 'fold': 0, 'epoch': 1},
                 best_state = {'training_loss': 0, 'training_accuracy': 0,
                               'validation_loss': 0,'validation_accuracy': 0,
                               'trail': 0, 'fold': 0, 'epoch': 0},
                 early_stop_thresh = 5, train_device='cuda',
                 resume_from=None, results=[]):

  model = model_parameters['model']
  optimizer = model_parameters['optimizer']

  kfolds = max_state['kfolds']
  epochs = max_state['epochs']

  best_validation_accuracy = best_state['validation_accuracy']
  best_validation_index = (best_state['trail']-1)*kfolds*epochs + \
                     (best_state['fold']-1)*epochs + (best_state['epoch']-1)

  trail = current_state['trail']
  fold = current_state['fold']
  epoch = current_state['epoch']

  #resume
  if not resume_from == None:
      resume_checkpoint = torch.load(resume_from)
      trail = resume_checkpoint['trail']
      fold = resume_checkpoint['fold']
      epoch = resume_checkpoint['epoch']
      best_state = resume_checkpoint['best_validation_accuracy']
      best_validation_accuracy = best_state['validation_accuracy']
      # load model and optimizer
      model.load_state_dict(checkpoint['model'])
      optimizer.load_state_dict(checkpoint['optimizer'])

  #data
  training_loader = DataLoader(TensorDataset(torch.tensor(X_train), torch.tensor(y_train)), batch_size=100, shuffle=True)
  validation_loader = DataLoader(TensorDataset(torch.tensor(X_test), torch.tensor(y_test)), batch_size=1)
  # added by Atif

  training_samples = training_loader.dataset.tensors[0]
  training_targets = training_loader.dataset.tensors[1]

  input_shape=(training_samples.shape[2], training_samples.shape[3], training_samples.shape[1])
  num_classes=np.unique(training_targets).shape[0]

  num_training_samples = len(training_loader.dataset)
  num_validation_samples = len(validation_loader.dataset)

  # For k fold results
  if trail== 0 and fold==0:
      results = [{'training_loss': 0, 'training_accuracy': 0,
                  'validation_loss': 0,'validation_accuracy': 0,
                  'trail': 0, 'fold': 0, 'epoch': 0}]*epochs

  for epoch in range(epoch, epochs+1):
    print('EPOCH {}/{}:'.format(epoch,epochs))
    training_loss, training_accuracy, validation_loss, validation_accuracy = \
      train_one_epoch(training_loader, validation_loader,
                      num_training_samples, num_validation_samples,
                      input_shape=input_shape, num_classes=num_classes,
                      model_parameters=model_parameters, train_device=train_device)

    current_index = (trail-1)*kfolds*epochs + (fold-1)*epochs + (epoch-1)
    results[current_index] = {'training_loss': training_loss/num_training_samples,
                              'training_accuracy': training_accuracy,
                              'validation_loss': validation_loss/num_validation_samples,
                              'validation_accuracy': validation_accuracy,
                              'trail': trail, 'fold': fold, 'epoch': epoch}

    print(f"Training: \n Training Accuracy: {training_accuracy}%, Average Training Loss: {training_loss/len(training_loader)}")
    print(f"Validation: \n Validation Accuracy: {validation_accuracy}%, Average Validation Loss: {validation_loss/len(validation_loader)}")

    if validation_accuracy > best_validation_accuracy:
        best_validation_accuracy = validation_accuracy
        best_validation_index = current_index
        # Updating best state
        best_state = {'training_loss': training_loss,
                      'training_accuracy': training_accuracy,
                      'validation_loss': validation_loss,
                      'validation_accuracy': validation_accuracy,
                      'trail': trail, 'fold': fold, 'epoch': epoch}
        # creating the best checkpoint and saving it in the file
        best_checkpoint = {
            'model': model.state_dict(),
            'optimizer': optimizer.state_dict(),
            'training_accuracy': training_accuracy,
            'validation_accuracy': validation_accuracy,
            'trail': trail,
            'fold': fold,
            'epoch': epoch,
            'best_state': best_state,
            'results': results,
        }
        torch.save(best_checkpoint, "best_checkpoint.pth")

    # creating the latest checkpoint and saving it in the file
    latest_checkpoint = {
        'model': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'training_accuracy': training_accuracy,
        'validation_accuracy': validation_accuracy,
        'trail': trail,
        'fold': fold,
        'epoch': epoch,
        'best_state': best_state,
        'results': results,
    }
    torch.save(latest_checkpoint, "latest_checkpoint.pth")

    if current_index - best_validation_index >= early_stop_thresh:
        print(f"Early stopped training at state (trail, fold, epoch) = ({trail}, {fold}, {epoch})")
        print(f"The best vaidation accuarcy was {best_state['validation_accuracy']} at state (trail, fold, epoch) = ({best_state['trail']}, {best_state['fold']}, {best_state['epoch']})")
        break  # terminate the training loop

  print(f'Results of Trail {trail}, Fold {fold} and Epoch {epoch}: {results}')
  return best_state

def reset_weights(m):
  '''
    Try resetting model weights to avoid
    weight leakage.
  '''
  for layer in m.children():
   if hasattr(layer, 'reset_parameters'):
    print(f'Reset trainable parameters of layer = {layer}')
    layer.reset_parameters()

def train_folds(ear_images, sub_labels,
                model_parameters = {'model_type': "Encoder+Classifier",
                                     'model': None, 'num_filters': 8,
                                     'optimizer': None,
                                     'loss_fn': torch.nn.CrossEntropyLoss(),
                                     'loss_fn2': torch.nn.MSELoss(),
                                     'lambda1': 0.5, 'lambda2': 0.5},
                 max_state = {'ntrails': 0, 'kfolds': 0, 'epochs': 1},
                 current_state = {'trail': 0, 'fold': 1, 'epoch': 1},
                 best_state = {'training_loss': 0, 'training_accuracy': 0,
                               'validation_loss': 0,'validation_accuracy': 0,
                               'trail': 0, 'fold': 0, 'epoch': 0},
                 early_stop_thresh = 5, train_device='cuda',
                 resume_from=None, results=[]):

    model = model_parameters['model']
    optimizer = model_parameters['optimizer']
    k_folds = max_state['kfolds']
    epochs_per_fold = max_state['epochs']

    #best_validation_accuracy = best_state['validation_accuracy']
    #best_validation_index = (best_state['trail']-1)*k_folds*epochs_per_fold + \
    #  (best_state['fold']-1)*epochs_per_fold + (best_state['epoch']-1)

    trail = current_state['trail']
    #fold = current_state['fold']
    #epoch = current_state['epoch']

    #resume
    if not resume_from == None:
        resume_checkpoint = torch.load(resume_from)
        current_state = {'trail': resume_checkpoint['trail'],
                         'fold': resume_checkpoint['fold'],
                         'epoch': resume_checkpoint['epoch']}
        best_state = resume_checkpoint['best_state']
        # load model and optimizer
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])

    # Set fixed random number seed
    kfold = StratifiedKFold(n_splits=k_folds, shuffle=True, random_state=42)

    # For k fold results
    if trail== 0:
        results = [{'training_loss': 0, 'training_accuracy': 0,
                     'validation_loss': 0,'validation_accuracy': 0,
                     'trail': 0, 'fold': 0, 'epoch': 0}]*(epochs_per_fold*k_folds)
        # results = np.zeros(k_folds)

    # Print k-fold results
    print(f'K-FOLD CROSS VALIDATION RESULTS FOR {k_folds} FOLDS')
    print('--------------------------------')
    sum = 0.0
    # K-fold Cross Validation model evaluation
    print(f'Current State: {current_state}')
    for fold, (train_ids, test_ids) in enumerate(kfold.split(ear_images, sub_labels)):

        # Print
        print(f'FOLD {fold+1}')
        print('--------------------------------')

        X_train = ear_images[train_ids, :, :, :]
        y_train = sub_labels[train_ids]
        X_test = ear_images[test_ids, :, :, : ]
        y_test = sub_labels[test_ids]

        print('Training dataset:\n',X_train.shape)
        print(y_train.shape)
        print('Test dataset:\n',X_test.shape)
        print(y_test.shape)

        # Reset model weights before each fold
        model.apply(reset_weights)
        current_state = {'trail': trail, 'fold': fold+1, 'epoch': 1}
        best_state = train_epochs(X_train, y_train, X_test, y_test,
                                  model_parameters=model_parameters,
                                  max_state=max_state,
                                  current_state=current_state,
                                  best_state=best_state,
                                  early_stop_thresh=early_stop_thresh,
                                  train_device=train_device,
                                  resume_from=resume_from, results=results)
        best_validation_accuracy = best_state['validation_accuracy']
        print(f'Fold {fold+1}: {best_validation_accuracy} %')
        sum += best_validation_accuracy

    k_folds_avg_validation_accuracy = sum/k_folds
    print(f'Average: {k_folds_avg_validation_accuracy} %')
    return k_folds_avg_validation_accuracy, best_state

def train_trails(ear_images, sub_labels,
                 model_parameters = {'model_type': "Encoder+Classifier",
                                     'model': None, 'num_filters': 8,
                                     'optimizer': None,
                                     'loss_fn': torch.nn.CrossEntropyLoss(),
                                     'loss_fn2': torch.nn.MSELoss(),
                                     'lambda1': 0.5, 'lambda2': 0.5},
                 max_state = {'ntrails': 0, 'kfolds': 0, 'epochs': 1},
                 current_state = {'trail': 1, 'fold': 1, 'epoch': 1},
                 best_state = {'training_loss': 0, 'training_accuracy': 0,
                               'validation_loss': 0,'validation_accuracy': 0,
                               'trail': 0, 'fold': 0, 'epoch': 0},
                 early_stop_thresh = 5, train_device='cuda',
                 resume_from=None):

    model = model_parameters['model']
    optimizer = model_parameters['optimizer']

    n_trails = max_state['ntrails']
    k_folds = max_state['kfolds']
    epochs_per_fold = max_state['epochs']

    #resume
    if not resume_from == None:
        resume_checkpoint = torch.load(resume_from)
        trail = resume_checkpoint['trail']
        fold = resume_checkpoint['fold']
        epoch = resume_checkpoint['epoch']
        current_state = {'trail': trail, 'fold': fold, 'epoch': epoch}
        best_state = resume_checkpoint['best_state']
        #best_validation_accuracy = best_state['validation_accuracy']
        # load model and optimizer
        model.load_state_dict(checkpoint['model'])
        optimizer.load_state_dict(checkpoint['optimizer'])
    else:
        trail = 1
        #fold = 1
        #epoch = 1
        #best_validation_accuracy = 0

    # For N trail results
    results = [{'training_loss': 0, 'training_accuracy': 0,
                'validation_loss': 0,'validation_accuracy': 0,
                'trail': 0, 'fold': 0, 'epoch': 1}]*(epochs_per_fold*k_folds*n_trails)
    # results = np.zeros((n_trails, k_folds))

    # Print N trail results
    print(f'N-TRAILS CROSS VALIDATION RESULTS FOR {k_folds} FOLDS')
    print('--------------------------------')
    sum = 0.0
    # N-trail Cross Validation model evaluation
    for trail in range(trail, n_trails+1):
        print(f"Trail: {trail}")
        X, y = shuffle(ear_images, sub_labels, random_state=42)
        current_state = {'trail': trail, 'fold': 1, 'epoch': 1}
        k_folds_avg_validation_accuracy, best_state = train_folds(X, y,
                                                                  model_parameters=model_parameters,
                                                                  max_state=max_state,
                                                                  current_state=current_state,
                                                                  best_state=best_state,
                                                                  early_stop_thresh=early_stop_thresh,
                                                                  train_device=train_device,
                                                                  resume_from=resume_from, results=results)

        print(f'Trail {trail}: {k_folds_avg_validation_accuracy} %')
        sum += k_folds_avg_validation_accuracy

    print(f'Average: {sum/n_trails} %')


from .base import DatabaseDownloader
import asyncpg
from fastapi import HTTPException, status

class PostgresDownloader(DatabaseDownloader):

    def __init__(self, connection_data:str, query:str, *args, **kwargs) -> None:
        super().__init__(connection_data, query, *args, **kwargs)

    async def get_data(self):
        """
        Получение данных из Postgresql
        """
        try:
            conn = await asyncpg.connect(self._connection_str)
            result = await conn.fetch(self.query)

            return [dict(row) for row in result]
        except Exception:
            raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f'Не удалось получить данные')


#!/usr/bin/env python3

from app import lambda_handler
import os, boto3, argparse

def mockS3Trigger(bucket, key):
    event = dict(
            {
                "Records": [
                {
                    "s3": {
                    "bucket": {
                        "name": bucket
                    },
                    "object": {
                        "key": key
                    }
                    }
                }
                ]
            }
    )

    return event

parser = argparse.ArgumentParser(description='Process a file inS3 bucket')
parser.add_argument('-f', '--filename', dest='filename', required=True, help="file to process")
parser.add_argument('-b', '--bucket', dest='bucket', required=True, help="S3 bucket name")
args = parser.parse_args()

key = args.filename
bucket = args.bucket

event = mockS3Trigger(bucket, key)

results = lambda_handler(event, None)

print(f'Received: {results}')


import requests
import sett
import json
import time
import webbrowser

def obtener_Mensaje_whatsapp(message):
    if 'type' not in message :
        text = 'mensaje no reconocido'
        return text

    typeMessage = message['type']
    if typeMessage == 'text':
        text = message['text']['body']
    elif typeMessage == 'button':
        text = message['button']['text']
    elif typeMessage == 'interactive' and message['interactive']['type'] == 'list_reply':
        text = message['interactive']['list_reply']['title']
    elif typeMessage == 'interactive' and message['interactive']['type'] == 'button_reply':
        text = message['interactive']['button_reply']['title']
    else:
        text = 'mensaje no procesado'

    return text

def enviar_Mensaje_whatsapp(data):
    try:
        whatsapp_token = sett.whatsapp_token
        whatsapp_url = sett.whatsapp_url
        headers = {'Content-Type': 'application/json',
                   'Authorization': 'Bearer ' + whatsapp_token}
        print("se envia ", data)
        response = requests.post(whatsapp_url,
                                 headers=headers,
                                 data=data)

        if response.status_code == 200:
            return 'mensaje enviado', 200
        else:
            return 'error al enviar mensaje', response.status_code
    except Exception as e:
        return e,403

def text_Message(number,text):
    data = json.dumps(
            {
                "messaging_product": "whatsapp",
                "recipient_type": "individual",
                "to": number,
                "type": "text",
                "text": {
                    "body": text
                }
            }
    )
    return data

"""def send_image_message(number, image_data, caption):

        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + "EAAH5ZCoK0SR0BO4QJyYnUjZC3CclxEkVRiZCY9LOz7ZCtr5OwNwVAgZBjr9v2ejihXQzsc8uqnKjSbRu9y9MtdN0U6Ou01ZBFs3zb4pfntFlvCbTdnDXfuY3MNloSZBBO47AguHbA1xSO2sNsHfYPTdleSmWpbERxETY2YZCYfq6r4CxgHqsa8KZBTGzR2brL3VGZAazhw5H7cnqdVTagqyg3WxkuZAZB7PevJycj4gBFKkZD"
        }

        data = {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "document",
            "document": {
                "link": "",
                "caption": caption,
                "filename": "lodfgo.png"  # Nome do arquivo de imagem
            }
        }

        response = requests.post(whatsapp_url, headers=headers, json=data, files={"file": ("imagem.jpg", image_data)})

        if response.status_code == 200:
            return 'Imagem enviada com sucesso!', 200
        else:
            return 'Erro ao enviar a imagem', response.status_code

        except Exception as e:
            return str(e), 403
"""

def buttonReply_Message(number, options, body, footer, sedd,messageId):
    buttons = []
    for i, option in enumerate(options):
        buttons.append(
            {
                "type": "reply",
                "reply": {
                    "id": sedd + "_btn_" + str(i+1),
                    "title": option
                }
            }
        )

    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "interactive",
            "interactive": {
                "type": "button",
                "body": {
                    "text": body
                },
                "footer": {
                    "text": footer
                },
                "action": {
                    "buttons": buttons
                }
            }
        }
    )
    return data

def listReply_Message(number, options, body, footer, sedd,messageId):
    rows = []
    for i, option in enumerate(options):
        rows.append(
            {
                "id": sedd + "_row_" + str(i+1),
                "title": option,
                "description": ""
            }
        )

    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "interactive",
            "interactive": {
                "type": "list",
                "body": {
                    "text": body
                },
                "footer": {
                    "text": footer
                },
                "action": {
                    "button": "OpÃ§Ãµes",
                    "sections": [
                        {
                            "title": "Secciones",
                            "rows": rows
                        }
                    ]
                }
            }
        }
    )
    return data

def document_Message(number, url, caption, filename):
    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "document",
            "document": {
                "link": url,
                "caption": caption,
                "filename": filename
            }
        }
    )
    return data

def sticker_Message(number, sticker_id):
    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "sticker",
            "sticker": {
                "id": sticker_id
            }
        }
    )
    return data

def get_media_id(media_name , media_type):
    media_id = ""
    if media_type == "sticker":
        media_id = sett.stickers.get(media_name, None)
    elif media_type == "image":
        media_id = sett.images.get(media_name, None)
    #elif media_type == "video":
    #    media_id = sett.videos.get(media_name, None)
    #elif media_type == "audio":
    #    media_id = sett.audio.get(media_name, None)
    return media_id

def replyReaction_Message(number, messageId, emoji):
    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "type": "reaction",
            "reaction": {
                "message_id": messageId,
                "emoji": emoji
            }
        }
    )
    return data

def replyText_Message(number, messageId, text):
    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "recipient_type": "individual",
            "to": number,
            "context": { "message_id": messageId },
            "type": "text",
            "text": {
                "body": text
            }
        }
    )
    return data

def markRead_Message(messageId):
    data = json.dumps(
        {
            "messaging_product": "whatsapp",
            "status": "read",
            "message_id":  messageId
        }
    )
    return data

def administrar_chatbot(text, number, messageId, name):
    text = text.lower() #mensaje que envio el usuario
    list = []
    print("mensaje del usuario: ", text)

    markRead = markRead_Message(messageId)
    list.append(markRead)

    if "hello" in text:

        url_da_sua_imagem = 'https://cdn.glitch.global/4eb4ae5c-8da2-472f-b91c-28d6afe4f15c/WhatsApp%20Image%202023-09-19%20at%2012.30.10.jpg?v=1695151589718'

    # Chame a funÃ§Ã£o para enviar a imagem
        data = json.dumps(
            {
                "messaging_product": "whatsapp",
                "recipient_type": "individual",
                "to": number,
                "type": "image",
                "image": {
                    "link": url_da_sua_imagem,
                    "caption": ""
                }
            }
        )
        enviar_Mensaje_whatsapp(data)

        body = "Welcome to fresh shop ðŸ‘‹! Please choose any option ðŸ‘‡"
        footer = "Fresh Shop Team"
        options = ["Product & Services", "Complain"]
        #options = ["Weitermachen","Datenschutz Lesen"]
        replyButtonData = buttonReply_Message(number, options, body, footer, "sed1",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "go to main page" in text:

        body = "Please choose any option ðŸ‘‡"
        footer = "Fresh Shop Team"
        options = ["Product & Services", "Complain"]
        #options = ["Weitermachen","Datenschutz Lesen"]
        replyButtonData = buttonReply_Message(number, options, body, footer, "sed2",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "complain" in text:

        body = "For requests, send a message to this number +8801875314620"
        footer = "Fresh Shop Team"
        options = ["Finish"]
        #options = ["Weitermachen","Datenschutz Lesen"]
        replyButtonData = buttonReply_Message(number, options, body, footer, "sed3",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "product & services" in text:

        body = "Please choose a Category from smart shop ðŸ‘‡"
        footer = "Fresh Shop Team"
        options = ["Grocery", "Fashion"]
        #options = ["Weitermachen","Datenschutz Lesen"]
        replyButtonData = buttonReply_Message(number, options, body, footer, "sed4",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "grocery" in text:

        body = "Please choose a brand for product catalog ðŸ‘‡"
        footer = "Fresh Shop Team"
        options = ["Fresh Brand", "RFL Brand"]#, "MigraciÃ³n Cloud", "Inteligencia de Negocio"

        """listReplyData = listReply_Message(number, options, body, footer, "sed2",messageId)
        sticker = sticker_Message(number, get_media_id("perro_traje", "sticker"))

        list.append(listReplyData)
        list.append(sticker)"""

        replyButtonData = buttonReply_Message(number, options, body, footer, "sed5",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "fashion" in text:

        body = "Please choose a brand for product catalog ðŸ‘‡"
        footer = "Fresh Shop Team"
        options = ["Fresh Brand", "RFL Brand"]#, "MigraciÃ³n Cloud", "Inteligencia de Negocio"

        """listReplyData = listReply_Message(number, options, body, footer, "sed3",messageId)
        sticker = sticker_Message(number, get_media_id("perro_traje", "sticker"))

        list.append(listReplyData)
        list.append(sticker)"""

        replyButtonData = buttonReply_Message(number, options, body, footer, "sed6",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "fresh brand" in text:

        document = document_Message(number, sett.document_url, "Here is the PDF", "Inteligencia de Negocio.pdf")
        enviar_Mensaje_whatsapp(document)

        textMessage = text_Message(number,"Please send a product list from pdf file")
        enviar_Mensaje_whatsapp(textMessage)

        replyButtonData = buttonReply_Message(number, options, body, footer, "sed7",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "rfl brand" in text:

        document = document_Message(number, sett.document_url, "Here is the PDF", "Inteligencia de Negocio.pdf")
        enviar_Mensaje_whatsapp(document)

        textMessage = text_Message(number,"Please send a product list from pdf file")
        enviar_Mensaje_whatsapp(textMessage)

        replyButtonData = buttonReply_Message(number, options, body, footer, "sed8",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    elif "pdf list" in text:

        body = "Please share your delivery address"
        footer = "Fresh Shop Team"
        options = ["Go To Main Page", "Support"]

        replyButtonData = buttonReply_Message(number, options, body, footer, "sed8",messageId)
        list.append(replyButtonData)
        text = text.strip().lower()

    for item in list:

        enviar_Mensaje_whatsapp(item)
        if text == "finalisieren":
          break

#al parecer para mexico, whatsapp agrega 521 como prefijo en lugar de 52,
# este codigo soluciona ese inconveniente.
def replace_start(s):
    if s.startswith("521"):
        return "52" + s[3:]
    else:
        return s

# para argentina
def replace_start(s):
    if s.startswith("549"):
        return "54" + s[3:]
    else:
        return s


"""File containing Customer class for our simulation"""

class Customer:
    def __init__(self, birth_time: float, system_customers: int, verbose: bool = False):
        """This class creates a new customer object that needs the lambda parameter for the birth process"""

        self.status = 'In Queue'
        # Time of arrival of the customer in the system
        self.arrival_time = birth_time
        # Time of entering the bus for the customer in the system
        self.boarded_time = float('inf')
        # Time of being served for the customer in the system
        self.departure_time = float('inf')
        # Number of people in system at time of arrival
        self.system_customers = system_customers

        self.verbose = verbose

    def board_bus(self, boarding_time: float):
        """Function that updates the status of the customer and notes down the time the customer boards the bus"""

        if self.verbose:
            print("Customer boards bus.")

        self.boarded_time = boarding_time
        self.status = 'On bus'

    def alight_bus(self, departure_time: float):
        """Function that updates the status of the customer and notes down the time the customer leaves the bus"""

        if self.verbose:
            print("Customer alights bus.")

        self.departure_time = departure_time
        self.status = 'Served'

    def calculate_stats(self):
        """This function returns all the relevant statistics for the customer while they were in the system"""

        return {
                'arrival_time'  : self.arrival_time,
                'boarded_time'  : self.boarded_time,
                'departure_time': self.departure_time,
                'waiting_time'  : self.boarded_time - self.arrival_time,
                'serving_time'  : self.departure_time - self.boarded_time,
                'time_in_system': self.departure_time - self.arrival_time,
                'customers_upon_arrival': self.system_customers,
                }


import bs4
from mitmproxy import http
import htmlmin
from utils import Utils

class Fandom:
    async def response(self, flow: http.HTTPFlow) -> None:
        if not flow.request.host.endswith("fandom.com"): return # host end

        if flow.response.status_code != 200: return  # process HTTP 200 only
        if len(flow.response.content) == 0: return  # skip empty responses
        if not Utils.is_html(flow): return # proccess html only

        soup = bs4.BeautifulSoup(flow.response.content, 'lxml')
        container = soup.select_one(".top-ads-container")
        container.decompose()
        flow.response.content = htmlmin.minify(soup.prettify(), remove_empty_space=True).encode(encoding='utf-8')

        if flow.request.host.startswith("genshin-impact"):
            if flow.request.path == "/wiki/Event":
                await Utils.inject(
                    flow,
                    {
                        "scripts": [
                            {"path": Utils.local_injector_url("fandom.com/genshin-impact/event_list.js")},
                        ],
                    }
                )
            if flow.request.path == "/wiki/World_Quest/List":
                await Utils.inject(
                    flow,
                    {
                        "scripts": [
                            {"path": Utils.local_injector_url("fandom.com/genshin-impact/world_quests_list.js")},
                        ],
                    }
                )
            if flow.request.path == "/wiki/Hidden_Exploration_Objective":
                await Utils.inject(
                    flow,
                    {
                        "scripts": [
                            {"path": Utils.local_injector_url("fandom.com/genshin-impact/hidden_exploration_objective.js")},
                        ],
                    }
                )


import requests
from bs4 import BeautifulSoup
import lxml
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

URL = "https://www.zillow.com/homes/for_rent/1-_beds/?searchQueryState=%7B%22pagination%22%3A%7B%7D%2C%22mapBounds%22%3A%7B%22west%22%3A-122.56825484228516%2C%22east%22%3A-122.29840315771484%2C%22south%22%3A37.69234177970014%2C%22north%22%3A37.85814816331502%7D%2C%22mapZoom%22%3A12%2C%22isMapVisible%22%3Afalse%2C%22filterState%22%3A%7B%22price%22%3A%7B%22max%22%3A872627%7D%2C%22beds%22%3A%7B%22min%22%3A1%7D%2C%22fore%22%3A%7B%22value%22%3Afalse%7D%2C%22mp%22%3A%7B%22max%22%3A3000%7D%2C%22auc%22%3A%7B%22value%22%3Afalse%7D%2C%22nc%22%3A%7B%22value%22%3Afalse%7D%2C%22fr%22%3A%7B%22value%22%3Atrue%7D%2C%22fsbo%22%3A%7B%22value%22%3Afalse%7D%2C%22cmsn%22%3A%7B%22value%22%3Afalse%7D%2C%22fsba%22%3A%7B%22value%22%3Afalse%7D%7D%2C%22isListVisible%22%3Atrue%7D"

headers = {
    "User-Agent": USER-AGENT,
    "Accept-Language": "en-US,en;q=0.9"
}

response = requests.get(URL, headers=headers)
webpage = response.text

soup = BeautifulSoup(webpage, "lxml")

price_list = []
for price in soup.find_all(name="div", class_="kJFQQX"):
    price_list.append(price.getText()[:6])

link_list = []
for link in soup.find_all(name="div", class_="clrcsE"):
    new_link = link.find('a')["href"]
    if new_link[0] != 'h':
        new_link = "https://www.zillow.com" + new_link
    link_list.append(new_link)

address_list = []
for address in soup.find_all(name="div", class_="bDmKKM"):
    new_address = address.find('address').getText()
    address_list.append(new_address)

chrome_driver_path = "C:/Development/chromedriver.exe"
ser = Service(chrome_driver_path)

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=ser, options=chrome_options)

for i in range(len(price_list)):
    driver.get(SHEET_URL)
    time.sleep(2)

    answer_1 = driver.find_element(by="xpath", value='//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
    answer_1.send_keys(address_list[i])

    answer_2 = driver.find_element(by="xpath", value='//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input')
    answer_2.send_keys(price_list[i])

    answer_3 = driver.find_element(by="xpath", value='//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input')
    answer_3.send_keys(link_list[i])

    submit_button = driver.find_element(by="xpath", value='//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div[1]/div/span/span')
    submit_button.click()