import csv
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# reading the data
data = pd.read_csv('rankings.txt')

#calculate the average score per language
average_per_language = pd.pivot_table(data, index= 'name', values= 'rank') #average is default aggrfunc
average_per_language = average_per_language.reset_index() #flatten to be able to work with it further along

#count the number of occurrences per lang
counts_per_language = pd.pivot_table(data, index= 'name', values= 'rank', aggfunc='count')

#now we want to merge the averages back into the original data, to sort all data points on those.
merged = data.merge(average_per_language, on='name')

#sort the values by their average rank
merged = merged.sort_values('rank_y', ascending = True)

# #now we want to merge the counts back into the original data, to *filter* all data points on those.
merged = merged.merge(counts_per_language, on='name')

#filter out the values with less than 100 rankings
merged_filtered = merged[merged['rank']> 100]

names = merged_filtered['name'] #retrieve just the names
values = [int(x) for x in merged_filtered['rank_x']] #get the rankings and make them into ints

#plot definitions
plt.title('Ranks sorted by average')
plt.xlabel('programming language')
plt.ylabel('rank')
plt.xlim((0,100))

# Create the first plot
plt.scatter(values, names, s=10, alpha=0.5, c='b')

#from the filtered dataframe, we now flatten again using the max of the ranking
# (they're all the same average, because this is the result of a merge)
names_with_averages = pd.pivot_table(merged_filtered, index= 'name', values= 'rank_y', aggfunc='max')
names_with_averages = names_with_averages.reset_index() #flatten pivot table we we can grab separate columns

#add an extra plot for the averages
plt.scatter(names_with_averages['rank_y'], names_with_averages['name'], alpha=0.5, s=30, marker="x", c='r')

plt.savefig("scatter_sorted_by_average.png")
plt.show()


import os
import seaborn as sns
import pandas as pd
import numpy as np
import torch
import matplotlib.pyplot as plt

from tqdm import tqdm
from sklearn.metrics import confusion_matrix, roc_curve, roc_auc_score

from .model import initialize_model, load_model
from .visualization import Labels, initialize_plot, savefig_and_close
from .logger import get_logger

LOGGER = get_logger("utils/evaluation")
DEVICE = os.getenv("DEVICE")

def get_predictions(model, dataloader):
    y_pred = []
    y_pred_prob = []
    y_true = []

    model.to(DEVICE)
    model.eval()

    for inputs, labels in tqdm(dataloader):
        inputs = inputs.to(DEVICE)
        labels = labels.to(DEVICE)

        outputs = model(inputs.float())
        y_pred_prob.extend(outputs.detach().cpu().numpy())

        outputs = (torch.max(torch.exp(outputs), 1)[1]).data.cpu().numpy()
        y_pred.extend(outputs)

        labels = labels.data.cpu().numpy()
        y_true.extend(labels)

    return y_true, y_pred, y_pred_prob

def evaluate_model(model, dataloader, classes: list, output_dir=None, close=True) -> None:
    y_true, y_pred, _ = get_predictions(model, dataloader)
    metric = confusion_matrix(y_true, y_pred)
    metric_df = pd.DataFrame(
        metric / np.sum(metric, axis=1)[:, None],
        index=[i for i in classes],
        columns=[i for i in classes],
    )

    labels = Labels("Confusion Matrix", "Predicted Class", "Actual Class")
    _, ax = initialize_plot(figsize=(10, 10), labels=labels)
    sns.heatmap(metric_df, cmap="crest", annot=True, fmt=".1f", ax=ax)
    savefig_and_close("confusion_matrix.png", output_dir, close)

def visualize_roc_curve(model_configs, dataloader, classes: list, output_dir=None, close=True) -> None:
    y_pred_prob = {}
    y_true = {}
    for model_config in model_configs:
        model_name = model_config["name"]
        model = initialize_model(model_config["arch"], "DEFAULT", len(classes), False)
        load_model(model, model_config["path"])

        y_true[model_name], _, y_pred_prob[model_name] = get_predictions(model, dataloader)
        y_pred_prob[model_name] = np.array(y_pred_prob[model_name])
        # Change y_true to onehot format
        y_true_tensor = torch.tensor(y_true[model_name])
        y_true_tensor = y_true_tensor.reshape((y_true_tensor.shape[0], 1))
        y_true[model_name] = torch.zeros(y_true_tensor.shape[0], len(classes))
        y_true[model_name].scatter_(dim=1, index=y_true_tensor, value=1)
        y_true[model_name] = np.array(y_true[model_name])

    for obj_index, obj_class in enumerate(classes):
        labels = Labels(f"ROC Curve ({obj_class})", "FPR", "TPR")
        _, ax = initialize_plot(figsize=(10, 10), labels=labels)
        for model_config in model_configs:
            model_name = model_config["name"]
            fpr, tpr, _ = roc_curve(y_true[model_name][:, obj_index], y_pred_prob[model_name][:, obj_index])
            auc = roc_auc_score(y_true[model_name][:, obj_index], y_pred_prob[model_name][:, obj_index])
            sns.lineplot(x=fpr, y=tpr, label=f"{model_config['name']}: AUC={auc:.4f}", ax=ax, errorbar=None)

        sns.lineplot(x=[0, 1], y=[0, 1], ax=ax, linestyle="--", errorbar=None)
        ax.set(xlim=[0.0, 1.0], ylim=[0.0, 1.05])
        plt.legend(loc="lower right")
        savefig_and_close(f"roc_curve_{obj_class}.png", output_dir, close)


import pika

from common.config import config
from common.logger import log

from common.messaging.consumer import Consumer
from scrapy_wrapper import run_crawl

class TaskConsumer(Consumer):
    """Consumes crawl tasks."""

    def __init__(self, mq_host, mq_port, heartbeat, exchange_name, exchange_type,
                 task_queue_name, task_routing_key, durable_queue=True):
        Consumer.__init__(self, mq_host, mq_port, heartbeat, exchange_name, exchange_type, task_queue_name, task_routing_key, durable_queue=durable_queue)

    def callback(self, ch, method, properties, body):
        """Action performed when recreiving messages."""

        log.info("Received task with body {}".format(body))

        try:
            # Ignore messages that are redelivered
            if method.redelivered == True:
                log.info("Task discarded as copy")
                ch.basic_ack(delivery_tag=method.delivery_tag)
            else:
                log.info("Start processing task")
                # Execute crawling task
                log.info("Execute crawl with spec: {}".format(body))
                finished = run_crawl(body, worker_flag=True)
                log.info("Scrapy worker finished: {}".format(finished))
                # Unsubscribe before sending ack to avoid accepting a task on closing
                log.info("Unsubscribing task queue.")
                self.channel.basic_cancel(self.consumer_tag)
                # send acc before closing
                ch.basic_ack(delivery_tag=method.delivery_tag)
                log.info("Finished processing task")
                exit()

        except Exception as e:
            log.exception(e)
            # ch.basic_ack(delivery_tag=method.delivery_tag)
            # log.info("Failed processing task")
            raise


# src/server/db/data_service.py

from db.database_manager import DatabaseManager

class DataService:
    def __init__(self, db_manager):
        self.db_manager = db_manager

    def run_query(self, query):
        conn = self.db_manager.connect()
        cursor = conn.cursor()

        cursor.execute(query)
        result = cursor.fetchall()
        data = [{'year': row[0], 'count': row[1]} for row in result]

        cursor.close()
        conn.close()

        return data

    def fetch_map_data(self, query):
        conn = self.db_manager.connect()
        cursor = conn.cursor()

        cursor.execute(query)
        result = cursor.fetchall()

        cursor.close()
        conn.close()

        return result


from sys import maxsize

class Group:
    """Класс описывающий группу и ее свойства."""

    def __init__(self, name=None, header=None, footer=None, id=None):
        self.name = name
        self.header = header
        self.footer = footer
        self.id = id

    def __repr__(self):
        # return "%s:%s" % self.id, self.name
        return f'{self.id}: {self.name}, {self.header}, {self.footer}'

    def __eq__(self, other):
        return ((
             self.id is None or other.id is None
             or self.id == other.id)
                and (self.name == other.name
                     or self.name is None
                     or other.name is None)
                and (self.header == other.header
                     or self.header is None
                     or other.header is None)
                and (self.footer == other.footer
                     or self.footer is None
                     or other.footer is None)
        )

    def if_or_max(self):
        """Метод сравнения списка по ключу ID."""
        if self.id:
            return int(self.id)
        return maxsize

class Contact:
    """Класс описывающий Контакт и его свойства."""

    def __init__(self, firstname=None, middlename=None, lastname=None,
            nickname=None, title=None, company=None, address=None,
            phone_home=None, phone_mobile=None, phone_work=None, fax=None,
            email=None, email2=None, email3=None, homepage=None, bday=None,
            bmonth=None, byear=None, aday=None, amonth=None, ayear=None,
            address2=None, phone2=None, notes=None, id=None,
            all_phones_from_home_page=None, all_gmail_from_home_page=None):
        self.firstname = firstname
        self.middlename = middlename
        self.lastname = lastname
        self.nickname = nickname
        # self.photo = photo
        self.title = title
        self.company = company
        self.address = address
        self.phone_home = phone_home  # работаем с этим полем
        self.phone_mobile = phone_mobile  # работаем с этим полем
        self.phone_work = phone_work  # работаем с этим полем
        self.fax = fax
        self.email = email
        self.email2 = email2
        self.email3 = email3
        self.homepage = homepage
        self.bday = bday
        self.bmonth = bmonth
        self.byear = byear
        self.aday = aday
        self.amonth = amonth
        self.ayear = ayear
        self.address2 = address2
        self.phone2 = phone2  # работаем с этим полем
        self.notes = notes
        self.id = id
        self.all_phones_from_home_page = all_phones_from_home_page
        self.all_gmail_from_home_page = all_gmail_from_home_page

    def __repr__(self):
        return f'''{self.id}:{self.firstname}, {self.lastname},
                    {self.company}, {self.address}'''

    def __eq__(self, other):
        return ((
            self.id is None or other.id is None
            or self.id == other.id)
                and (self.firstname == other.firstname
                     or self.firstname is None
                     or other.firstname is None)
                and (self.lastname == other.lastname
                     or self.lastname is None
                     or other.lastname is None)
                and (self.middlename == other.middlename
                     or self.middlename is None
                     or other.middlename is None)
                and (self.nickname == other.nickname
                     or self.nickname is None
                     or other.nickname is None)
                # self.photo = photo
                and (self.title == other.title
                     or self.title is None
                     or other.title is None)
                and (self.company == other.company
                     or self.company is None
                     or other.company is None)
                and (self.address == other.address
                     or self.address is None
                     or other.address is None)
                and (self.phone_home == other.phone_home
                     or self.phone_home is None
                     or other.phone_home is None)
                and (self.phone_mobile == other.phone_mobile
                     or self.phone_mobile is None
                     or other.phone_mobile is None)
                and (self.phone_work == other.phone_work
                     or self.phone_work is None
                     or other.phone_work is None)
                and (self.fax == other.fax
                     or self.fax is None
                     or other.fax is None)
                and (self.email == other.email
                     or self.email is None
                     or other.email is None)
                and (self.email2 == other.email2
                     or self.email2 is None
                     or other.email2 is None)
                and (self.email3 == other.email3
                     or self.email3 is None
                     or other.email3 is None)
                and (self.homepage == other.homepage
                     or self.homepage is None
                     or other.homepage is None)
                and (self.bday == other.bday
                     or self.bday is None
                     or other.bday is None)
                and (self.bmonth == other.bmonth
                     or self.bmonth is None
                     or other.bmonth is None)
                and (self.byear == other.byear
                     or self.byear is None
                     or other.byear is None)
                and (self.aday == other.aday
                     or self.aday is None
                     or other.aday is None)
                and (self.amonth == other.amonth
                     or self.amonth is None
                     or other.amonth is None)
                and (self.ayear == other.ayear
                     or self.ayear is None
                     or other.ayear is None)
                and (self.address2 == other.address2
                     or self.address2 is None
                     or other.address2 is None)
                and (self.phone2 == other.phone2
                     or self.phone2 is None
                     or other.phone2 is None)
                and (self.notes == other.notes
                     or self.notes is None
                     or other.notes is None)
        )

    def if_or_max(self):
        """Метод сравнения списка по ключу ID."""
        if self.id:
            return int(self.id)
        return maxsize


import argparse
import json
from collections import Counter

import numpy as np
import datasets
from tqdm import trange
import torch

def main():
    """
    Example:
        python run_mbrd.py \
            --path outputs/bigbench/bigbench_ipa_codex_original_N16.json \
            --mode bertscore \
            --save_path outputs/bigbench/bigbench_ipa_codex_bertscore_N16.json
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', type=str, default='outputs/bigbench/bigbench_ipa_codex_original_N16.json')
    parser.add_argument('--mode', type=str, default='bertscore')
    parser.add_argument('--save_path', type=str, default='outputs/bigbench/bigbench_ipa_codex_bertscore_N16.json')
    args = parser.parse_args()

    # Load Metric
    if args.mode == 'mbrd':
        bleurt = datasets.load_metric("bleurt")
    elif args.mode == 'bertscore':
        bertscore = datasets.load_metric("bertscore")
    elif args.mode == 'lcs':
        # pip3 install -i https://test.pypi.org/simple/ string2string==0.0.3
        from string2string.edit_distance import EditDistAlgs
        algs_unit = EditDistAlgs()

    path = args.path
    mode = args.mode
    save_path = args.save_path

    # Read
    file = open(path, 'r').read()
    data = json.loads(file)
    outputs = data['extracted_outputs']
    if 'inputs' in data:  # check
        assert len(data['extracted_outputs']) == len(data['inputs'])

    # Loop over Outputs
    new_data = data.copy()
    new_data['indices'] = [0 for _ in range(len(outputs))]
    for i in trange(len(outputs)):
        n = len(outputs[i])
        matrix = np.zeros((n, n))

        # MBR with BLEURT
        if mode == 'bleurt':
            for j1, cand1 in enumerate(outputs[i]):
                for j2, cand2 in enumerate(outputs[i]):
                    with torch.inference_mode():
                        score = bleurt.compute(predictions=[cand1], references=[cand2])['scores'][0]
                        matrix[j1][j2] = score
            matrix = np.sum(matrix, axis=1)
            index = np.argmax(matrix)

        # MBRD with BERTScore
        elif mode == 'bertscore':
            for j1, cand1 in enumerate(outputs[i]):
                for j2, cand2 in enumerate(outputs[i]):
                    with torch.inference_mode():
                        score = bertscore.compute(predictions=[cand1], references=[cand2], lang='en')['f1'][0]
                        matrix[j1][j2] = score
            matrix = np.sum(matrix, axis=1)
            index = np.argmax(matrix)

        # Majority Voting
        elif mode == 'majority':
            counter = Counter(outputs[i])
            txt, count = counter.most_common(1)[0]
            if count > 1:
                index = outputs[i].index(txt)
            else:
                index = 0

        # LCS (Longest Common Substring)
        elif mode == 'lcs':
            for j1, cand1 in enumerate(outputs[i]):
                cand1_split = cand1.split(' ')
                for j2, cand2 in enumerate(outputs[i]):
                    cand2_split = cand2.split(' ')
                    max_length = max(len(cand1_split), len(cand2_split))
                    dist, _ = algs_unit.longest_common_subsequence(
                        cand1_split,
                        cand2_split,
                        printBacktrack=False,
                        boolListOfList=True
                        )
                    score = dist / max_length
                    matrix[j1][j2] = score
            matrix = np.sum(matrix, axis=1)
            index = np.argmax(matrix)

        # Choose First / Sample Once
        elif mode == 'sample_once':
            index = 0

        # Random Choice
        elif mode == 'random':
            index = np.random.randint(0, n)

        # Not Implemented Yet...
        else:
            raise NotImplementedError()

        new_data['indices'][i] = int(index)
        new_data['extracted_outputs'][i] = [outputs[i][index]]

    # Save
    with open(save_path, 'w') as f:
        json.dump(new_data, f)

if __name__ == "__main__":
    main()


import json
import logging
import os

import boto3

logger = logging.getLogger()

def lambda_handler(event, context):
    user_table_name = os.environ['USER_TABLE']
    new_user = json.loads(event['body'])
    dynamodb = boto3.resource('dynamodb')
    users = dynamodb.Table(user_table_name)
    response = users.put_item(Item=new_user)
    logger.info(response)
    return {
        'statusCode': 200
    }


import os
import asyncio

import discord
from discord.ext import commands

import card_data
import card_art
import card_voice
import sleeves
import deck_code

command_prefix = os.environ["BOT_PREFIX"].split()

intents = discord.Intents.default()
intents.messages = True

bot = commands.Bot(
    command_prefix=command_prefix,
    description=os.environ.get("DESCRIPTION", "Shadowverse info bot"),
    intents=intents,
)

class CardNotFoundError(Exception):
    def __init__(self, query: list):
        self.query = query
        super().__init__(query)

class CardArtError(Exception):
    def __init__(self, card_id: int, card_name: str):
        self.card_id = card_id
        self.card_name = card_name
        super().__init__(card_id, card_name)

class DeckCodeError(Exception):
    def __init__(self, deck_code: str, errors: list):
        self.deck_code = deck_code
        self.errors = errors
        super().__init__(deck_code, errors)

# Hack: avoid raising UnexpectedQuoteError
commands.view._all_quotes = set()

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")
    if os.environ.get("DEV") is None:
        status = os.environ.get("STATUS", f"{command_prefix[0]}help")
        activity = discord.Game(name=status)
        await bot.change_presence(activity=activity)

@bot.command()
async def invite(ctx):
    """Generate a link to invite me to your server"""

    app_info = await bot.application_info()
    link = f"https://discord.com/api/oauth2/authorize?client_id={app_info.id}&permissions=18432&scope=bot"
    await ctx.send(f"Invite me to your server: <{link}>")

@bot.command(aliases=["list", "l", "search", "s"])
async def cards(ctx, *query):
    """List all matching cards.

    Keyword search is supported. Examples:
    - search 6pp shadow gold spell
    - search earth sigil
    - search havencraft "repair mode"
    - search legendary "steel rebellion"

    """

    async with ctx.typing():
        cards = await card_data.get()
    results = card_data.find(cards, query)
    if not results:
        raise CardNotFoundError(query)
    else:
        max_results = 20
        if len(results) <= max_results:
            lines = [f"Found {len(results)} cards:"]
            lines += [card_data.effective_card_name(card) for card in results]
            await ctx.send("\n".join(lines))
        else:
            lo = 0
            hi = max_results
            lines = [f"Found {len(results)} cards, displaying {lo+1}-{hi}:"]
            lines += [card_data.effective_card_name(card) for card in results[lo:hi]]
            msg = await ctx.send("\n".join(lines))
            await msg.add_reaction("⬅")
            await msg.add_reaction("➡")

            def check(reaction, user):
                return (
                    user != bot.user
                    and reaction.message.id == msg.id
                    and reaction.emoji in ["➡", "⬅"]
                )

            while True:
                try:
                    reaction, usr = await bot.wait_for(
                        "reaction_add", timeout=30.0, check=check
                    )
                except asyncio.TimeoutError:
                    await msg.remove_reaction("➡", bot.user)
                    await msg.remove_reaction("⬅", bot.user)
                    break
                else:
                    if reaction.emoji == "➡":
                        hi = min(len(results), hi + max_results)
                        lo = hi - max_results
                    elif reaction.emoji == "⬅":
                        lo = max(0, lo - max_results)
                        hi = lo + max_results

                    lines = [f"Found {len(results)} cards, displaying {lo+1}-{hi}:"]
                    lines += [
                        card_data.effective_card_name(card) for card in results[lo:hi]
                    ]
                    await msg.edit(content="\n".join(lines))

@bot.command(aliases=["c", "text", "t"])
async def card(ctx, *query):
    """Display a card's stats and text"""

    async with ctx.typing():
        cards = await card_data.get()
    results = card_data.find(cards, query)
    if not results:
        raise CardNotFoundError(query)
    else:
        result = results[0]
        embed = discord.Embed.from_dict(card_data.info_embed(result))
        await ctx.send(embed=embed)

@bot.command(aliases=["flavourtext", "flavor", "flavour", "flairtext", "flair", "f"])
async def flavortext(ctx, *query):
    """Display a card's flavortext"""

    async with ctx.typing():
        cards = await card_data.get()
    results = card_data.find(cards, query)
    if not results:
        raise CardNotFoundError(query)
    else:
        result = results[0]
        embed = discord.Embed.from_dict(card_data.flavor_embed(result))
        await ctx.send(embed=embed)

async def art_gen(ctx, query: list, which: str):
    """Helper function for art commands"""

    async with ctx.typing():
        cards = await card_data.get()

    results = card_data.find(cards, query)
    if not results:
        raise CardNotFoundError(query)
    result = results[0]
    card_id = result["card_id"]
    card_name = card_data.effective_card_name(result)

    async with ctx.typing():
        image = await card_art.svgdb_get(card_id, which)

    if image is None:
        raise CardArtError(card_id, card_name)
    else:
        await ctx.send(card_name, file=discord.File(image, "0.png"))

@bot.command(aliases=["v"])
async def voice(ctx, *query):
    async with ctx.typing():
        cards = await card_data.get()

    results = card_data.find(cards, query)
    if not results:
        raise CardNotFoundError(query)
    result = results[0]

    async with ctx.typing():
        embed = await card_voice.svgdb_embed(result)

    await ctx.send(embed=discord.Embed.from_dict(embed))

@bot.command(aliases=["img", "a"])
async def art(ctx, *query):
    """Display a card's base art"""

    await art_gen(ctx, query, "0")

@bot.command(aliases=["evoimg", "evo", "e"])
async def evoart(ctx, *query):
    """Display a card's evolved art"""

    await art_gen(ctx, query, "1")

@bot.command()
async def sleeve(ctx, *query):
    """Display sleeve art"""

    # async with ctx.typing():
    #     if not query:
    #         image, sleeve_name = await sleeves.get_random()
    #     else:
    #         sleeve_id, sleeve_name = await sleeves.find_sleeve(query)
    #         if not sleeve_id:
    #             raise CardNotFoundError(query)
    #         image = await sleeves.get_asset(sleeve_id)

    # if image is None:
    #     raise CardArtError(sleeve_id, sleeve_name)
    # else:
    #     await ctx.send(sleeve_name, file=discord.File(image, "0.png"))

    await ctx.send("There is a technical issue with the sleeve command. Sorry!")

@bot.command(aliases=["deck", "code"])
async def deckcode(ctx, code: str):
    """Look up a deck code."""

    async with ctx.typing():
        result = await deck_code.get(code)

    errors = result.get("errors")
    if errors:
        raise DeckCodeError(code, errors)

    embed = discord.Embed.from_dict(deck_code.embed(result))
    await ctx.send(embed=embed)

@bot.command(hidden=True)
async def randomcard(ctx):
    async with ctx.typing():
        cards = await card_data.get()
    card = card_data.random_card(cards)
    embed = discord.Embed.from_dict(card_data.info_embed(card))
    await ctx.send(embed=embed)

@bot.command(hidden=True)
async def update(ctx):
    async with ctx.typing():
        await card_data._update()

@bot.command(hidden=True)
async def eggsplosion(ctx):
    async with ctx.typing():
        cards = await card_data.get()
    card = card_data.random_card(cards, eggsplosion=True)["card_name"]
    await ctx.send(f"{card} dies to Eggsplosion")

@bot.command(aliases=["bugreport", "bug", "report"])
async def feedback(ctx, *, message: str):
    """Report feedback to the bot dev"""

    log_channel = bot.get_channel(int(os.environ["LOG_CHANNEL"]))
    app_info = await bot.application_info()
    await log_channel.send(
        f"<@{app_info.owner.id}> Feedback from {ctx.author} in {ctx.guild} {ctx.channel}: ```{message}```"
    )
    await ctx.send("Feedback sent. Thank you!")

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, discord.ext.commands.CommandNotFound):
        if ctx.invoked_with in ["art1", "art2", "art3", "art4", "art5", "art6"]:
            await ctx.send(
                f"{error}\n"
                f"Tip: Use `{ctx.prefix}evoart` to look up evolved art. "
                "For alternate card art, you can specify 'alt' or the card set. Examples:\n"
                f"- `{ctx.prefix}art fairy circle alt`\n"
                f"- `{ctx.prefix}art albert leader`\n"
                f"- `{ctx.prefix}art albert prebuilt`\n"
                f"- `{ctx.prefix}art witch snap fate`\n"
            )
        else:
            await ctx.send(
                f"{error}\n"
                f"Tip: Use `{ctx.prefix}card` to look up a card. "
                f"See `{ctx.prefix}help` for other commands."
            )
        return
    elif isinstance(error, discord.ext.commands.CommandInvokeError):
        error = error.original
        if isinstance(error, CardNotFoundError):
            await ctx.send(f"Found no cards matching `{error.query}`")
        elif isinstance(error, CardArtError):
            await ctx.send(f'Failed to get card art for "{error.card_name}"')
        elif isinstance(error, DeckCodeError):
            await ctx.send(f"Failed to get deck code `{error.deck_code}`")
    if "LOG_CHANNEL" in os.environ:
        log_channel = bot.get_channel(int(os.environ["LOG_CHANNEL"]))
        await log_channel.send(
            f"Error from {ctx.author} in {ctx.guild} {ctx.channel}:\n"
            f"{ctx.message.jump_url}\n"
            f"```{repr(error)}```"
        )
    else:
        await commands.Bot.on_command_error(bot, ctx, error)

bot.run(os.environ["DISCORD_TOKEN"])


from bitarray import bitarray
from bitarray.util import ba2int, int2ba
from permuter import Permuter

class Des():
    permuter: Permuter
    keys: list

    def __init__(self, key: bitarray):
        self.permuter = Permuter()
        self.keys = self.generateKeys(key)

    def generateKeys(self, key: bitarray):
        keys = list()

        c = self.permuter.permuteC0(key)
        d = self.permuter.permuteD0(key)

        for i in range(16):
            c = c << self.permuter.si[i]
            d = c << self.permuter.si[i]

            newKey = self.permuter.permuteCP(c + d)
            keys.append(newKey)

        return keys

    def funcFeistel(self, bits: bitarray, key: bitarray):
        e = self.permuter.permuteE(bits)
        z = e ^ key

        sBlocks = bitarray()
        for k in range(8):
            # берем поочередно 6 битов из 48
            bits = z[k * 6 : (k + 1) * 6]

            iBits = bits[:1] + bits[5:]
            jBits = bits[1:5]

            i = ba2int(iBits)
            j = ba2int(jBits)

            sBlocks += int2ba(self.permuter.permuteSBlock(k, i * 16 + j), length=4)

        return self.permuter.permuteP(sBlocks)

    def encode(self, bits: bitarray):
        bits = self.permuter.permuteIP(bits)

        lPart = bits[:32]
        rPart = bits[32:]

        for i in range(16):
            tmp   = lPart.copy()
            lPart = rPart.copy()
            rPart = tmp ^ self.funcFeistel(rPart, self.keys[i])

        return self.permuter.permuteIPInverse(lPart + rPart)

    def decode(self, bits: bitarray):
        bits = self.permuter.permuteIP(bits)

        lPart = bits[:32]
        rPart = bits[32:]

        for i in range(15, -1, -1):
            tmp = rPart.copy()
            rPart = lPart.copy()
            lPart = tmp ^ self.funcFeistel(lPart, self.keys[i])

        return self.permuter.permuteIPInverse(lPart + rPart)


from splinter import Browser
from bs4 import BeautifulSoup
import pandas as pd
import datetime as dt
import time
import re

def scrape_all():
    browser = Browser("chrome", executable_path="chromedriver", headless=True)
    news_title, news_paragraph = mars_news(browser)
    data = {
        "news_title": news_title,
        "news_paragraph": news_paragraph,
        "featured_image": featured_image(browser),
        "hemispheres": hemispheres(browser),
        "weather": twitter_weather(browser),
        "facts": mars_facts(),
        "last_modified": dt.datetime.now()
    }
    browser.quit()
    return data

def mars_news(browser):
    url = "https://mars.nasa.gov/news/"
    browser.visit(url)
    browser.is_element_present_by_css("ul.item_list li.slide", wait_time=0.5)
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    try:
        slide = soup.select_one("ul.item_list li.slide")
        title = slide.find("div", class_="content_title").get_text()
        news_p = slide.find(
            "div", class_="article_teaser_body").get_text()
    except AttributeError:
        return None, None
    return title, news_p

def featured_image(browser):
    url = "https://www.jpl.nasa.gov/spaceimages/?search=&category=Mars"
    browser.visit(url)
    image = browser.find_by_id("full_image")
    image.click()
    browser.is_element_present_by_text("more info", wait_time=0.5)
    data = browser.links.find_by_partial_text("more info")
    data.click()
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    img = soup.select_one("figure.lede a img")
    try:
        img_rel = img.get("src")
    except AttributeError:
        return None
    img = f"https://www.jpl.nasa.gov{img_rel}"
    return img

def hemispheres(browser):

    url = (
        "https://astrogeology.usgs.gov/search/"
        "results?q=hemisphere+enhanced&k1=target&v1=Mars"
    )
    browser.visit(url)
    hemi_urls = []
    for i in range(4):
        browser.find_by_css("a.product-item h3")[i].click()
        hemi_data = scrape_hemisphere(browser.html)
        hemi_urls.append(hemi_data)
        browser.back()
    return hemi_urls

def twitter_weather(browser):
    url = "https://twitter.com/marswxreport?lang=en"
    browser.visit(url)
    time.sleep(5)
    html = browser.html
    soup = BeautifulSoup(html, "html.parser")
    tweet = {"class": "tweet", "data-name": "Mars Weather"}
    mars_tweet = soup.find("div", attrs=tweet)
    try:
        weather = mars_tweet.find("p", "tweet-text").get_text()
    except AttributeError:
        pattern = re.compile(r'sol')
        weather = soup.find('span', text=pattern).text
    return weather

def scrape_hemisphere(html_text):
    hemi_soup = BeautifulSoup(html_text, "html.parser")
    try:
        title = hemi_soup.find("h2", class_="title").get_text()
        sample = hemi_soup.find("a", text="Sample").get("href")
    except AttributeError:
        title = None
        sample = None
    hemisphere = {
        "title": title,
        "img_url": sample
    }
    return hemisphere

def mars_facts():
    try:
        df = pd.read_html("http://space-facts.com/mars/")[0]
    except BaseException:
        return None
    df.columns = ["description", "value"]
    df.set_index("description", inplace=True)
    return df.to_html(classes="table table-striped")

if __name__ == "__main__":
    print(scrape_all())


from models.user import UserCls
from models.topic import TopicCls, CommentCls, TopicCollection
from . import *

main = Blueprint('user_blue', __name__)
upload_dir = '/static/uploads/'

@main.route('/profile/<username>')
def profile(username):
    u = UserCls.query.filter_by(username=username).first()
    if u:
        cur_u = cur_user()
        tpc_rel = TopicCls.query.join(CommentCls, CommentCls.topic_id==TopicCls.id)\
                .filter(CommentCls.sender_id==u.id)\
                .order_by(CommentCls.created_time.desc()).limit(5)#.filter(CommentCls.sender_id==u.id)
        tpc_create = TopicCls.query.filter_by(user_id=u.id).order_by(TopicCls.created_time.desc()).limit(5)

        return render_template('user/profile.html', user=u, topics_created=tpc_create,
                                topics_related=tpc_rel, cur_user=cur_u)
    else:
        abort(404)

def valid_img_upload(filename):
    suffix = filename.split('.')[-1]
    return suffix.lower() in ['png', 'jpg', 'gif']

@main.route('/addimg', methods=['post'])
def add_img():
    u = cur_user()
    f = request.files.get('avatar')
    if f and valid_img_upload(f.filename):
        filename = str(uuid.uuid4())
        path = upload_dir + filename + '.' + f.filename.split('.')[-1]
        f.save(path[1:])
        u.avatar = path
        u.save()
    return redirect(url_for('user_blue.profile', username=u.username))

@main.route('/profile/<username>/topics')
def user_topics(username):
    u = UserCls.query.filter_by(username=username).first()
    if u:
        cur_u = cur_user()
        page = request.args.get('p', 1, type=int)
        pagination = TopicCls.query.filter_by(user_id=u.id)\
                    .order_by(TopicCls.created_time.desc()).paginate(page, per_page=8)
        user_topics = pagination.items

        return render_template('user/usertopics.html', user=u, topics_created=user_topics,
                               pagination=pagination, cur_user=cur_u)
    else:
        abort(404)

@main.route('/profile/<username>/favorite')
@login_required
def user_favorite(username):
    u = UserCls.query.filter_by(username=username).first()
    cur_u = cur_user()
    if u.id == cur_u.id:
        page = request.args.get('p', 1, type=int)
        pagination = TopicCls.query.join(TopicCollection, TopicCollection.topic_id == TopicCls.id) \
            .filter(TopicCollection.user_id == u.id).order_by(TopicCollection.created_time.desc()) \
            .paginate(page, per_page=8)

        favorite = pagination.items

        return render_template('user/userfavorite.html', user=u, favorite=favorite,
                               pagination=pagination, cur_user=cur_u)
    else:
        abort(403)

@main.route('/profile/<username>/replies')
def user_replied_topics(username):
    u = UserCls.query.filter_by(username=username).first()
    if u:
        cur_u = cur_user()
        page = request.args.get('p', 1, type=int)
        pagination = TopicCls.query.join(CommentCls, CommentCls.topic_id == TopicCls.id) \
            .filter(CommentCls.sender_id == u.id) \
            .order_by(CommentCls.created_time.desc()).paginate(page, per_page=8)

        tpc_rel = pagination.items

        return render_template('user/user_replied_topics.html', user=u, topics_replied=tpc_rel,
                               pagination=pagination, cur_user=cur_u)
    else:
        abort(403)


import subprocess
import os
import shlex

from historian_reader.shell import historian

import doeund as m
from ..logger import logger
from .model import ShellSession, ShellCommand

HISTORY = os.path.expanduser('~/.dadawarehouse/history')

def download():
    RSYNC = ['rsync', '--archive', '--sparse']
    subdirectories = ['history-nsa/', 'history-home/', 'history-laptop/']
    for subdirectory in subdirectories:
        remote = 'safe:' + os.path.join('rsync', subdirectory)
        logger.debug(' '.join(RSYNC + [remote, HISTORY]))
        rsync = subprocess.Popen(RSYNC + [remote, HISTORY])
        rsync.wait()

def update(sessionmaker):
    download()
    session = sessionmaker()
    shell_history = os.path.join(HISTORY, 'shell')
    previous_shells = (row[0] for row in session.query(ShellSession.filename))
    for log in historian(directory = shell_history, skip = previous_shells):
        shell_session = ShellSession(filename = log['session'],
                                     datetime = log['session_date'])
        todo = [shell_session]
        for command_datetime, command_string in log['commands']:
            command = ShellCommand(
                shellsession = shell_session,
                datetime = command_datetime,
                full_command = command_string)
            todo.append(command)

        session.add_all(todo)
        session.commit()
        logger.info('Inserted commands from shell "%s"' % log['session'])


import tkinter as tk
from tkinter import ttk
import consts

class ActivityLabel(ttk.LabelFrame):
    """
    A label showing the activity/state of something
    """  # specific purpose object, could be generalized later

    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self._ACTIVE_TEXT = tk.StringVar(master=parent, name="ACTIVE_DISPLAY")
        self._ACTIVE_TEXT.set(consts.NOT_ACTIVE_STRING)

        self._label = ttk.Label(self, textvariable=self._ACTIVE_TEXT,
                                foreground=consts.NOT_ACTIVE_COLOR)
        # place instead of grid to center for some reason doesn't compute
        # the height correctly, and results in needing magic numbers to offset
        # self._label.place(relx=0.5, rely=0.5, anchor="center")
        self._label.grid(row=0, column=0, pady="0 10")
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

    def update(self, state: bool):
        """
        Update what is shown in this label
        @args
        - state: boolean representing the activity of the thing this label
                 represents
        """
        if state:
            self._ACTIVE_TEXT.set(consts.ACTIVE_STRING)
            self._label.configure(foreground=consts.ACTIVE_COLOR)
        else:
            self._ACTIVE_TEXT.set(consts.NOT_ACTIVE_STRING)
            self._label.configure(foreground=consts.NOT_ACTIVE_COLOR)


import torch
import torch.nn as nn
import torchvision.transforms.functional as TF
import torch.nn.functional as F

class DoubleConv(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(DoubleConv, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(inplace=True),
        )

    def forward(self, x):
        return self.conv(x)

# Implementation inspired by Aladdin Persson - PyTorch Image Segmentation Tutorial with U-NET: everything from scratch baby
class CNN_with_Unet(nn.Module):
    def __init__(
        self,
        in_channels=5,
        out_channels=1,
        features=[64, 128, 256, 512],
        num_of_class=2,
        dist_from_center=10,
        drop_out=0.3,
        hidden_nodes=512,
    ):
        # UNET Convolution
        super(CNN_with_Unet, self).__init__()
        self.ups = nn.ModuleList()
        self.downs = nn.ModuleList()
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)

        # Down part of UNET
        for feature in features:
            self.downs.append(DoubleConv(in_channels, feature))
            in_channels = feature

            # Up part of UNET
        for feature in reversed(features):
            self.ups.append(
                nn.ConvTranspose2d(
                    feature * 2,
                    feature,
                    kernel_size=2,
                    stride=2,
                )
            )
            self.ups.append(DoubleConv(feature * 2, feature))

        self.bottleneck = DoubleConv(features[-1], features[-1] * 2)
        self.final_conv = nn.Conv2d(features[0], out_channels, kernel_size=1)

        # Fully Connected Layer
        self.flatten = nn.Flatten()

        self.fc1 = nn.Linear(
            in_features=(2 * dist_from_center) ** 2, out_features=hidden_nodes
        )
        self.drop1 = nn.Dropout(p=drop_out)

        self.fc2 = nn.Linear(in_features=hidden_nodes, out_features=hidden_nodes)
        self.drop2 = nn.Dropout(p=drop_out)

        self.out = nn.Linear(in_features=hidden_nodes, out_features=num_of_class)

    def forward(self, x):
        skip_connections = []

        for down in self.downs:
            x = down(x)
            skip_connections.append(x)
            x = self.pool(x)

        x = self.bottleneck(x)
        skip_connections = skip_connections[::-1]

        for idx in range(0, len(self.ups), 2):
            x = self.ups[idx](x)
            skip_connection = skip_connections[idx // 2]

            if x.shape != skip_connection.shape:
                x = TF.resize(x, size=skip_connection.shape[2:], antialias=True)

            concat_skip = torch.cat((skip_connection, x), dim=1)
            x = self.ups[idx + 1](concat_skip)

        x = self.final_conv(x)

        x = self.flatten(x)

        x = F.relu(self.fc1(x))
        x = self.drop1(x)

        x = F.relu(self.fc2(x))
        x = self.drop2(x)

        x = self.out(x)

        return x


from plant import Plant

class Gardener:
    def __init__(
            self,
            name: str = 'unknown',
            *plants: Plant,
    ):
        self.name = name
        self.tracked_plants = plants

    def take_care_of_plants(self):
        for plant in self.tracked_plants:
            plant.grow()

    def take_harvest(self):
        basket = []
        for plant in self.tracked_plants:
            if plant.is_ripen():
                basket.append(plant.collect_harvest())
            else:
                print(f'{plant} еще не созрело для сбора')
        return basket


"""Producer object."""

import pika
from datetime import datetime as dt

from common.logger import log
from .establish_rmq_connection import establish_rmq_connection

class Producer(object):
    """Producer for sending messages into queues.

    Inactive producer does not send heartbeats. Therefore, connection has to be
    restablished after certain time of inactivity.
    """

    def __init__(self, mq_host, mq_port, heartbeat):
        self.mq_host = mq_host
        self.mq_port = mq_port
        self.heartbeat = heartbeat
        # establish connection
        self.establish_connection()

    def establish_connection(self):
        """Establish connection to rmq server."""

        self.connection = establish_rmq_connection(self.mq_host, self.mq_port, self.heartbeat)
        self.channel = self.connection.channel()
        # initialize last hearbeat time stamp
        self.last_heartbeat = dt.now()

    def publish(self, exchange_name, exchange_type, queue_name, routing_key, message, durable_queue=True):
        """Publish message to queue.

        Reestablishes connection if expired."""

        # check if connection is expired and reestablish connection
        if (dt.now() - self.last_heartbeat).total_seconds() >= self.heartbeat:
            self.establish_connection()

        # create exchange
        self.channel.exchange_declare(exchange=exchange_name, exchange_type=exchange_type)

        # initialize durable queue and mark messages as persistent if required
        if durable_queue == True:
            # create route
            self.channel.queue_declare(queue=queue_name, durable=durable_queue)
            # create queue
            self.channel.queue_bind(exchange=exchange_name, queue=queue_name, routing_key=routing_key)
            # set delivery mode
            delivery_mode = 2
        else:
            delivery_mode = 1

        self.channel.basic_publish(
            exchange=exchange_name,
            routing_key=routing_key,
            body=message,
            properties=pika.BasicProperties(
                delivery_mode=delivery_mode,  # make message persistent (delivery_mode=2)
            ))

        log.info("Message sent to queue {} with routing key {} and body {}.".format(queue_name, routing_key, message))

    def close(self):
        """Close producer connection to rmq server."""

        log.info("Close producer connection")
        self.connection.close()


# Write a function which converts the input string to uppercase.
# For example:-
# [10, 14, 2, 23, 19] --> 42 (= 23 + 19)
# [99, 2, 2, 23, 19] --> 122 (= 99 + 23)
# Input sequence contains minimum two elements and every element is an integer.

def my_function():
    a=[]
    size=int(input("enter the size of list"))
    i=0
    while i<size:
        i=i+1
        val=int(input("enter the number"))
        a.append(val)
    print(a)
    maxval=max(a)
    print("max value in list",maxval)
    a.remove(maxval)
    smax=max(a)
    print("second max value in the list",smax)
    print("sum of first max and second max",maxval+smax)
my_function()


from setuptools import setup

package_name = 'bpl_control'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='johnsumskas',
    maintainer_email='john@sumskas.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'joint_state_publisher = bpl_control.joint_state_publisher:main',
            'control_node = bpl_control.control_node:main',
            'end_effector_pose_publisher = bpl_control.end_effector_pose_publisher:main'
            # 'b7_gui = bpl_control.b7_gui:main',
            # 'b5_gui = bpl_control.b5_gui:main'
        ],
    },
)


#!/usr/bin/python3
"""Defines all common attributes/methods for other classes"""
import models
from uuid import uuid4
from datetime import datetime

class BaseModel:
    """Instantiates the basemodel for all other classes.

    Attributes:
        id (string): unique uuid identifier
        created_at (datetime): current datetime when an instance is created
        updated_at (datetime): current datetome when an instance is updated
    """

    def __init__(self, *args, **kwargs):
        """Initializes an instance of BaseModel

        Args:
            args (any): Unused
            kwargs (dict): Key/Value pairs of attributes

        Methods:
            save(self): updates updated_at withe the current datetime
            to_dict(self): returns an updated dict version of the instance
        """
        dateform = "%Y-%m-%dT%H:%M:%S.%f"
        self.id = str(uuid4())
        self.created_at = datetime.today()
        self.updated_at = datetime.today()
        if (len(kwargs) != 0):
            for k, v in kwargs.items():
                if k == 'created_at' or k == 'updated_at':
                    self.__dict__[k] = datetime.strptime(v, dateform)
                else:
                    self.__dict__[k] = v
        else:
            models.storage.new(self)

    def save(self):
        """Updates updated_at with the current datetime"""
        self.updated_at = datetime.today()
        models.storage.save()

    def to_dict(self):
        """Return an updated dict version of the instance"""
        new_dict = self.__dict__.copy()
        new_dict.update({"__class__": self.__class__.__name__})
        new_dict.update({"created_at": self.created_at.isoformat()})
        new_dict.update({"updated_at": self.updated_at.isoformat()})
        return new_dict

    def __str__(self):
        """Returns string representaion of instance"""
        clname = self.__class__.__name__
        return "[{}] ({}) {}".format(clname, self.id, self.__dict__)


import numpy as np
import matplotlib.pyplot as plt
from pyplm.pipelines import model_pipeline, data_pipeline
from pyplm.analyse import tail

plt.style.use("/Users/mk14423/Dropbox/mpl-styles/thesisbody.mplstyle")

file = '/Users/mk14423/Desktop/PaperData/HCP_rsfmri_added_data.hdf5'
group = 'individuals'
# group = 'grouped'
dataset = 'inferredModels'

# pipe = data_pipeline(file, group, dataset)
# print(len(pipe.mods))

# print(pipe.mods['left'].shape)
# hs_indi, Js_indi = pipe.get_parameters('individuals')
# # print(pipe.__dict__.items())
# exit()
fig, ax = plt.subplots()
modpipe = model_pipeline(file, group, dataset)
# print(modpipe.datasets.shape)
# plt.matshow(modpipe.datasets[0])
# plt.show()
modpipe.split_hemispheres(which='left')
# print(modpipe.datasets.shape)
J_meanstds = modpipe.select_parameters(parameter_name='J')
# fig, ax = plt.subplots()
# ax.plot(J_meanstds[0, :], J_meanstds[1, :], ls='none')
# plt.show()
modpipe.transform_to_distributions(
    filter_level=0, bins=100, density=True)

modpipe.tail_distributions(tail='positive')
mcs, xs, yfit = tail.fit_power_laws(modpipe.datasets)
# nMods = len(modpipe.datasets)
nMods = 3
for iMod in range(0, nMods):
    xy = modpipe.datasets[iMod]
    x = xy[0, :]
    y = xy[1, :]
    ax.plot(x, y, label=f'm: {mcs[iMod, 0]:.2f} c {mcs[iMod, 1]:.2f}')
    ax.plot(xs[iMod], yfit[iMod], marker=',', c='k', ls='--')

group = 'grouped'
modpipe = model_pipeline(file, group, dataset)
modpipe.split_hemispheres(which='left')
J_meanstds = modpipe.select_parameters(parameter_name='J')
modpipe.transform_to_distributions(
    filter_level=0, bins=100, density=True)

modpipe.tail_distributions(tail='positive')
mcs, xs, yfit = tail.fit_power_laws(modpipe.datasets)
for iMod in range(0, len(modpipe.datasets)):
    xy = modpipe.datasets[iMod]
    x = xy[0, :]
    y = xy[1, :]
    ax.plot(x, y, label=f'm: {mcs[iMod, 0]:.2f} c {mcs[iMod, 1]:.2f}')
    ax.plot(xs[iMod], yfit[iMod], marker=',', c='k')
plt.xlabel(r'$J_{ij}$')
plt.ylabel(r'$P(J_{ij})$')
plt.legend()
plt.show()
print('-----')
modpipe.tail_distributions(tail='positive')
mcs, xs, yfit = tail.fit_power_laws(modpipe.datasets)
fig, ax = plt.subplots()
for iMod in range(0, len(modpipe.datasets)):
    xy_tail = modpipe.datasets[iMod]
    print(xy_tail.shape)
    x = xy_tail[0, :]
    y = xy_tail[1, :]
    ax.plot(x, y)
plt.show()

print(mcs.shape)
fig, ax = plt.subplots(2, 1)
ax = ax.ravel()
mean = np.mean(mcs[:, 0])
std = np.std(mcs[:, 0])
lbl = f'm: {mean:.2f} pm {std:.2f}'
ax[0].hist(mcs[:, 0], bins=20, label=lbl)
ax[0].legend()

mean = np.mean(mcs[:, 1])
std = np.std(mcs[:, 1])
lbl = f'c: {mean:.2f} pm {std:.2f}'
ax[1].hist(mcs[:, 1], bins=20, label=lbl)
ax[1].legend()
plt.show()
# distros = modpipe.parameter_distributions(Js, 0, bins=101, density=True)
# for distro in distros:
#     plt.plot(distro[0], distro[1])
#     plt.show()
# hs_meanstd = modpipe.parameter_moments(hs)
# Js_meanstd = modpipe.parameter_moments(Js)
# # plt.plot(hs_meanstd[0, :],hs_meanstd[1, :], ls='none')
# plt.plot(Js_meanstd[0, :],Js_meanstd[1, :], ls='none')
# plt.show()
# # print(modpipe.Js.shape, modpipe.hs.shape)
# # print(modpipe.models.shape)


from datetime import timedelta
import logging
from .binary_sensors.target_rate import CarbonIntensityTargetRate

from homeassistant.exceptions import ConfigEntryNotReady

from .const import (
  DOMAIN,

  CONFIG_TARGET_NAME,

  DATA_RATES_COORDINATOR
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=1)

async def async_setup_entry(hass, entry, async_add_entities):
  """Setup sensors based on our entry"""

  if CONFIG_TARGET_NAME in entry.data:
    if DOMAIN not in hass.data or DATA_RATES_COORDINATOR not in hass.data[DOMAIN]:
      raise ConfigEntryNotReady

    await async_setup_target_sensors(hass, entry, async_add_entities)

  return True

async def async_setup_target_sensors(hass, entry, async_add_entities):
  config = dict(entry.data)

  if entry.options:
    config.update(entry.options)

  coordinator = hass.data[DOMAIN][DATA_RATES_COORDINATOR]

  async_add_entities([CarbonIntensityTargetRate(coordinator, config)], True)


"""
Example of usage of the URLMatcher library
"""
import dataclasses
import random
import time
from collections import Counter

from url_matcher import Patterns, URLMatcher
from url_matcher.matcher import IncludePatternsWithoutDomainError

matcher = URLMatcher()

# Let's add a rule for books to scrape product
patterns = Patterns(include=["books.toscrape.com/catalogue/"], exclude=["/catalogue/category/"])
matcher.add_or_update("books product", patterns)

# Now a rule for product list in books to scrape
patterns = Patterns(
    include=["books.toscrape.com/catalogue/category/", "books.toscrape.com/|", "books.toscrape.com/index.html|"]
)
matcher.add_or_update("books productList", patterns)

# Let's try it

url = "https://books.toscrape.com/catalogue/soumission_998/index.html"
assert matcher.match(url) == "books product"

url = "https://books.toscrape.com/catalogue/category/books/fiction_10/index.html"
assert matcher.match(url) == "books productList"

url = "https://amazon.com"
assert not matcher.match(url)

# Adding a pattern without domain fails

try:
    matcher.add_or_update("won't work", Patterns(["/path"]))
    raise AssertionError
except IncludePatternsWithoutDomainError:
    ...

# But the empty pattern works. It matches anything

assert URLMatcher({"Anything": Patterns([""])}).match("http://anything")

# Now let's see that priorities are working. They are applied only if several
# rules match the URL.

patterns = Patterns(["priority.com"])
matcher.add_or_update("low priority", dataclasses.replace(patterns, priority=200))
matcher.add_or_update("high priority", dataclasses.replace(patterns, priority=300))
assert matcher.match("http://priority.com") == "high priority"

# Let's invert the priorities

matcher.add_or_update("low priority", dataclasses.replace(patterns, priority=300))
matcher.add_or_update("high priority", dataclasses.replace(patterns, priority=200))
assert matcher.match("http://priority.com") == "low priority"

# Let's check the speed creating patterns for many domains and matching
# urls for these domains.

def add_patterns(domain):
    patterns = Patterns(include=[f"{domain}/catalogue/?param=book"], exclude=["/catalogue/category/"])
    matcher.add_or_update(f"{domain} product", patterns)

    patterns = Patterns(include=[f"{domain}/catalogue/category/?param=book_list", f"{domain}/", f"{domain}/index.html"])
    matcher.add_or_update(f"{domain} productList", patterns)

N_DOMAINS = 500
N_URLS = 300

URLS = [
    "https://books.toscrape.com/catalogue/soumission_998/index.html?param=book&p1=23&p2=45",
    "https://books.toscrape.com/catalogue/category/books/fiction_10/index.html?param=book_list&p5=23&p6=45",
]

# Adding the patterns
for idx in range(N_DOMAINS):
    add_patterns(idx)

urls = []
for _ in range(N_URLS):
    url = random.choice(URLS)
    domain = random.randint(0, N_DOMAINS - 1)
    url = url.replace("books.toscrape.com", f"{domain}")
    urls.append((domain, url))

# Let's try to match the urls
start = time.perf_counter()
counter: Counter = Counter()
for domain, url in urls:
    match = matcher.match(url)
    counter[bool(match)] += 1
    assert match and f"{domain}" in match
end = time.perf_counter()

# It took in my machine ~ 0.04 millis per URL
print(f"{((end-start)/N_URLS)*1000:.3f} milliseconds per URL. Total {end-start} seconds to match {N_URLS} URLs")

print("Everything worked fine!")


# import the necessary packages
import os
import gc
from imutils import paths
import argparse
import cv2
import numpy as np
from osgeo import gdal, ogr, osr
import time
import easygui
import fnmatch
import sys
import re
import subprocess
import mmap
from multiprocessing import Pool
from multiprocessing import cpu_count
from timeit import default_timer as timer
import time
import subprocess
from tqdm import tqdm

def Gdal_Func(work_data):
    epsg = str(work_data[6])
    tif_out = str(work_data[4])
    vrt = str(work_data[5])
    X1=int(work_data[0])
    X2=int(work_data[1])
    X3=int(work_data[2])
    X4=int(work_data[3])
    gsd = str(work_data[7]/100)

    opt= gdal.WarpOptions(options=['TFW=YES','TILED=YES','BLOCKXSIZE=512'], format='GTiff', outputBounds=[X1,X2,X3,X4], xRes=gsd, yRes=gsd, dstSRS=epsg, multithread=True, resampleAlg=gdal.GRIORA_Bilinear)
    gdal.Warp(tif_out,vrt,options=opt)

def pool_handler(work):
    p = Pool(nbr_cpu)
    for _ in tqdm(p.imap_unordered(Gdal_Func, work), total=len(work)):
        pass

def striplist(l):
    return([x.strip() for x in l])

def cls():
    os.system('cls' if os.name=='nt' else 'clear')

def compLat_Long(degs, mins, secs, comp_dir):
    return (degs + (mins / 60) + (secs / 3600)) * comp_dir

def mapcount(filename):
    f = open(filename, "r+")
    buf = mmap.mmap(f.fileno(), 0)
    lines = 0
    readline = buf.readline
    while readline():
        lines += 1
    return lines-1

def getSignOf(chifre):
    if chifre >= 0:
        return 1
    else:
        return -1

def update_progress(progress):
    barLength = 30 # Modify this to change the length of the progress bar

    block = int(round(barLength*progress))
    text = "\rPercent: [{0}] {1}% ".format( "#"*block + "-"*(barLength-block), int(progress*100))
    sys.stdout.write(text)
    sys.stdout.flush()
    #print('\n')

def epsgtoepsg(EPSG_in, lon, lat, EPSG_out):

 InSR = osr.SpatialReference()
 InSR.ImportFromEPSG(EPSG_in)
 OutSR = osr.SpatialReference()
 OutSR.ImportFromEPSG(EPSG_out)

 Point = ogr.Geometry(ogr.wkbPoint)
 Point.AddPoint(lon,lat) # use your coordinates here
 Point.AssignSpatialReference(InSR)    # tell the point what coordinates it's in
 Point.TransformTo(OutSR)              # project it to the out spatial reference

 return (Point.GetX(),Point.GetY())

def time_elapsed(start):
       end = timer()
       hours, rem = divmod(end-start, 3600)
       minutes, seconds = divmod(rem, 60)
       print('Time elapsed (hh:mm:ss.ms) {:0>2}:{:0>2}:{:05.2f}'.format(int(hours),int(minutes),seconds))

if __name__ == '__main__':

 dirphoto = easygui.diropenbox(msg=None, title="Selectionez le dossier des orthos à decouper et translater", default=None )
 list = fnmatch.filter(os.listdir(dirphoto), '*.tif')
 total_con=len(list)
 D1 = str(total_con)
 msg = str(total_con) +" orthos voulez-vous continuer?"
 title = "Merci de confirmer"
 if easygui.ynbox(msg, title, ('Yes', 'No')): # show a Continue/Cancel dialog
     pass # user chose Continue else: # user chose Cancel
 else:
     exit(0)

 x = [os.path.join(r,file) for r,d,f in os.walk(dirphoto) for file in f if file.endswith(".tif")]
 dirmosaic = easygui.diropenbox(msg=None, title="Selectionez le dossier de sortie (sur un autre volume svp)", default=None )

 cl=0
 ci=0
 cls()

 nbr_cpu = cpu_count()

 cpu_in = easygui.integerbox(msg='Vous avez %i cores, combien souhaitez-vous en allouer?' %nbr_cpu, title='Cores CPU', default=2, lowerbound=2, upperbound=nbr_cpu-2, image=None, root=None)

 eps_in = easygui.integerbox(msg='Entrez le code EPSG des orthos originalles', title='Code EPSG', default=3946, lowerbound=0, upperbound=99999, image=None, root=None)
 source = osr.SpatialReference()
 error = source.ImportFromEPSG(eps_in)
 if error==0:
  pass
 else:
   easygui.msgbox("Le code EPSG n'est pas valide, l'application va se fermer!!")
   sys.exit(0)

 eps_out = easygui.integerbox(msg='Entrez le code EPSG des orthos en sorties', title='Code EPSG', default=2154, lowerbound=0, upperbound=99999, image=None, root=None)
 source = osr.SpatialReference()
 error = source.ImportFromEPSG(eps_out)
 if error==0:
  pass
 else:
   easygui.msgbox("Le code EPSG n'est pas valide, l'application va se fermer!!")
   sys.exit(0)

 cut_shp = easygui.fileopenbox(msg='Selectionez le fichier shp (polygone englobant) en code EPSG:%i de l\'emprise des orthos en sorties' %eps_out, title='Shp',default=dirmosaic+'\\*.shp', filetypes=['*.shp'])
 driver = ogr.GetDriverByName('ESRI Shapefile')
 dataSource = driver.Open(cut_shp, 0)
 # Check to see if shapefile is found.
 if dataSource is None:
    easygui.msgbox("Impossible d\'ouvrir %s l'application va se fermer!!!"%cut_shp)
    sys.exit(0)
 else:
    layer = dataSource.GetLayer()
    featureCount = layer.GetFeatureCount()
    spatialRef = layer.GetSpatialRef()
    epsg_shp = (spatialRef.GetAuthorityCode(None))

    if str(epsg_shp) != str(eps_out):
       easygui.msgbox('Attention ce shp n\'est pas de la même projection aue votre ortho en sortie l\'application va se fermer!!!')
       sys.exit(0)

    if featureCount>1:
        easygui.msgbox('Attention ce shp contient plus de 1 polygone l\'application va se fermer!!!')
        sys.exit(0)
    else:
        for feature in layer:
             geom = feature.GetGeometryRef()
             Wkt  = geom.ExportToWkt()
             geo = geom.GetGeometryName()
             if geo == 'POLYGON':
                pass
             else:
                easygui.msgbox('Attention ce shp n\'est pas un polygone, l\'application va se fermer!!!')
                sys.exit(0)

 ortho_res = easygui.integerbox(msg='Entrez le GSD en centimètre ', title='GSD', default=5, lowerbound=1, upperbound=1000, image=None, root=None)

 ortho_size = easygui.integerbox(msg='Entrez la taille de la dalle en mètres', title='Taille de la dalle', default=200, lowerbound=100, upperbound=10000, image=None, root=None)

 epsgin = "EPSG:" + str(eps_in)
 epsgout = "EPSG:" + str(eps_out)

 print('creation du fichier vrt, patientez...')
 vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', outputSRS=epsgin)
 mon_vrt = dirphoto+'\cutline.vrt'

 gdal.BuildVRT(mon_vrt,x, options=vrt_options)
 #gdal.BuildVRT(mon_vrt,x)
 #rast_obj = gdal.BuildVRT('/vsimem/inmem.vrt',x,options=vrt_options)

 rast_obj = gdal.Open(mon_vrt)

 ulx, xres, xskew, uly, yskew, yres  = rast_obj.GetGeoTransform()

 ncols = rast_obj.RasterXSize
 nrows = rast_obj.RasterYSize
 del rast_obj
 x1 = ulx
 y1 = uly - (xres*nrows)
 x2 = ulx + (xres*ncols)
 y2 = uly

 x1_t0,y1_t0 = epsgtoepsg(eps_in,x1, y1,eps_out)
 x2_t0,y2_t0 = epsgtoepsg(eps_in,x2, y2,eps_out)

 x1_t0 = int(x1_t0 /ortho_size)*ortho_size
 y1_t0 = int(y1_t0 /ortho_size)*ortho_size
 x2_t0 = int(x2_t0 /ortho_size)*ortho_size+ortho_size
 y2_t0 = int(y2_t0 /ortho_size)*ortho_size+ortho_size
 print(x1_t0,y1_t0,x2_t0,y2_t0)
 xdalles = int((x2_t0-x1_t0)/ortho_size)
 ydalles = int((y2_t0-y1_t0)/ortho_size)
 total_dalles = int(xdalles*ydalles)

 cls()
 print('Creation de la liste des dalles pour le traitement sur %i cores'%cpu_in)
 time.sleep(1)
 cls()
 #####################################################
 Photos = np.empty((total_dalles, 8), dtype=object)
 #####################################################

 num = 0

 for i in range(0, xdalles-1):
   x_t = x1_t0+(i*ortho_size)
   X1=str(int(x_t))
   X2=str(int(x_t+ortho_size))
   for ii in range(0, ydalles-1):
      ci  += 1

      y_t = y1_t0+(ii*ortho_size)
      Y1=str(int(y_t))
      Y2=str(int(y_t+ortho_size))

      layer.SetSpatialFilterRect(float(X1),float(Y1),float(X2),float(Y2))
      num = layer.GetFeatureCount()
      if num > 0:
         tif_out = dirmosaic+'\\'+str(X1[:5])+'_'+str(Y2[:6])+'.tif'

         Photos[cl,0] = X1
         Photos[cl,1] = Y1
         Photos[cl,2] = X2
         Photos[cl,3] = Y2
         Photos[cl,4] = tif_out
         Photos[cl,5] = mon_vrt
         Photos[cl,6] = epsgout
         Photos[cl,7] = ortho_res
         cl  += 1

      num = 0
      layer.SetSpatialFilter(None)
      update_progress(ci/total_dalles)

 result = Photos[:cl]

 cls()
 print('Demarage de GdalWarp sur : '+ str(len(result)) +' photos soyez patient...')
 time.sleep(2)
 pool_handler(result)
 exit(0)
 cls()
 print('Fin')


from time import time
from unicodedata import name
from unittest import result

from monarch.monarch_objects import PhaseSpace
from monarch.visual_tools import plot_2d_phase_space, plot_3d_phase_space, plot_histogram
from monarch.uart import UART

from matplotlib import pyplot as plt
import numpy as np

# Define the Lorentz attractor.
sigma = 10
rho = 28
beta = 8/3

ode = (
	lambda x,y,z : sigma * (y - x),
	lambda x,y,z : x * (rho - z) - y,
	lambda x,y,z : x * y - beta * z
)

phase_space = PhaseSpace(
    ode_system = ode,
    dt = 0.001,
    resolution = 32,
    max_limit = 64,
    four_quadrant = True,
    compress_space = False
)

#plot_histogram(phase_space)

fpga = UART(phase_space)
output_data = fpga.primary_eval(timesteps=100)
test_data = phase_space.run_simulation([2,2,2],timesteps=100)
print(output_data)

ax = plt.axes(projection='3d')
ax.plot3D(output_data[:, 0], output_data[:, 1], output_data[:, 2], 'gray')
ax.plot3D(test_data[:, 0], test_data[:, 1], test_data[:, 2], 'blue')
plt.show()


from cffi import FFI
import numpy as np
import time

grid_shape = (512, 512)

ffi = FFI()
ffi.cdef("void evolve(double **in, double **out, double D, double dt);")
# open vscode ex0723_cffi
lib = ffi.dlopen("../ex0721_ffi_ctypes/diffusion.so")

def evolve(grid, out, dt, D=1.0):
    pointer_grid = ffi.cast("double**", grid.ctypes.data)
    pointer_out = ffi.cast("double**", out.ctypes.data)
    lib.evolve(pointer_grid, pointer_out, D, dt)

def run_experiment(num_iterations):
    next_grid = np.zeros(grid_shape)
    grid = np.zeros(grid_shape)

    block_low = int(grid_shape[0] * 0.4)
    block_high = int(grid_shape[0] * 0.5)
    grid[block_low:block_high, block_low:block_high] = 0.005

    start = time.time()
    for i in range(num_iterations):
        evolve(grid, next_grid, 0.1)
        grid, next_grid = next_grid, grid
    return time.time() - start

if __name__ == "__main__":
    print(f"diffusion took {run_experiment(500)} seconds")
    # diffusion took 0.05642199516296387 seconds


class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        less =curr_less = ListNode(-1)
        greater = curr_greater =  ListNode(-1)

        while head:
            if head.val < x:
                curr_less.next = ListNode(head.val)
                curr_less = curr_less.next
            else:
                curr_greater.next = ListNode(head.val)
                curr_greater = curr_greater.next

            head = head.next

        curr_less.next = greater.next

        return less.next


import numpy as np
v0 = 0.3
a = 0.003
tr = v0 / a
nsteps = 10000
dt = tr  * 2.0 / nsteps
tprime = 0.0
t = 0.0
for i in range(nsteps):
    t = t + dt
    v = v0 - a * t
    x = v0 * t - a * t**2 / 2.0
    tprime += np.sqrt(1.0 - v** 2) * dt
    tdiff = tprime - (t-x)
    if(tdiff < 0):
        print(t - x)
        break


#!/usr/bin/python3
"""
Function that prints the name
The validations to do is the
variable don't be a != string
"""

def say_my_name(first_name, last_name=""):
    """Prints the first and last name"""
    if type(first_name) is not str:
        raise TypeError("first_name must be a string")
    if type(last_name) is not str:
        raise TypeError("last_name must be a string")

    print("My name is", first_name, last_name)


from django.db import models
from django.forms import ModelForm, TextInput

class Task(models.Model):
    title = models.CharField('Название', max_length=50)
    task = models.TextField('Описание')

    objects = models.Manager()

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Задача'
        verbose_name_plural = 'Задачи'

class TaskForm(ModelForm):
    class Meta:
        model = Task
        fields = ['title', 'task']
        widgets = {
            "title": TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Введите название'
            }),
            "title": TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Введите описание'
            })
        }


#!/usr/bin/python
#-*- encoding: utf-8 -*-
# 表格生成工具
# 默认使用Tab分割字段
# 如果列数不相同，左对齐

import sys
import argparse

def main():
    # 判断参数
    parser = argparse.ArgumentParser('make a table')
    parser.add_argument('-d', '--delimiter', default='\t', help='delimiter of columns, default is Tab')
    args = parser.parse_args()

    # 准备变量
    row_list = []           # 缓存所有的输入行，消耗内存
    column_length_list = [] # 每列的长度
    column_count = 0        # 列数

    # 读取输入
    for line in sys.stdin:
        column_list = line.strip().split(args.delimiter)
        row_list.append(column_list)
        # 更新列数
        if len(column_list) > column_count:
            column_count = len(column_list)
        # 更新没列长度
        while len(column_length_list) < column_count:
            column_length_list.append(0)
        for i in range(0, min(column_list, column_count)):
            if len(column_list[i]) > column_length_list[i]:
                column_length_list[i] = len(column_list[i])

    # 行分割符
    line_seperator = ''
    for i in column_length_list:
        line_seperator += '+'
        for j in range(0, i+2):
            line_seperator += '-'
    line_seperator += '+\n'

    # 输入表格
    for row in row_list:
        sys.stdout.write(line_seperator)
        for i in range(0, len(row)):
            sys.stdout.write('| %s' % row[i])
            for j in range(0, column_length_list[i] - len(row[i]) + 1):
                sys.stdout.write(' ')
        if len(row) < column_count:
            for i in range(len(row), column_count):
                sys.stdout.write('|')
                for j in range(0, column_length_list[i]+2):
                    sys.stdout.write(' ')
        sys.stdout.write('|\n')
    sys.stdout.write(line_seperator)

if __name__ == '__main__':
    main()


import sys, os, time, re, traceback

import numpy as np
import matplotlib.pyplot as plt

import scipy.optimize as opti

from PIL import Image
from pytesseract import image_to_string

import peakdetect as pdet

from bead_util import find_all_fnames

import cv2

def get_devnum(filename, device_prefix='dev'):
    ### Image naming convention often includes a standard device prefix
    ### like "dev" which defines a subfolder. Look for this identifier in
    ### a given filename. Function should fail if it can't find anything
    try:
        devstr = re.findall("/{:s}[0-9]+/".format(device_prefix), filename)[0]
        devnum = int( re.findall("[0-9]+", devstr)[0] )
        return devnum
    except Exception:
        return None

class SEMImage:

    def __init__(self):
        self.path = ''

        self.full_img_arr = []
        self.img_arr = []
        self.info_bar = []
        self.scale_bar = []

        self.scale_fac = 0.0

    def load(self, filename, bit_depth=16.0):
        '''Load the image into the class container and separate the
           image itself, the information bar, and the scale bar.

           Default sizes are for 1024px wide images from the
           Magellan SEM at SNSF.'''

        self.path = filename
        self.full_img_arr = np.array( Image.open(filename) )
        self.bit_depth = bit_depth

        self.shape = self.full_img_arr.shape

        maxval = self.shape[1] * (2.0**bit_depth - 1.0)
        inds = np.arange(self.shape[0])[np.sum(self.full_img_arr, axis=1) >= 0.99*maxval]

        ### Compute some cropping indices by finding the white borders around the
        ### information. This works for images saved by FEI SEMs, untested on others
        ###    (LOTS OF MAGIC NUMBERS HERE, NEED TO MAKE MORE TRANSPARENT/DYNAMIC)
        starty = int(inds[::-1][1])
        stopy = starty+1 + np.argmax(np.sum(self.full_img_arr[starty+1:-2,-50:], axis=1))
        startx = self.shape[1] - np.argmax(self.full_img_arr[starty+1,:-1][::-1]) - 2

        ### Crop that shit baby
        self.img_arr = self.full_img_arr[:starty,:]
        self.info_bar = self.full_img_arr[starty:,:]
        self.scale_bar = self.full_img_arr[starty:stopy,startx:]
        self.scale_bar = self.scale_bar[1:-1,1:-1]

    def read_scale_bar(self, plot=False, verbose=False):
        '''Try using the Tesseract OCR to automatically read the scale bar.'''
        try:
            output = image_to_string(self.scale_bar[:,100:300], lang='eng')
            number = re.search("[0-9]+", output)[0]
            units = re.search("[a-z]+", output)[0]
        except Exception:
            traceback.print_exc()
            number = 0
            units = 'um'

        if verbose:
            print('FOUND SCALE: ', number, units)

        ### Figure out the units
        if units == 'nm':
            fac = 1.0e-9
        if units == 'um':
            fac = 1.0e-6
        if units == 'mm':
            fac = 1.0e-3

        self.scale_bar_len = float(number) * fac

    def rough_calibrate(self, scale_bar_len=0.0, verbose=True, plot=False):
        '''Using the scale bar portion of the image, find the pixel -> distance
           calibration factor and save this as a class attribute.'''

        ### Try auto-finding the scale from the bar, or query the user
        ### if the automatic procedure fails
        if not scale_bar_len:

            self.read_scale_bar(plot=plot, verbose=verbose)
            scale_bar_len = self.scale_bar_len

            if not scale_bar_len:
                print()
                print("COULDN'T AUTOMATICALLY DETERMINE SCALE BAR SIZE...")

                plt.imshow(self.full_img_arr, cmap='gray')
                plt.figure()
                plt.imshow(self.scale_bar, cmap='gray')
                plt.show()

                scale_bar_len = float(input('ENTER LENGTH [m]: '))

        ### Define some lengths
        full_len = self.scale_bar.shape[1]
        cut_size = int(full_len / 3.0)

        ### Crop the left and right portions of the scale bar to look for
        ### the endcaps of the bar
        first_third = self.scale_bar[:,:cut_size]
        last_third = self.scale_bar[:,-cut_size:]

        ### Marginalize the first and last third and take the max to
        ### to robustly find the end cap
        first_third_marginalized = np.sum(first_third, axis=0)
        first_third_ind = np.argmax(first_third_marginalized)

        last_third_marginalized = np.sum(last_third, axis=0)
        last_third_ind = full_len - np.argmax(last_third_marginalized[::-1]) - 1

        ### Compute the pixel length between these endcaps
        pixel_len = last_third_ind - first_third_ind

        ### Compute the calibration factor
        self.scale_fac = scale_bar_len / pixel_len
        if verbose:
            print('Conversion from pixels to meters', self.scale_fac)

        ### Plot the results of this procedure for debugging
        if plot:
            fig = plt.figure(constrained_layout=True,figsize=(7,3))
            gs = fig.add_gridspec(2, 2)
            ax00 = fig.add_subplot(gs[0,0])
            ax00.imshow(first_third, cmap='gray')
            ax00.set_title('Left endcap')

            ax01 = fig.add_subplot(gs[0,1])
            ax01.imshow(last_third, cmap='gray')
            ax01.set_title('Right endcap')

            ax1 = fig.add_subplot(gs[1,:])
            ax1.imshow(self.scale_bar, cmap='gray')
            ax1.axvline(first_third_ind, color='r')
            ax1.axvline(last_third_ind, color='r')
            ax1.set_title('Output of Calibration: {:d} pixels / {:0.1g} meters'\
                        .format(pixel_len, scale_bar_len))
            fig.tight_layout()
            plt.show()

    def find_edges(self, xinds=None, yinds=None, edge_width=10, plot=False, verbose=False, \
                   vertical=False, horizontal=False, blur_kernel=3, \
                   thresh_fac=0.0, pos_thresh_fac=0.25, neg_thresh_fac=0.25, \
                   image_edge_exclusion=10):
        '''Function to find vertical or horizontal edges (sharp intensity gradients)
           based on marginalizing the image. Not a super smart function, and fails
           when the images are noisy (i.e. when noise ~ edge contrast). Pre-blurring
           can help'''

        if (not horizontal) and (not vertical):
            vertical = True

        if xinds is None:
            xinds = (0, self.shape[1])
        if yinds is None:
            yinds = (0, self.shape[0])

        blur_kernel = int(blur_kernel)

        ### Cast to 8-bit image values
        self._make_8bit()
        temp = np.copy(self.img_arr_8bit[yinds[0]:yinds[1],xinds[0]:xinds[1]])

        shape = temp.shape

        ### Blur the image slightly with gaussian blurring to reduce noise
        blurred = cv2.GaussianBlur(temp, (blur_kernel,blur_kernel), 0)

        ### Compute the horizontal or vertical gradient of each row of pixels in the
        ### cropped image and add these all up, as vertical edges usually have
        ### distinctive intensity gradients
        if vertical:
            deriv_sum = np.zeros(shape[1])
            for row in range(shape[0]):
                deriv_sum += np.gradient(blurred[row,:])
        else:
            deriv_sum = np.zeros(shape[0])
            for col in range(shape[1]):
                deriv_sum += np.gradient(blurred[:,col])

        deriv_sum -= np.mean(deriv_sum)

        ### Define some thresholds
        if thresh_fac:
            pos_thresh_fac = np.copy(thresh_fac)
            neg_thresh_fac = np.copy(thresh_fac)
        pos_thresh = pos_thresh_fac * np.max(deriv_sum)
        neg_thresh = neg_thresh_fac * np.min(deriv_sum)

        ### Find the indices above and below the defined thresholds
        if vertical:
            all_inds = np.arange(shape[1])
        else:
            all_inds = np.arange(shape[0])
        inds_above = all_inds[deriv_sum > pos_thresh]
        inds_below = all_inds[deriv_sum < neg_thresh]

        pos_peaks = []
        neg_peaks = []

        pos_peaks = self._parse_threshold_indices(inds_above, deriv_sum, image_edge_exclusion)
        neg_peaks = self._parse_threshold_indices(inds_below, deriv_sum, image_edge_exclusion)

        ### Simultaneously analyze positive and negative peaks, looking for the distinctive
        ### paired peaks that arise when imaging 3-dimensional structures with scanning
        ### electron microscopy. Still accepts peaks that aren't paired, but includes a
        ### boolean flag that is False when there is only a single positive or a single
        ### negative peak in the gradient associated with the image
        edge_locations = []
        neg_done = []
        for pos_peak in pos_peaks:
            found_match = False
            for neg_peak_ind, neg_peak in enumerate(neg_peaks):
                if np.abs(pos_peak[0] - neg_peak[0]) <= edge_width:
                    ### Avoid double counting closely spaced edges
                    if neg_peak_ind in neg_done:
                        continue
                    edge_locations.append([np.average([pos_peak[0], neg_peak[0]], \
                                            weights=np.abs([pos_peak[1], neg_peak[1]])), True])
                    neg_done.append(neg_peak_ind)
                    found_match = True
                    break
            if found_match:
                continue
            edge_locations.append([pos_peak[0], False])

        for neg_peak_ind, neg_peak in enumerate(neg_peaks):
            if neg_peak_ind in neg_done:
                continue
            edge_locations.append([neg_peak[0], False])

        ### If desired, plot the gradient analysis showing all positive and negative
        ### peaks identified, as well as the source image itself and the locations of
        ### any edges
        if plot:
            if vertical:
                fig, axarr = plt.subplots( 2,1,figsize=(8,8), sharex=True, \
                                            gridspec_kw={'height_ratios': (3,1)} )
            else:
                fig, axarr = plt.subplots( 1,2,figsize=(10,8), sharey=True, \
                                            gridspec_kw={'width_ratios': (3,1)} )

            if vertical:
                axarr[1].plot(deriv_sum)
                axarr[1].set_title('Sum of horizontal gradients')
                axarr[1].set_xlim((0,shape[1]))
                for peak in pos_peaks:
                    axarr[1].axvline(peak[0], ls=':', lw=3, color='r')
                for peak in neg_peaks:
                    axarr[1].axvline(peak[0], ls=':', lw=3, color='b')
            else:
                axarr[1].plot(deriv_sum, range(shape[0]))
                axarr[1].set_title('Sum of vertical gradients')
                axarr[1].set_ylim((0,shape[0]))
                for peak in pos_peaks:
                    axarr[1].axhline(peak[0], ls=':', lw=3, color='r')
                for peak in neg_peaks:
                    axarr[1].axhline(peak[0], ls=':', lw=3, color='b')

            axarr[0].set_title('Found edges')
            axarr[0].imshow(temp, cmap='gray')
            for edge in edge_locations:
                if edge[1]:
                    alpha = 1.0
                else:
                    alpha = 0.4

                if vertical:
                    axarr[0].axvline(edge[0], ls=':', lw=3, color='r', alpha=alpha)
                else:
                    axarr[0].axhline(edge[0], ls=':', lw=3, color='r', alpha=alpha)

            axarr[0].set_aspect('auto')

            fig.tight_layout()
            plt.show()

        ### Adjust for the cropping so the returned edges are relative to the pixel
        ### space of the uncropped original image
        for edge_ind, edge in enumerate(edge_locations):
            if vertical:
                offset = xinds[0]
            if horizontal:
                offset = yinds[0]

            edge_locations[edge_ind][0] += offset

        return edge_locations

    def find_circles(self, xinds=None, yinds=None, \
                     blur_kernel=3, th_block_size=15, constant=2, global_thresh=220, \
                     accumulator=1, min_dist=0, min_radius=0, max_radius=0, \
                     Hough_param1=200, Hough_param2=20, radius_adj=-3, \
                     plot=False, verbose=False):
        '''Function to find circles in an image. After some pre-processing, it applies the
           Hough transform to find circles. Because of the blurring and thresholding, the
           radii from the HoughCircles() function need to be adjusted slightly. This is
           NOT done well at this point, so it's a user-supplied argument.'''

        if min_dist == 0:
            min_dist = 0.01 * np.min(self.shape)

        if max_radius == 0:
            max_radius = np.max(self.shape)

        if xinds is None:
            xinds = (0, self.shape[1])
        if yinds is None:
            yinds = (0, self.shape[0])

        blur_kernel = int(blur_kernel)

        ### Cast to 8-bit image values
        self._make_8bit()
        temp = np.copy(self.img_arr_8bit[yinds[0]:yinds[1],xinds[0]:xinds[1]])

        ### Blur the image to reduce noise and improve the circle detection
        blur = cv2.GaussianBlur(temp, (blur_kernel, blur_kernel), 0)

        ### For the first step, apply an adaptive gaussian threshold to the blurred
        ### image, which should help to keep this method robust against non-uniform
        ### illumination of features. Produces a binary image
        th = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
                                   cv2.THRESH_BINARY_INV, th_block_size, constant)

        th_copy = np.copy(th)

        ### Re-blur the threshold image so only thick contrast gradients survive
        new_blur = cv2.GaussianBlur(th, (5, 5), 0)

        ### Apply a strict global threshold to the re-blurred image to retain the thick
        ### gradients that weren't blurred
        _, new_th = cv2.threshold(new_blur, global_thresh, 255, cv2.THRESH_BINARY)

        ### Apply the Hough Transform to the second threshold image
        circles = cv2.HoughCircles(new_th, cv2.HOUGH_GRADIENT, accumulator, \
                                   min_dist, param1=Hough_param1, param2=Hough_param2, \
                                   minRadius=min_radius, maxRadius=max_radius)

        if plot:
            if circles is not None:
                fig, ax = plt.subplots(1,1)
                fig2, ax2 = plt.subplots(1,1)
                ax.imshow(new_th, cmap='gray', zorder=1)
                ax2.imshow(temp, cmap='gray', zorder=1)
                for circle_ind, circle in enumerate(circles[0]):
                    ax.scatter([circle[0]], [circle[1]], color='g', marker='X', s=25, zorder=2)
                    ax2.scatter([circle[0]], [circle[1]], color='g', marker='X', s=25, zorder=2)
                    plot_circle = plt.Circle([circle[0], circle[1]], circle[2], \
                                             color='r', fill=False, zorder=3)
                    plot_circle2 = plt.Circle([circle[0], circle[1]], circle[2]+radius_adj, \
                                             color='r', fill=False, zorder=3)
                    ax.add_artist(plot_circle)
                    ax2.add_artist(plot_circle2)

                    circles[0][circle_ind][2] += radius_adj

                plt.show()
                input()
            else:
                fig, ax = plt.subplots(1,1)
                ax.imshow(new_th, cmap='gray', zorder=1)
                print('NO CIRCLES FOUND IN THRESHOLD IMAGE')

                plt.show()
                input()

        return circles[0]

    ### Internal methods so function definitions aren't insane. Probably should try to put more
    ### stuff down here so I look like a legit programmer

    def _make_8bit(self):
        '''Convert images arrays to 8bit values for use with openCV.'''

        self.full_img_arr_8bit = (self.full_img_arr * (256.0 / (2.0**self.bit_depth))).astype(np.uint8)
        self.img_arr_8bit = (self.img_arr * (256.0 / (2.0**self.bit_depth))).astype(np.uint8)

    def _parse_threshold_indices(self, indices, deriv_sum, exclusion):
        '''Process a list of indices giving the locations of "deriv_sum" above/below
           a threshold value, finding and averaging the locations and values of
           consecutive indices. Assumes indices are monotonic'''

        peaks = []

        cinds = []
        for ind in indices:

            ### Determine if the current above/below-threshold index is in
            ### the edge exclusion zone. Because of how this loop works
            ### we only continue on the first bad condition
            bad_cond_1 = (ind < exclusion)
            bad_cond_2 = (ind > (len(deriv_sum) - exclusion))
            if bad_cond_1:
                continue

            ### Check the end condition, which is necessary to handle peaks that only
            ### span a single index, but exist at the end of the list
            end_cond = ind == indices[-1]

            if not len(cinds):
                cinds.append(ind)
                if not end_cond:
                    continue

            if (ind - cinds[-1]) > 1 or end_cond:
                ### Append the mean location and mean value of all the most
                ### most recent set of consecutive indices (a peak)
                loc = np.mean(cinds)
                val = np.mean(deriv_sum[cinds])
                peaks.append([loc, val])

                ### Start the list over with the new, non-consecutive index.
                ### If end_cond == True, this behavior doesn't matter
                cinds = [ind]
            else:
                cinds.append(ind)

            ### Since indices are monotonic, if we reached the edge of the second
            ### exclusion zone, there can be no more valid indices and we can break
            if bad_cond_2:
                break

        return peaks


import sys
from collections import namedtuple
from queue import LifoQueue

Fish = namedtuple('Fish', ['timer', 'creation_time'])

no_days = 256
initial = [Fish(int(i) + 1, no_days) for i in sys.stdin.readline().strip().split(',')]

cache = dict()

def get_num_children(node):
    if node in cache:
        return cache[node]

    remaining = node.creation_time - node.timer
    s = 1

    if remaining >= 0:
        for time in range(remaining, -1, -7):
            child = Fish(9, time)

            if child not in cache:
                no_child = get_num_children(child)
                cache[child] = no_child
            else:
                no_child = cache[child]

            s += no_child

    cache[node] = s
    return s

total = 0
for f in initial:
    total += get_num_children(f)
print(total)


string = input().split(", ")
beggars_count = int(input())

string_into_integers = []
for nums in string:
    string_into_integers.append(int(nums))

all_results = []
starting_index = 0

while starting_index < beggars_count:
    sum_for_one_beggar = 0
    for indx in range(starting_index, len(string), beggars_count):
        received_sum = string_into_integers[indx]
        sum_for_one_beggar += received_sum
    all_results.append(sum_for_one_beggar)
    starting_index += 1

print(all_results)


import os
import serial

serialPort = serial.Serial(port="COM4", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)

while True:
    # Wait until there is data waiting in the serial buffer
    if serialPort.in_waiting > 0:
        # Read data out of the buffer until a carriage return / new line is found
        serialString = serialPort.readline()  # STM -> PC
        serialString = serialString.strip()

        # Print the contents of the serial data
        print(serialString)


import re
import urllib.parse
import os
import pandas as pd
import hashlib

def read_file(file_name):
    with open(file_name, 'r') as file:
        return file.read()

def readlines_file(file_name):
    # 行毎のリストを返す
    with open(file_name, 'r') as file:
        return file.readlines()

def save_file(file_name, text):
    with open(file_name, 'w') as file:
        file.write(text)

lines = read_file('Natural_Activity_main_rev.tsv').split('\n') # .splitでstrからlist

# 空行の削除
lines = list(filter(lambda line: line != '', lines))

# 不要行の削除
pattern0=re.compile(r'sp\tactivity\treference')
lines = [x for x in lines if re.sub('sp\tactivity\treference', r'', x)]
pattern00=re.compile(r'\(1432\srows\)')
lines = [x for x in lines if re.sub('\(1432\srows\)', r'', x)]

# 指定列の取り出し
#cal1 = list(map(lambda line: line.split('\t')[0], lines))
cal3 = list(map(lambda line: line.split('\t')[2], lines))

# 複数の値（雑誌名）の区切り（;）記号を改行に変換
pattern03a=re.compile(r'\;')
cal3_sep = [re.sub('\;', r'\n', x) for x in cal3]

###
"""
# URLエンコード
cal3_url = [urllib.parse.quote(i, safe='\n') for i in cal3_sep]
"""

###

# 雑誌名をリテラルとしてtsv保存
pattern03=re.compile(r'(.+)')
cal3_leteral = [re.sub('(.+)', r'"\1"', x) for x in cal3_sep]

save_file('Natural_Activity_main_cal3_literal.tsv', "\n".join(cal3_leteral))

###

# 改行や空白文字を削除
cal3_leteral = list(map(lambda x: x.strip(), cal3_leteral)) # .strip()で文頭，文末のスペース，改行を削除

# タブ区切りで並べたリストを作成 zipで複数のシーケンスオブジェクトを同時にループしたが，
# 結合したカラムがずれるために，実行回避
##lines_ttl = ["{0}\t{1}".format(line1, line2) for line1, line2 in zip(cal3_http, cal3_leteral)]
##save_file('References_title_fromNatural_Activity_main.ttl', "\n".join(lines_ttl)) # .joinで，改行で'\n'で行を連結

# 代わりに，とりあえずcsvでカラムを結合
df1 = pd.read_csv('Natural_Activity_main_cal3_literal.tsv')
df2 = pd.read_csv('Natural_Activity_main_cal3_literal.tsv')
df_concat = pd.concat([df1,df2],axis=1)
df_concat = df_concat.drop_duplicates() #重複行の削除
df_concat.to_csv('merged_Natural_Activity_main_cal3_literal.tsv', sep='\t', index=False)

# tsvをリスト形式に変換
ttl = read_file('merged_Natural_Activity_main_cal3_literal.tsv').split('\n') # .splitでlist

# ソート（不要かも）
ttl_sorted = sorted(ttl) # 並べ替え
ttl_set = set(ttl_sorted) # 重複削除

# 検証用 save_file('ttl.txt', "\n".join(ttl))

# 空行の削除
ttl = list(filter(lambda line: line != '', ttl))

# 指定列の取り出し
cal1 = list(map(lambda line: line.split('\t')[0], ttl))
cal2 = list(map(lambda line: line.split('\t')[1], ttl))

# md5でハッシュ化（subject）
cal1_md5 = [hashlib.md5(i.encode('utf-8')).hexdigest() for i in cal1]
cal1_md5_title = [re.sub('(.+)', r'<http://purl.jp/knapsack/References/\1>\t<http://purl.org/dc/elements/1.1/title>', x) for x in cal1_md5]

# リテラル(object)
cal2_literal = [re.sub('(.+)', r'"\1" . ', x) for x in cal2]

# 検証用 save_file('cal1_md5.txt', "\n".join(cal1_md5))
# 検証用 save_file('cal2_literal.txt', "\n".join(cal2_literal))

# zip関数を使ってトリプル作成
xxx = ["{0}\t{1}".format(line1, line2) for line1, line2 in zip(cal1_md5_title, cal2_literal)]

# ソート
xxx_sorted = sorted(xxx) # 並べ替え
xxx_set = set(xxx_sorted) # 重複削除

save_file('md5References_title_fromNatural_Activity_main.ttl', "\n".join(xxx_set)) # .joinで，改行で'\n'で行を連結

# 参照: https://www.suzu6.net/posts/21/
# 参照: https://www.suzu6.net/posts/22/


from .models import Student, NewId
from database.db_manager import base_manager

ID = 0
SURNAME = 1
NAME = 2
GROUP_ID = 3

def get_students(group_id: int):
    res = base_manager.execute("SELECT S.id, S.surname, S.name, GS.group_id "
                               "FROM students S "
                               "INNER JOIN group_students GS ON GS.student_id = S.id "
                               "WHERE GS.group_id = ?", args=(group_id, ), many=True)
    students = []
    print(res)
    for student in res['data']:
        students.append(Student(id=student[ID], surname=student[SURNAME], name=student[NAME],
                                group_id=student[GROUP_ID]))
    return students

def add_new_student(new_student: Student, ):
    res = base_manager.execute("INSERT INTO students(surname, name) "
                               "VALUES (?, ?) "
                               "RETURNING id", args=(new_student.surname, new_student.name,))
    return NewId(code=res['code'], id=res['data'][0][0])

def update_student(student_id: int, student: Student):
    res = base_manager.execute("UPDATE students SET surname = ?, name = ? WHERE id = ?",
                               args=(student.surname, student.name))
    return NewId(code=res['code'], id=student_id)

def delete_student(student_id: int):
    res = base_manager.execute("DELETE FROM students WHERE id = ?",
                               args=(student_id,))
    return NewId(code=res['code'], id=student_id)


# this is O()
# This could/should be refactored, rather than pass args through to track where we are in total, +/- currentl location in the lines the call recursion, to sum total where we are

def binary_search(numbers, item, first=0, last=None):
    if not last:
        last = len(numbers) - 1

    if item == numbers[first]:
        return 0
    elif item == numbers[last]:
        return last

    midpoint = (first + last) // 2

    if item == numbers[midpoint]:
        return midpoint
    elif item > numbers[midpoint]:
        return binary_search(numbers, item, midpoint + 1)
    elif item < numbers[midpoint]:
        return binary_search(numbers, item, first + 1, midpoint - 1)

my_list = [5, 7, 2, 12, 66, 543, 2, 4, 8, 75, 35, 3, 43, 23, 42]
my_list = sorted(my_list)
print(my_list)

print(binary_search(my_list, 543))


# class User:
#     pass

# user_1 = User()
# user_1.id = "001"
# user_1.username = "Finbarr Obierefu"
# print(user_1.username)

# user_2 = User()
# user_2.id = "002"

# class Car:
#     def __init__(self, brand, color,type, control, number_of_seats):
#         self.fuel_efficiency = 'hybrid'
#         self.color = 'color'
#         self.type = 'type'
#         self.control = 'control'
#         self.transmission = 'Automatic'
#         self.brand = brand
#         self.number_of_tires = 4
#         self.number_of_seats = 'number_of_seats'

#     def move(self,forward,backward):

#         if forward =='x':
#             print("Moving forward")
#         if backward=='y':
#             print("moving backward")

# chisom_car = Car( brand ='Lexus', color = 'Blue', type = 'SUV', control = '4WD', number_of_seats= 7)
# chuka_car = Car( brand ='Toyota', type = 'Salon', color='Black', control = '2WD', number_of_seats=5)
# print(chisom_car.number_of_tires)
# chisom_car.move('x','y')

class User:
    def __init__(self,username,user_id):
        self.username = username
        self.user_id = user_id
        self.following = 0
        self.followers = 0

    def follow(self,user):
        self.following=self.following+1
        user.followers+=1

user1 = User(username= "Finbarr Obierefu", user_id= "001")
user2 = User(username="Chisom Amete", user_id= "002")

user1.follow(user2)

print(user1.followers)
print(user1.following)
print(user2.followers)
print(user2.following)


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

'''
在工具栏中添加工具按钮时，默认不显示文字，添加的文字作为tooltip提示信息
'''

class Statusbar(QMainWindow):
    def  __init__(self):
        super(Statusbar, self).__init__()
        self.iniui()
    def iniui(self):
        self.setWindowTitle("toolbar demo ")
        self.resize(300,400)

        bar=self.menuBar()
        file= bar.addMenu('File')
        file.addAction('show')
        file.triggered.connect(self.processtrigger)
        self.setCentralWidget(QTextEdit())
        self.statusBar=QStatusBar()
        self.setStatusBar(self.statusBar)
    def processtrigger(self,q):
        if q.text()=='show':
            self.statusBar.showMessage(q.text()+'菜单被点击了',5000)
if __name__ == '__main__':
    app=QApplication (sys.argv)
    window=Statusbar()
    window.show()
    sys.exit(app.exec_())