import openpyxl
from openpyxl.styles import Font
import datetime
import re

filename = 'spendings.xlsx'

wb = openpyxl.load_workbook(filename)
ws_names = wb.sheetnames

print(wb.sheetnames)

sheetname = input('What sheet do you need? T/O: ')
sheet_index = ws_names.index(sheetname)

wb.active = sheet_index

def counting():

    for col in wb.active.iter_cols(min_row=2, max_row=1000, max_col=4, values_only=True):
        for i, cell in enumerate(col):
            row = i + 1

            if cell is not None:
                None

            else:
                all = input('\nWhat did you buy? ')

                if all == ' ':
                    return row
                else:
                    cost = all.split(' ')
                    cost = int(cost[len(cost) - 1])

                    item = all.replace(str(cost), '')

                    wb.active['A'+str(row)] = item
                    wb.active['B'+str(row)] = cost

                Bold = Font(size=13, bold=True)
                wb.active['D1'].font = Bold
                wb.active['D1'] = 'TOTAL'
                wb.active['D2'] = '=SUM(B1:B100)'

                if (wb.active['A'+str(row)]).value != '.':
                    wb.active['C'+str(row)] = datetime.datetime.strptime("2020/08/11, 14:35", "%Y/%m/%d, %H:%M").now()

                else:
                    wb.active['C'+str(row)] == ' '

    return row

counting()

wb.save(filename)

# я дізнаюся рядок , потімпишу, що купив і додаю у клітинку, пишу ціну і додаю в іншу клітинку
#
# тепер мені потрібно створити змінну row у якій змінюватимуться значення із кожним колом.
# і в кожному колі додавати до row 1 і записувати значення, а якщо хзначення == no або ні
# то переривати програму і все зберігати.


import argparse
import json
import sys

from omero.gateway import BlitzGateway
from omero.cli import cli_login
from omero.model import StatsInfoI
from omero.rtypes import rdouble

# For an Image (ID), set min/max values for channel stats
# Usage: python set_channel_minmax.py 3453 '{"0":[0,2000], "1":[1, 1100]}'

def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('image', type=int, help='Image ID')
    parser.add_argument('minmax', help=(
        'Channel min/max values as JSON {"0":[0,100], "1":[1, 110]}'))
    args = parser.parse_args(argv)

    with cli_login() as cli:
        conn = BlitzGateway(client_obj=cli._client)

        image_id = args.image
        minmax_vals = json.loads(args.minmax)
        image = conn.getObject("Image", image_id)

        # Set statsInfo min & max
        for index, ch in enumerate(image.getChannels(noRE=True)):
            if str(index) in minmax_vals:
                minmax = minmax_vals[str(index)]
            else:
                continue
            si = ch.getStatsInfo()
            if si is None:
                si = StatsInfoI()
            else:
                si = si._obj
            if minmax[0] is not None:
                si.globalMin = rdouble(minmax[0])
            if minmax[1] is not None:
                si.globalMax = rdouble(minmax[1])
            ch._obj.statsInfo = si
            ch.save()

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


from numpy import linalg as la
from .utils import rec_from_factors

def PoF(X, factors, mask=None):
    """
    The percentage of fitness (POF) metric
    INPUT:
        - <tensor> X: the original tensor
        - <tensor> rec: the reconstructed tensor
    OUTPUT:
        - <scalar> PoF_metric: the metric, higher is better
    """
    if mask is None:
        Rec = rec_from_factors(factors)
        PoF_metric = 1 - la.norm(Rec - X) / la.norm(X)
    else:
        Rec = rec_from_factors(factors)
        PoF_metric = 1 - la.norm(mask * Rec - X) / la.norm(X)
    return PoF_metric


# inputs 文件夹和 testplans 中获得输入，生成 json 格式的测试用例

import json
from subprocess import run, PIPE
from os import path

join = path.join

def execf(params):
    p=run(['./source/a.out']+params, stdout=PIPE)
    return(p.stdout)

with open('./testplans.alt/universe') as fr:
    plans = fr.readlines()

res=[]

for item in plans:
    print('running {}'.format(item))

    params = item.strip().split()

    res.append({
        'params': params,
        'output': execf(params).decode('ascii'),
    })

with open('cases.json','w') as fr:
    json.dump(res, fr, indent=2)


import logging
import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import State, StatesGroup
# работа с базой данных
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

import base.logics
from base.models import User, Purchase

# Работа с файловой системой и пр.
from os import getenv
from sys import exit
# Для превращения словарей в объекты (хз чет не догоняю)
from munch import DefaultMunch
import datetime

# Пользовательские материалы в проекте
# from content.descriptions import instructions, help, greetings, admins_start
import config

# Todo - выбрать способ наполнения базы текстами

# Todo Добавить продажу: кнопка, ссылка на акк. и  его проверка, оплата, уведомление об оплате со ссылкой.
# Todo Учёт заданий. Создано, оплачено, выполнено.

# Todo Заказать анализы: кнопка - сслыка - проверка - оплата -?контроль оплаты
#  - сообщение об оплате - сообщение о заказе админу.

# состояние пользователя в чате - возможно удалить
class dialog(StatesGroup):
    spam = State()
    blacklist = State()
    whitelist = State()

# Включение бота и диспетчера
bot_token = getenv("BOT_TOKEN")
if not bot_token:
    exit("Error: no token provided")
storage = MemoryStorage()
bot = Bot(token=bot_token)
dp = Dispatcher(bot=bot, storage=storage)
logging.basicConfig(level=logging.INFO)

# запускаем движок базы данных
engine = create_engine(config.database)

# Пример админ меню на развитие функционала
kb = types.ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(types.InlineKeyboardButton(text="Рассылка"))
kb.add(types.InlineKeyboardButton(text="Добавить в ЧС"))
kb.add(types.InlineKeyboardButton(text="Убрать из ЧС"))
kb.add(types.InlineKeyboardButton(text="Статистика"))

# Хороший генератор кнопок. Если потребуются разные форматы на 3 и более столбца, то применять. Пока как пример.
def build_menu(buttons, n_cols,
               header_buttons=None,
               footer_buttons=None):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, [header_buttons])
    if footer_buttons:
        menu.append([footer_buttons])
    return menu

def initiation():

    if base.logics.get_users():
        print(f'С базой всё ОК, запускаем сервис')
    else:
        None
    # Todo Если с базой не ОК - не работать дальше.

async def first_time_user(message):
    session = Session(bind=engine)
    d = session.query(User.id).filter(User.id == message.from_user.id).scalar()
    f = message.from_user
    if d is None:
        print(f'Такого пользователя нет')
        user = User(id=f.id,
                    is_bot=f.is_bot,
                    first_name=f.first_name,
                    username=f.username,
                    last_name=f.last_name,
                    language_code=f.language_code,
                    can_join_groups=f.can_join_groups,
                    can_read_all_group_messages=f.can_read_all_group_messages,
                    supports_inline_queries=f.supports_inline_queries,
                    role='user',
                    updated=datetime.datetime.now()
                    )
        try:
            session.add(user)
            session.commit()
        except Exception as e:
            print(f'При добавлении базы возникла ошибка {e}')
        else:
            print(f'Добавили пользователя {f.first_name}')
            return True
    else:
        print(f'Стартанул пользователь {message.from_user.first_name}')
        return False

async def admin_user(message):
    session = Session(bind=engine)
    d = session.query(User.role).filter(User.id == message.from_user.id).scalar()
    print(d)
    if d == 'admin':
        print(f'Зашёл Админ')
        return True
    else:
        return False

async def start(message: types.Message):
    session = Session(bind=engine)
    if await first_time_user(message):
        await message.answer(greetings)
        await message.answer(help)
    elif await admin_user(message):
        # Создание клавиатуры со switch-кнопками и отправка сообщения для админа
        switch_keyboard = types.InlineKeyboardMarkup()
        switch_keyboard.add(types.InlineKeyboardButton(
            text="Отправить бота",
            switch_inline_query=""))
        switch_keyboard.add(types.InlineKeyboardButton(
            text="Вызвать бота здесь",
            switch_inline_query_current_chat=""))
        await message.answer(admins_start, reply_markup=switch_keyboard)
    else:
        await message.answer(help)

# Todo проработать передачу данных и состояний через
data = []
i_key = str()
i_value = str()

async def instruction_key(message: types.Message):
    global i_key
    keyboard = types.InlineKeyboardMarkup(row_width=2, inline_keyboard=None)
    buttons = []
    for note_type in instructions.keys():
        buttons.append(types.InlineKeyboardButton(text=note_type, callback_data=note_type))
    keyboard.add(*buttons)

    await message.answer("Выбери <u>вид записи</u>.", reply_markup=keyboard, parse_mode=types.ParseMode.HTML)

@dp.callback_query_handler(lambda call: call.data == 'back_to_instruction')
async def back_to_instruction(call: types.CallbackQuery):
    await call.answer()
    await bot.edit_message_reply_markup(call.message.chat.id,
                                        message_id=call.message.message_id)  # удаляем кнопки у последнего сообщения
    await bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id)

    await instruction_key(call.message)

@dp.callback_query_handler(lambda call: call.data in instructions.keys())
async def instruction_value(call: types.CallbackQuery):

    await call.answer()
    print(f'Запрос от пользователя "{call.from_user.first_name}" - "{call.data}"')
    global data, i_key
    i_key = call.data
    await bot.edit_message_reply_markup(call.message.chat.id,
                                        message_id=call.message.message_id)  # удаляем кнопки у последнего сообщения
    keyboard = types.InlineKeyboardMarkup(row_width=2)
    # Собираем меню кнопок
    buttons = []
    for element in instructions[i_key]:
        buttons.append(types.InlineKeyboardButton(text=element, callback_data=element))
    keyboard.add(*buttons)
    # отдельно добавляем кнопку Назад, чтобы она всегда была внизу и одна
    keyboard.add(types.InlineKeyboardButton(text="Назад", callback_data="back_to_instruction"))
    data = instructions[i_key].keys()
    await call.message.edit_text(f'и элемент из "{call.data}" ? ', reply_markup=keyboard)

@dp.callback_query_handler(lambda call: call.data in data)
async def instruction_notice(call: types.CallbackQuery):
    global i_value
    i_value = call.data
    # print(data)
    # print(i_key)
    # print(i_value)
    # print(call)
    print(f'Запрос деталей от пользователя "{call.from_user.first_name}" - "{call.data}"')
    await bot.answer_callback_query(callback_query_id=call.id,
                                    show_alert=True,
                                    text=f'В {call.data} важно:\n {instructions[i_key][i_value]}')
    await call.answer()

# Система хранения состояний. Пока не разобрался.
async def shutdown(dispatcher: Dispatcher):
    await dispatcher.storage.close()
    await dispatcher.storage.wait_closed()

async def main():
    initiation()
    try:
        dp.register_message_handler(start, commands={"start"})
        dp.register_message_handler(instruction_key, commands={"ads_formats"})
        await dp.start_polling()
    finally:
        await bot.close()

asyncio.run(main())


from DNNCTF import DNNCTF
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

DATASET = 'iris.data'
MODEL = 'model'

df = pd.read_csv(DATASET, header=None)

data = df.iloc[:, :-1]
target = df.iloc[:,-1]

x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2, random_state=0)
print('Dataset created.')

classifier = DNNCTF(
    path=MODEL,
    layers =[10, 10, 10],
    learning_rate=0.001,
    steps=1000,
    batch_size=10,
    log=True
    )
print('Classifier instantiated.')

classifier.fit(x_train, y_train)
print('Classifier trained.')

classifier = DNNCTF(path=MODEL, log=False)
print('Classifier restored.')

y_pred = classifier.predict(x_test)
print('Classifier tested')

accuracy = accuracy_score(y_test, y_pred)
print(accuracy)


from abc import ABCMeta, abstractmethod
from collections import OrderedDict
from blenderneuron.activity import Activity

class RootGroup:
    __metaclass__ = ABCMeta

    def __init__(self):
        self.name = ""
        self.roots = OrderedDict()

        self.import_synapses = False
        self.interaction_granularity = 'Cell'

        self.record_activity = False
        self.recording_granularity = 'Cell'
        self.record_variable = "v"
        self.recording_period = 1.0
        self.recording_time_start = 0
        self.recording_time_end = 0

        self.activity = Activity()

    def __str__(self):
        return self.name

    def clear_activity(self):
        # Clear group level activity
        self.activity.clear()

        # Cell and section level activity
        for root in self.roots.values():
            root.clear_activity(recursive=True)

        # Segment level
        for root in self.roots.values():
            root.clear_3d_segment_activity()

    def to_dict(self,
                include_activity=False,
                include_root_children=False,
                include_coords_and_radii=False):
        """

        :param include_activity:
        :param include_root_children:
        :param include_coords_and_radii:
        :return:
        """
        result = {
            "name": self.name,
            "roots": [root.to_dict(include_activity, include_root_children, include_coords_and_radii) for root in self.roots.values()],
            "import_synapses": self.import_synapses,
            "interaction_granularity": self.interaction_granularity,
            "record_activity": self.record_activity,
            "recording_granularity": self.recording_granularity,
            "record_variable": self.record_variable,
            "recording_period": self.recording_period,
            "recording_time_start": self.recording_time_start,
            "recording_time_end": self.recording_time_end,
        }

        if include_activity:
            result.update({
                "activity": self.activity.to_dict(),
            })

        return result


import unittest
import tempfile
import os
import sys
import pathlib
import contextlib

HERE = pathlib.Path(__file__).absolute().parent
# print(HERE.parent)
sys.path.insert(0, str(HERE.parent))

IMGUI_H = HERE.parent / 'libs/imgui/imgui.h'

from pycpptool.get_tu import get_tu
from clang import cindex

@contextlib.contextmanager
def tmp(src):
    fd, tmp_name = tempfile.mkstemp(prefix='tmpheader_', suffix='.h')
    os.close(fd)
    with open(tmp_name, 'w', encoding='utf-8') as f:
        f.write(src)
    try:
        yield pathlib.Path(tmp_name)
    finally:
        os.unlink(tmp_name)

class StructDecl:
    def __init__(self, name, *fields):
        self.name = name
        self.fields = fields

    def __eq__(self, rhs) -> bool:
        return (self.name == rhs.name and len(self.fields) == len(rhs.fields))

    def __repr__(self) -> str:
        return f'struct {self.name}{{}}'

    @classmethod
    def parse(cls, c: cindex.Cursor) -> 'StructDecl':
        fields = []
        for child in c.get_children():
            fields.append(child)
        # if len(fields) == 0. forward decl
        return StructDecl(c.spelling, *fields)

class TypedefDecl:
    def __init__(self, name: str, src: str):
        self.name = name
        self.src = src

    def __repr__(self) -> str:
        return f'typedef {self.name} = {self.src}'

    @classmethod
    def parse(cls, c: cindex.Cursor) -> 'TypedefDecl':
        children = [child for child in c.get_children()]
        assert (len(children) == 0)
        # tokens = [token.spelling for token in c.get_tokens()]
        return TypedefDecl(c.spelling, c.underlying_typedef_type)

EXPECTS = {
    'ImDrawChannel': StructDecl('ImDrawChannel'),
    'ImDrawCmd': StructDecl('ImDrawCmd'),
    'ImDrawData': StructDecl('ImDrawData'),
    'ImDrawList': StructDecl('ImDrawList'),
    'ImDrawListSharedData': StructDecl('ImDrawListSharedData'),
    'ImDrawListSplitter': StructDecl('ImDrawListSplitter'),
    'ImDrawVert': StructDecl('ImDrawVert'),
    'ImFont': StructDecl('ImFont'),
    'ImFontAtlas': StructDecl('ImFontAtlas'),
    'ImFontConfig': StructDecl('ImFontConfig'),
    'ImFontGlyph': StructDecl('ImFontGlyph'),
    'ImFontGlyphRangesBuilder': StructDecl('ImFontGlyphRangesBuilder'),
    'ImColor': StructDecl('ImColor'),
    'ImGuiContext': StructDecl('ImGuiContext'),
    'ImGuiIO': StructDecl('ImGuiIO'),
    'ImGuiInputTextCallbackData': StructDecl('ImGuiInputTextCallbackData'),
    'ImGuiListClipper': StructDecl('ImGuiListClipper'),
    'ImGuiOnceUponAFrame': StructDecl('ImGuiOnceUponAFrame'),
    'ImGuiPayload': StructDecl('ImGuiPayload'),
    'ImGuiSizeCallbackData': StructDecl('ImGuiSizeCallbackData'),
    'ImGuiStorage': StructDecl('ImGuiStorage'),
    'ImGuiStyle': StructDecl('ImGuiStyle'),
    'ImGuiTextBuffer': StructDecl('ImGuiTextBuffer'),
    'ImGuiTextFilter': StructDecl('ImGuiTextFilter'),
}

def parse(c: cindex.Cursor):
    if c.kind == cindex.CursorKind.UNEXPOSED_DECL:
        tokens = [t.spelling for t in c.get_tokens()]
    elif c.kind == cindex.CursorKind.STRUCT_DECL:
        return StructDecl.parse(c)
    elif c.kind == cindex.CursorKind.TYPEDEF_DECL:
        return TypedefDecl.parse(c)
    else:
        print(c.kind)

class ImGuiTest(unittest.TestCase):
    def test_int(self) -> None:
        tu = get_tu(IMGUI_H)
        self.assertIsInstance(tu, cindex.TranslationUnit)

        # TRANSLATION_UNIT
        c: cindex.Cursor = tu.cursor
        self.assertIsInstance(c, cindex.Cursor)
        self.assertEqual(cindex.CursorKind.TRANSLATION_UNIT, c.kind)
        self.assertIsNone(c.location.file)

        count = 0
        for i, c in enumerate(c.get_children()):
            if c.location.file.name != str(IMGUI_H):
                continue
            count += 1
            with self.subTest(i=i, spelling=c.spelling):
                self.assertEqual(EXPECTS.get(c.spelling), parse(c))
            if count > len(EXPECTS):
                break

if __name__ == '__main__':
    unittest.main()


from django.urls import path
from .views import *

urlpatterns = [
    path('addtocart/<int:pk>/', add_to_cart, name='addtocart'),
    path('cart/', cart_view, name='cart'),
    path('remove/<int:pk>/', remove_from_cart, name='removecart'),
    path('increase/<str:pk>/', increase_cart, name='increasecart'),
    path('decrease/<str:pk>/', decrease_cart, name='decreasecart'),
]


from asyncpg.connection import Connection

class DBWorker:
    def __init__(self, conn: Connection):
        self.conn = conn

    async def get_all_tokens_output(self):
        sql = """select tot."id", u."id" as user_id, tot.tokens, u."bep_address",tot.created_date_time from "tokens_output" tot join "user" u on tot.user_id = u."id" """
        return await self.conn.fetch(sql)

    async def delete_tokens_output(self, id):
        sql = "delete from \"tokens_output\" where \"id\" = $1"
        return await self.conn.fetch(sql, id)


import torch
import numpy as np
from frac_brownian import frac_brownian

# Sampling

def fcir(r0, a, b, sigma, H, m, n, T, device=None, seed=123):
    """
        Inputs:
            r0: float tensor of shape (m,1), initial rates
            a: float tensor of shape (m), long term mean level
            b: float tensor of shape (m), speed of reversion
            sigma: float tensor of shape (m), instantaneous volatility
            H: float, Hurst exponent
            m: int, nb of trajs
            n: int, nb of points
            T: float, trajs generated between [0,T]
            device: torch device
            seed: int
        Outputs:
            dict of torch float tensor of shape (m,n): m fCIR trajectories of n points on an uniformly spaced grid on [0,T]
    """
    if seed is not None:
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)

    delta_t = T/n*torch.ones(1, dtype=torch.float, device=device)
    fBm = frac_brownian(H, m, n, T, device, seed)
    fBm_increments = fBm[:,1:] - fBm[:,:-1]

    r = r0*torch.ones(m, n, dtype=torch.float, device=device)
    for i in range(1,n):
        r[:,i] = r[:,i-1] + a*delta_t*(b-r[:,i-1]) + sigma*torch.sqrt(torch.abs(r[:,i-1]))*fBm_increments[:,i-1]
    res = {
        'traj':r
    }
    return res


import gevent
from gevent import getcurrent
from gevent.pool import Group

group = Group()

def hello_from(n):
    print('Size of group', len(group))
    print('Hello from Greenlet %s' % id(getcurrent()))

group.map(hello_from, xrange(3))

def intensive(n):
    gevent.sleep(3 - n)
    return 'task', n

print('Ordered')

ogroup = Group()

'''
think of it this way:
def imap(function, *iterables):
    # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
    iterables = map(iter, iterables)
    while True:
        args = [next(it) for it in iterables]
        if function is None:
            yield tuple(args)
        else:
            yield function(*args)
'''
# why is this executing all at once?
for i in ogroup.imap(intensive, xrange(3)):
    print(i)

print('Unordered')

group = Group()

'''
imap_unordered
The same as imap() except that the ordering of the results from the returned iterator should be considered in arbitrary order.
'''
# why is this iterating one at a time?
# gevent is deterministic.  hence each greenlet is processed in order in deterministed way
for i in igroup.imap_unordered(intensive, xrange(3)):
    print(i)


"""Charge subscriptions' spending to a cost centre."""
from datetime import date, timedelta
from typing import Any, Dict, List, Optional
from uuid import UUID

import sqlalchemy
from fastapi import Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy import and_, between, desc, func, insert, select

from rctab.constants import ADMIN_OID
from rctab.crud.accounting_models import (
    cost_recovery,
    cost_recovery_log,
    finance,
    usage,
)
from rctab.crud.auth import token_admin_verified
from rctab.crud.models import database
from rctab.crud.schema import CostRecovery, UserRBAC
from rctab.routers.accounting.routes import router
from rctab.routers.accounting.usage import authenticate_usage_app

class CostRecoveryMonth(BaseModel):
    """A month to do cost-recovery for."""

    # The first day of the month
    first_day: date

def validate_month(
    month_to_recover: CostRecoveryMonth,
    last_recovered_month: Optional[CostRecoveryMonth],
) -> None:
    """Raise if month shouldn't be recovered."""
    if last_recovered_month:
        # If we have previously recovered a month,
        # we know exactly which month we should be recovering
        month_expected = last_recovered_month.first_day + timedelta(days=31)
        month_expected = date(
            year=month_expected.year, month=month_expected.month, day=1
        )
        if month_to_recover.first_day != month_expected:
            raise HTTPException(
                status_code=400, detail="Expected " + month_expected.isoformat()[:-3]
            )

    # We can never recover later than last month
    middle_of_this_month = date.today().replace(day=15)
    middle_of_last_month = middle_of_this_month - timedelta(days=30)
    first_of_last_month = middle_of_last_month.replace(day=1)
    if month_to_recover.first_day > first_of_last_month:
        raise HTTPException(
            status_code=400,
            detail="Cannot recover later than " + first_of_last_month.isoformat()[:-3],
        )

async def calc_cost_recovery(
    recovery_month: CostRecoveryMonth, commit_transaction: bool, admin: UUID
) -> List[CostRecovery]:
    """Calculates the cost recovery for a given period.

    Cost recovery must have been calculated for all previous months
    but not for this month.
    """
    last_recovered_day = await database.fetch_one(
        select([cost_recovery_log]).order_by(desc(cost_recovery_log.c.month))
    )
    last_recovered_month = (
        CostRecoveryMonth(first_day=last_recovered_day["month"])
        if last_recovered_day
        else None
    )
    if commit_transaction:
        validate_month(recovery_month, last_recovered_month)

    transaction = await database.transaction()
    try:

        cost_recovery_ids = []

        # We're only interested in subscriptions if they have a finance record
        subscription_ids = [
            r["sub_id"]
            for r in await database.fetch_all(
                select(
                    [func.distinct(finance.c.subscription_id).label("sub_id")]
                ).where(
                    between(
                        recovery_month.first_day, finance.c.date_from, finance.c.date_to
                    )
                )
            )
        ]

        for subscription_id in subscription_ids:

            usage_row = await database.fetch_one(
                select([func.sum(usage.c.total_cost).label("the_sum")])
                .where(
                    func.date_trunc(
                        "month", sqlalchemy.cast(usage.c.date, sqlalchemy.Date)
                    )
                    == func.date_trunc(
                        "month",
                        sqlalchemy.cast(recovery_month.first_day, sqlalchemy.Date),
                    )
                )
                .where(usage.c.subscription_id == subscription_id)
            )
            # This should always return a row, though the_sum can be NULL
            assert usage_row
            total_usage = usage_row["the_sum"] or 0.0
            usage_recharged = 0

            # The lower the value of priority, the higher importance
            finance_periods = await database.fetch_all(
                select([finance])
                .where(
                    and_(
                        between(
                            recovery_month.first_day,
                            finance.c.date_from,
                            finance.c.date_to,
                        ),
                        finance.c.subscription_id == subscription_id,
                    )
                )
                .order_by(finance.c.priority)
            )

            # We divide the usage between the eligible finance periods
            for finance_period in finance_periods:

                cost_recovery_row = await database.fetch_one(
                    select([func.sum(cost_recovery.c.amount).label("the_sum")]).where(
                        cost_recovery.c.finance_id == finance_period["id"]
                    )
                )

                # This should always return a row, though the_sum can be NULL
                assert cost_recovery_row
                recovered_amount = cost_recovery_row["the_sum"] or 0.0

                recoverable_amount = min(
                    # The smallest of
                    # a) the amount remaining on the finance ID and
                    # b) the amount of usage yet to be charged
                    finance_period["amount"] - recovered_amount,
                    total_usage - usage_recharged,
                )

                # Keep track of how much of this subscription's usage
                # has been recharged
                usage_recharged += recoverable_amount

                cost_recovery_id = await database.execute(
                    insert(
                        cost_recovery,
                        {
                            "subscription_id": finance_period["subscription_id"],
                            "month": recovery_month.first_day,
                            "finance_code": finance_period["finance_code"],
                            "amount": recoverable_amount,
                            "date_recovered": None,
                            "finance_id": finance_period["id"],
                            "admin": admin,
                        },
                    )
                )
                cost_recovery_ids.append(cost_recovery_id)

        inserted_rows = await database.fetch_all(
            select([cost_recovery]).where(cost_recovery.c.id.in_(cost_recovery_ids))
        )

        # Note that we patch CostRecovery as a unit testing hack
        cost_recoveries = [CostRecovery(**dict(cr)) for cr in inserted_rows]

        if commit_transaction:
            await database.execute(
                insert(
                    cost_recovery_log,
                    {"month": recovery_month.first_day, "admin": admin},
                )
            )
            await transaction.commit()
        else:
            await transaction.rollback()

        return cost_recoveries

    except BaseException:
        await transaction.rollback()
        raise

@router.post("/app-cost-recovery", status_code=200)
async def calc_cost_recovery_app(
    recovery_period: CostRecoveryMonth,
    _: Dict[str, str] = Depends(authenticate_usage_app),
) -> Any:
    """Route for the usage app to trigger cost recovery calculation."""
    await calc_cost_recovery(
        recovery_period, commit_transaction=True, admin=UUID(ADMIN_OID)
    )

    return {"status": "success", "detail": "cost recovery calculated"}

@router.post("/cli-cost-recovery", response_model=List[CostRecovery])
async def post_calc_cost_recovery_cli(
    recovery_month: CostRecoveryMonth, user: UserRBAC = Depends(token_admin_verified)
) -> Any:
    """Route for the CLI to trigger cost recovery calculation."""
    resp = await calc_cost_recovery(
        recovery_month, commit_transaction=True, admin=user.oid
    )

    return resp

@router.get("/cli-cost-recovery", response_model=List[CostRecovery])
async def get_calc_cost_recovery_cli(
    recovery_month: CostRecoveryMonth, user: UserRBAC = Depends(token_admin_verified)
) -> Any:
    """Route for the CLI to do a dry-run of the cost recovery calculation."""
    result = await calc_cost_recovery(
        recovery_month, commit_transaction=False, admin=user.oid
    )
    return result


"""The module contains a base class for Mediator,
from which is inherited in all blockchain mediators.

Author: Jan Jakub Kubik (xkubik32)
Date: 14.3.2023
"""
import random
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Set

from base.logs import create_logger

class ActionObjectStore:
    """A container for storing objects associated with actions.

    The ActionObjectStore class provides a convenient way to store objects
    that are associated with specific actions. It allows adding and removing
    objects, retrieving objects for a given action, getting the list of all
    actions, and clearing the entire store.

    Attributes:
        _store (Dict[Any, List[Any]]): A dictionary containing lists of objects keyed by actions.
        _all_actions (List[Any]): A list of all actions in the store.
    """

    def __init__(self):
        """Initialize the ActionObjectStore with an empty store and an empty list of actions."""
        self._store: Dict[Any, List[Any]] = {}
        self._all_actions: List[Any] = []

    def add_object(self, action: Any, obj: Any = None) -> None:
        """Add an object associated with the given action to the store.

        Args:
            action (Any): The action to associate with the object.
            obj (Any, optional): The object to store. Defaults to None.
        """
        if action not in self._store:
            self._store[action] = [obj]
        else:
            self._store[action].append(obj)

        self._all_actions.append(action)

    def remove_object(self, action: Any, obj: Any = None) -> None:
        """Remove an object associated with the given action from the store.

        Args:
            action (Any): The action associated with the object.
            obj (Any, optional): The object to remove. Defaults to None.
        """
        if action in self._store and obj in self._store[action]:
            self._store[action].remove(obj)
            self._all_actions.remove(action)

            if not self._store[action]:
                del self._store[action]

    def get_objects(self, action: Any) -> List:
        """Retrieve a list of objects associated with the given action.

        Args:
            action (Any): The action to retrieve objects for.

        Returns:
            List: List of objects associated with the action.
        """
        return self._store.get(action, [])

    def get_actions(self) -> List:
        """Retrieve the list of all actions in the store.

        Returns:
            List: List of all actions.
        """
        return self._all_actions

    def clear(self) -> None:
        """Clear the store and the list of actions."""
        self._store.clear()
        self._all_actions.clear()

class SimulationManagerBase(ABC):
    """Abstract base class for all blockchain simulation managers."""

    def __init__(self, simulation_config: Dict[str, Any], blockchain: str):
        self.log = create_logger(blockchain)
        self.config: Dict[str, Any] = self.__call_parse_config(simulation_config)

    @abstractmethod
    def parse_config(self, simulation_config: Dict[str, Any]) -> Dict[str, Any]:
        """Parse the configuration for the simulation.

        Args:
            simulation_config (Dict[str, Any]): The simulation configuration dictionary.

        Returns:
            Dict[str, Any]: Parsed configuration dictionary.
        """
        raise NotImplementedError

    @abstractmethod
    def run(self) -> None:
        """Run the simulation."""
        raise NotImplementedError

    @abstractmethod
    def resolve_overrides(self) -> None:
        """Resolve the 'override' actions in the simulation."""
        raise NotImplementedError

    @abstractmethod
    def resolve_matches(self) -> None:
        """Resolve the 'match' actions in the simulation."""
        raise NotImplementedError

    @staticmethod
    def choose_leader(choices: List[Any], weights: List[float]) -> Any:
        """Select a leader for the current round according to the given weights.

        Args:
            choices (List[Any]): List of possible leaders.
            weights (List[float]): List of weights for each leader.

        Returns:
            Any: The selected leader.
        """
        return random.choices(choices, weights, k=1)[0]

    def validate_blockchain_config_keys(
        self, dictionary: Dict[str, Any], expected_keys: Set[str]
    ) -> None:
        """Validate top-level keys in the parsed simulation config.

        Args:
            dictionary (Dict[str, Any]): The dictionary to validate.
            expected_keys (Set[str]): The set of expected keys.
        """
        self.log.info("validating simulation config")
        dict_keys = set(dictionary.keys())
        if dict_keys != expected_keys:
            raise ValueError(
                f"You are missing or you have used redundant top-level keys in the YAML config."
                f" Please use just these keys: {expected_keys}"
            )

    def general_config_validations(
        self,
        simulation_config: Dict[str, Any],
        expected_keys_extra: Optional[List[str]] = (),
    ) -> Dict[str, Any]:
        """
        Validates the general configuration of a simulation.

        Args:
            simulation_config (Dict[str, Any]): The simulation configuration dictionary.
            expected_keys_extra (Optional[List[str]]): The extra expected keys to validate.
                                                       By default, it is an empty list.

        Raises:
            ValueError: If there is not exactly 1 honest miner or no selfish
            miners in the configuration.

        Returns:
            Dict[str, Any]: The validated simulation configuration dictionary.
        """
        # check main keys
        expected_keys: Set[str] = {
            "consensus_name",
            "miners",
            "simulation_mining_rounds",
        }
        for key in expected_keys_extra:
            expected_keys.add(key)

        sim_config: Dict[str, Any] = list(simulation_config.values())[0]
        self.validate_blockchain_config_keys(sim_config, expected_keys)

        miners: Dict[str, Any] = sim_config["miners"]
        if len(list(miners["honest"])) != 1:
            raise ValueError("You must setup exactly 1 honest miner")
        if len(list(miners["selfish"])) == 0:
            raise ValueError("You must setup at least 1 selfish miner")

        return sim_config

    def __call_parse_config(self, simulation_config: Dict[str, Any]) -> Dict[str, Any]:
        return self.parse_config(simulation_config)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Created By  : SpeedCode210
# version ='2.0'
# System dependencies : tar, gzip
# Pip dependencies : Pillow
# ---------------------------------------------------------------------------
"""Builds a Flatpak source archive from binaries"""
# ---------------------------------------------------------------------------
import config
import os
from PIL import Image

def generate_flatpak_source():
    try:
        print("-"*os.get_terminal_size().columns)
    except:
        print("-------------------------------------------")
    print("  SmartPackager - Building Flatpak sources archive")
    try:
        print("-"*os.get_terminal_size().columns)
    except:
        print("-------------------------------------------")
    path = f"/tmp/flatpaksource"
    os.system(f"mkdir -p {path}")
    os.system(f'cp -R {config.BASE_DIR}/* {path}')
    if os.path.exists(f"{config.BASE_DIR}/App.png") :
        image = Image.open(f"{config.BASE_DIR}/App.png")
        new_image = image.resize((128, 128))
        new_image.save(f'{path}/128.png')
        new_image = image.resize((64, 64))
        new_image.save(f'{path}/64.png')
    os.system(f"sh -c 'cd {path} && tar -czvf /tmp/{config.NAME.lower()}-source-{config.VERSION}.tar.gz ./'")
    os.system(f"cp /tmp/{config.NAME.lower()}-source-{config.VERSION}.tar.gz {config.OUTPUT_FOLDER}")
    os.system(f"rm -rf {path}")

if __name__ == "__main__":
    generate_flatpak_source()


import os
import unittest

import numpy as np
from common import (
    j_loader,
)

from deepmd.descriptor import (
    DescrptSeA,
)
from deepmd.env import (
    tf,
)

GLOBAL_ENER_FLOAT_PRECISION = tf.float64
GLOBAL_TF_FLOAT_PRECISION = tf.float64
GLOBAL_NP_FLOAT_PRECISION = np.float64

#
from common import (
    tests_path,
)

from deepmd.nvnmd.data.data import (
    jdata_deepmd_input,
)
from deepmd.nvnmd.utils.config import (
    nvnmd_cfg,
)

class TestModel(tf.test.TestCase):
    def setUp(self):
        # generate data of GeTe
        a = 3.01585  # crystal constant
        natom = 64
        box = np.zeros([9])
        box[0] = a * 4
        box[4] = a * 4
        box[8] = a * 4
        box = box.reshape([-1, 9])
        types = np.zeros([natom], dtype=np.int32)
        types[32:] = 1
        types = types.reshape([1, natom])
        coord = np.zeros([natom, 3])
        ct = 0
        ct2 = 32
        for ix in range(4):
            for iy in range(4):
                for iz in range(4):
                    if (ix + iy + iz) % 2 == 0:
                        coord[ct] = np.array([ix * a, iy * a, iz * a])
                        ct += 1
                    else:
                        coord[ct2] = np.array([ix * a, iy * a, iz * a])
                        ct2 += 1
        coord = coord.reshape([1, natom * 3])
        natoms = np.array([64, 64, 32, 32])
        mesh = np.array([0, 0, 0, 2, 2, 2])
        #
        self.box = box
        self.types = types
        self.coord = coord
        self.natoms = natoms
        self.mesh = mesh

    def test_descriptor_one_side_qnn(self):
        """: test se_a of NVNMD with quantized value.

        Reference:
            test_descrpt_se_a_type.py

        Note:
        ----
            The test_nvnmd_se_a.py must be run after test_nvnmd_entrypoints.py.
            Because the data file map.npy ia generated in running test_nvnmd_entrypoints.py.
        """
        tf.reset_default_graph()
        # open NVNMD
        jdata_cf = jdata_deepmd_input["nvnmd"]
        jdata_cf["config_file"] = str(tests_path / os.path.join("nvnmd", "config.npy"))
        jdata_cf["weight_file"] = str(tests_path / os.path.join("nvnmd", "weight.npy"))
        jdata_cf["map_file"] = str(tests_path / os.path.join("nvnmd", "map.npy"))
        jdata_cf["enable"] = True
        nvnmd_cfg.init_from_jdata(jdata_cf)
        nvnmd_cfg.quantize_descriptor = True
        nvnmd_cfg.restore_descriptor = True
        # load input
        jfile = str(tests_path / os.path.join("nvnmd", "train_cnn.json"))
        jdata = j_loader(jfile)
        ntypes = nvnmd_cfg.dscp["ntype"]

        # build descriptor
        jdata["model"]["descriptor"].pop("type", None)
        descrpt = DescrptSeA(**jdata["model"]["descriptor"], uniform_seed=True)

        t_coord = tf.placeholder(
            GLOBAL_TF_FLOAT_PRECISION, [None, None], name="i_coord"
        )
        t_type = tf.placeholder(tf.int32, [None, None], name="i_type")
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name="i_natoms")
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name="i_box")
        t_mesh = tf.placeholder(tf.int32, [None], name="i_mesh")
        is_training = tf.placeholder(tf.bool)

        dout = descrpt.build(
            t_coord,
            t_type,
            t_natoms,
            t_box,
            t_mesh,
            {},
            reuse=False,
            suffix="_se_a_nvnmd",
        )
        # data
        feed_dict_test = {
            t_coord: self.coord,
            t_box: self.box,
            t_type: self.types,
            t_natoms: self.natoms,
            t_mesh: self.mesh,
            is_training: False,
        }
        # run
        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [model_dout] = sess.run([dout], feed_dict=feed_dict_test)
        model_dout = model_dout.reshape([-1])
        # compare
        ref_dout = [
            0.0136348009,
            0.0083287954,
            -0.0639075041,
            0.0129179955,
            0.0050876141,
            -0.0390378237,
            0.0078909397,
            0.0022796392,
            0.2995386124,
            -0.0605479479,
        ]
        places = 10
        np.testing.assert_almost_equal(model_dout[0:10], ref_dout, places)
        # close NVNMD
        jdata_cf["enable"] = False
        nvnmd_cfg.init_from_jdata(jdata_cf)

if __name__ == "__main__":
    unittest.main()


import os
from configparser import ConfigParser
import threading
import signal
import csv
import zmq
import random
import time

def main():
    """
    This data feeder simulates the idea of having a constant stream of data entering
    our system, by reading csv files and pushing them row by row (as dicts) to the DAG
    source.

    It reads from two csvs (posts and comments) and sends them, indiscriminately (the
    DAG will then sort them out according to their type)
    """

    config = ConfigParser()
    config.read("./config.ini")

    # We can set up an env var 'TEST_LINES' to limit the number of rows sent
    test_lines = int(os.environ.get("TEST_LINES", 0))
    source_host = (
        config["NETWORK"]["source_hostname"],
        int(config["NETWORK"]["source_port"]),
    )
    posts_file, comments_file = (
        config["FEEDER"]["posts_file"],
        config["FEEDER"]["comments_file"],
    )

    # Have a dedicated event to track if we are shutting down (on SIGTERMs) and need to
    # stop our loop
    is_shutdown = threading.Event()
    signal.signal(signal.SIGTERM, lambda _n, _f: is_shutdown.set())

    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.connect(f"tcp://{source_host[0]}:{source_host[1]}")

    with open(posts_file) as posts, open(comments_file) as comments:
        posts_reader = csv.DictReader(
            posts, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
        )
        comments_reader = csv.DictReader(
            comments, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
        )

        comments_row, posts_row = None, None
        i, j = 0, 0
        while not is_shutdown.is_set():
            # We randomly send data to the system
            # We prefer sending posts over comments as those are more valuable to the
            # system, but in reality we should have a bigger influx of comments than
            # posts
            coin_toss = random.random()
            if coin_toss <= 0.75:
                posts_row = next(posts_reader, None)
                if posts_row:
                    socket.send_json(posts_row)
                    i += 1
            else:
                comments_row = next(comments_reader, None)
                if comments_row:
                    socket.send_json(comments_row)
                    j += 1

            # Breaking conditions:
            # - SIGTERM
            if is_shutdown.is_set():
                break
            # - Finished sending our n test lines
            if test_lines and (i + j >= test_lines):
                break
            # - End of file for both csvs
            if not comments_row and not posts_row:
                break
        # Wait a bit before shutting down
        time.sleep(5)

    print(f"Finished sending {i+j} lines from {posts_file} and {comments_file}")
    print(f"Sending EOF")
    socket.send_json({'type': 'EOF'})

    socket.setsockopt(zmq.LINGER, 0)
    socket.close()
    context.term()

if __name__ == "__main__":
    main()


import functools
import types
import typing
from http import client
from timeit import default_timer
from typing import Collection, Dict
from urllib.request import (  # pylint: disable=no-name-in-module,import-error
    OpenerDirector,
    Request,
)

from opentelemetry import context

# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.urllib.package import _instruments
from opentelemetry.instrumentation.urllib.version import __version__
from opentelemetry.instrumentation.utils import (
    _SUPPRESS_INSTRUMENTATION_KEY,
    http_status_to_status_code,
)
from opentelemetry.metrics import Histogram, get_meter
from opentelemetry.propagate import inject
from opentelemetry.semconv.metrics import MetricInstruments
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import Span, SpanKind, get_tracer
from opentelemetry.trace.status import Status
from opentelemetry.util.http import (
    ExcludeList,
    get_excluded_urls,
    parse_excluded_urls,
    remove_url_credentials,
)

_excluded_urls_from_env = get_excluded_urls("URLLIB")

_RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]]
_ResponseHookT = typing.Optional[
    typing.Callable[[Span, Request, client.HTTPResponse], None]
]

class URLLibInstrumentor(BaseInstrumentor):
    """An instrumentor for urllib
    See `BaseInstrumentor`
    """

    def instrumentation_dependencies(self) -> Collection[str]:
        return _instruments

    def _instrument(self, **kwargs):
        """Instruments urllib module

        Args:
            **kwargs: Optional arguments
                ``tracer_provider``: a TracerProvider, defaults to global
                ``request_hook``: An optional callback invoked that is invoked right after a span is created.
                ``response_hook``: An optional callback which is invoked right before the span is finished processing a response
                ``excluded_urls``: A string containing a comma-delimited
                    list of regexes used to exclude URLs from tracking
        """
        tracer_provider = kwargs.get("tracer_provider")
        tracer = get_tracer(
            __name__,
            __version__,
            tracer_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )
        excluded_urls = kwargs.get("excluded_urls")
        meter_provider = kwargs.get("meter_provider")
        meter = get_meter(
            __name__,
            __version__,
            meter_provider,
            schema_url="https://opentelemetry.io/schemas/1.11.0",
        )

        histograms = _create_client_histograms(meter)

        _instrument(
            tracer,
            histograms,
            request_hook=kwargs.get("request_hook"),
            response_hook=kwargs.get("response_hook"),
            excluded_urls=_excluded_urls_from_env
            if excluded_urls is None
            else parse_excluded_urls(excluded_urls),
        )

    def _uninstrument(self, **kwargs):
        _uninstrument()

    def uninstrument_opener(
        self, opener: OpenerDirector
    ):  # pylint: disable=no-self-use
        """uninstrument_opener a specific instance of urllib.request.OpenerDirector"""
        _uninstrument_from(opener, restore_as_bound_func=True)

def _instrument(
    tracer,
    histograms: Dict[str, Histogram],
    request_hook: _RequestHookT = None,
    response_hook: _ResponseHookT = None,
    excluded_urls: ExcludeList = None,
):
    """Enables tracing of all requests calls that go through
    :code:`urllib.Client._make_request`"""

    opener_open = OpenerDirector.open

    @functools.wraps(opener_open)
    def instrumented_open(opener, fullurl, data=None, timeout=None):
        if isinstance(fullurl, str):
            request_ = Request(fullurl, data)
        else:
            request_ = fullurl

        def get_or_create_headers():
            return getattr(request_, "headers", {})

        def call_wrapped():
            return opener_open(opener, request_, data=data, timeout=timeout)

        return _instrumented_open_call(
            opener, request_, call_wrapped, get_or_create_headers
        )

    def _instrumented_open_call(
        _, request, call_wrapped, get_or_create_headers
    ):  # pylint: disable=too-many-locals
        if context.get_value(
            _SUPPRESS_INSTRUMENTATION_KEY
        ) or context.get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY):
            return call_wrapped()

        url = request.full_url
        if excluded_urls and excluded_urls.url_disabled(url):
            return call_wrapped()

        method = request.get_method().upper()

        span_name = method.strip()

        url = remove_url_credentials(url)

        labels = {
            SpanAttributes.HTTP_METHOD: method,
            SpanAttributes.HTTP_URL: url,
        }

        with tracer.start_as_current_span(
            span_name, kind=SpanKind.CLIENT, attributes=labels
        ) as span:
            exception = None
            if callable(request_hook):
                request_hook(span, request)

            headers = get_or_create_headers()
            inject(headers)

            token = context.attach(
                context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
            )
            start_time = default_timer()
            try:
                result = call_wrapped()  # *** PROCEED
            except Exception as exc:  # pylint: disable=W0703
                exception = exc
                result = getattr(exc, "file", None)
            finally:
                elapsed_time = round((default_timer() - start_time) * 1000)
                context.detach(token)

            if result is not None:
                code_ = result.getcode()
                labels[SpanAttributes.HTTP_STATUS_CODE] = str(code_)

                if span.is_recording() and code_ is not None:
                    span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, code_)
                    span.set_status(Status(http_status_to_status_code(code_)))

                ver_ = str(getattr(result, "version", ""))
                if ver_:
                    labels[
                        SpanAttributes.HTTP_FLAVOR
                    ] = f"{ver_[:1]}.{ver_[:-1]}"

            _record_histograms(
                histograms, labels, request, result, elapsed_time
            )

            if callable(response_hook):
                response_hook(span, request, result)

            if exception is not None:
                raise exception.with_traceback(exception.__traceback__)

        return result

    instrumented_open.opentelemetry_instrumentation_urllib_applied = True
    OpenerDirector.open = instrumented_open

def _uninstrument():
    """Disables instrumentation of :code:`urllib` through this module.

    Note that this only works if no other module also patches urllib."""
    _uninstrument_from(OpenerDirector)

def _uninstrument_from(instr_root, restore_as_bound_func=False):
    instr_func_name = "open"
    instr_func = getattr(instr_root, instr_func_name)
    if not getattr(
        instr_func,
        "opentelemetry_instrumentation_urllib_applied",
        False,
    ):
        return

    original = instr_func.__wrapped__  # pylint:disable=no-member
    if restore_as_bound_func:
        original = types.MethodType(original, instr_root)
    setattr(instr_root, instr_func_name, original)

def _create_client_histograms(meter) -> Dict[str, Histogram]:
    histograms = {
        MetricInstruments.HTTP_CLIENT_DURATION: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_DURATION,
            unit="ms",
            description="Measures the duration of outbound HTTP requests.",
        ),
        MetricInstruments.HTTP_CLIENT_REQUEST_SIZE: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_REQUEST_SIZE,
            unit="By",
            description="Measures the size of HTTP request messages.",
        ),
        MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE: meter.create_histogram(
            name=MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE,
            unit="By",
            description="Measures the size of HTTP response messages.",
        ),
    }

    return histograms

def _record_histograms(
    histograms, metric_attributes, request, response, elapsed_time
):
    histograms[MetricInstruments.HTTP_CLIENT_DURATION].record(
        elapsed_time, attributes=metric_attributes
    )

    data = getattr(request, "data", None)
    request_size = 0 if data is None else len(data)
    histograms[MetricInstruments.HTTP_CLIENT_REQUEST_SIZE].record(
        request_size, attributes=metric_attributes
    )

    if response is not None:
        response_size = int(response.headers.get("Content-Length", 0))
        histograms[MetricInstruments.HTTP_CLIENT_RESPONSE_SIZE].record(
            response_size, attributes=metric_attributes
        )


from typing import Any, List, Tuple
from .constants._sync_mode import ASYNC
from .opencv._cv_border_types import BORDER_DEFAULT
from ..native import make_native_object

import sys
matx = sys.modules['matx']

class _LaplacianBlurOpImpl:
    """ LaplacianBlur Impl """

    def __init__(self,
                 device: Any,
                 pad_type: str = BORDER_DEFAULT) -> None:
        self.op: matx.NativeObject = make_native_object(
            "VisionLaplacianBlurGeneralOp", device())
        self.pad_type: str = pad_type

    def __call__(self,
                 images: List[matx.runtime.NDArray],
                 ksizes: List[int],
                 scales: List[float],
                 sync: int = ASYNC) -> List[matx.runtime.NDArray]:
        batch_size: int = len(images)
        assert len(
            ksizes) == batch_size, "The ksize number for laplacian blur should be equal to batch size."
        assert len(
            scales) == batch_size, "The scale number for laplacian blur should be equal to batch size."

        return self.op.process(images, ksizes, scales, self.pad_type, sync)

class LaplacianBlurOp:
    """ Apply laplacian blur on input images.
    """

    def __init__(self,
                 device: Any,
                 pad_type: str = BORDER_DEFAULT) -> None:
        """ Initialize LaplacianBlurOp

        Args:
            device (Any) : the matx device used for the operation
            pad_type (str, optional) : pixel extrapolation method, if border_type is BORDER_CONSTANT, 0 would be used as border value.
        """
        self.op: _LaplacianBlurOpImpl = matx.script(_LaplacianBlurOpImpl)(device, pad_type)

    def __call__(self,
                 images: List[matx.runtime.NDArray],
                 ksizes: List[int],
                 scales: List[float],
                 sync: int = ASYNC) -> List[matx.runtime.NDArray]:
        """ Apply average blur on input images.

        Args:
            images (List[matx.runtime.NDArray]): target images.
            ksizes (List[int]): conv kernel size for each image, laplacian kernel is a square shaped kernel, so each item in this list is an integer.
            scales (List[float]): scale factor for laplacian blur
            sync (int, optional): sync mode after calculating the output. when device is cpu, the params makes no difference.
                                    ASYNC -- If device is GPU, the whole calculation process is asynchronous.
                                    SYNC -- If device is GPU, the whole calculation will be blocked until this operation is finished.
                                    SYNC_CPU -- If device is GPU, the whole calculation will be blocked until this operation is finished, and the corresponding CPU array would be created and returned.
                                  Defaults to ASYNC.
        Returns:
            List[matx.runtime.NDArray]: converted images

        Example:

        >>> import cv2
        >>> import matx
        >>> from matx.vision import LaplacianBlurOp

        >>> # Get origin_image.jpeg from https://github.com/bytedance/matxscript/tree/main/test/data/origin_image.jpeg
        >>> image = cv2.imread("./origin_image.jpeg")
        >>> device_id = 0
        >>> device_str = "gpu:{}".format(device_id)
        >>> device = matx.Device(device_str)
        >>> # Create a list of ndarrays for batch images
        >>> batch_size = 3
        >>> nds = [matx.array.from_numpy(image, device_str) for _ in range(batch_size)]
        >>> ksizes = [3, 5, 3]
        >>> scales = [1.0, 0.5, 0.5]

        >>> op = LaplacianBlurOp(device)
        >>> ret = op(nds, ksizes, scales)
        """
        return self.op(images, ksizes, scales, sync)


import requests
import streamlit as st
nome_do_filme = st.text_input('Choose Your Movie: ')
if st.button('Consultar Filmes'):

    #é recomendado procurar filmes populares. Link fonte não possui um banco de dados extenso
    link = 'https://www.omdbapi.com/?apikey=90c1591a&t=' + nome_do_filme
    dvd = requests.get(link)
    filme = dvd.json()
    # utilizei a chave response para verificar se o filme estava na API
    if filme['Response'] == 'True':
        actors = filme['Actors'].split(', ')
        first_actor = actors[0]
        st.write("Atores: ", filme['Actors'])
        st.write("Gênero: ", filme['Genre'])
        st.write("Classificação Indicativa: ", filme['Rated'])
        st.write("\n")

        link2 = 'https://www.omdbapi.com/?apikey=90c1591a&s=' + first_actor  # Search for movies by first actor's name
        dvd2 = requests.get(link2)
        search_results = dvd2.json()

        if search_results['Response'] == 'True' and search_results['totalResults'] != '0':
            movies = search_results['Search']  # Limit the number of movies to 3
            st.write("Primeiro ator: ", first_actor)
            st.write("Outros filmes dele:")
            for movie in movies:
               st.write(movie['Title'])
        else:
          st.write("Não foram encontrados outros filmes para este ator.")


# drunktweet.py
'''
Print out possible drunk tweets from anywhere in the world.

Usage: ./drunktweet.py London, UK
'''

import regex
import requests
import json
import simplejson, urllib
import sys

def geosearch(query):
    args = {
        'address': query,
        'sensor': 'false',
    }
    googurl = "http://maps.googleapis.com/maps/api/geocode/json?" + urllib.urlencode(args)
    result = simplejson.load(urllib.urlopen(googurl))

    #{'lat': 21.6345879, 'lng': -160.0860288}
    sw=[str(result['results'][0]['geometry']['viewport']['southwest']['lng']),str(result['results'][0]['geometry']['viewport']['southwest']['lat'])]
    ne=[str(result['results'][0]['geometry']['viewport']['northeast']['lng']),str(result['results'][0]['geometry']['viewport']['northeast']['lat'])]

    return [sw,ne]

# Terms for being "wasted"
terms = { 'drunk','wasted','buzzed','hammered','plastered' }

# A fuzzy regex for people who can't type
pat = regex.compile(r"(?:\L<terms>){i,d,s,e<=2}$", regex.I, terms=terms)

# Connect to the Twitter streaming API
url   = "https://stream.twitter.com/1/statuses/filter.json"
location=geosearch(" ".join(sys.argv[1:]))
parms = {
    'locations' : location[0][0]+","+location[0][1]+","+location[1][0]+","+location[1][1]
    }
auth  = ('diffra','password')
r = requests.post(url, data=parms, auth=auth)

# Print possible candidates
for line in r.iter_lines():
    if line:
        tweet = json.loads(line)
        status = tweet.get('text',u'')
        words = status.split()
        if any(pat.match(word) for word in words):
           print(tweet['user']['screen_name'], status)


import random
import sqlite3

class Account:

    @staticmethod
    def get_card_checksum(card_number: str) -> int:
        numbers_sum = 0
        for index in range(15):
            number = int(card_number[index])
            if index % 2 == 0:
                number *= 2
            if number > 9:
                number -= 9
            numbers_sum += number
        numbers_sum %= 10
        checksum = 10 - numbers_sum
        return checksum % 10

    @staticmethod
    def generate() -> "Account":
        random_account_number = random.randint(0, 999999999)
        number_15 = "400000" + "%09d" % random_account_number
        number = number_15 + str(Account.get_card_checksum(number_15))
        pin = "%04d" % random.randint(0, 9999)
        return Account(number, pin)

    def __init__(self, _number: str, _pin: str):
        self.number = _number
        self.pin = _pin

class Database:

    def __init__(self, file_name: str):
        self.conn = sqlite3.connect(file_name)
        self.conn.execute("CREATE TABLE IF NOT EXISTS card(id INTEGER, number TEXT, pin TEXT, balance INTEGER DEFAULT 0)")
        self.conn.commit()

    def insert_account(self, account: Account) -> None:
        self.conn.execute("INSERT INTO card VALUES (?, ?, ?, ?)",
                          (account.number, account.number, account.pin, 0))
        self.conn.commit()

    def check_login(self, number: str, pin: str) -> bool:
        c = self.conn.cursor()
        c.execute('SELECT number FROM card WHERE number = :number AND pin = :pin',
                  {"number": number, "pin": pin})
        return c.fetchone() is not None

    def get_balance(self, number: str) -> int:
        c = self.conn.cursor()
        c.execute('SELECT balance FROM card WHERE number = :number',
                  {"number": number})
        return c.fetchone()[0]

    def add_income(self, account: Account, income: int) -> None:
        self.conn.execute('UPDATE card SET balance = balance + :income WHERE id = :number',
                  {'income': income, 'number': account.number})
        self.conn.commit()

    def make_transfer(self, account: Account, account_to_transfer: str, amount: int) -> None:
        c = self.conn.cursor()
        c.execute('SELECT balance FROM card WHERE id = :number',
                  {"number": account.number})
        current_balance = c.fetchone()[0]

        if amount_to_transfer > current_balance:
            print("Not enough money!")
            return

        self.conn.execute('UPDATE card SET balance = balance - :amount WHERE id = :number',
                          {'amount': amount, 'number': account.number})
        self.conn.execute('UPDATE card SET balance = balance + :amount WHERE id = :number',
                          {'amount': amount, 'number': account_to_transfer})
        self.conn.commit()
        print("Success!")

    def account_exists(self, number: str) -> bool:
        c = self.conn.cursor()
        c.execute('SELECT number FROM card WHERE number = :number',
                  {"number": number})
        return c.fetchone() is not None

    def close_account(self, account: Account) -> None:
        self.conn.execute('DELETE FROM card WHERE number = :number',
                          {'number': account.number})
        self.conn.commit()
        print("The account has been closed!")

    def close(self) -> None:
        self.conn.commit()
        self.conn.close()

def login():  # -> Account
    print("Enter your card number:")
    input_card_number = input()

    print("Enter your PIN:")
    input_pin = input()

    if not db.check_login(input_card_number, input_pin):
        print("Wrong card number or PIN!")
        return None

    print("You have successfully logged in!")
    return Account(input_card_number, input_pin)

if __name__ == '__main__':
    db = Database('card.s3db')

    while True:
        print()
        print("1. Create an account")
        print("2. Log into account")
        print("0. Exit")

        option = int(input())

        if option == 1:
            account = Account.generate()
            db.insert_account(account)
            print("Your card has been created")
            print("Your card number:")
            print(account.number)
            print("Your card PIN:")
            print(account.pin)

        elif option == 2:
            account = login()
            if account is None:
                continue

            while True:
                print()
                print("1. Balance")
                print("2. Add income")
                print("3. Do transfer")
                print("4. Close account")
                print("5. Log out")
                print("0. Exit")

                logged_option = int(input())

                if logged_option == 1:
                    balance = db.get_balance(account.number)
                    print(f"Balance: {balance}")

                elif logged_option == 2:
                    print("Enter income:")
                    income_to_add = int(input())
                    db.add_income(account, income_to_add)
                    print("Income was added!")

                elif logged_option == 3:
                    print("Transfer")

                    print("Enter card number:")
                    account_to_transfer = input()
                    if account_to_transfer == account.number:
                        print("You can't transfer money to the same account!")
                        continue
                    if Account.get_card_checksum(account_to_transfer) != int(account_to_transfer[-1:]):
                        print("Probably you made mistake in the card number. Please try again!")
                        continue
                    if not db.account_exists(account_to_transfer):
                        print("Such a card does not exist.")
                        continue

                    print("Enter how much money you want to transfer:")
                    amount_to_transfer = int(input())
                    db.make_transfer(account, account_to_transfer, amount_to_transfer)

                elif logged_option == 4:
                    db.close_account(account)
                    break

                elif logged_option == 5:
                    print("You have successfully logged out!")
                    break

                elif logged_option == 0:
                    db.close()
                    exit(0)

        elif option == 0:
            print("Bye!")
            break

    db.close()


import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, LineString
from .utils import report_progress
from .lsm import snap_to_waterbodies
import warnings

def get_dm_nodes(dw_keys_df, district, mz_type ="d", out_type=int):
    df = dw_keys_df[dw_keys_df.oid == district]
    dm_nodes = [out_type(i) for i in df[df.kty == mz_type].nid.unique()]
    return dm_nodes

def find_unique_dm_links(
        lsw_nodes_gdf: gpd.GeoDataFrame,
        dw_keys_df: pd.DataFrame,
        mz_type ="d") -> list:
    """Finding unique DM nodes."""
    lsw_dm_links = []
    for dw, df in dw_keys_df.groupby(by=["oid"]):
        dm_nodes = df.loc[df.kty == mz_type].nid.unique()
        if len(dm_nodes) == 1:
            df = lsw_nodes_gdf[lsw_nodes_gdf["DWRN"] == dw]
            for row in df.itertuples():
                lsw_dm_links += [(row.LSWFINAL, dm_nodes[0])]
    return lsw_dm_links

def find_routing_to_dm_links(
        lsw_nodes_gdf: gpd.GeoDataFrame,
        lsw_gdf: gpd.GeoDataFrame,
        dw_keys_df: pd.DataFrame,
        lkm_links_gdf: gpd.GeoDataFrame,
        lsm_lhm_gdf: gpd.GeoDataFrame,
        lkm_waterlichamen_gdf: gpd.GeoDataFrame,
        ) -> list:

    warnings.simplefilter(action='ignore', category=UserWarning)
    # algemene functie om links te vinden mbv een LKM link
    def find_links(row, lsw_id):
        lsw_dm_links = []
        polygon = lsw_gdf.at[lsw_id, "geometry"]
        df = lsm_lhm_gdf[lsm_lhm_gdf.within(polygon)]# we kijken of er lateralen in het gebied van de LSW liggen

        if not df.empty:
            lsw_dm_links +=[(lsw_id, i) for i in df.DM.unique()]

        if row.nodeto.startswith("LSM"): # de link moet starten met LSM
            point = Point(row.geometry.bounds[2:]) # de Point van de nodeto
            waterlichaam_gdf = lkm_waterlichamen_gdf[lkm_waterlichamen_gdf.intersects(point)] # het waterlichaam waar punt op snapt

            if not waterlichaam_gdf.empty: # er moet wél een waterlichaam gevonden worden
                waterlichaam = waterlichaam_gdf.iloc[0] # dan pakken we het eerste waterlichaam (als het goed is is de lengte altijd 1)
                df = lsm_lhm_snapped_gdf[lsm_lhm_snapped_gdf.within(waterlichaam.geometry)] # we zoeken naar lsm_lhm laterale knopen binnen het waterlichaam

                if not df.empty: # we hebben gevonden!
                    lsw_dm_links = [(lsw_id, i) for i in df.DM.unique()] # hier maken we links van de lsw_id naar de unieke DM-knopen
                    lsw_dm_links = [i for i in lsw_dm_links if i[1] in dm_nodes_stringified]

        return lsw_dm_links

    # set lsw_gdf index to LSWFINAL
    if lsw_gdf.index.name !="LSWFINAL":
        lsw_gdf.set_index("LSWFINAL", inplace=True)

    # snap lsm_lhm to water-bodies
    lsm_lhm_snapped_gdf = snap_to_waterbodies(lsm_lhm_gdf, lkm_waterlichamen_gdf, offset=250)

    all_links = []

    # iterate trough lsw_nodes and find
    for idx, row in enumerate(lsw_nodes_gdf.itertuples()):
        report_progress(idx, len(lsw_nodes_gdf))
        lsw_id = row.LSWFINAL
        district = row.DWRN
        dm_nodes = get_dm_nodes(dw_keys_df, district)
        dm_nodes_stringified = [str(i) for i in dm_nodes]
        lkm_links_iter = lkm_links_gdf[lkm_links_gdf.nodefrom == str(lsw_id)].itertuples()

        for row in lkm_links_iter:
            links = []
            links = find_links(row, lsw_id)

            if (not links):
                counter = 0

                while (not links) and (counter < 10) :
                    df = lkm_links_gdf[lkm_links_gdf.nodefrom == row.nodeto]

                    if not df.empty:
                        row = df.iloc[0]
                        links = find_links(row, lsw_id)
                    counter += 1

            all_links += [i for i in links if i not in all_links]

    return all_links

def find_shortest_dm_links(
        lsw_nodes_gdf: gpd.GeoDataFrame,
        dw_keys_df: pd.DataFrame,
        dm_nodes_gdf: gpd.GeoDataFrame
        ):

    all_links = []
    for row in lsw_nodes_gdf.itertuples():
        lsw_id = row.LSWFINAL
        district = row.DWRN
        dm_nodes = get_dm_nodes(dw_keys_df, district)
        dm_nodes = [str(i) for i in dm_nodes]

        dm_node = dm_nodes_gdf.at[dm_nodes_gdf.loc[dm_nodes_gdf.ID.isin(dm_nodes)].distance(row.geometry).sort_values().index[0], "ID"]

        all_links += [(lsw_id, int(dm_node))]

    return all_links

def links_to_geodataframe(
        lsw_dm_links: list,
        lsw_nodes_gdf: gpd.GeoDataFrame,
        dm_nodes_gdf: gpd.GeoDataFrame
        ):
    """Convert lsw_dm_links list to a GeoDataFrame with LineStrings."""
    warnings.simplefilter(action='ignore', category=FutureWarning)

    lsw_dm_links_gdf = gpd.GeoDataFrame(lsw_dm_links, columns = ["node_from", "node_to"], geometry = gpd.GeoSeries(), crs=28992)

    def make_line_string(row):
        report_progress(row._name, len(lsw_dm_links_gdf))
        point_from = lsw_nodes_gdf.at[row.node_from, "geometry"]
        point_to = dm_nodes_gdf.loc[dm_nodes_gdf.ID == str(row.node_to)].geometry

        return LineString([[point_from.x, point_from.y], [point_to.x, point_to.y]])

    lsw_dm_links_gdf.loc[:, "geometry"] = lsw_dm_links_gdf.apply((lambda x: make_line_string(x)), axis=1)

    lsw_dm_links_gdf["node_from"] = lsw_dm_links_gdf["node_from"].astype(str)
    lsw_dm_links_gdf["node_to"] = lsw_dm_links_gdf["node_to"].astype(str)

    return lsw_dm_links_gdf


from odoo import models, _

import logging
_logger = logging.getLogger(__name__)

class PaymentTransaction(models.Model):
    _inherit = 'payment.transaction'

    # Override to confirm payments totaling the amount_total_deposit
    def _check_amount_and_confirm_order(self):
        self.ensure_one()
        for order in self.sale_order_ids.filtered(lambda so: so.state in ('draft', 'sent')):
            # default amount as originally calculated
            amount = order.amount_total
            if order.amount_total_deposit:
                amount = order.amount_total_deposit
            if order.currency_id.compare_amounts(self.amount, amount) == 0:
                order.with_context(send_email=True).action_confirm()
            else:
                _logger.warning(
                    '<%s> transaction AMOUNT MISMATCH for order %s (ID %s): expected %r, got %r',
                    self.acquirer_id.provider,order.name, order.id,
                    amount, self.amount,
                )
                order.message_post(
                    subject=_("Amount Mismatch (%s)") % self.acquirer_id.provider,
                    body=_("The order was not confirmed despite response from the acquirer (%s): order total is %r but acquirer replied with %r.") % (
                        self.acquirer_id.provider,
                        amount,
                        self.amount,
                    )
                )


from django.urls import path

from .views import PlanCreateAPIView, PlanListAPIView, PlanRetrieveUpdateDestroyAPIView

urlpatterns = [
    #  subscriptions plan urls
    path("", PlanListAPIView.as_view(), name="list_subscriptions"),
    path("<str:id>/", PlanRetrieveUpdateDestroyAPIView.as_view(), name="create_retrieve_destroy"),
    path("plan/create/", PlanCreateAPIView.as_view(), name="create_subscription"),

]


# Pygame library
import pygame

# Dash Display
class Dash(pygame.sprite.Sprite):
    # Properties
    surface = pygame.Surface((150,40))
    font = None

    # Constructor
    def __init__(self):
        """
        Entrée: None
        Sortie: None
        """
        # Call Sprite Constructor
        pygame.sprite.Sprite.__init__(self)

    # Update
    def update(self, rpm, gear, text=" "):
        """
        Entrée: RPM, Vitesse Actuelle, Texte Optionel
        Sortie: None
        """

        # Font
        if self.font == None:
            self.font = pygame.font.Font("../assets/font.ttf", 10)

        # Reset Dash
        self.surface.fill((0,255,0))
        self.surface.set_colorkey((0,255,0))

        # Display RPM
        self.surface.blit(
            self.font.render(str(rpm)+" RPM", False, (0,0,0)),
            (0,0)
        )
        # Display Gear
        self.surface.blit(
            self.font.render("Gear "+str(gear), False, (0,0,0)),
            (0,15)
        )
        # Display Text for Debug
        self.surface.blit(
            self.font.render(text, False, (0,0,0)),
            (0,30)
        )


import requests, urllib, re, os, json, pyrebase
from bs4 import BeautifulSoup as BS
import txt
# print(txt.user)
session = requests.session()
pageURL = 'https://zenpencils.com/'
page = session.post(pageURL)
# print(page.text)
# Home Page details
# Finding total number of pages
homepage = BS(page.text,"html.parser").find('form',{'class':'comic-list-dropdown-form'})
# print(homepage)
pages = homepage.find_all('option',{'class':'level-0'})
print("\n  Finding Links .... \n ")

for i in pages:
    print(json.dumps({'key':i.get_text(),'value':i['value']}))
    print(" Writing Links into FDB ....  ")
    txt.db.child("zenpencils").push({'key':i.get_text(),'value':i['value']}, txt.user['idToken'])
# print(json.dumps(data))


import queue
import time

import pytest

from johnsnowlabs import nlp, settings
from johnsnowlabs.auto_install.databricks.install_utils import (
    cluster_exist_with_name_and_runtime,
    get_db_client_for_token,
    get_cluster_id,
    wait_till_cluster_running,
    _get_cluster_id,
)
from johnsnowlabs.auto_install.health_checks.generate_endpoint_test import (
    generate_endpoint_test,
)
from johnsnowlabs.utils.enums import DatabricksClusterStates
from tests.utilsz import secrets as sct

@pytest.fixture()
def azure_gpu_node_type():
    return "Standard_NC4as_T4_v3"

@pytest.fixture()
def azure_cpu_node():
    return "Standard_DS3_v2"

@pytest.fixture()
def aws_cpu_node():
    return "i3.xlarge"

@pytest.fixture()
def azure_creds():
    import tests.utilsz.secrets as sct

    lic = sct.db_lic_azure
    host = sct.ckl_host_azure
    token = sct.ckl_token_azure
    return lic, host, token

@pytest.fixture()
def azure_trial_creds():
    import tests.utilsz.secrets as sct

    lic = sct.azure_trail_lic2
    host = sct.azure_trail_host2
    token = sct.azure_trail_token2
    return lic, host, token

@pytest.fixture()
def aws_creds():
    import tests.utilsz.secrets as sct

    lic = sct.ckl_lic_aws
    host = sct.ckl_host_aws
    token = sct.ckl_token_aws
    return lic, host, token

@pytest.fixture
def creds(request):
    return request.getfixturevalue(request.param)

@pytest.fixture
def node_type(request):
    return request.getfixturevalue(request.param)

def host_creds(host):
    if host == "aws":
        return aws_creds()
    elif host == "azure":
        return azure_creds()
    else:
        raise Exception("Unknown host")

def assert_job_suc(state):
    assert state["state"]["result_state"] == "SUCCESS"

def run_endpoint_tests(test_cluster_id, host, token, model):
    # generate job.py script and submit it
    assert_job_suc(
        nlp.run_in_databricks(
            generate_endpoint_test(model, sct.container_lic_json),
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name=f"endpoint_creation_test_run_{model}",
        )
    )

def run_cluster_test_suite(test_cluster_id, host, token):
    # run test suite again a existing cluster

    # Test modules
    import johnsnowlabs.auto_install.health_checks.hc_test as hc_test
    import johnsnowlabs.auto_install.health_checks.ocr_test as ocr_test
    import johnsnowlabs.auto_install.health_checks.nlp_test as nlp_test

    assert_job_suc(
        nlp.run_in_databricks(
            nlp_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="nlp_test",
        )
    )

    assert_job_suc(
        nlp.run_in_databricks(
            hc_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="hc_test",
        )
    )

    assert_job_suc(
        nlp.run_in_databricks(
            ocr_test,
            databricks_cluster_id=test_cluster_id,
            databricks_host=host,
            databricks_token=token,
            run_name="ocr_test",
        )
    )

def get_one_model_per_class():
    # todo actually generate from spellbook
    return ["sentiment", "ner", "spell", "bert", "elmo", "albert", "roberta"]

def delete_all_test_clusters(db_client):
    for i in range(10):
        while _get_cluster_id(db_client, f"TEST_CLUSTER_{i}"):
            cluster_id = _get_cluster_id(db_client, f"TEST_CLUSTER_{i}")
            db_client.cluster.permanent_delete_cluster(cluster_id)
            print(f"Deleted cluster {cluster_id}")
            time.sleep(5)

def get_or_create_test_cluster(
    creds, node_type, n=0, runtime=None, clean_workspace=False
):
    """
    Creates a cluster with name TEST_CLUSTER_{n} and runtime runtime if it doesn't exist.
    If it exists, it checks if it's running and if not, it starts it.
    "exists" means another cluster with same name and runtime exists.
    """
    lic, host, token = creds
    # runtime = "9.1.x-scala2.12"
    if runtime is None:
        runtime = settings.db_spark_version
    cluster_name = f"TEST_CLUSTER_{n}"
    db_client = get_db_client_for_token(host, token)

    # 1) Check if cluster with name and correct runtime exists and it's running state If not, create it.
    if cluster_exist_with_name_and_runtime(db_client, cluster_name, runtime):
        cluster_id = get_cluster_id(db_client, cluster_name, runtime)

        status = DatabricksClusterStates(
            db_client.cluster.get_cluster(
                cluster_id,
            )["state"]
        )

        # if status is terminated, try restart it
        if status == DatabricksClusterStates.TERMINATED:
            db_client.cluster.start_cluster(cluster_id=cluster_id)
            wait_till_cluster_running(db_client, cluster_id)
            status = DatabricksClusterStates(
                db_client.cluster.get_cluster(
                    cluster_id,
                )["state"]
            )
        if status == DatabricksClusterStates.RUNNING:
            return cluster_id
        else:
            print(
                f"Could not find or start {cluster_name} with runtime {runtime} creating a new one"
            )

    else:
        #  no cluster exists, create new one
        cluster_id = nlp.install(
            spark_version=runtime,
            json_license_path=lic,
            databricks_host=host,
            databricks_token=token,
            driver_node_type_id=node_type,
            node_type_id=node_type,
            cluster_name=cluster_name,
            clean_cluster=clean_workspace,
            block_till_cluster_ready=True,
        )
        return cluster_id

def subtester_thread(cluster_id, job_que, host, token, results):
    while not job_que.empty():
        try:
            model = job_que.get_nowait()
        except queue.Empty:
            return
        try:
            run_endpoint_tests(cluster_id, host, token, model)
            print(f"{model} success!")
            results[model] = "SUCCESS"
        except Exception as e:
            print(f"{model} failed!")
            results[model] = f"FAILED: {str(e)}"

db_cloud_node_params = pytest.mark.parametrize(
    "creds, node_type",
    [
        ("aws_creds", "aws_cpu_node"),
        ("azure_creds", "azure_cpu_node"),
        # ("azure_creds", "azure_gpu_node_type"),
    ],
    indirect=["creds", "node_type"],
)
db_cloud_params = pytest.mark.parametrize(
    "creds",
    [
        "aws_creds",
        "azure_creds",
    ],
    indirect=["creds"],
)


import json
from rdflib import Graph, Namespace, URIRef, Literal
from modules.mention_handler import MentionHandler
from modules.attribute_handler import AttributeHandler
from modules.entity_handler import EntityHandler
from modules.relation_handler import RelationHandler
from modules.coref_handler import CorefHandler

SCHEMA_ORG_NAMESPACE = "http://schema.org/"
SCHEMA_ORG = Namespace(SCHEMA_ORG_NAMESPACE)

class Pipeline():

    def __init__(self, g):
        self.g = g

    def setup(self, config, metadata):
        # self.tokenizer = CoreNLPProcessor(config)

        # self.tokenizer.process("test sentence")

        self.mention_handler = MentionHandler(config)
        self.attribute_handler = AttributeHandler(config, self.mention_handler.client, self.mention_handler.annotators,
                                          self.mention_handler.props)
        self.coref_handler = CorefHandler(config, self.mention_handler.client, self.mention_handler.annotators,
                                          self.mention_handler.props)

        relations = json.load(open("../rules/edges/relations.json"))
        queries = json.load(open("../rules/nodes/queries.json"))

        self.g = Graph()
        self.metadata = metadata

        self.entity_handler = EntityHandler(relations, queries, self.g, self.metadata)

        self.relation_handler = RelationHandler(relations, self.mention_handler.client, self.mention_handler.annotators,
                                                self.mention_handler.props)

    def process(self, sentence):
        result, extractions = self.mention_handler.process_sentence(sentence)
        print(*[ent for ent in extractions], sep='\n')
        attribute_response = self.attribute_handler.process(sentence, result, extractions)
        self.coref_handler.process(sentence, result, extractions)
        print(*[ent for ent in extractions], sep='\n')

        entity_mentions_index = {}
        entity_response = self.entity_handler.process(extractions, entity_mentions_index)
        relation_response = self.relation_handler.process_relations(sentence, entity_mentions_index, self.g, result)

        for s, p, o in self.g:
            print((s, p, o))

        #visualize_graph(self.g)

        response = ''
        if len(attribute_response) > 0:
            response += 'Attributes: ' + '
'.join(attribute_response) + '
'

        if len(entity_response) > 0:
            response += 'Entities: ' + ', '.join(entity_response) + '
'

        if len(relation_response) > 0:
            response += 'Relations: ' + '
'.join(relation_response) + '
'

        return response


#!/usr/bin/python3
"""class BaseModel"""

import uuid
from datetime import datetime
import time
import models

class BaseModel:
    """class BaseModel defines common sttributes for all classes"""

    def __init__(self, *args, **kwargs):
        """initializes object"""
        if kwargs:
            for key, value in kwargs.items():
                if key == "created_at" or key == "updated_at":
                    value = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
                self.__dict__[key] = value

        else:
            self.id = str(uuid.uuid4())
            self.created_at = datetime.now()
            self.updated_at = datetime.now()
            models.storage.new(self)

    def __str__(self):
        """prints class name and id"""
        return ("[{}] ({}) {}".format(type(self).__name__,
                self.__class__.__name__, self.id, self.__dict__))

    def to_dict(self):
        """returns dictionary containing key values"""
        newDict = self.__dict__.copy()
        newDict["__class__"] = self.__class__.__name__
        newDict["created_at"] = self.created_at.isoformat()
        newDict["updated_at"] = self.updated_at.isoformat()

        return newDict

    def save(self):
        """updates public instance with current datetime"""
        self.updated_at = datetime.now()
        models.storage.new(self)
        models.storage.save()


#!/usr/bin/env python3
"""Custom killing of apps."""
import sys

import i3ipc
from i3_helpers import sh

CTRL_Q = ['Slack', 'discord']
CUSTOM = ['Chromium', 'Brave-browser']

def _get_windows(i3, which='all', get_ws=False):
    tree = i3.get_tree()
    focused_win = tree.find_focused()
    if which == 'focused':
        windows = [focused_win]
    elif which == 'all':
        windows = [win for ws in tree.workspaces() for win in ws.leaves()]
    if get_ws:
        return windows, focused_win.workspace().name
    else:
        return windows

def kill_custom(i3, which):
    """Kill apps differently according to their class."""
    windows, focused_ws = _get_windows(i3, which, get_ws=True)
    for win in windows:
        i3.command(f'[con_id={win.id}] focus')
        win_class = win.window_class
        if win_class in CUSTOM:
            sh(f'xdotool key --window {win.window} comma+k+v')
            # If the window was not killed using xdotool then do kill it with i3
            remaining_window_classes = [win.window_class for win in _get_windows(i3)]
            if win_class in remaining_window_classes:
                i3.command('kill')
        elif win_class in CTRL_Q:
            sh(f'xdotool key --window {win.window} ctrl+q')
        else:
            i3.command('kill')
    i3.command(f'workspace {focused_ws}')

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--which', '-w', default='focused')
    pars_args = parser.parse_args()

    i3_conn = i3ipc.Connection()
    kill_custom(i3_conn, which=pars_args.which)
    sys.exit(0)


import sys
import json
from PyQt5 import Qsci
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont
from PyQt5.QtCore import Qt, QRegExp
from gui import Ui_PyJaMa

class JsonSyntaxHighlighter(QSyntaxHighlighter):
    def __init__(self, parent):
        super(JsonSyntaxHighlighter, self).__init__(parent)
        self.json_format = QTextCharFormat()
        self.json_format.setForeground(Qt.darkBlue)
        self.json_format.setFontWeight(QFont.Bold)

        self.string_format = QTextCharFormat()
        self.string_format.setForeground(Qt.darkGreen)

        self.number_format = QTextCharFormat()
        self.number_format.setForeground(Qt.red)

    def highlightBlock(self, text):
        state = self.previousBlockState()
        startIndex = 0

        for i, char in enumerate(text):
            if state == 0:
                if char == '"':
                    if i == 0 or text[i - 1] != '\\':
                        startIndex = i
                        state = 1
                elif char in ('{', '}', '[', ']'):
                    self.setFormat(i, 1, self.json_format)
                elif char.isDigit() or char in ('-', '.', 'e', 'E'):
                    state = 2
            elif state == 1:  # Inside a string
                if char == '"' and text[i - 1] != '\\':
                    self.setFormat(startIndex, i - startIndex + 1, self.string_format)
                    state = 0
            elif state == 2:  # Inside a number
                if not (char.isDigit() or char in ('-', '.', 'e', 'E')):
                    self.setFormat(startIndex, i - startIndex, self.number_format)
                    state = 0

        self.setCurrentBlockState(state)

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_PyJaMa()  # Create an instance of the generated UI class

        self.ui.setupUi(self)  # Set up the UI
        Qsci.QsciLexerJSON(self.ui.field_input)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())


import os
import torch

from fairseq.tasks import FairseqTask, register_task

from sightseq.data import CocoDetectionDataset
from sightseq.data.coco_utils import ConvertCocoPolysToMask
import sightseq.data.transforms as T

@register_task('object_detection')
class ObjectDetectionTask(FairseqTask):
    """
    Train a object detection model.

    Args:
        transforms:
    """

    @staticmethod
    def add_args(parser):
        """Add task-specific arguments to the parser."""
        # fmt: off
        parser.add_argument('data', metavar='FOLDER',
                            help='path to data directory')
        parser.add_argument('--num-classes', type=int, default=-1, metavar='N',
                            help='number of output classes of the model (including the background).'
                                 ' If box_predictor is specified, num_classes should be None')
        parser.add_argument('--max-positions', default=2048, type=int,
                            help='max input length')
        parser.add_argument('--aspect-ratio-group-factor', type=int, default=0, metavar='N',
                            help='data sampler with aspect ratio group')
        # fmt: on

    def __init__(
        self, args, num_classes, transforms=None,
        rpn_anchor_generator=None, rpn_head=None,
        box_roi_pool=None, box_predictor=None, box_head=None,
    ):
        super().__init__(args)
        self.num_classes = num_classes
        self.transforms = transforms
        self.rpn_anchor_generator = rpn_anchor_generator
        self.rpn_head = rpn_head
        self.box_roi_pool = box_roi_pool
        self.box_predictor = box_predictor
        self.box_head = box_head

    @classmethod
    def build_transforms(cls, args):
        transforms = [ConvertCocoPolysToMask()]
        transforms.append(T.ToTensor())

        return transforms

    @classmethod
    def setup_task(cls, args, **kwargs):
        """Setup the task (e.g., load dictionaries).

        Args:
            args (argparse.Namespace): parsed command-line arguments
        """
        # build transforms
        transforms = cls.build_transforms(args)
        return cls(
            args,
            num_classes=args.num_classes,
            transforms=transforms,
        )

    def load_dataset(self, split, **kwargs):
        """Load a given dataset split.

        Args:
            split (str): name of the split (e.g., train, valid, test)
        """
        # Read input images and targets
        image_root = os.path.join(self.args.data, '{}2017'.format(split))
        annotation_file = os.path.join(self.args.data, 'annotations', 'instances_{}2017.json'.format(split))

        t = self.transforms or []
        if split == 'train':
            t.append(T.RandomHorizontalFlip(0.5))
        transforms = T.Compose(t)

        self.datasets[split] = CocoDetectionDataset(image_root, annotation_file, transforms=transforms)

        # if split == 'train':
        #     self.dataset[split].remove_images_without_annotations()

    def build_generator(self, args):
        from sightseq.coco_generator import ObjectDetectionGenerator
        return ObjectDetectionGenerator()

    def valid_step(self, sample, model, criterion):
        model.train()
        with torch.no_grad():
            loss, sample_size, logging_output = criterion(model, sample)
        return loss, sample_size, logging_output

    def max_positions(self):
        """Return the max input length allowed by the task."""
        # The source should be less than *args.max_positions*
        return self.args.max_positions


from .base_dataset import Base, BaseDataset, CenterCrop, RandCrop3D, RandomRotion, RandomIntensityChange, RandomFlip, NumpyType, Pad
from .base_dataset import make_image_label, normalization, label_processing

import torch
import glob
import numpy as np
import nibabel as nib
from torchvision.transforms import Compose

from dataset.base_dataset import RandomRotion

class BraTS20(BaseDataset):
    def __init__(self, cfg):
        BaseDataset.__init__(self, cfg)
        if self.cfg.isTrain:
            self.paths = glob.glob(self.cfg.train_path+'/*')
            self.transforms = Compose([
                # search
                # RandCrop3D((128, 128, 48)),
                # RandomRotion(10),
                # RandomIntensityChange((0.1, 0.1)),
                # RandomFlip(0),
                # NumpyType((np.float32, np.float64)),

                # train
                RandCrop3D((192, 192, 48)), # 64, 64, 64
                RandomRotion(10),
                RandomIntensityChange((0.1, 0.1)),
                RandomFlip(0),
                NumpyType((np.float32, np.float64)),
            ])
        else:
            self.paths = glob.glob(self.cfg.val_path+'/*')
            self.transforms = Compose([
                Pad((0, 0, 0, 5, 0)),
                # CenterCrop((192, 192, 48)),
                NumpyType((np.float32, np.int64)),
            ])

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

    def __getitem__(self, idx):
        path = self.paths[idx]
        name = path.split('/')[-1]
        images, labels, affine = make_image_label(path)

        flair, t1, t1ce, t2 = images
        flair = normalization(flair)
        t1 = normalization(t1)
        t1ce = normalization(t1ce)
        t2 = normalization(t2)

        images = []
        images.append(flair)
        images.append(t1)
        images.append(t1ce)
        images.append(t2)
        images = np.asarray(images)
        # print('norm, max, min', images[0, ...].max(), images[0, ...].min(), images.shape)
        images, labels = images[..., np.newaxis], labels[np.newaxis, ...]
        images = np.swapaxes(images, 0, -1)

        images, labels = self.transforms([images, labels])
        labels = label_processing(labels)

        images = np.ascontiguousarray(images.transpose(0, 4, 1, 2, 3)[0])
        labels = np.ascontiguousarray(labels[0])

        images = torch.from_numpy(images)
        labels = torch.from_numpy(labels)

        # print('torch, maxmin, ', images[0].max(), images[0].min(), images.shape)

        return {'img': images, 'label': labels, 'name': name, 'affine': affine}

    def collate(self, batch):
        return [torch.cat(v) for v in zip(*batch)]