import xml.etree.ElementTree as ET
import os, sys, json, time
from appDiscovery import listDevices, listSoftware
from apkDiscovery import downloadAPK
from resultDiscovery import results

def getBillingID():

    billingID = None
    tree = ET.parse('placeholder/config.xml')
    root = tree.getroot()
    for connection in root:
        placeholderBillingID = connection.text

    os.system('clear')
    print('AutoCOVERT')
    print('\nUNDER DEVELOPMENT\n')
    billingIDInput = input('Enter billing ID: (Leave Blank to use Placeholder)')
    if billingIDInput == '':
        billingID = placeholderBillingID
    else:
        billingID = billingIDInput

    return billingID

def printProgramInfo(billingID,deviceList,appList,apkFolderPath):
    os.system('clear')
    print('AutoCOVERT\n\nUNDER DEVELOPMENT\n')
    print('Billing ID:',billingID)
    print('Number of Devices Found:',len(deviceList))
    print('Number of Applications Found:',len(appList))
    print('Path to downloaded APK\'s',apkFolderPath)
    shouldContinue = input('Press ENTER to continue: (Or type c to cancel)\n')
    if shouldContinue == '':
        pass
    else:
        sys.exit('\nProcess Ended by User Input\n')

def main():
    billingID = getBillingID()

    # Get list of all android devices on an institutional account
    deviceList = listDevices(billingID)
    appList = []

    # Create folder for downloaded APKs
    os.system('cd ~/working/covert/app_repo && mkdir testBundle')
    apkFolderPath = '~/working/Covert/app_repo/testBundle'

    # Create list of applications installed company-wide
    for device in deviceList:
        installedSoftware = listSoftware(device)
        for app in installedSoftware:
            appList.append(app)

    # Download APK File for every application in the appList
    for app in appList:
        downloadAPK(app,apkFolderPath)

    printProgramInfo(billingID,deviceList,appList,apkFolderPath)

    # Analyze Bundle Created
    os.system('clear')
    print('Analyzing with COVERT...')
    os.system('cd ~/working/covert && ./covert.sh testBundle')

    # Finalize Results
    results()

main()


#phân phối nhị thức âm

import math
def binomial(n, k):
    if(k >= 0 and k <= n):
        if(k == 0 or k == n):
            return 1
        else:
            result = 1
            for i in range(1, k+1):
                result *= (n-k+i) * 1.0 / i * 1.0;
            return int(result);
    else:
        return 0;

def prob(n, p, r):
    number = binomial(n, n-r+1) * 1.0
    return number * pow(p,r) * pow(1-p,n-r+1);

def infoMeasure(n, p, r) :
    number = prob(n, p, r)
    info = -math.log2(number)
    return info

def sumProb(N, p, r):
    sum = 0.0
    for i in range(r, N + 1):
        sum += prob(i, p, r)
    return sum

def approxEntropy(N, p, r):
    entropy = 0
    for i in range(r, N + 1):
        entropy += prob(i, p, r) * infoMeasure(i, p, r)
    return entropy


from xing.core.BasePlugin import BasePlugin
from xing.utils import http_req, get_logger
from xing.core import PluginType, SchemeType

class Plugin(BasePlugin):
    def __init__(self):
        super(Plugin, self).__init__()
        self.plugin_type = PluginType.POC
        self.vul_name = "发现 Apache Shiro"
        self.app_name = 'Shiro'
        self.scheme = [SchemeType.HTTP, SchemeType.HTTPS]

    def verify(self, target):
        url = target + "/"
        headers = {
            "Cookie": "rememberMe=1"
        }

        set_cookie = http_req(url, headers=headers).headers.get('Set-Cookie', "")

        if "rememberMe=deleteMe" in set_cookie:
            self.logger.success("found shiro {}".format(target))
            return True
        else:
            return False


import MapGeneration as MapGen

class Node:
    def __init__(self, coords, parent=None, child=set()):
        self.coords = coords
        self.parent = parent
        self.child = child

# limited area
def isValid(grid, curr, start, end):
    low = min([start[0], end[0]])
    high = max([start[0], end[0]])
    left = min([start[1], end[1]])
    right = max([start[1], end[1]])
    x = curr[0]
    y = curr[1]
    if x > high or x < low or y > right or y < left:  # is it out of bounds?
        return False
    return True

# finds the shortest path with highest sum(probs) to traverse
# if you want to get to point b, why travel over deep caves when you can travel
# over flat and search on the way

# DFS execution starting at (sx,sy) reaching (gx,gy), returns goal node if success, returns None if not
def DFS(grid, start, end):
    # gridlength = len(grid)
    # sx = start[0]
    # sy = start[1]
    # gx = end[0]
    # gy = end[1]
    startNode = Node(start)
    # stack = []
    # stack.append(startNode)
    visited = {start}
    curr = {startNode}

    while True:
        next_move = set()

        for x in curr:
            # print(x.coords)
            if x.coords == end:
                return startNode
            if isValid(grid, (x.coords[0] + 1, x.coords[1]), start, end):
                if (x.coords[0] + 1, x.coords[1]) not in visited:
                    empty = set()
                    new = Node((x.coords[0] + 1, x.coords[1]), x, empty)
                    next_move.add(new)
                    x.child = {new} | x.child
            if isValid(grid, (x.coords[0] - 1, x.coords[1]), start, end):
                if (x.coords[0] - 1, x.coords[1]) not in visited:
                    empty = set()
                    new = Node((x.coords[0] - 1, x.coords[1]), x, empty)
                    next_move.add(new)
                    x.child = {new} | x.child
            if isValid(grid, (x.coords[0], x.coords[1] + 1), start, end):
                if (x.coords[0], x.coords[1] + 1) not in visited:
                    empty = set()
                    new = Node((x.coords[0], x.coords[1] + 1), x, empty)
                    next_move.add(new)
                    x.child = {new} | x.child
            if isValid(grid, (x.coords[0], x.coords[1] - 1), start, end):
                if (x.coords[0], x.coords[1] - 1) not in visited:
                    empty = set()
                    new = Node((x.coords[0], x.coords[1] - 1), x, empty)
                    next_move.add(new)
                    x.child = {new} | x.child
        for v in curr:
            visited.add(v.coords)
        # print("visited", visited)
        curr = next_move
    # return startNode

# turns the non binary tree created by DFS into
def get_paths(startNode, path_list=[]):
    # print("start", startNode.coords, path_list)
    if len(startNode.child) == 0:
        curr = startNode
        path = []
        while curr.parent:
            path.insert(0, curr.coords)
            curr = curr.parent
        path.insert(0, curr.coords)
        # print(path)
        path_list.append(path)
        return path_list

    for child in startNode.child:
        # print("child", child.coords)
        path_list = get_paths(child, path_list)
    return path_list

# picks the path with the highest sum utility to traverse
def best_path(grid, probs, start, end):
    tree = DFS(grid, start, end)
    empty_list = []
    path_list = get_paths(tree, empty_list)

    best_utility = 0
    best_path = []
    for path in path_list:
        utility = 0
        for coord in path:
            x = coord[0]
            y = coord[1]
            utility += probs[x][y].utility
        if utility > best_utility:
            best_path = path
            best_utility = utility
    return best_path

# start = (4, 1)
# end = (5, 2)
# terrains = {0.1: "flat", 0.3: "hilly", 0.7: "forested", 0.9: "grid of caverns"}
# grid = MapGen.makeMap(10, terrains)
# tree = DFS(grid, start, end)
# path = get_paths(tree)
# for x in path:
#     print(x)
#     print()

# a = Node((0, 0))
# b = Node((1, 1), a)
# c = Node((2, 2), b)
# d = Node((3, 3), b)
# e = Node((4, 4), b)
# f = Node((5, 5), a)
# a.child = {b, f}
# b.child = {c, d}
# b.child.add(e)
# path_list = get_paths(a)
# # print(path_list)
# for x in path_list:
#     print(x)
#     print()


import bz2
import json
import sys
import time

import pywikibot

def main():
    wdsite = pywikibot.Site('wikidata', 'wikidata')
    p31 = {}
    c = 0
    t1 = time.time()
    dumpdate = sys.argv[1]
    f = bz2.open('/public/dumps/public/wikidatawiki/entities/%s/wikidata-%s-all.json.bz2' % (dumpdate, dumpdate), 'r')
    for line in f:
        line = line.decode('utf-8')
        line = line.strip('\n').strip(',')
        if line.startswith('{') and line.endswith('}'):
            json1 = json.loads(line)
            q = json1['id']
            if 'claims' in json1:
                if 'P31' in json1['claims']:
                    for x in json1['claims']['P31']:
                        p31x = ''
                        if 'mainsnak' in x and \
                           'datavalue' in x['mainsnak'] and \
                           'value' in x['mainsnak']['datavalue'] and \
                           'id' in x['mainsnak']['datavalue']['value']:
                            p31x = x['mainsnak']['datavalue']['value']['id']
                        else:
                            continue
                        if p31x:
                            #print(q, 'P31', p31x)
                            if p31x in p31:
                                p31[p31x] += 1
                            else:
                                p31[p31x] = 1
        c += 1
        if c % 1000 == 0:
            print(c, time.time()-t1)
            t1 = time.time()

    p31list = [[y, x] for x, y in p31.items()]
    p31list.sort(reverse=True)
    rows = []
    c = 1
    for x, y in p31list[:1000]:
        rows.append('| %s || {{Q|%s}} || %s ' % (c, y, x))
        c += 1
    rows = '\n|-\n'.join(rows)
    table = """
{| class="wikitable sortable"
! # !! {{P|P31}} !! Frequency
|-
%s
|}""" % (rows)
    page = pywikibot.Page(wdsite, 'User:Emijrp/sandbox2')
    page.text = table
    page.save('BOT - Updating table')
    #with open('dump.p31.txt', 'w') as f:
    #    f.write(output)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 10 14:39:58 2022

@author: tylerpruitt
"""

from objects import Metric

def movingAverage(stockDataList, period):
    """
    Moving Average

    Parameters
        stockDataList : a list of StockData objects ordered in time
        period : int, number of closing prices to average for each moving average

    Returns
        MA : a list of Metrics with values of moving averages
    """

    MA = []

    for i in range(len(stockDataList) - period + 1):
        total = 0

        for j in range(i, i+period):

            total += stockDataList[j].close

        total /= period

        MA.append(Metric(stockDataList[i + period - 1].datetime, total))

    return MA

def exponentialMovingAverage(stockDataList, period, smoothing):
    """
    Weighted Average

    Parameters
        stockDataList : a list of StockData objects ordered in time
        period : int, number of closing prices to average for each moving average
        smoothing : float or int, a customizable parameter

    Returns
        EMA : a list of Metrics with values of exponential moving averages
    """

    EMA = []

    # First calculate the moving average for the first length
    MA = 0

    for i in range(period):
        MA += stockDataList[i].close

    MA /= period

    EMA.append(Metric(stockDataList[period-1].datetime, MA))

    # Calculate the EMA for the rest of the lengths
    for i in range(period, len(stockDataList)):
        EMA.append(Metric(stockDataList[i].datetime, (stockDataList[i].close * (smoothing / (1 + period))) + (EMA[-1] * (1 - (smoothing / (1 + period))))))

    return EMA

def relativeStrengthIndex(stockDataList, period):
    """
    Relative Strength Index (RSI)

    Parameters
        stockDataList : a list of StockData objects ordered in time
        period : int, number of prices to average for each RSI

    Returns
        RSI : a list of Metrics with values of relative strength index
    """

    RSI = []

    for i in range(len(stockDataList) - period + 1):
        averageGain, averageLoss = 0, 0
        countGain, countLoss = 0, 0

        for j in range(i, i+period):
            if stockDataList[j].percentChange > 0:
                countGain += 1
                averageGain += stockDataList[j].percentChange
            elif stockDataList[j].percentChange < 0:
                countLoss += 1
                averageLoss += abs(stockDataList[j].percentChange)

        averageGain /= countGain
        averageLoss /= countLoss

        RSI.append(Metric(stockDataList[i + period - 1].datetime, 100 - 100 / (1 + (averageGain / averageLoss))))

    return RSI

def MACD(stockDataList, fastPeriod, slowPeriod):
    """
    Parameters
    ----------
    stockDataList : a list of StockData objects ordered in time
    fastPeriod : int, a period of data points
    slowPeriod : int, a period of data points

    Returns
    -------
    macd :

    Assumes smoothing of 1 in EMA calculation
    """
    macd = []

    fastEMA = exponentialMovingAverage(stockDataList, fastPeriod, 1)

    slowEMA = exponentialMovingAverage(stockDataList, slowPeriod, 1)

    for i in range(len(slowEMA)):
        for j in range(len(fastEMA)):
            if slowEMA[i].datetime == fastEMA[j].datetime:
                macd.append(Metric(slowEMA[i].datetime, fastEMA[j].value - slowEMA[i].value))

    return macd


from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase,sessionmaker
from sqlalchemy import Column, Integer, String, Date, Boolean, DateTime, Float
from sqlalchemy import text
import json
import requests
from datetime import datetime

fmp_api_key = ""
with open("env.json","r") as json_file:
    json_content = json.load(json_file)
    fmp_api_key = json_content["fmp_api"]["api_key"]

class Base(DeclarativeBase):
    # sqlalchemy.exc.InvalidRequestError: Cannot use 'DeclarativeBase'
    # directly as a declarative base class. Create a Base by creating a subclass of it.
    pass

class StocksListCron(Base):
    __tablename__ = "Stockslist"
    id = Column(Integer, primary_key=True)
    symbol = Column(String(10), unique=True, nullable=False)
    name = Column(String(100), nullable=False)
    sector = Column(String(50), nullable=False)
    subSector = Column(String(50), nullable=False)
    headQuarter = Column(String(100), nullable=True)
    dateFirstAdded = Column(Date, nullable=True)
    cik = Column(String(10), nullable=False)
    founded = Column(Date, nullable=False)
    isTracked = Column(Boolean, nullable=True)

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

    def get_stock_list(self):
        Session = sessionmaker(bind=self.engine)
        stocks_list = []
        with Session() as session:
            stocks_list_in_db = session.query(
                StocksListCron.name,
                StocksListCron.symbol,
            ).all()

            for stock in stocks_list_in_db:
                stocks_list.append((stock[1]))

        return stocks_list

class StockCron(Base):
    __tablename__ = (
        "default_value"  # Default table name is necessary, later changed in init call.
    )
    __table_args__ = {"extend_existing": True}
    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False)
    open = Column(Float, nullable=False)
    low = Column(Float, nullable=False)
    high = Column(Float, nullable=False)
    close = Column(Float, nullable=False)
    volume = Column(Float, nullable=False)

    def __init__(self, stock_index, stock_code, engine):
        self.stock_index = stock_index
        self.stock_code = stock_code
        self.engine = engine
        StockCron.__table__.name = stock_code

    def get_ohlc(self):
        if self.stock_index== "nasdaq":
            fmp_api = "https://financialmodelingprep.com/api/v3/quote/{}?apikey={}".format(
                self.stock_code, fmp_api_key
            )
        else:
            fmp_api = "https://financialmodelingprep.com/api/v3/quote/{}.NS?apikey={}".format(
                self.stock_code, fmp_api_key
            )
        response = requests.get(fmp_api).json()[0]
        # ltp = response["previousClose"]
        return response

    def update_ohlc_in_db(self, ohlc_data):
        open_val = ohlc_data["open"]
        high_val = ohlc_data["dayHigh"]
        low_val = ohlc_data["dayLow"]
        # update close_val with latest price that is fetched from fmpapis.
        close_val = ohlc_data["price"]
        volume_val = ohlc_data["volume"]
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        Session = sessionmaker(bind=self.engine)
        with Session() as session:
            sql_query_final = f"""
                INSERT INTO `{self.stock_code}` (date, open, high, low, close, volume)
                VALUES ('{current_time}', {open_val}, {high_val}, {low_val}, {close_val}, {volume_val})
            """
            final_result = session.execute(text(sql_query_final))
            session.commit()


# 15 Написать программу получающую набор произведений чисел от 1 до N.
# Пример: пусть N = 4, тогда [ 1, 2, 6, 24 ]

def factorial_row(n):
    ls = [x for x in range(1, n + 1)]
    mult = 1
    i = 0
    while i < n:
        ls[i] *= mult
        mult = ls[i]
        i += 1
    return ls

print(factorial_row(6))


"""Module that helps in setting up moderation roles for the server."""
from operator import mod
from typing import Text

import discord
from discord import Guild, Member, Role, TextChannel
from discord.ext import commands
from discord.ext.commands.context import Context

from zorander.core.models import ModerationRoles

class NotEnoughPermissions(commands.CommandError):
    """Class that raises errors when there is not enough permissions."""

    pass

class SetModerationRoles(commands.CommandError):
    """Class that raises errors when moderation roles are not setup"""

    pass

class NotHigherRole(commands.CommandError):
    """Class that raises errors when author's top role is lower than member's top role"""

    pass

class Permissions:
    """Custom class for setting guild perms."""

    async def has_permissions(self, member: Member, permission: str) -> bool:
        """
        Checks if the provided member has required permissions.

        Parameters
        ----------
        member : Member
            Member that needs to be checked
        permission : str
            Permission that needs to be checked.

        Returns
        -------
        bool
            Returns a boolean value if the user has the certain permission.
        """
        if getattr(member.guild_permissions, permission, False):
            return True
        return False

    async def rolecheck(self, member: Member, role: Role) -> bool:
        """Function that checks if member has a certain role

        Parameters
        ----------
        member : Member
            Member that needs to be checked for roles.
        role : Role
            Role that needs to be checked

        Returns
        -------
        bool
            Returns a boolean value if the user has certain roles.
        """
        if role in member.roles:
            return True
        return False

    async def fetch_role_data(self, guild: Guild) -> dict:
        model = await ModerationRoles.get_or_none(guild_id=guild.id)
        if model is None:
            raise SetModerationRoles(
                """Please setup moderation roles before using any moderation commands.
            Use `role admin <roleid>`, `role mod <roleid>`, `role staff <roleid>` to set moderation roles.
            """
            )
        adminrole = discord.utils.get(guild.roles, id=model.admin_role)
        modrole = discord.utils.get(guild.roles, id=model.mod_role)
        staffrole = discord.utils.get(guild.roles, id=model.staff_role)
        roles = {
            "adminrole": adminrole,
            "modrole": modrole,
            "staffrole": staffrole,
        }
        return roles

    async def staff_role_check(self, ctx: Context, guild: Guild) -> None:
        """
        Function that checks if member has the staff role for the guild

        Parameters
        ----------
        ctx : Context
            Context of the command invokation.
        guild : Guild
            Guild where the command was invoked in

        Raises
        ------
        NotEnoughPermissions
            Raise this error when the user doesn't have the staff role of the server.
        """
        staffrole = (await self.fetch_role_data(guild)).get("staffrole")
        if not (
            await self.has_permissions(ctx.author, "manage_messages")
            or await self.rolecheck(ctx.author, staffrole)
        ):
            raise NotEnoughPermissions(
                f"You don't have the required permissions or,\nYou don't have {staffrole.mention} role to run this command."
            )

    async def mod_role_check(self, ctx: Context, guild: Guild) -> None:
        """
        Function that checks if member has the mod role for the guild

        Parameters
        ----------
        ctx : Context
            Context of the command invokation.
        guild : Guild
            Guild where the command was invoked in

        Raises
        ------
        NotEnoughPermissions
            Raise this error when the user doesn't have the mod role of the server.
        """
        modrole = (await self.fetch_role_data(guild)).get("modrole")
        if not (
            await self.has_permissions(ctx.author, "kick_members")
            or await self.rolecheck(ctx.author, modrole)
        ):
            raise NotEnoughPermissions(
                f"You don't have the required permissions or,\nYou don't have {modrole.mention} role to run this command."
            )

    async def admin_role_check(self, ctx: Context, guild: Guild) -> None:
        """
        Function that checks if member has the admin role for the guild

        Parameters
        ----------
        ctx : Context
            Context of the command invokation.
        guild : Guild
            Guild where the command was invoked in

        Raises
        ------
        NotEnoughPermissions
            Raise this error when the user doesn't have the admin role of the server.
        """
        adminrole = (await self.fetch_role_data(guild)).get("adminrole")
        if not (
            await self.has_permissions(ctx.author, "manage_guild")
            or await self.rolecheck(ctx.author, adminrole)
        ):
            raise NotEnoughPermissions(
                f"You don't have the required permissions or,\nYou don't have {adminrole.mention} role to run this command."
            )

    async def log_channel_check(self, guild: Guild) -> TextChannel:
        """Function that check if the log channel exists, if not it creates one

        Returns
        -------
        TextChannel
            Log channel for the server.
        """
        log_channel = discord.utils.get(guild.text_channels, name="mod-logs")
        if log_channel is None:
            log_channel = await guild.create_text_channel("mod-logs")
            await log_channel.set_permissions(
                guild.default_role, view_channel=False, send_messages=False
            )
        return log_channel

    async def muted_role_check(self, guild: Guild) -> Role:
        """Function that checks for muted role in the server, if doesn't exist it creates it for you.

        Parameters
        ----------
        guild : Guild
            Guild where the command is invoked in.

        Returns
        -------
        Role
            Muted role of the server.
        """
        muted_role = discord.utils.get(guild.roles, name="Muted")
        if muted_role is None:
            muted_role = await guild.create_role(name="Muted")
            for channel in guild.channels:
                await channel.set_permissions(
                    muted_role,
                    speak=False,
                    send_messages=False,
                    read_message_history=True,
                )
        return muted_role

    def has_higher_role(self, author: discord.Member, member: discord.Member) -> None:
        """Checks if command invokation author's top role is greater than member's top role or not

        Parameters
        ----------
        author : discord.Member
            Author of the command invoked.
        member : discord.Member
            Member Object

        Raises
        ------
        NotHigherRole
            Raises this error when author's top role is greater than member's top role
        """
        if not author.top_role > member.top_role:
            raise NotHigherRole(
                "You cannot run moderation actions on the users on same rank as you or higher than you."
            )


load("//build/kernel/kleaf/impl:hermetic_exec.bzl", "hermetic_exec_test")
load(":py_test_hack.bzl", "run_py_binary_cmd")

visibility("//build/kernel/kleaf/...")

def kernel_module_test(
        name,
        modules = None,
        **kwargs):
    """A test on artifacts produced by [kernel_module](#kernel_module).

    Args:
        name: name of test
        modules: The list of `*.ko` kernel modules, or targets that produces
            `*.ko` kernel modules (e.g. [kernel_module](#kernel_module)).
        **kwargs: Additional attributes to the internal rule, e.g.
          [`visibility`](https://docs.bazel.build/versions/main/visibility.html).
          See complete list
          [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes).
    """
    test_binary = Label("//build/kernel/kleaf/artifact_tests:kernel_module_test")
    args = []
    data = [test_binary]
    if modules:
        args.append("--modules")
        args += ["$(rootpaths {})".format(module) for module in modules]
        data += modules

    hermetic_exec_test(
        name = name,
        data = data,
        script = run_py_binary_cmd(test_binary),
        args = args,
        timeout = "short",
        **kwargs
    )

def kernel_build_test(
        name,
        target = None,
        **kwargs):
    """A test on artifacts produced by [kernel_build](#kernel_build).

    Args:
        name: name of test
        target: The [`kernel_build()`](#kernel_build).
        **kwargs: Additional attributes to the internal rule, e.g.
          [`visibility`](https://docs.bazel.build/versions/main/visibility.html).
          See complete list
          [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes).
    """
    test_binary = Label("//build/kernel/kleaf/artifact_tests:kernel_build_test")
    args = []
    data = [test_binary]
    if target:
        args += ["--artifacts", "$(rootpaths {})".format(target)]
        data.append(target)

    hermetic_exec_test(
        name = name,
        data = data,
        script = run_py_binary_cmd(test_binary),
        args = args,
        timeout = "short",
        **kwargs
    )

def initramfs_modules_options_test(
        name,
        kernel_images,
        expected_modules_options,
        **kwargs):
    """Tests that initramfs has modules.options with the given content.

    Args:
        name: name of the test
        kernel_images: name of the `kernel_images` target. It must build initramfs.
        expected_modules_options: file with expected content for `modules.options`
        **kwargs: Additional attributes to the internal rule, e.g.
          [`visibility`](https://docs.bazel.build/versions/main/visibility.html).
          See complete list
          [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes).
    """
    test_binary = Label("//build/kernel/kleaf/artifact_tests:initramfs_modules_options_test")
    args = [
        "--expected",
        "$(rootpath {})".format(expected_modules_options),
        "$(rootpaths {})".format(kernel_images),
    ]

    hermetic_exec_test(
        name = name,
        data = [
            expected_modules_options,
            kernel_images,
            test_binary,
        ],
        script = run_py_binary_cmd(test_binary),
        args = args,
        timeout = "short",
        **kwargs
    )

def initramfs_modules_lists_test(
        name,
        kernel_images,
        expected_modules_list = None,
        expected_modules_recovery_list = None,
        expected_modules_charger_list = None,
        **kwargs):
    """Tests that the initramfs has modules.load* files with the given content.

    Args:
        name: name of the test
        kernel_images: name of the `kernel_images` target. It must build initramfs.
        expected_modules_list: file with the expected content for `modules.load`
        expected_modules_recovery_list: file with the expected content for `modules.load.recovery`
        expected_modules_charger_list: file with the expected content for `modules.load.charger`
        **kwargs: Additional attributes to the internal rule, e.g.
          [`visibility`](https://docs.bazel.build/versions/main/visibility.html).
          See complete list
          [here](https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes).
    """
    test_binary = Label("//build/kernel/kleaf/artifact_tests:initramfs_modules_lists_test")
    args = []

    if expected_modules_list:
        args += [
            "--expected_modules_list",
            "$(rootpath {})".format(expected_modules_list),
        ]

    if expected_modules_recovery_list:
        args += [
            "--expected_modules_recovery_list",
            "$(rootpath {})".format(expected_modules_recovery_list),
        ]

    if expected_modules_charger_list:
        args += [
            "--expected_modules_charger_list",
            "$(rootpath {})".format(expected_modules_charger_list),
        ]

    args.append("$(rootpaths {})".format(kernel_images))

    hermetic_exec_test(
        name = name,
        data = [
            expected_modules_list,
            expected_modules_recovery_list,
            expected_modules_charger_list,
            kernel_images,
            test_binary,
        ],
        script = run_py_binary_cmd(test_binary),
        args = args,
        timeout = "short",
        **kwargs
    )


import os
from flask import Flask, jsonify ,request, send_file
from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.sql import text
import pandas as pd
from datetime import datetime, timedelta
import requests

app = Flask(__name__)

# Define the database URL

DATABASE_URL = os.environ.get('DATABASE_URL')

db_connection = None

# Function to check database connection
def check_db_connection():
    try:
        db_connection.execute(text("select 1;"))
        return True
    except OperationalError as e:
        print("OperationalError:", e)
        return False
    finally:
        db_connection.rollback()

# Health check endpoint
@app.route('/health', methods=['GET'])
def health_check():
    if check_db_connection() and db_connection is not None:
        return jsonify({"status": "OK"}), 200
    else:
        return jsonify({"status": "Failure"}), 500

@app.route('/truck/<truck_id>', methods=['PUT'])
def update_provider_id(truck_id):
    new_provider_id = request.json.get('new_provider_id')
    if not new_provider_id :
        return jsonify({"error": "Provider ID is required"}), 400

    try:
        result = db_connection.execute(text("UPDATE Trucks SET provider_id =:provider_id WHERE id =:id"),{
            'id': truck_id,
            'provider_id': new_provider_id
        })
        if result.rowcount == 0:
            return jsonify({"error":"Truck ID not found"}),404
        db_connection.commit()
        return jsonify({"status": "OK"}), 200
    except Exception as e:
        print("Failed to update provider id with error:", e)
        return jsonify({"status": "Failure"}), 500

@app.route("/provider/<int:provider_id>", methods=["PUT"])
def update_provider(provider_id):
    provider_new_name = request.json.get('name')
    id = db_connection.execute(text("SELECT id FROM Provider WHERE id = :provider_id"),{
        'provider_id':provider_id
    }).fetchone()

    if provider_new_name is None:
        return jsonify({"error": "No Provider name is provided"}), 400
    if not id:
        return jsonify({"error": "Provider id is required"}), 400
    try:
        db_connection.execute(text("UPDATE Provider SET name =:name WHERE id= :id"),{
            'id':provider_id,
            'name':provider_new_name,
        })
        db_connection.commit()
        return jsonify({"status": "OK"}), 200
    except Exception as e:
        print("Failed to update provider name with error: ", e)
        return jsonify({"status": "Failure"}), 500

@app.route("/provider", methods=["POST"])
def create_provider():
    provider_name = request.json.get('provider_name')

    if not provider_name:
        return jsonify({"error": "provider name is required"}), 400

    try:
        result = db_connection.execute(text("INSERT INTO Provider (name) VALUES (:name)"), {'name': provider_name},)
        db_connection.commit()
        id =result.lastrowid
        return jsonify({'id': id}),201
    except Exception as e:
        print("failed to add provider name with error: ", e)
        return jsonify({"error": "Faild to add new provider name."}), 500

@app.route('/truck', methods=['POST'])
def add_truck_id():
    id = request.json.get('id')
    provider_id = request.json.get('provider_id')

    # Check if truck ID was provided
    if not id:
        return jsonify({"error": "Truck ID is required"}), 400

    try:
        db_connection.execute(text("INSERT INTO Trucks (id, provider_id) VALUES (:id, :provider_id)"), {
            'id': id,
            'provider_id': provider_id
        })
        db_connection.commit()
        return jsonify({"status": "success", "message": "Truck added!"}), 201
    except Exception as e:
        print("failed to add truck with error: ", e)
        return jsonify({"error": "Truck with this ID already exists or the provided provider_id doesn't match any existing providers."}), 400

@app.route('/truck/<truck_id>',methods=['GET'])
def get_truck_weight(truck_id):
    t1_str = request.args.get('from', datetime.now().replace(day=1).strftime('%Y%m%d%H%M%S'))
    t2_str = request.args.get('to', datetime.now().strftime('%Y%m%d%H%M%S'))

    t1 = datetime.strptime(t1_str, '%Y%m%d%H%M%S')
    t2 = datetime.strptime(t2_str, '%Y%m%d%H%M%S')
    weight_api = f"http://51.17.41.172:8081/item/{truck_id}?from={t1}&to={t2}"
    params = {

        "id": "id",
        "sessions": "sessions",
        "tara":  "tara"

    }
    try:
        response = requests.get(weight_api, params=params)
        return response.json(), 200
    except:
        return jsonify({"error": "invalid truck id"}), 400

# > Reminder: `Bruto = Neto (fruit) + Tara (truck) + sum(Tara (containers))`

@app.route('/bill/<id>', methods=['GET'])
def get_bill(id):
    t1 = request.args.get('from', datetime.now().replace(day=1).strftime('%Y%m%d%H%M%S'))
    t2 = request.args.get('to', datetime.now().strftime('%Y%m%d%H%M%S'))
    f = 'out'

    # Fetch provider name from the Provider table
    provider_name = db_connection.execute(text("SELECT name FROM Provider WHERE id = :id"), {'id': id}).fetchone()

    if not provider_name:
        return jsonify({"error": "Provider not found"}), 404

    provider_name = provider_name[0]

    # Fetch truck IDs associated with the given provider
    truck_ids = db_connection.execute(text("SELECT id FROM Trucks WHERE provider_id = :id"), {'id': id}).fetchall()
    truck_ids = [truck_id[0] for truck_id in truck_ids]

    # Fetch weight sessions data from the weight service for each truck
    total_amount = 0
    total_pay = 0
    products = []

    t1_formatted = datetime.strptime(t1, '%Y%m%d%H%M%S').strftime('%d/%m/%Y %H:%M:%S')
    t2_formatted = datetime.strptime(t2, '%Y%m%d%H%M%S').strftime('%d/%m/%Y %H:%M:%S')

    for truck_id in truck_ids:
        # Get weight records from the /weight API
        session_api = f"http://51.17.41.172:8081/weight?from={t1}&to={t2}&filter={f}"
        session_response = requests.get(session_api)

        if session_response.status_code == 200:
            session_data = session_response.json()
            for session_record in session_data:
                product = session_record.get("produce")
                neto = session_record.get("neto")

                if neto == 'na':
                    print("Skipping record with 'na' neto value")
                    continue

                # Fetch the rate based on the product and provider_id (scope)
                rate_query = text("""
                    SELECT rate FROM Rates
                    WHERE product_id = :product AND (scope = 'All' OR scope = :provider_id)
                """)
                rate_result = db_connection.execute(rate_query, {
                    'product': product,
                    'provider_id': id
                }).fetchone()

                if rate_result is not None:
                    rate = rate_result[0]
                    pay = float(neto) * float(rate)
                    total_amount += float(neto)
                    total_pay += pay

                    # Update the products list
                    products.append({
                        "product": product,
                        "count": 1,
                        "amount": float(neto),
                        "rate": float(rate),
                        "pay": pay
                    })
                else:
                    print(f"Failed to fetch rate data for product {product}")

        else:
            print(f"Failed to fetch session data for truck {truck_id}")

    bill = {
        "id": id,
        "name": provider_name,
        "from": t1_formatted,
        "to": t2_formatted,
        "truckCount": len(truck_ids),
        "products": products,
        "total": total_pay
    }

    return jsonify(bill), 200

@app.route('/rates', methods=['POST'])
def upload_rates_from_excel():
    try:
        file = request.files.get('file')

        if not file:
            return jsonify({"error": "No file provided"}), 400

        # Save the uploaded Excel file
        file_path = os.path.join('in', file.filename)
        file.save(file_path)

        # Process and save the rates from the Excel file
        process_rates_from_excel(file_path)

        return jsonify({"status": "success", "message": "Rates uploaded successfully"}), 201
    except Exception as e:
        print("Failed to upload rates with error:", e)
        return jsonify({"error": "Failed to upload rates"}), 500
    finally:
        if os.path.exists(file_path):
            os.remove(file_path)

# Function to process rates from Excel file
def process_rates_from_excel(file_path):
    rates_df = pd.read_excel(file_path)

    # Clear old rates before updating with new rates
    db_connection.execute(text("DELETE FROM Rates"))
    db_connection.commit()

    # Process and save the rates to the database
    for _, row in rates_df.iterrows():
        product_id = row['Product']
        rate = row['Rate']
        scope = row['Scope']
        # provider_id = row.get('Provider', None)  # Assuming 'Provider' column in the Excel

        # Handle scope-based rate or 'ALL' rate
        # if scope == 'ALL':
        #     provider_id = None

        # Insert rate into the database
        try:
            db_connection.execute(text("""
                INSERT INTO Rates (product_id, rate, scope)
                VALUES (:product_id, :rate, :scope)
            """), {
                'product_id': product_id,
                'rate': rate,
                'scope': scope
            })
            db_connection.commit()
        except Exception as e:
            print("Failed to update rates with error:", e)

@app.route('/rates', methods=['GET'])
def download_rates_as_excel():
    try:
        # Fetch rates from the database
        rates_df = pd.read_sql("SELECT r.product_id, r.rate, p.name AS scope FROM Rates r LEFT JOIN Provider p ON r.scope = p.id", db_connection)

        excel_path = 'in/rates.xlsx'
        rates_df.to_excel(excel_path, index=False)

        return send_file(excel_path, as_attachment=True), 200
    except Exception as e:
        print("Failed to download rates with error:", e)
        return jsonify({"error": "Failed to download rates"}), 500
    finally:
        if os.path.exists(excel_path):
            os.remove(excel_path)

def main():
    global db_connection

    try:
        engine = create_engine(DATABASE_URL)
        db_connection = engine.connect()
    except OperationalError as e:
        print("failed to connect to db with error: ", e)
        exit(1)

    app.run(host='0.0.0.0', port=5000, debug=True)

if __name__ == '__main__':
    main()


import matplotlib
import numpy
from matplotlib import pyplot

matplotlib.style.use('sv1_style.mplstyle')

ax_signal = pyplot.subplot(3, 1, 1)
signal = numpy.random.random_sample(36)*4 - 2 # random sample scaled -2 to 2
ax_signal.stem(range(36), signal, use_line_collection=True)
ax_signal.set(xlabel='Folgenkindex k ->', ylabel='Amplitude x[k]',
        title='Eingangssignal', xlim=[0, 35])

ax_window = pyplot.subplot(3, 1, 2)
window = numpy.concatenate([[0]*10, [1]*16, [0]*10])
ax_window.step(range(36), window, where='mid')
ax_window.set(xlabel='Folgenkindex k ->', ylabel='Amplitude x[k]',
        title='Rechteck-Fensterfunktion', xlim=[0, 35])

ax_filtered = pyplot.subplot(3, 1, 3)
filtered_signal = signal * window # filtering in time domain
ax_filtered.stem(range(36), filtered_signal, use_line_collection=True)
ax_filtered.set(xlabel='Folgenkindex k ->', ylabel='Amplitude x[k]',
        title='gefenstertes Signal', xlim=[0, 35])

pyplot.show()


import logging
import os
import threading

from beaker.middleware import SessionMiddleware
from genshi.template import loader
from genshi.template.plugin import MarkupTemplateEnginePlugin
from paste import gzipper
from paste.cascade import Cascade
from paste.registry import RegistryManager
from paste.response import header_value, remove_header
from paste.urlmap import URLMap
from paste.urlparser import StaticURLParser
from paste.deploy.converters import asbool
from paste.deploy.config import PrefixMiddleware
from pylons.middleware import ErrorHandler, StatusCodeRedirect
from pylons.wsgiapp import PylonsApp as _PylonsApp
from routes.middleware import RoutesMiddleware
import sqlalchemy
from sqlalchemy.pool import Pool
from sqlalchemy.exc import DisconnectionError
from tw.core.view import EngineManager
import tw.api

from mediadrop import monkeypatch_method
from mediadrop.config.environment import load_environment
from mediadrop.lib.auth import add_auth
from mediadrop.migrations.util import MediaDropMigrator
from mediadrop.model import metadata, DBSession
from mediadrop.plugin import events

log = logging.getLogger(__name__)

class PylonsApp(_PylonsApp):
    """
    Subclass PylonsApp to set our settings on the request.

    The settings are cached in ``request.settings`` but it's best to
    check the cache once, then make them accessible as a simple dict for
    the remainder of the request, instead of hitting the cache repeatedly.

    """
    def register_globals(self, environ):
        _PylonsApp.register_globals(self, environ)
        request = environ['pylons.pylons'].request

        if environ['PATH_INFO'] == '/_test_vars':
            # This is a dummy request, probably used inside a test or to build
            # documentation, so we're not guaranteed to have a database
            # connection with which to get the settings.
            request.settings = {
                'intentionally_empty': 'see mediadrop.config.middleware',
            }
        else:
            request.settings = self.globals.settings

def setup_prefix_middleware(app, global_conf, proxy_prefix):
    """Add prefix middleware.

    Essentially replaces request.environ[SCRIPT_NAME] with the prefix defined
    in the .ini file.

    See: http://wiki.pylonshq.com/display/pylonsdocs/Configuration+Files#prefixmiddleware
    """
    app = PrefixMiddleware(app, global_conf, proxy_prefix)
    return app

class DBSessionRemoverMiddleware(object):
    """Ensure the contextual session ends at the end of the request."""
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        try:
            return self.app(environ, start_response)
        finally:
            DBSession.remove()

class FastCGIScriptStripperMiddleware(object):
    """Strip the given fcgi_script_name from the end of environ['SCRIPT_NAME'].

    Useful for the default FastCGI deployment, where mod_rewrite is used to
    avoid having to put the .fcgi file name into the URL.
    """
    def __init__(self, app, fcgi_script_name='/mediadrop.fcgi'):
        self.app = app
        self.fcgi_script_name = fcgi_script_name
        self.cut = len(fcgi_script_name)

    def __call__(self, environ, start_response):
        script_name = environ.get('SCRIPT_NAME', '')
        if script_name.endswith(self.fcgi_script_name):
            environ['SCRIPT_NAME'] = script_name[:-self.cut]
        return self.app(environ, start_response)

def create_tw_engine_manager(app_globals):
    def filename_suffix_adder(inner_loader, suffix):
        def _add_suffix(filename):
            return inner_loader(filename + suffix)
        return _add_suffix

    # Ensure that the toscawidgets template loader includes the search paths
    # from our main template loader.
    tw_engines = EngineManager(extra_vars_func=None)
    tw_engines['genshi'] = MarkupTemplateEnginePlugin()
    tw_engines['genshi'].loader = app_globals.genshi_loader

    # Disable the built-in package name template resolution.
    tw_engines['genshi'].use_package_naming = False

    # Rebuild package name template resolution using mostly standard Genshi
    # load functions. With our customizations to the TemplateLoader, the
    # absolute paths that the builtin resolution produces are erroneously
    # treated as being relative to the search path.

    # Search the tw templates dir using the pkg_resources API.
    # Expected input: 'input_field.html'
    tw_loader = loader.package('tw.forms', 'templates')

    # Include the .html extension automatically.
    # Expected input: 'input_field'
    tw_loader = filename_suffix_adder(tw_loader, '.html')

    # Apply this loader only when the filename starts with tw.forms.templates.
    # This prefix is stripped off when calling the above loader.
    # Expected input: 'tw.forms.templates.input_field'
    tw_loader = loader.prefixed(**{'tw.forms.templates.': tw_loader})

    # Add this path to our global loader
    tw_engines['genshi'].loader.search_path.append(tw_loader)
    return tw_engines

def setup_tw_middleware(app, config):
    app_globals = config['pylons.app_globals']
    app = tw.api.make_middleware(app, {
        'toscawidgets.framework': 'pylons',
        'toscawidgets.framework.default_view': 'genshi',
        'toscawidgets.framework.engines': create_tw_engine_manager(app_globals),
    })
    return app

class DBSanityCheckingMiddleware(object):
    def __init__(self, app, check_for_leaked_connections=False,
                 enable_pessimistic_disconnect_handling=False):
        self.app = app
        self._thread_local = threading.local()
        self.is_leak_check_enabled = check_for_leaked_connections
        self.is_alive_check_enabled = enable_pessimistic_disconnect_handling
        self.pool_listeners = {}
        pool = self._pool()
        if self.is_leak_check_enabled or self.is_alive_check_enabled:
            sqlalchemy.event.listen(pool, 'checkout', self.on_connection_checkout)
            self.pool_listeners['checkout'] = self.on_connection_checkout
        if self.is_leak_check_enabled:
            sqlalchemy.event.listen(pool, 'checkin', self.on_connection_checkin)
            self.pool_listeners['checkin'] = self.on_connection_checkin

    def _pool(self):
        engine = metadata.bind
        return engine.pool

    def __call__(self, environ, start_response):
        try:
            return self.app(environ, start_response)
        finally:
            leaked_connections = len(self.connections)
            if leaked_connections > 0:
                msg = 'DB connection leakage detected: ' + \
                    '%d db connection(s) not returned to the pool' % leaked_connections
                log.error(msg)
                self.connections.clear()

    def tear_down(self):
        pool = self._pool()
        for target, fn in self.pool_listeners.items():
            sqlalchemy.event.remove(pool, target, fn)

    @property
    def connections(self):
        if not hasattr(self._thread_local, 'connections'):
            self._thread_local.connections = dict()
        return self._thread_local.connections

    def check_for_live_db_connection(self, dbapi_connection):
        # Try to check that the current DB connection is usable for DB queries
        # by issuing a trivial SQL query. It can happen because the user set
        # the 'sqlalchemy.pool_recycle' time too high or simply because the
        # MySQL server was restarted in the mean time.
        # Without this check a user would get an internal server error and the
        # connection would be reset by the DBSessionRemoverMiddleware at the
        # end of that request.
        # This functionality below will prevent the initial "internal server
        # error".
        #
        # This approach is controversial between DB experts. A good blog post
        # (with an even better discussion highlighting pros and cons) is
        # http://www.mysqlperformanceblog.com/2010/05/05/checking-for-a-live-database-connection-considered-harmful/
        #
        # In MediaDrop the check is only done once per request (skipped for
        # static files) so it should be relatively light on the DB server.
        # Also the check can be disabled using the setting
        # 'sqlalchemy.check_connection_before_request = false'.
        #
        # possible optimization: check each connection only once per minute or so,
        # store last check time in private attribute of connection object.

        # code stolen from SQLAlchemy's 'Pessimistic Disconnect Handling' docs
        cursor = dbapi_connection.cursor()
        try:
            cursor.execute('SELECT 1')
        except:
            msg = u'received broken db connection from pool, resetting db session. ' + \
                u'If you see this error regularly and you use MySQL please check ' + \
                u'your "sqlalchemy.pool_recycle" setting (usually it is too high).'
            log.warning(msg)
            # The pool will try to connect again up to three times before
            # raising an exception itself.
            raise DisconnectionError()
        cursor.close()

    def on_connection_checkout(self, dbapi_connection, connection_record, connection_proxy):
        if self.is_alive_check_enabled:
            self.check_for_live_db_connection(dbapi_connection)
        if self.is_leak_check_enabled:
#             if len(self.connections) > 0:
#                 import traceback
#                 traceback.print_stack(limit=15)
            self.connections[id(dbapi_connection)] = True

    def on_connection_checkin(self, dbapi_connection, connection_record):
        connection_id = id(dbapi_connection)
        # connections might be returned *after* this middleware called
        # 'self.connections.clear()', we should not break in that case...
        if connection_id in self.connections:
            del self.connections[connection_id]

def setup_db_sanity_checks(app, config):
    check_for_leaked_connections = asbool(config.get('db.check_for_leaked_connections', False))
    enable_pessimistic_disconnect_handling = asbool(config.get('db.enable_pessimistic_disconnect_handling', False))
    if (not check_for_leaked_connections) and (not enable_pessimistic_disconnect_handling):
        return app

    return DBSanityCheckingMiddleware(app, check_for_leaked_connections=check_for_leaked_connections,
                                      enable_pessimistic_disconnect_handling=enable_pessimistic_disconnect_handling)

def setup_gzip_middleware(app, global_conf):
    """Make paste.gzipper middleware with a monkeypatch to exempt SWFs.

    Gzipping .swf files (application/x-shockwave-flash) provides no
    extra compression and it also breaks Flowplayer 3.2.3, and
    potentially others.

    """
    @monkeypatch_method(gzipper.GzipResponse)
    def gzip_start_response(self, status, headers, exc_info=None):
        self.headers = headers
        ct = header_value(headers, 'content-type')
        ce = header_value(headers, 'content-encoding')
        self.compressible = False
        # This statement is the only change in this monkeypatch:
        if ct and (ct.startswith('text/') or ct.startswith('application/')) \
            and 'zip' not in ct and ct != 'application/x-shockwave-flash':
            self.compressible = True
        if ce:
            self.compressible = False
        if self.compressible:
            headers.append(('content-encoding', 'gzip'))
        remove_header(headers, 'content-length')
        self.headers = headers
        self.status = status
        return self.buffer.write
    return gzipper.make_gzip_middleware(app, global_conf)

def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
    """Create a Pylons WSGI application and return it

    ``global_conf``
        The inherited configuration for this application. Normally from
        the [DEFAULT] section of the Paste ini file.

    ``full_stack``
        Whether this application provides a full WSGI stack (by default,
        meaning it handles its own exceptions and errors). Disable
        full_stack when this application is "managed" by another WSGI
        middleware.

    ``static_files``
        Whether this application serves its own static files; disable
        when another web server is responsible for serving them.

    ``app_conf``
        The application's local configuration. Normally specified in
        the [app:<name>] section of the Paste ini file (where <name>
        defaults to main).

    """
    # Configure the Pylons environment
    config = load_environment(global_conf, app_conf)
    alembic_migrations = MediaDropMigrator.from_config(config, log=log)
    db_is_current = True
    if not alembic_migrations.is_db_scheme_current():
        log.warn('Running with an outdated database scheme. Please upgrade your database.')
        db_is_current = False
    plugin_mgr = config['pylons.app_globals'].plugin_mgr
    db_current_for_plugins = plugin_mgr.is_db_scheme_current_for_all_plugins()
    if db_is_current and not db_current_for_plugins:
        log.warn(db_current_for_plugins.message)
        db_is_current = False
    if db_is_current:
        events.Environment.database_ready()

    # The Pylons WSGI app
    app = PylonsApp(config=config)

    # Allow the plugin manager to tweak our WSGI app
    app = plugin_mgr.wrap_pylons_app(app)

    # Routing/Session/Cache Middleware
    app = RoutesMiddleware(app, config['routes.map'], singleton=False)
    app = SessionMiddleware(app, config)

    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)

    # add repoze.who middleware with our own authorization library
    app = add_auth(app, config)

    # ToscaWidgets Middleware
    app = setup_tw_middleware(app, config)

    # Strip the name of the .fcgi script, if using one, from the SCRIPT_NAME
    app = FastCGIScriptStripperMiddleware(app)

    # If enabled, set up the proxy prefix for routing behind
    # fastcgi and mod_proxy based deployments.
    if config.get('proxy_prefix', None):
        app = setup_prefix_middleware(app, global_conf, config['proxy_prefix'])

    # END CUSTOM MIDDLEWARE

    if asbool(full_stack):
        # Handle Python exceptions
        app = ErrorHandler(app, global_conf, **config['pylons.errorware'])

        # by default Apache uses  a global alias for "/error" in the httpd.conf
        # which means that users can not send error reports through MediaDrop's
        # error page (because that POSTs to /error/report).
        # To make things worse Apache (at least up to 2.4) has no "unalias"
        # functionality. So we work around the issue by using the "/errors"
        # prefix (extra "s" at the end)
        error_path = '/errors/document'
        # Display error documents for 401, 403, 404 status codes (and
        # 500 when debug is disabled)
        if asbool(config['debug']):
            app = StatusCodeRedirect(app, path=error_path)
        else:
            app = StatusCodeRedirect(app, errors=(400, 401, 403, 404, 500),
                                     path=error_path)

    # Cleanup the DBSession only after errors are handled
    app = DBSessionRemoverMiddleware(app)

    # Establish the Registry for this application
    app = RegistryManager(app)

    app = setup_db_sanity_checks(app, config)

    if asbool(static_files):
        # Serve static files from our public directory
        public_app = StaticURLParser(config['pylons.paths']['static_files'])

        static_urlmap = URLMap()
        # Serve static files from all plugins
        for dir, path in plugin_mgr.public_paths().iteritems():
            static_urlmap[dir] = StaticURLParser(path)

        # Serve static media and podcast images from outside our public directory
        for image_type in ('media', 'podcasts'):
            dir = '/images/' + image_type
            path = os.path.join(config['image_dir'], image_type)
            static_urlmap[dir] = StaticURLParser(path)

        # Serve appearance directory outside of public as well
        dir = '/appearance'
        path = os.path.join(config['app_conf']['cache_dir'], 'appearance')
        static_urlmap[dir] = StaticURLParser(path)

        # We want to serve goog closure code for debugging uncompiled js.
        if config['debug']:
            goog_path = os.path.join(config['pylons.paths']['root'], '..',
                'closure-library', 'closure', 'goog')
            if os.path.exists(goog_path):
                static_urlmap['/scripts/goog'] = StaticURLParser(goog_path)

        app = Cascade([public_app, static_urlmap, app])

    if asbool(config.get('enable_gzip', 'true')):
        app = setup_gzip_middleware(app, global_conf)

    app.config = config
    return app


import multiprocessing
from multiprocessing import Pool
import numpy as np
import pickle
import os
from pathlib import Path
from functools import partial

from skimage import morphology

from ._deriving_widgets import MM3Container, PlanePicker, FOVChooser
from magicgui.widgets import ComboBox, FileEdit

from .utils import organize_cells_by_channel

from ._deriving_widgets import (
    load_specs,
    load_time_table,
    information,
    warning,
    load_seg_stack,
    load_subtracted_stack,
    load_unmodified_stack,
    SegmentationMode,
)

def find_cell_intensities(
    params,
    time_table,
    fov_id,
    peak_id,
    Cells,
    midline=False,
    channel_name="c2",
    seg_mode=SegmentationMode.OTSU,
):
    """
    Finds fluorescenct information for cells. All the cells in Cells
    should be from one fov/peak. See the function
    organize_cells_by_channel()
    """

    # Load fluorescent images and segmented images for this channel
    try:
        sub_channel = "sub_" + channel_name
        # fl_stack = load_stack_params(params, fov_id, peak_id, postfix=sub_channel)
        fl_stack = load_subtracted_stack(params["ana_dir"], params["experiment_name"], fov_id, peak_id, sub_channel)
        information("Loading subtracted channel to analyze.")
    except FileNotFoundError:
        warning("Could not find subtracted channel! Skipping.")
        return

    # seg_stack = load_stack_params(params, fov_id, peak_id, postfix="seg_unet")
    seg_stack = load_seg_stack(
        ana_dir=params["ana_dir"],
        experiment_name=params["experiment_name"],
        fov_id=fov_id,
        peak_id=peak_id,
        seg_mode=seg_mode,
    )

    # determine absolute time index
    times_all = []
    for fov in time_table:
        times_all = np.append(times_all, list(time_table[fov].keys()))
    times_all = np.unique(times_all)
    times_all = np.sort(times_all)
    times_all = np.array(times_all, np.int_)
    t0 = times_all[0]  # first time index

    # Loop through cells
    for Cell in Cells.values():
        # give this cell two lists to hold new information
        Cell.fl_tots = []  # total fluorescence per time point
        Cell.fl_area_avgs = []  # avg fluorescence per unit area by timepoint
        Cell.fl_vol_avgs = []  # avg fluorescence per unit volume by timepoint

        if midline:
            Cell.mid_fl = []  # avg fluorescence of midline

        # and the time points that make up this cell's life
        for n, t in enumerate(Cell.times):
            # create fluorescent image only for this cell and timepoint.
            fl_image_masked = np.copy(fl_stack[t - t0])
            fl_image_masked[seg_stack[t - t0] != Cell.labels[n]] = 0

            # append total flourescent image
            Cell.fl_tots.append(np.sum(fl_image_masked))
            # and the average fluorescence
            Cell.fl_area_avgs.append(np.sum(fl_image_masked) / Cell.areas[n])
            Cell.fl_vol_avgs.append(np.sum(fl_image_masked) / Cell.volumes[n])

            if midline:
                # add the midline average by first applying morphology transform
                bin_mask = np.copy(seg_stack[t - t0])
                bin_mask[bin_mask != Cell.labels[n]] = 0
                med_mask, _ = morphology.medial_axis(bin_mask, return_distance=True)
                # med_mask[med_dist < np.floor(cap_radius/2)] = 0
                # print(img_fluo[med_mask])
                if np.shape(fl_image_masked[med_mask])[0] > 0:
                    Cell.mid_fl.append(np.nanmean(fl_image_masked[med_mask]))
                else:
                    Cell.mid_fl.append(0)

    # The cell objects in the original dictionary will be updated,
    # no need to return anything specifically.

def find_cell_intensities_worker(
    params,
    fov_id,
    peak_id,
    Cells,
    midline=True,
    channel="c2",
    seg_mode=SegmentationMode.OTSU,
):
    """
    Finds fluorescenct information for cells. All the cells in Cells
    should be from one fov/peak. See the function
    organize_cells_by_channel()
    This version is the same as find_cell_intensities but return the Cells object for collection by the pool.
    The original find_cell_intensities is kept for compatibility.
    """
    information("Processing peak {} in FOV {}".format(peak_id, fov_id))
    # Load fluorescent images and segmented images for this channel
    # fl_stack = load_stack_params(params, fov_id, peak_id, postfix=channel)
    fl_stack  = load_unmodified_stack(params["ana_dir"], params["experiment_name"], fov_id, peak_id, postfix=channel)
    # seg_stack = load_stack_params(params, fov_id, peak_id, postfix="seg_otsu")
    seg_stack = load_seg_stack(
        ana_dir=params["ana_dir"],
        experiment_name=params["experiment_name"],
        fov_id=fov_id,
        peak_id=peak_id,
        seg_mode=seg_mode,
    )

    # determine absolute time index
    time_table = load_time_table(params["ana_dir"])
    times_all = []
    for fov in time_table:
        times_all = np.append(times_all, [int(x) for x in fov.keys()])
    times_all = np.unique(times_all)
    times_all = np.sort(times_all)
    times_all = np.array(times_all, np.int_)
    t0 = times_all[0]  # first time index

    # Loop through cells
    for Cell in Cells.values():
        # give this cell two lists to hold new information

        fl_tots = []  # total fluorescence per time point
        fl_area = []  # avg fluorescence per unit area by timepoint
        fl_vol = []  # avg fluorescence per unit volume by timepoint

        if midline:
            Cell.mid_fl = []  # avg fluorescence of midline

        # and the time points that make up this cell's life
        for n, t in enumerate(Cell.times):
            # create fluorescent image only for this cell and timepoint.
            fl_image_masked = np.copy(fl_stack[t - t0])
            fl_image_masked[seg_stack[t - t0] != Cell.labels[n]] = 0

            # append total fluorescent image
            fl_tots.append(np.sum(fl_image_masked))
            # and the average fluorescence
            fl_area.append(np.sum(fl_image_masked) / Cell.areas[n])
            fl_vol.append(np.sum(fl_image_masked) / Cell.volumes[n])

            if midline:
                # add the midline average by first applying morphology transform
                bin_mask = np.copy(seg_stack[t - t0])
                bin_mask[bin_mask != Cell.labels[n]] = 0
                med_mask, _ = morphology.medial_axis(bin_mask, return_distance=True)
                if np.shape(fl_image_masked[med_mask])[0] > 0:
                    Cell.mid_fl.append(np.nanmean(fl_image_masked[med_mask]))
                else:
                    Cell.mid_fl.append(0)

        Cell.setattr("fl_tots_c{0}".format(channel), fl_tots)
        Cell.setattr("fl_area_c{0}".format(channel), fl_area)
        Cell.setattr("fl_vol_c{0}".format(channel), fl_vol)

    # return the cell object to the pool initiated by mm3_Colors.
    return Cells

# load cell file
def colors(params, fl_channel, seg_method, cellfile_path):
    """
    Finds fluorescenct information for cells.
    Parameters
    ----------
    params : dict
        Dictionary of parameters
    fl_channel : str
        Fluorescent channel to analyze
    seg_method : str
        Segmentation method to use
    cellfile_path : str
        Path to cell file

    Returns
    -------
    None
    """
    information("Loading cell data.")
    # if namespace.cellfile:
    #     cell_file_path = namespace.cellfile.name
    # else:
    #     warning('No cell file specified. Using complete_cells.pkl.')
    #     cell_file_path = p['cell_dir'] / 'complete_cells.pkl'

    with open(cellfile_path, "rb") as cell_file:
        Complete_Cells = pickle.load(cell_file)

    # if namespace.seg_method:
    #     seg_method = 'seg_'+str(namespace.seg_method)
    # else:
    #     warning('Defaulting to otsu segmented cells')
    #     seg_method = 'seg_otsu'

    # load specs file
    specs = load_specs(params["ana_dir"])

    # load time table. Puts in params dictionary
    time_table = load_time_table(params["ana_dir"])
    params["time_table"] = time_table

    # make list of FOVs to process (keys of channel_mask file)
    fov_id_list = sorted([fov_id for fov_id in specs.keys()])

    # remove fovs if the user specified so
    # if user_spec_fovs:
    #     fov_id_list[:] = [fov for fov in fov_id_list if fov in user_spec_fovs]

    information("Processing %d FOVs." % len(fov_id_list))

    # create dictionary which organizes cells by fov and peak_id
    Cells_by_peak = organize_cells_by_channel(Complete_Cells, specs)

    # multiprocessing
    color_multiproc = False
    if color_multiproc:
        Cells_to_pool = []
        for fov_id in fov_id_list:
            peak_ids = Cells_by_peak[fov_id].keys()
            peak_id_Cells = Cells_by_peak[fov_id].values()
            fov_ids = [fov_id] * len(peak_ids)

            Cells_to_pool += zip(fov_ids, peak_ids, peak_id_Cells)
        pool = Pool(processes=params["num_analyzers"])

        mapfunc = partial(
            find_cell_intensities_worker, params, fl_channel, seg_method, cellfile_path
        )
        Cells_updates = pool.starmap_async(mapfunc, Cells_to_pool)
        # [pool.apply_async(find_cell_intensities(fov_id, peak_id, Cells, midline=True, channel=namespace.channel)) for fov_id in fov_id_list for peak_id, Cells in Cells_by_peak[fov_id].items()]

        pool.close()  # tells the process nothing more will be added.
        pool.join()
        update_cells = (
            Cells_updates.get()
        )  # the result is a list of Cells dictionary, each dict contains several cells
        update_cells = {
            cell_id: cell for cells in update_cells for cell_id, cell in cells.items()
        }
        for cell_id, cell in update_cells.items():
            Complete_Cells[cell_id] = cell

    # for each set of cells in one fov/peak, compute the fluorescence
    else:
        for fov_id in fov_id_list:
            if fov_id in Cells_by_peak:
                information("Processing FOV {}.".format(fov_id))
                for peak_id, Cells in Cells_by_peak[fov_id].items():
                    information("Processing peak {}.".format(peak_id))
                    find_cell_intensities(
                        params,
                        time_table,
                        fov_id,
                        peak_id,
                        Cells,
                        midline=False,
                        channel_name=fl_channel,
                    )

    # Just the complete cells, those with mother and daugther
    cell_filename = os.path.basename(cellfile_path)
    with open(params["cell_dir"] / (cell_filename[:-4] + "_fl.pkl"), "wb") as cell_file:
        pickle.dump(Complete_Cells, cell_file, protocol=pickle.HIGHEST_PROTOCOL)

    information("Finished.")

class Colors(MM3Container):
    def create_widgets(self):
        self.cellfile_widget = FileEdit(
            label="cell_file",
            value=Path("./analysis/cell_data/complete_cells.pkl"),
            tooltip="Cell file to be analyzed",
        )

        ## allow user to choose multiple planes to analyze
        self.plane_widget = PlanePicker(
            self.valid_planes,
            label="analysis plane",
            tooltip="Fluoresence plane that you would like to analyze",
        )
        self.segmentation_method_widget = ComboBox(
            label="segmentation method", choices=["Otsu", "U-net"]
        )
        self.fov_widget = FOVChooser(self.valid_fovs)

        self.set_plane()
        self.set_segmentation_method()
        self.set_cellfile()
        self.set_fovs(self.valid_fovs)

        self.plane_widget.changed.connect(self.set_plane)
        self.segmentation_method_widget.changed.connect(self.set_segmentation_method)
        self.cellfile_widget.changed.connect(self.set_cellfile)
        self.fov_widget.connect_callback(self.set_fovs)

        self.append(self.plane_widget)
        self.append(self.segmentation_method_widget)
        self.append(self.cellfile_widget)
        self.append(self.fov_widget)

    def set_params(self):
        # These have been wittled down to bare minimum.
        self.params = {
            "experiment_name": self.experiment_name,
            "ana_dir": self.analysis_folder,
            "FOV": self.fovs,
            "fl_plane": self.fl_plane,
            "cell_file": self.cellfile,
            "num_analyzers": multiprocessing.cpu_count(),
            "output": "TIFF",
            "chnl_dir": self.analysis_folder / "channels",
            "seg_dir": self.analysis_folder / "segmented",
            "cell_dir": self.analysis_folder / "cell_data",
            "sub_dir": self.analysis_folder / "subtracted",
        }

    def run(self):
        self.set_params()
        self.viewer.window._status_bar._toggle_activity_dock(True)

        colors(self.params, self.fl_plane, self.segmentation_method, str(self.cellfile))

    def set_plane(self):
        self.fl_plane = self.plane_widget.value

    def set_segmentation_method(self):
        self.segmentation_method = self.segmentation_method_widget.value

    def set_cellfile(self):
        self.cellfile = self.cellfile_widget.value

    def set_fovs(self, fovs):
        self.fovs = list(set(fovs))


import re
from pygments.lexer import RegexLexer, words, default, include, bygroups
from pygments.token import Text, Comment, Whitespace, Operator, Keyword, Name, String, Number, Punctuation

__all__ = ['BerryLexer']

line_re = re.compile('.*?\n')

class BerryLexer(RegexLexer):

    # For Berry <https://github.com/berry-lang/berry> source code (version v0.1.0)
    # Contributed by Shaun Brown (Beormund) <https://github.com/Beormund>

    name = 'Berry'
    aliases = ['berry', 'be']
    filenames = ['*.be']
    mimetypes = ['text/x-berry', 'application/x-berry']

    _name = r'\b[^\W\d]\w*'

    # (def)\b((?:\s)*)
    # (def)\b(\s*$)(?<!\()
    # (def)\b((?:\s)*)(?=[^\W\d]\w*)

    tokens = {
        'root': [
            include('whitespace'),
            include('numbers'),
            include('keywords'),
            (r'(def)\b((?:\s)*)' + '(?=' + _name + ')', bygroups(Keyword.Declaration, Text), 'funcname'),
            (r'(class)\b((?:\s)*)', bygroups(Keyword.Declaration, Text), 'classname'),
            (r'(import)\b((?:\s)*)', bygroups(Keyword.Namespace, Text), 'import'),
            include('expr')
        ],
        'expr': [
            (r'[^\S\n]+', Text),
            (r'\.\.|[~!%^&*+=|?:<>/-]', Operator),
            (r'[(){}\[\],.;]', Punctuation),
            include('controls'),
            include('builtins'),
            include('funccall'),
            include('member'),
            include('name'),
            include('strings')
        ],
        'whitespace': [
            (r'\s+', Whitespace),
            (r'#-(.|\n)*?-#', Comment.Multiline),
            (r'#(\n|[\w\W]*?\n)', Comment.Single)
        ],
        'keywords': [
            (words((
                'as', 'break', 'continue', 'import', 'static', 'self', 'super'),
                suffix=r'\b'), Keyword.Reserved),
            (r'(true|false|nil)\b', Keyword.Constant),
            (r'(var|def)\b', Keyword.Declaration)
        ],
        'controls': [
            (words((
                'if', 'elif', 'else', 'for', 'while', 'do', 'end', 'break',
                'continue', 'return', 'try', 'except', 'raise'),
                suffix=r'\b'), Operator.Word)
        ],
        'builtins': [
            (words((
                'assert', 'bool', 'input', 'classname', 'classof', 'number', 'real',
                'bytes', 'compile', 'map', 'list', 'int', 'isinstance', 'print',
                'range', 'str', 'super', 'module', 'size', 'issubclass', 'open',
                'file', 'type', 'call'),
                suffix=r'\b'), Name.Builtin)
        ],
        'numbers': [
            (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex),
            (r'[-]?\d+', Number.Integer),
            (r'([-]?\d*\.?\d+)(?:[eE]([-+]?\d+))?', Number.Float)
        ],
        'name': [
            (_name, Name)
        ],
        'funcname': [
            (_name, Name.Function, '#pop')
        ],
        'funccall': [
            ( _name + r'(?=\s*\()', Name.Function, '#pop')
        ],
        'member': [
            ( r'(?<=\.)' + _name + r'\b(?!\()', Name.Attribute, '#pop')
        ],
        'classname': [
            (_name, Name.Class, '#pop'),
        ],
        'import': [
            (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
        ],
        'strings': [
            (r'"(?:\\?.)*?"', String.Double, '#pop'),
            (r'\'(?:\\?.)*?\'', String.Single, '#pop')
        ]
    }


'''
K
K u
K u s
K u s h
K u s h a
K u s h a l
'''
String=input('enter the string:')
for row in range(len(String)):
    for col in range(row+1):
        print(String[col],end=' ')
    print()

Pattern(Python)/Important printing pattern- PYTHON/02.Printing string .py


from __future__ import annotations

from aiogram import executor, types, Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext

# This Project Tools
from __init__ import API_BOT_TOKEN

from logger_settings.logger_setup import set_logger
from logger_settings.error_answer import error_function

from core_buttons.keyboard_button import menuButton
from core_buttons.inline_buttons import menuButtons

from handlers.report_handler import ReportTool

bot = Bot(token=API_BOT_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot=bot, storage=storage)

class MyBot(ReportTool):
    def __init__(self):
        super().__init__(dp)
        dp.register_message_handler(self.start_handler, commands=["start"])
        dp.register_message_handler(self.main_menu, text="🔵 Головне Меню 🟡")
        dp.register_callback_query_handler(self.back_state_button, state="*", text="Back State")
        dp.register_callback_query_handler(self.source_code, text="Source Code")

    @staticmethod
    async def start_handler(message: types.Message):
        logger = await set_logger(namespace="MyBot.start_handler", uid=message.chat.id)
        logger.info("sent /start command")
        try:
            answer = "Мої вітання !\n" \
                "Для того щоб почати користуватися ботом, натисніть на кнопку \"Головне меню\". " \
                "В самому меню натисніть кнопку \"Налаштування\"."
            await message.answer(text=answer, reply_markup=menuButton)
        except Exception as exception:
            await error_function(message, logger, exception)

    @staticmethod
    async def main_menu(message: types.Message):
        logger = await set_logger(namespace="MyBot.main_menu", uid=message.chat.id)
        logger.info("pressed \"Menu\" button")
        try:
            await message.answer(text="Виберіть наступну дію: ", reply_markup=menuButtons)
        except Exception as exception:
            await error_function(message, logger, exception)

    @staticmethod
    async def back_state_button(call: types.CallbackQuery, state: FSMContext):
        logger = await set_logger(namespace="MyBot.back_state_button", uid=call.message.chat.id)
        try:
            current_state = await state.get_state()
            if current_state is None:
                logger.info("this back_state_button is not active")
                await call.message.answer(text="Кнопка не дійсна...")
                return
            logger.info("clicked on back_state_button; return back")
            await state.finish()
            await call.message.answer(text="Повернувся назад")
            await call.message.answer(
                text="Виберіть наступну дію:",
                reply_markup=menuButtons
            )
        except Exception as exception:
            await error_function(call.message, logger, exception)

    @staticmethod
    async def source_code(call: types.CallbackQuery):
        logger = await set_logger(namespace="MyBot.source_code", uid=call.message.chat.id)
        logger.info("pressed \"Source Code\" button")
        try:
            await call.message.answer(text="Вихідний код проекту >>> \nhttps://github.com/progdad/")
            await call.message.answer(
                text="Виберіть наступну дію:",
                reply_markup=menuButtons
            )
        except Exception as exception:
            await error_function(call.message, logger, exception)

if __name__ == '__main__':
    reportBot = MyBot()
    executor.start_polling(dp, skip_updates=True)


#########################################################################################
#                                     Disclaimer                                        #
#########################################################################################
# (c) 2014, Mobile-Sandbox
# Author: Michael Spreitzenbarth (research@spreitzenbarth.de)
#
# This program is free software you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#########################################################################################
#                                      Imports                                          #
#########################################################################################
import simplejson, urllib, urllib2, functools
#########################################################################################
#                                     Functions                                         #
#########################################################################################
try:
    import posthandler
    post_opener = urllib2.build_opener(posthandler.MultipartPostHandler)
except ImportError:
    posthandler = None

class ModuleNotFound(Exception):
    ''' Module has not been found '''

class VirusTotalAPI(object):
    api_url = "http://www.virustotal.com/api/"
    api_methods = ["get_file_report",
                   "get_url_report",
                   "scan_url",
                   "make_comment"]  # Generic dynamic property methods (_call_api)
    special_methods = ["scan_file"] # Methods with their own function

    def __init__(self, api_key):
        self.api_key = api_key
        for method in self.api_methods:
            setattr(self, method, functools.partial(self._call_api,method,key=self.api_key))
        for smethod in self.special_methods:
            setattr(self, smethod, functools.partial(getattr(self,"_special_"+smethod),key=self.api_key))

    def _call_api(self, function, **kwargs):
        url = self.api_url + function + ".json"
        data = urllib.urlencode(kwargs)
        req = urllib2.Request(url,data)
        returned = urllib2.urlopen(req).read()
        return simplejson.loads(returned)

    def _special_scan_file(self, **kwargs):
        if not posthandler:
            raise ModuleNotFound("posthandler module needed to submit files")
        json = post_opener.open(self.api_url + "scan_file.json",kwargs).read()
        return simplejson.loads(json)


import os

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def _parse_function(filename):
  image_string = tf.read_file(filename)

  # image = tf.image.decode_image(image_string)
  # image = tf.image.resize_image_with_crop_or_pad(image, 28, 28)
  # image = tf.image.convert_image_dtype(image, tf.float32)

  image = tf.image.decode_jpeg(image_string, channels=3)
  image = tf.image.convert_image_dtype(image, tf.float32)
  # image = tf.image.resize_images(image, [28, 28])

  return image

def show_imgs(images):
  plt.figure(figsize=[10,10])

  for i in range(images.shape[0]):
    plt.subplot(5, 5, i + 1)
    plt.imshow(images[i, :, :], cmap='gray')
    plt.axis('off')

  plt.subplots_adjust(wspace=0.03, hspace=0.03)
  plt.show()

tf.enable_eager_execution()
(train_images, _), (test_images, _) = tf.keras.datasets.mnist.load_data()

print(len(train_images))
print(len(test_images))

test_images = test_images.reshape(test_images.shape[0], 28, 28).astype('float32')
test_images = (test_images - 127.5) / 127.5
np.random.shuffle(test_images)

print(test_images[:25].shape)
show_imgs(test_images[:25])


from xmlrpc.client import MAXINT
from utilsAlgo import readGraphEdgeList, createGraphDicDirected, saveToFile

def findAllShortestPaths(graph,n):
    paths = []
    dic = bfs2(graph, 1,n)
    nodes = [i for i in range(1, n+1)]
    for elem in nodes:
        if dic[elem]==MAXINT:
            paths.append("-1")
        else:
            paths.append(str(dic[elem]))
    return " ".join(paths)
def bfs3(edgeList, start, n):
    dist ={}
    for i in range(1,n+1):
        dist[i] = -1
    dist[start]=0
    q = [start]
    while len(q) != 0:
        u = q.pop()
        for edge in edgeList:
            if dist[edge[1]] == -1:
                q.append(edge[1])
                dist[edge[1]]=dist[edge[0]]+1
    return dist
def bfs2(graph, s,n):
    #print("GRAPH {}".format(graph))
    dist = {s: 0}
    if s in graph:
        for i in range(1,n+1):
            dist[i]=MAXINT
        dist[s]=0
        q = [s]
        while len(q) != 0:
            u = q.pop()
            if u in graph:
                for v in graph[u]:
                    if dist[v] == MAXINT:
                        q.append(v)
                        dist[v]=dist[u]+1
    return dist
def bfs(graph, source, target, visited, pathLength = 0, debug = False):
    visited[source]=True
    if debug:
        print("{} {} {}".format(graph, source, target))
    if source == target:
        return pathLength
    for neighbour in graph[source]:
        if visited[neighbour] == False and neighbour in graph:
            if debug:
                print("{} {} {} NEIGHBOR {} VISITED {} {}".format(graph, source, target, neighbour, visited, visited[neighbour]))
            return bfs(graph,neighbour, target,visited,pathLength+1, debug=debug)
    return -1
def test(fname, debug = False):
    n,m,edgeList = readGraphEdgeList(fname)
    #print(bfs3(edgeList,1,n))
    graph = createGraphDicDirected(edgeList)
    if debug:
        print(findAllShortestPaths(graph,n))
    else:
        saveToFile(findAllShortestPaths(graph,n), "rosalind_bfs_output.txt")
if __name__ =='__main__':
    test("rosalind_bfs_test.txt",True)
    test("rosalind_bfs.txt",True)


import pytest

from eth_abi.utils.padding import (
    zpad,
)

@pytest.mark.parametrize(
    "value,length,expected",
    (
        (b"", 5, b"\x00\x00\x00\x00\x00"),
        (b"abc", 5, b"\x00\x00abc"),
        (b"abcde", 5, b"abcde"),
        (b"abcdef", 5, b"abcdef"),
    ),
)
def test_zpadding(value, length, expected):
    actual = zpad(value, length)
    assert actual == expected


import math
import bpy
import numpy as np
import os

def quaternion_from_matrix(matrix, isprecise=False):
    M = np.array(matrix, dtype=np.float64, copy=False)[:4, :4]
    if isprecise:
        q = np.empty((4, ))
        t = np.trace(M)
        if t > M[3, 3]:
            q[0] = t
            q[3] = M[1, 0] - M[0, 1]
            q[2] = M[0, 2] - M[2, 0]
            q[1] = M[2, 1] - M[1, 2]
        else:
            i, j, k = 1, 2, 3
            if M[1, 1] > M[0, 0]:
                i, j, k = 2, 3, 1
            if M[2, 2] > M[i, i]:
                i, j, k = 3, 1, 2
            t = M[i, i] - (M[j, j] + M[k, k]) + M[3, 3]
            q[i] = t
            q[j] = M[i, j] + M[j, i]
            q[k] = M[k, i] + M[i, k]
            q[3] = M[k, j] - M[j, k]
        q *= 0.5 / math.sqrt(t * M[3, 3])
    else:
        m00 = M[0, 0]
        m01 = M[0, 1]
        m02 = M[0, 2]
        m10 = M[1, 0]
        m11 = M[1, 1]
        m12 = M[1, 2]
        m20 = M[2, 0]
        m21 = M[2, 1]
        m22 = M[2, 2]
        # symmetric matrix K
        K = np.array([[m00 - m11 - m22, 0.0, 0.0, 0.0],[m01 + m10, m11 - m00 - m22, 0.0, 0.0],[m02 + m20, m12 + m21, m22 - m00 - m11, 0.0],[m21 - m12, m02 - m20, m10 - m01, m00 + m11 + m22]])
        K /= 3.0
        # quaternion is eigenvector of K that corresponds to largest eigenvalue
        w, V = np.linalg.eigh(K)
        q = V[[3, 0, 1, 2], np.argmax(w)]
    if q[0] < 0.0:
        np.negative(q, q)
    return q

def set_camera_by_pose(camera, pose):
    # seems the coordinate of blender is aligned by y+ to z and x+ to x+
    R_blender = np.asarray([[1,0,0],
                            [0,0,-1],
                            [0,1,0]]) # x_blend = R_blender @ x_wrd
    cam_pt = (pose[:,:3].T @ -pose[:,3:])[...,0] # 3 in x_wrd
    cam_rot = pose[:,:3]
    # x_cam = R_rot @ x_wrd = R @ R_blender.T @ x_blender
    cam_rot = cam_rot @ R_blender.T
    cam_pt = R_blender @ cam_pt
    cam_rot = np.diag([1,-1,-1]) @ cam_rot

    camera.location[0] = cam_pt[0]
    camera.location[1] = cam_pt[1]
    camera.location[2] = cam_pt[2]
    camera.rotation_mode = 'QUATERNION'
    q = quaternion_from_matrix(cam_rot.T)
    camera.rotation_quaternion[0] = q[0]
    camera.rotation_quaternion[1] = q[1]
    camera.rotation_quaternion[2] = q[2]
    camera.rotation_quaternion[3] = q[3]

def add_env_light(fn):
    world_tree = bpy.context.scene.world.node_tree
    env_node = world_tree.nodes.new(type='ShaderNodeTexEnvironment')
    back_node = world_tree.nodes['World Output']
    world_tree.links.new(env_node.outputs['Color'], back_node.inputs['Surface'])
    bpy.ops.image.open(filepath=os.path.abspath(fn))
    env_node.image = bpy.data.images[fn.split(os.path.sep)[-1]]

def pose_inverse(pose):
    R = pose[:,:3].T
    t = - R @ pose[:,3:]
    return np.concatenate([R,t],-1)

def az_el_to_points(azimuths, elevations):
    z = np.sin(elevations)
    x = np.cos(azimuths)*np.cos(elevations)
    y = np.sin(azimuths)*np.cos(elevations)
    return np.stack([x,y,z],-1) #

def look_at_with_up(view_pts,center,up):
    up = up/np.linalg.norm(up) # 3
    view_dir = center[None,:]-view_pts
    view_dir /= np.linalg.norm(view_dir,2,1,keepdims=True)
    z_axis = view_dir # n,3
    y_axis = up[None,:] - np.sum(view_dir * up[None,:], 1,keepdims=True) * view_dir # n,3
    y_axis = -y_axis
    y_axis /= np.linalg.norm(y_axis,2,1,keepdims=True)
    x_axis = np.cross(y_axis,z_axis) # n,3
    rotation = np.stack([x_axis,y_axis,z_axis],2).transpose([0,2,1])
    return rotation

def generate_relghting_poses(num, azimuth, elevation, dist):
    num = num
    begin_az = azimuth
    el = elevation
    dist = dist
    az = np.deg2rad(begin_az) + np.linspace(-np.pi/2, np.pi/2, num)
    el = np.ones_like(az) * np.deg2rad(el)

    cam_pts = az_el_to_points(az, el)
    R_trans = np.asarray([[1,0,0],[0,0,-1], [0,1,0]]) # x_norm = R_trans @ x_wrd
    cam_rots = look_at_with_up(cam_pts, np.asarray([0,0,0],np.float32), np.asarray([0,0,1],np.float32)) # R_cam @ x_norm = x_cam
    cam_rots = cam_rots @ R_trans[None,:,:] # R_cam @ R_trans @ x_wrd = x_cam
    cam_trans = np.asarray([0, 0, dist])[None,:,None] # 1,3,1
    cam_trans = np.repeat(cam_trans,num,0) # 32,3,1
    poses = np.concatenate([cam_rots,cam_trans],-1)
    return poses

def add_env_light(fn):
    world_tree = bpy.context.scene.world.node_tree
    env_node = world_tree.nodes.new(type='ShaderNodeTexEnvironment')
    back_node = world_tree.nodes['World Output']
    world_tree.links.new(env_node.outputs['Color'], back_node.inputs['Surface'])
    bpy.ops.image.open(filepath=os.path.abspath(fn))
    env_node.image = bpy.data.images[fn.split(os.path.sep)[-1]]

def setup(h, w, tile_size=4096, samples=4096):
    # set environment
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.cycles.device = 'GPU'
    # bpy.context.scene.render.tile_x = 128 # use gpu to speed up rendering
    # bpy.context.scene.render.tile_y = 128
    bpy.context.scene.cycles.tile_size = tile_size
    bpy.context.scene.cycles.samples = samples

    # set output settings
    bpy.context.scene.render.resolution_x = w
    bpy.context.scene.render.resolution_y = h
    bpy.context.scene.render.resolution_percentage = 100
    bpy.context.scene.render.image_settings.color_mode = 'RGBA'
    bpy.context.scene.render.image_settings.file_format = 'PNG'

    # delete default box
    bpy.ops.object.select_all(action='DESELECT')
    bpy.data.objects['Cube'].select_set(True)
    bpy.ops.object.delete()
    bpy.ops.object.select_all(action='DESELECT')
    # delete default light
    bpy.data.objects['Light'].select_set(True)
    bpy.ops.object.delete()
    bpy.ops.object.select_all(action='DESELECT')


import requests
from  bs4 import BeautifulSoup

def risersAndFallers(URL):
    rarr = []
    page = requests.get(URL)
    soup  = BeautifulSoup(page.content, "html.parser")
    mt = soup.find('div', class_='market-table')
    tbody= mt.find("tbody")
    for t in tbody.find_all("tr"):
        #print("*",t)
        arr = []
        elems = t.find_all("td")
        for e in elems:
            a  = e.find('a')
            if (a is not None):
                #print("@",a.attrs['href'])
                arr.append(a.attrs['href'])
            #print("@", e.text)
            arr.append(e.text)
        #print(arr)
        rarr.append(arr)
    return rarr

if __name__ == "__main__":
    # First page of risers and fallers (t-1)
    URL = "https://www.sharesmagazine.co.uk/market-scan/risers-fallers?page=1"
    # First page of volume spikes relative to monthly average
    #URL = "https://www.sharesmagazine.co.uk/market-scan/volume-analysis?dateComparison=2&index=UKX&volumePctChange=%2C&price=%2C&pricePctChange=&sortBy=volumePctChange&sortDirection=volumeIncrease&filtersubmitbtn=Update"
    # First page of constant risers (3 consecutive days of rises)
    #URL = "https://www.sharesmagazine.co.uk/market-scan/constant-risers-fallers?page=1&sortBy=risers&days=3&filtersubmitbtn=Update"
    # First page of monthly highs
    #URL = "https://www.sharesmagazine.co.uk/market-scan/highs-lows?page=1&highLow=high&period=monthly&filtersubmitbtn=Update"
    # First page of risers and fallers (t-4w)
    #URL = "https://www.sharesmagazine.co.uk/market-scan/risers-fallers?dateComparison=2020-07-15&sortDirection=risers&index=&price=%2C&filtersubmitbtn=Update"
    listOfRisersFallers = risersAndFallers(URL)
    print(listOfRisersFallers)


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

# overwrite the comparison function, so the node can be comparable
ListNode.__lt__ = lambda x, y: (x.val < y.val)

class Solution(object):
  def mergeKLists(self, lists):
    """
    :type lists: List[ListNode]
    :rtype: ListNode
    """
    heap = []
    p = dummy = ListNode(-1)
    for i in range(0, len(lists)):
      node = lists[i]
      if not node:
        continue
      heapq.heappush(heap, node)

    while heap:
      value, node = heapq.heappop(heap)
      p.next = node
      p = p.next
      if node.next:
        node = node.next
        heapq.heappush(heap, node)
    return dummy.next


import os
#choose GPU
os.environ["CUDA VISIBLE_DEVICES"]="0"

#import data
import input_data
mnist= input_data.read_data_sets("MNIST_data/",one_hot=True)

import time
start_time = time.time()
print('start time: ',time.strftime("%Y-%m-%d  %H:%M:%S",time.localtime()))

import tensorflow as tf
x = tf.placeholder("float",[None,784])
y_ = tf.placeholder("float",[None,10])

#define weight and bias
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)
def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial)

#first convolutional layer,kernal size 3*3,dilation rate=1, 1 input channel,32 output channel
W_conv1 = weight_variable([3,3,1,32])
b_conv1 = bias_variable([32])

#[batch,weidth,height,number of channel(grey)]
x_image =tf.reshape(x,[-1,28,28,1])

c_dilat1 =tf.nn.conv2d(x_image, W_conv1, strides=[1,1,1,1],padding='SAME',use_cudnn_on_gpu=True,
                       data_format='NHWC', dilations=[1, 1, 1, 1], name='dilation1')
h_conv1 = tf.nn.relu(c_dilat1+b_conv1)

#second convolutional layer
W_conv2 = weight_variable([3,3,32,32])
b_conv2 = bias_variable([32])
c_dilat2 = tf.nn.conv2d(h_conv1, W_conv2, strides=[1,1,1,1], padding= 'SAME',use_cudnn_on_gpu=True,
                        data_format='NHWC', dilations=[1, 1, 1, 1], name= 'dilation2')
h_conv2 = tf.nn.relu(c_dilat2+b_conv2)

#third convolutional layer
W_conv3 = weight_variable([3,3,32,32])
b_conv3 = bias_variable([32])
c_dilat3 = tf.nn.conv2d(h_conv2, W_conv3, strides= [1,1,1,1], padding= 'SAME',use_cudnn_on_gpu=True,
                        data_format= 'NHWC', dilations= [1, 2, 2, 1], name= 'dilation3')
h_conv3 = tf.nn.relu(c_dilat3 + b_conv3)

#fourth convolutional layer
W_conv4 = weight_variable([3,3,32,32])
b_conv4 = bias_variable([32])
c_dilat4 = tf.nn.conv2d(h_conv3, W_conv4, strides= [1,1,1,1], padding= 'SAME',use_cudnn_on_gpu=True,
                        data_format= 'NHWC', dilations= [1, 4, 4, 1], name='dilation4')
h_conv4 = tf.nn.relu(c_dilat4 + b_conv4)

#fifth layer
W_conv5 = weight_variable([3,3,32,32])
b_conv5 = bias_variable([32])
c_dilat5 = tf.nn.conv2d(h_conv4, W_conv5, strides= [1,1,1,1], padding= 'SAME',use_cudnn_on_gpu=True,
                        data_format= 'NHWC', dilations= [1, 1, 1, 1], name='dilation5')
h_conv5 = tf.nn.relu(c_dilat5 + b_conv5)

#1*1 layer
W_conv6 = weight_variable([1,1,32,10])
b_conv6 = bias_variable([10])
c_dilat6 = tf.nn.conv2d(h_conv5, W_conv6,strides= [1,1,1,1], padding= 'SAME',use_cudnn_on_gpu=True,
                        data_format= 'NHWC', dilations= [1,1,1,1], name= 'dilation6')
c_flat = tf.reshape(c_dilat6,[-1,28*28*10])

import time
run_time = time.time()
print(' time: ',time.strftime("%Y-%m-%d  %H:%M:%S",time.localtime()))
#output layer
W_fc2 = weight_variable([28*28*10,10])
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(c_flat, W_fc2)+b_fc2)

#evaluation
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))

#save the model
#saver = tf.train.Saver()

#run
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:
        train_accuracy = accuracy.eval(feed_dict={x: batch[0],y_:batch[1]})
        print("step %d,training accuracy %g" % (i, train_accuracy))
    train_step.run(feed_dict={x: batch[0],y_: batch[1]})

print("test accuracy%g" % accuracy.eval(feed_dict={x:mnist.test.images,
                                                   y_:mnist.test.labels}))
#save model
#saver.save(sess,"model.ckpt")
#show training time
end_time = time.time()
print('end time: ',time.strftime("%Y-%m-%d  %H:%M:%S",time.localtime()))


def canConstruct(a):
    result = sum(a)
    if result % 3 == 0:
        return "Yes"
    else:
        return "No"

if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        n = int(input())

        a = list(map(int, input().rstrip().split()))

        result = canConstruct(a)
        print(result)

        # Time Complexity: O(N)