from collections import OrderedDict
from itertools import zip_longest
from logging import warning
from typing import Tuple

import pandas as pd
import numpy as np
from pathlib import Path
from tqdm import tqdm

from . import AlignTypeEnum, BoxAlignCalculator
from .bbox_clustering import index_to_prior_name_map, target_iou

DEBUG_COMPARISON_KEEP_DATA = False

def match_report_by_label(priors_guess, gt_bbox, match_iou, box_aligner, label_dict, unique_str):
    hashed = hash(priors_guess.to_csv())
    np_labels = gt_bbox.label.unique()
    if not np.any(np_labels > 0):
        raise RuntimeError('Expected at least one label that has positive id')
    new_mx_iou_w_prior, new_percent_matched_n = intersect_prior_with_gt_bbox_df(
        box_aligner=box_aligner,
        iou_min_threshold=match_iou,
        priors_df=priors_guess,
        gt_bbox_df=gt_bbox,
        column_name_map=index_to_prior_name_map('{}_iou_%s' % (str(match_iou)), priors_guess),
        precalc_score_df=None,
        precalc_score_name_map=None,
        debug_result_data=Path('iou_pass_opt_dataview-{}_priors-{}_iou-{}.pkl'.format(unique_str, hashed, match_iou))
    )
    new_mx_iou_w_prior = new_mx_iou_w_prior.set_index(gt_bbox.index)
    new_stats = {}
    label_name_map = {_id: _name for _name, _id in label_dict.items()}
    for label in np_labels:
        label_group = gt_bbox.label == label
        label_name = label_name_map[label]
        new_stats[label_name] = new_mx_iou_w_prior[label_group].apply(lambda t: np.count_nonzero(t > match_iou), axis=0)
    match_stats = pd.DataFrame(new_stats)
    return match_stats

def intersect_prior_with_gt_bbox_df(
        box_aligner: BoxAlignCalculator,
        iou_min_threshold: float,
        priors_df: pd.DataFrame,
        gt_bbox_df: pd.DataFrame,
        column_name_map: dict = None,
        precalc_score_df: pd.DataFrame = None,
        precalc_score_name_map: dict = None,
        override_pd_columns: list = None,
        debug_result_data: Path = None,
        no_match_fill_value=np.NaN,
) -> Tuple[pd.DataFrame, pd.Series]:

    def calc_percent_match_per_column(df: pd.DataFrame) -> pd.Series:
        return df.apply(lambda t: np.sum(~np.isnan(t)) / float(len(t)) * 100, axis=0)

    if debug_result_data and debug_result_data.exists():
        print('using pre-calculated data in {}'.format(debug_result_data))
        result = pd.read_pickle(path=str(debug_result_data))
        matched = calc_percent_match_per_column(result)
        return result, matched

    only_priors = list(column_name_map.keys()) if column_name_map else priors_df.index
    if precalc_score_name_map is not None:
        if column_name_map is not None:
            only_priors = {k: 'selected' for k in column_name_map if k in precalc_score_name_map.keys()}
        else:
            try:
                only_priors = only_priors.intersection(list(precalc_score_name_map.keys()))
            except Exception as ex:
                raise IndexError('without supplying column_name_map, precalc_score assumed the same index and failed')

    result = OrderedDict()
    col_query = ['width', 'height'] if not override_pd_columns else override_pd_columns

    if precalc_score_df is not None:
        assert type(precalc_score_df) is pd.DataFrame, "sorry, no documentation yet"
        if precalc_score_name_map is None:
            warning('Precalculated score dataframe without column name mapping, using fallback - column_name_map')
            precalc_score_name_map = column_name_map

    def create_query(gt_bbox: pd.DataFrame, prior_name_in_df: str):
        precalc_col = []
        if precalc_score_df is not None:
            col_name = precalc_score_name_map[prior_name_in_df]
            precalc_col = precalc_score_df[col_name].values
        return zip_longest(gt_bbox[col_query].values, precalc_col)

    def prior_tqdm(priors: pd.DataFrame, action_msg: str = None):
        msg = action_msg if action_msg else 'Intersecting'
        select = priors.index.isin(only_priors)
        return tqdm(zip(priors[select].index, priors[select][col_query].values),
                    total=len(only_priors), desc=msg + ' with priors...')

    for prior_name, prior in prior_tqdm(priors_df):
        metric = []
        for bbox, precalc in create_query(gt_bbox_df, prior_name):
            if precalc and np.isnan(precalc):
                metric.append(no_match_fill_value)
                continue
            shifts = box_aligner(prior, bbox)
            score = np.max(target_iou(prior, bbox, shifts))
            score = score if score > iou_min_threshold else no_match_fill_value
            metric.append(score)
        column_name = column_name_map.get(prior_name) if column_name_map else prior_name
        result[column_name] = metric

    result = pd.DataFrame(result)
    matched = calc_percent_match_per_column(result)

    if DEBUG_COMPARISON_KEEP_DATA and debug_result_data:
        result.to_pickle(path=str(debug_result_data))

    return result, matched

def generate_match_report(gt_bbox: pd.DataFrame, prev_priors: pd.DataFrame, new_priors: pd.DataFrame,
                          label_dict=None, match_iou=None, unique_str='', use_random_box_align=False):

    use_random_box_align = use_random_box_align if match_iou and match_iou > 0 else False
    match_iou = match_iou if match_iou else 0.4
    label_dict = {str(k): k for k in gt_bbox.label.unique()} if label_dict is None else label_dict

    align_strat = AlignTypeEnum.BottomLeft if not use_random_box_align else AlignTypeEnum.MultiRandom
    box_aligner = BoxAlignCalculator(align_strategy=align_strat)

    if not prev_priors.empty:
        print("Intersecting input priors with ground truth:")
        prev_stats = match_report_by_label(prev_priors, gt_bbox, match_iou, box_aligner=box_aligner,
                                           label_dict=label_dict, unique_str=unique_str)
    else:
        prev_stats = None
    print("Intersecting new priors with ground truth:")
    new_stats = match_report_by_label(new_priors, gt_bbox, match_iou, box_aligner=box_aligner,
                                      label_dict=label_dict, unique_str=unique_str)

    if prev_stats is not None:
        print('\n')
        print("Original prior match report:")
        print('\n'+prev_stats.to_string())

    print('\n')
    print("Optimized prior match report:")
    print('\n'+new_stats.to_string())

    return prev_stats, new_stats


import urllib.request
import bs4 as bs
import pandas as pd
import re
import numpy as np
from multiprocessing import Pool
import time
import sys
# For simulating the table on the webpage which is dynamically loaded.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

# Reformats the string into readable text.
def pretty_text(text):
    final = (((text).replace(u'\xa0', u' ')).replace(u'\r ',u'\n'))
    return final

def parse(url):
    #print(".")
    sys.stdout.write("+") # Testing
    sys.stdout.flush()
    try:
        hdr = {'User-Agent':'Mozilla/5.0'}
        req = urllib.request.Request(url,headers=hdr)
        sauce = urllib.request.urlopen(req, timeout=10).read()
        soup = bs.BeautifulSoup(sauce,'html.parser') #Beautiful Soup object

        # Data Extraction from the url.
        poem = (pretty_text(soup.find_all('div', class_="o-poem")[0].text))

        title = soup.find_all('h1')[0].text

        poet = soup.find_all('a', href=re.compile('.*poets/.*'))[0].text

        tags = soup.find_all('a', href=re.compile('.*topics.*'))
        tags = [tag.text for tag in tags]
        tags = ",".join(tags)

        #print(poet)
        #sys.stdout.write('.')
        #sys.stdout.flush()
        #df_.iloc[i] = [title,poem,poet,tags]
        return(title,poem,poet,tags)

    except Exception as IndexError:
        return(None,None,None,None)
    except Exception as e:
        print(e)
        return(None,None,None,None)

def load(total_pages, total_batches):
    file_names = [""]*total_batches
    for i in range(4,total_batches+4):
        #File name formating.
        file_names[i-4] = "PoetryFoundationUrls"+str(i*total_pages+1)+"-"+str(total_pages*(i+1))+".txt"
    return file_names

def main():
    #File name details.
    total_pages   = 400
    urls_per_page = 20
    total_batches = 1
    total_poems = total_pages*urls_per_page

    url_file_names = load(total_pages, total_batches)
    print(url_file_names)
    for file_name in url_file_names:

        print("START: "+file_name)

        urls = np.loadtxt(file_name, dtype="str")
        print(urls.size)
        urls = np.unique(urls) #Deleting the repeated url links.
        print(urls.size)

        # Multiprocessing the extractions.
        i=0
        while(i<urls.size):
            timenow = time.time()
            p = Pool()
            data = 0
            if(i+200<urls.size):
                data = p.map(parse,urls[i:i+200])
            else:
                data = p.map(parse,urls[i:urls.size])
            print()
            print("Should terminate now, took",time.time()-timenow)
            p.terminate()
            p.join()

            print("DONE: "+file_name + str(i) +"-"+str(i+200))
            dataF = pd.DataFrame(columns=["Title","Poem","Poet","Tags"], index=list(range(1,200+1)))

            for row,rowNo in zip(data,list(range(total_poems))):
                dataF.iloc[rowNo] = [row[0],row[1],row[2],row[3]]
            dataF = dataF.dropna()
            dataF.to_csv("G:\\OneDrive - University of Edinburgh\\Poem Generation\\WebScrapping for Poems\\PoemData\\PoetryFoundationData"+file_name[len("PoetryFoundationUrls"):]+ str(i) +"-"+str(i+200)+".csv")
            i=i+200

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('OS', '0017_auto_20151022_2126'),
    ]

    operations = [
        migrations.AlterField(
            model_name='ordem_servicos',
            name='setor',
            field=models.CharField(blank=True, max_length=1, null=True, verbose_name=b'Setor', choices=[(b'H', b'Software'), (b'S', b'Hardware')]),
        ),
    ]


# choose directory or file for selection
import tkinter as tk
from tkinter import filedialog
import os
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from gefxml_reader import Cpt, Bore, Test

main_win = tk.Tk()

logo1 = tk.PhotoImage(file='./img/LogoAmsterdam.png')
tk.Label(main_win, image=logo1).place(x=15, y=95)

logo2 = tk.PhotoImage(file='./img/LogoWapen_van_amsterdam.png')
tk.Label(main_win, image=logo2).place(x=15, y=5)

script_version = ''
script_name = 'Thomas van der Linden'
tk.Label(main_win, text='GEF to gpkg Python Script ', fg='black', font='Courier 16 bold').pack()
tk.Label(main_win, text='Lees GEF bestanden in map met Python =) of kies één of meerdere GEF-file(s)', fg='black', font='Courier 12').pack()
tk.Label(main_win, text = 'Script: ' + script_name, fg='grey', font='Courier 10').place(x=800, y=280)
tk.Label(main_win, text = 'Versie: ' + script_version, fg='grey', font='Courier 10').place(x=1095, y=280)

main_win.geometry("1200x300")
main_win.sourceFolder = ''
main_win.sourceFiles = []

def chooseDir():
    main_win.sourceFolder = filedialog.askdirectory(parent=main_win, title='Please select a directory')

b_chooseDir = tk.Button(main_win, text="Select Folder", width=20, height= 3, command=chooseDir)
b_chooseDir.place(x=335, y=95)
b_chooseDir.width = 100
b_chooseDir.config(font=('Courier 14'))

def chooseFiles():
    main_win.sourceFiles = filedialog.askopenfilenames(parent=main_win, title='Please select a directory', filetypes=(("GEF-files","*.GEF"),("gef-files","*.gef")))

b_chooseFiles = tk.Button(main_win, text="Select File(s)", width=20, height=3, command=chooseFiles)
b_chooseFiles.place(x=635, y=95)
b_chooseFiles.width = 100
b_chooseFiles.config(font=('Courier 14'))

def ContinueButton():
    main_win.destroy()

b_ContinueButton = tk.Button(text="Continue", width=20, height=3, command=ContinueButton)
b_ContinueButton.place(x=635, y=195)
b_ContinueButton.width = 100
b_ContinueButton.config(font=('Courier 14 bold'))

main_win.mainloop()

projectids = []
projectnames = []
companies = []
tests = []
geometries = []
types = []
df = pd.DataFrame()

def gef2gpkg(files):
    for f in files:
        try:
            testType = Test().load_gef(f)
            if testType == 'cpt':
                cpt = Cpt()
                cpt.load_gef(f)
                projectids.append(cpt.projectid)
                projectnames.append(cpt.projectname)
                tests.append(cpt.testid)
                companies.append(cpt.companyid)
                geometries.append(Point(cpt.easting, cpt.northing))
                types.append('CPT')

            elif testType == 'bore':
                bore = Bore()
                bore.load_gef(f)
                projectids.append(bore.projectid)
                projectnames.append(bore.projectname)
                tests.append(bore.testid)
                companies.append(bore.companyid)
                geometries.append(Point(bore.easting, bore.northing))
                types.append('BORE')
        except:
            print(f'{f} fout in bestand')
            pass

    df["projectid"] = projectids
    df["projectname"] = projectnames
    df["test"] = tests
    df["company"] = companies
    df["type"] = types
    gdf = gpd.GeoDataFrame(df, geometry=geometries).set_crs("EPSG:28992")
    return gdf

if main_win.sourceFolder != '':
    filelist = os.listdir(main_win.sourceFolder)
    files = [f'{main_win.sourceFolder}/{f}' for f in filelist if f.lower().endswith('gef')]
    gdf = gef2gpkg(files)
    gdf.to_file(f'{main_win.sourceFolder}/GEF.gpkg', driver='GPKG')

elif len(main_win.sourceFiles) >= 1:
    files = list(main_win.sourceFiles)
    gdf = gef2gpkg(files)
    gdf.to_file(f'./output/GEF.gpkg', driver='GPKG')


import logging
from tiddlywebplugins.wikidata import recordFields
from tiddlywebplugins.wikidata.sendEmail import send

def emailAvox(query, domain='wiki-data.com'):
    domainStem = domain.split('.')[0]
    requestType = query['requestType'][0]
    name = query['name'][0]
    email = query['email'][0]
    try:
        country = query['country'][0]
    except:
        country = "not set"
    company = query['company'][0]
    if requestType == 'request':
        avid = query['avid'][0]
        legal_name = query['legal_name'][0]
        additional_info = query.get('additional_info', [''])[0]
        to = ['requestinformation.'+domainStem+'@avox.info', 'jnthnlstr@googlemail.com']
        subject = 'Request for more information'
        body = 'SPECIFIC REQUEST re: additional information request\n' \
            'for '+legal_name+' (AVID = '+avid+')\n'
        if additional_info != '':
            body += 'Additional info: '+additional_info+'\n'
        body += 'Name: '+name+'\n' \
            'Email address: '+email+'\n' \
            'Country: '+country+'\n' \
            'Company: '+company+'\n'
    elif requestType == 'challenge':
        avid = query['avid'][0]
        legal_name = query['legal_name'][0]
        original_legal_name = query['original_legal_name'][0]
        source = query['source'][0]
        to = ['foundanerror.'+domainStem+'@avox.info', 'jnthnlstr@googlemail.com']
        subject = 'Challenge record'
        body = 'SPECIFIC REQUEST re: correction\n' \
            'for '+original_legal_name+' (AVID = '+avid+')\n' \
            'Name: '+name+'\n' \
            'Email address: '+email+'\n' \
            'Country: '+country+'\n' \
            'Company: '+company+'\n' \
            'Source for challenge: '+source+'\n' \
            'Challenge details\n--------------\n'
        #for field, _ in recordFields.recordFields: # is this syntax valid?
        for field, label, tooltip in recordFields.recordFields:
            try:
                body += label+': '+query[field][0]+'\n'
            except KeyError:
                pass
    elif requestType == 'suggest_new':
        source = query['source'][0]
        to = ['registerentity.'+domainStem+'@avox.info', 'jnthnlstr@googlemail.com']
        subject = domain+' AVID Entity Registration'
        body = """Submittor info
--------------
Name: %s
Email address: %s
Country: %s
Company: %s

Sources:
%s

Record info
--------------
""" % (name, email, country, company, source)
        for field, label, tooltip in recordFields.recordFields:
            try:
                body += label + ': ' + query[field][0] + '\n'
            except KeyError:
                pass
    elif requestType == 'wdds':
        telephone = query['telephone'][0]
        comments = query['comments'][0]
        to = ['wiki-data@avox.info', 'jnthnlstr@googlemail.com']
        subject = 'Wiki-Data File Download Service request'
        body = """Submittor info
--------------
Name: %s
Email address: %s
Company: %s
Telephone: %s

Comments:
%s
--------------
""" % (name, email, company, telephone, comments)
    else:
        to = 'jnthnlstr@googlemail.com'
        subject = 'Unknown contact type'
        body = 'Query: %s' % repr(query)
    logging.debug('to: %s , subject: %s body: %s', repr(to), subject, body)
    send(to, subject=subject, body=body, from_='avox@'+domain)


from edrnsite.vanity import MESSAGE_FACTORY as _

# User data schema
from plone.app.users.userdataschema import IUserDataSchemaProvider
from plone.app.users.userdataschema import IUserDataSchema
from plone.app.users.browser.personalpreferences import UserDataPanelAdapter

# Zope schema & component arch
from zope import schema
from zope.interface import implements

# Utils
from datetime import datetime, date
from DateTime import DateTime as oldDateTime

class IVainUserDataSchema(IUserDataSchema):
    '''Vain user data schema extends the Plone standard user data schema and adds fields for vanity.'''
    vanityPageUpdateDate = schema.Date(
        title=_(u'Vanity Page Update Date'),
        description=_(u"The date the user's vanity page was last updated; if unset, it means never updated."),
        required=False,
    )
    vanityPageLocation = schema.TextLine(
        title=_(u'Vanity Page Location'),
        description=_(u"Where in the site the user's vanity page is located."),
        required=False,
    )

class UserDataSchemaProvider(object):
    '''Utility that provides the vain user data schema on demand'''
    implements(IUserDataSchemaProvider)
    def getSchema(self):
        return IVainUserDataSchema

class VaneUserDataPanelAdapter(UserDataPanelAdapter):
    '''Add methods to get/set vanityPageUpdateDate.'''
    @property
    def vanityPageUpdateDate(self):
        '''Last time vanity page was updated'''
        value = self.context.getProperty('vanityPageUpdateDate', None)
        if value is None:
            return date.min
        elif isinstance(value, basestring):
            return date.strptime(value, '')
        elif isinstance(value, oldDateTime):
            dt = value.asdatetime()
            return date(dt.year, dt.month, dt.day)
        elif isinstance(value, datetime):
            return date(value.year, value.month, value.day)
        # Assume it's a date at this point
        return value
    @vanityPageUpdateDate.setter
    def vanityPageUpdateDate(self, value):
        return self.context.setMemberProperties({'vanityPageUpdateDate': value})
    @property
    def vanityPageLocation(self):
        '''Location of vanity page in the portal'''
        return self.context.getProperty('vanityPageLocation', None)
    @vanityPageLocation.setter
    def vanityPageLocation(self, value):
        return self.context.setMemberProperties({'vanityPageLocation': value})


# import logging
import logging
import os
import socket
from datetime import datetime
import argparse
from pathlib import Path
import pandas as pd

# set up logging variables
log = logging.getLogger(__name__)
unique_str = datetime.now().strftime("%b%d_%H-%M-%S")

Path("logs").mkdir(parents=True, exist_ok=True)
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s | %(levelname)s\t| %(message)s",
    handlers=[
        logging.FileHandler(
            os.path.join(
                os.getcwd(), "logs", f"{unique_str}_{socket.gethostname()}.log"
            ),
            mode="w",
            encoding="utf-8",
        ),
        logging.StreamHandler(),
    ],
)

THRESHOLD = 0.003

def rare_edge(transition_matrix, source, destination):
    try:
        if transition_matrix.loc[source, destination] > THRESHOLD:
            return True
        else:
            return False
    except KeyError:
        return False

def find_all_paths(
    transition_matrix, source, destination, visited, path, visited_nodes, all_paths
):
    visited.append(source)
    path.append(source)
    visited_nodes.add(source)
    if source == destination:
        all_paths.append(path[:])
    else:
        for neighbor in transition_matrix.columns:
            if neighbor not in visited_nodes and rare_edge(
                transition_matrix, source, neighbor
            ):
                find_all_paths(
                    transition_matrix,
                    neighbor,
                    destination,
                    visited,
                    path,
                    visited_nodes,
                    all_paths,
                )
    visited.remove(source)
    path.pop()
    visited_nodes.remove(source)

def get_transition_matrix(df):
    src_counts = df.groupby("src")["count"].sum().reset_index()

    transition_matrix = df.merge(src_counts, on="src", suffixes=["", "_src"])
    transition_matrix["probability"] = 100 * (
        transition_matrix["count"] / transition_matrix["count_src"]
    )

    transition_matrix = transition_matrix.pivot(
        index="src", columns="dst", values="probability"
    )
    transition_matrix = transition_matrix.fillna(0.0)

    return transition_matrix

def get_regularity_score(transition_matrix, path):
    score = 1
    for i in range(len(path) - 1):
        src = path[i]
        dst = path[i + 1]
        probability = transition_matrix.loc[src, dst]
        score *= probability
    return score

def postprocessing_result(path_to_score_dict):

    path_length = lambda path: len(path.split(" -> "))
    decay_factor = 4
    norm_score = lambda path, score: score * pow(decay_factor, path_length(path))
    normalized_path_to_score_dict = {
        path: norm_score(path, score) for path, score in path_to_score_dict.items()
    }

    # Normalize the scores to be between 0 and 10
    max_score = max(normalized_path_to_score_dict.values())
    min_score = min(normalized_path_to_score_dict.values())
    for path, score in normalized_path_to_score_dict.items():
        normalized_path_to_score_dict[path] = (
            10 * (score - min_score) / (max_score - min_score)
        )

    # Sort the paths based on the highest score
    sorted_paths = sorted(
        normalized_path_to_score_dict.items(), key=lambda x: x[1], reverse=True
    )
    return sorted_paths

def find_gadget_chains(input_file, processed_file, output_file):
    log.info(f"input:{input_file}, proc:{processed_file}, output:{output_file}")

    input_df = pd.read_csv(input_file)
    proc_df = pd.read_csv(processed_file)

    transition_matrix = get_transition_matrix(proc_df)

    source = input_df["entry"].values[0]
    destination = input_df["target"].values[0]

    log.info(f"src: {source} dst:{destination}")

    visited = []
    path = []
    visited_nodes = set()
    all_paths = []
    find_all_paths(
        transition_matrix, source, destination, visited, path, visited_nodes, all_paths
    )

    if all_paths:
        log.info(f"{len(all_paths)} # of path exists from {source} to {destination}")

        sorted_paths = postprocessing_result(
            {
                " -> ".join(path): get_regularity_score(transition_matrix, path)
                for path in all_paths
            }
        )

        with open(output_file, "w") as of:
            for path, score in sorted_paths:
                log.info(f"{path}: {score:.2e}")
                of.write(f"{path}: {score:.2e}\n")
    else:
        log.info(f"No path exists from {source} to {destination}.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(prog="Gadget Finder")

    parser.add_argument(
        "-i",
        "--input",
        action="store",
        type=str,
        default="input.csv",
        help="Configuration File Name",
    )
    parser.add_argument(
        "-p",
        "--processed",
        action="store",
        type=str,
        default="proccreate_windows_processed.csv",
        help="Configuration File Name",
    )
    parser.add_argument(
        "-o",
        "--output",
        action="store",
        type=str,
        default="output.txt",
        help="Output File Name",
    )

    args = parser.parse_args()

    find_gadget_chains(args.input, args.processed, args.output)


"""Program to check for messages in a channel containing a URL and Description, adding that to a database and periodically sending one of those cool links to the #cool links channel on the r/discord_bots discord server."""

# pylint: disable=invalid-name, wrong-import-order, multiple-imports, no-member, line-too-long

import discord, aiosqlite, yaml, re, discord_colorize, datetime
from typing import List, Dict
from discord.ext import commands, tasks
from rich.console import Console

# --- Constants --- #

bot = commands.Bot(command_prefix=">", self_bot=True, case_insensitive=True)
bot.colors = discord_colorize.Colors()
bot.databasePath = "database.db"
bot.console = Console()
bot.urlRegex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
bot.titleRegex = r"(?<=Title:)(.*)(?=\n)"
bot.descriptionRegex = r"(?<=Desc:)(.*)(?=\n)"
with open(
    "config.yml", "r", encoding="utf-8"
) as configFile:  # NOTE: Convert testingConfig.yml to config.yml before pushing.
    config = yaml.safe_load(configFile)
configCopy = config.copy()
for key, value in configCopy.items():
    if isinstance(value, str):
        try:
            config[key] = int(value)
        except ValueError:
            pass
del configCopy
bot.config = config

# --- Functions --- #

async def readyDatabase() -> bool:
    """Checks if the database is ready to be used. Else creates a database - username: str, id: int, title: str, description: str, url: str, date_added: str, sent: bool

    Returns:
        Boolean: Whether or not the database was initialised successfully.
    """
    async with aiosqlite.connect(bot.databasePath) as db:
        await db.execute(
            """CREATE TABLE IF NOT EXISTS links(
            username TEXT,
            id INTEGER,
            title TEXT,
            description TEXT,
            url TEXT,
            date_added TEXT,
            sent BOOLEAN
        )"""
        )
        await db.commit()
        return True

def findURLs(string: str) -> List[str]:
    """Finds all URLs in a string and returns them as a list.

    Args:
        string (str): The string to search for URLs in.

    Returns:
        List[str]: A list of URLs found in the string.
    """
    url = re.findall(bot.urlRegex, string)
    return [Url[0] for Url in url]

def returnInfo(string: str) -> Dict[str, str]:
    """Finds the title and description in a string and returns them as a dictionary.

    Args:
        string (str): The string to search for the title and description in.

    Returns:
        Dict[str, str]: A dictionary containing the title and description.
    """
    title = re.findall(bot.titleRegex, string)
    description = re.findall(bot.descriptionRegex, string)
    return {"title": title[0].strip(), "description": description[0].strip()}

# --- Events --- #

@bot.event
async def on_ready() -> None:
    """Event called when the bot is ready."""
    bot.console.log(
        f"Logged in [green]successfully[/green] as [blue]{bot.user}[/blue]."
    )
    bot.console.log("The config file was loaded [green]successfully[/green].")
    await readyDatabase()
    bot.console.log("The database was loaded [green]successfully[/green].")

@bot.event
async def on_message(message: discord.Message) -> None:
    """Event called when a message is sent in a channel.

    Args:
        message (discord.Message): The message that was sent in a channel.
    """
    if message.channel.id != bot.config["submissionChannel"]:
        return
    if message.author.id == bot.user.id:
        return
    replyMessage = await message.reply(
        f"Thank you for your contribution.\n\n```ansi\n{bot.colors.colorize('STATUS:', fg='green', bold=True)} {bot.colors.colorize('Processing...', fg='yellow', underline=True)}\n```"
    )
    links = findURLs(message.content)
    if len(links) == 0:
        await replyMessage.edit(
            content=f"```ansi\n{bot.colors.colorize('STATUS:', fg='red', bold=True)} {bot.colors.colorize('No links found. Stopping.', fg='red', underline=True)}\n```"
        )
        return
    info = returnInfo(message.content)
    if info["title"] == "":
        await replyMessage.edit(
            content=f"```ansi\n{bot.colors.colorize('STATUS:', fg='red', bold=True)} {bot.colors.colorize('No title found. Stopping.', fg='red', underline=True)}\n```"
        )
        return
    if info["description"] == "":
        await replyMessage.edit(
            content=f"```ansi\n{bot.colors.colorize('STATUS:', fg='red', bold=True)} {bot.colors.colorize('No description found. Stopping.', fg='red', underline=True)}\n```"
        )
        return
    title = info["title"]
    description = info["description"]
    url = links[0]
    del links, info
    depthString = f"\n\nTitle: {title}\nDesc: {description}\nURL: {url}"  # .format(title, description, url)
    await replyMessage.edit(
        content=f"```ansi\n{bot.colors.colorize('STATUS:', fg='green', bold=True)} {bot.colors.colorize(f'Processing...{depthString}', fg='yellow', underline=True)}\n```"
    )
    async with aiosqlite.connect(bot.databasePath) as db:
        await db.execute(
            """INSERT INTO links(username, id, title, description, url, date_added, sent) VALUES(?,?,?,?,?,?,?)""",
            (
                f"{message.author.name}#{message.author.discriminator}",
                message.author.id,
                title,
                description,
                url,
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                False,
            ),
        )
        await db.commit()
    await replyMessage.edit(
        content=f"```ansi\n{bot.colors.colorize('STATUS:', fg='green', bold=True)} {bot.colors.colorize(f'Added to database!{depthString}', fg='green', underline=True)}\n```"
    )

# --- Tasks --- #

@tasks.loop(minutes=bot.config["sendingDelay"])
async def sendLinks() -> None:
    """Task that sends links from the database to the submission channel."""
    bot.console.log("Sending random suggestion.")
    async with aiosqlite.connect(bot.databasePath) as db:
        link = await db.execute(
            """SELECT * FROM links WHERE sent = 0 ORDER BY RANDOM() LIMIT 1"""
        )
        link = await link.fetchone()
        if link is None:
            bot.console.log("No links to send.")
            return
        channel = bot.get_channel(bot.config["toSendChannel"])
        author = link[0]
        authorId = link[1]
        title = link[2]
        description = link[3]
        url = link[4]
        date = link[5]

        toSend = f"""```ansi\n{bot.colors.colorize('Cool Link:', fg='green', bold=True)} {bot.colors.colorize(title, fg='yellow', underline=True)}```\n{description}\n\nURL: {url}\nSent by {author} (<@{authorId}>) on {date}."""
        await channel.send(toSend)

        await db.execute("""UPDATE links SET sent = 1 WHERE id = ?""", (link[1],))
        await db.commit()
        bot.console.log(f"Sent link {url}.")

@sendLinks.before_loop
async def before_sendLinks() -> None:
    """Task that sends links from the database to the submission channel."""
    await bot.wait_until_ready()
    bot.console.log("[green]Starting[/green] the sendLinks task.")

# --- Running --- #

if __name__ == "__main__":
    sendLinks.start()
    bot.run(bot.config["token"])


"""This example demonstrates creating an open file dialog."""
import webview
import threading
import os

def open_file_dialog():
    import time
    time.sleep(5)
    print(
        webview.create_file_dialog(
            webview.OPEN_DIALOG,
            allow_multiple=True,
            directory=os.getcwd()))

if __name__ == '__main__':
    t = threading.Thread(target=open_file_dialog)
    t.start()

    webview.create_window(
        "Open file dialog example",
        "http://www.vallender-genetics.ml/shiny/")


import datetime
from fastapi import APIRouter
from mongo_ops import library_db
from models import Student
from utils.customHandlers import customResponse

"""
Below are 5 student data related endpoints:
1. /students              - GET    - Returns all the students data present in the Students DB.
2. /students              - POST   - Adds data for one more student in the Students DB.
3. /students/{student_id} - GET    - Returns data for the student matching with the given id.
4. /students/{student_id} - PUT    - Updates data for the student matching with the given id.
5. /students/{student_id} - DELETE - Removes data for the student matching with the given id.
"""

router = APIRouter()
students_collection = library_db.students

# Get all the students in the DB
@router.get("/students")
async def get_all_students():
    res = await students_collection.find().to_list(None)
    return customResponse(res)

# Get a specific student from the DB
@router.get("/students/{student_id}")
async def get_student(student_id: int):
    res = await students_collection.find_one({'id': student_id})
    return customResponse(res)

# Add another student in the DB
@router.post("/students")
async def add_student(student: Student):
    res = await students_collection.insert_one({
        'name': student.name,
        'id': student.id,
        'school':student.school,
        'date_added': datetime.datetime.now(),
        'date_updated': datetime.datetime.now()
    })

    if res.acknowledged:
        return {'insert_id': str(res.inserted_id)}
    return {'status': 'unable to add data'}

# Update a students data in the DB
@router.put("/students/{student_id}")
async def update_student_data(student_id: int, student: Student):
    updated_data = {'date_updated': datetime.datetime.now()}
    if student.name:
        updated_data.update({'name': student.name})
    if student.id:
        updated_data.update({'id': student.id})
    if student.school:
        updated_data.update({'school': student.school})
    if student.num_books_issued:
        updated_data.update({'num_books_issued': student.num_books_issued})
    if student.has_currently_issued_book:
        updated_data.update({'has_currently_issued_book': student.has_currently_issued_book})
    res = await students_collection.update_one({'id': student_id}, {"$set": updated_data})
    print(res)
    if res.acknowledged:
        return {'status': f'found {res.matched_count} entries, and updated {res.modified_count} entries'}
    return {'status': 'unable to delete data'}

# Delete a students data from the DB
@router.delete("/students/{student_id}")
async def delete_student_data(student_id: int):
    res = await students_collection.delete_one({'id': student_id})
    if res.acknowledged:
        return {'status': f'deleted {res.deleted_count} entries'}
    return {'status': 'unable to delete data'}


from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse
import logging
import os
import signal
import sys
import tempfile
import threading

# Import our curl test data helper
from util import ClosingFileHandler, TestData

if sys.version_info.major >= 3:
    import configparser
else:
    import ConfigParser as configparser

# impacket needs to be installed in the Python environment
try:
    import impacket
except ImportError:
    sys.stderr.write('Python package impacket needs to be installed!\n')
    sys.stderr.write('Use pip or your package manager to install it.\n')
    sys.exit(1)
from impacket import smb as imp_smb
from impacket import smbserver as imp_smbserver
from impacket.nt_errors import (STATUS_ACCESS_DENIED, STATUS_NO_SUCH_FILE,
                                STATUS_SUCCESS)

log = logging.getLogger(__name__)
SERVER_MAGIC = "SERVER_MAGIC"
TESTS_MAGIC = "TESTS_MAGIC"
VERIFIED_REQ = "verifiedserver"
VERIFIED_RSP = "WE ROOLZ: {pid}\n"

class ShutdownHandler(threading.Thread):
    """Cleanly shut down the SMB server

    This can only be done from another thread while the server is in
    serve_forever(), so a thread is spawned here that waits for a shutdown
    signal before doing its thing. Use in a with statement around the
    serve_forever() call.
    """

    def __init__(self, server):
        super(ShutdownHandler, self).__init__()
        self.server = server
        self.shutdown_event = threading.Event()

    def __enter__(self):
        self.start()
        signal.signal(signal.SIGINT, self._sighandler)
        signal.signal(signal.SIGTERM, self._sighandler)

    def __exit__(self, *_):
        # Call for shutdown just in case it wasn't done already
        self.shutdown_event.set()
        # Wait for thread, and therefore also the server, to finish
        self.join()
        # Uninstall our signal handlers
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        # Delete any temporary files created by the server during its run
        log.info("Deleting %d temporary files", len(self.server.tmpfiles))
        for f in self.server.tmpfiles:
            os.unlink(f)

    def _sighandler(self, _signum, _frame):
        # Wake up the cleanup task
        self.shutdown_event.set()

    def run(self):
        # Wait for shutdown signal
        self.shutdown_event.wait()
        # Notify the server to shut down
        self.server.shutdown()

def smbserver(options):
    """Start up a TCP SMB server that serves forever

    """
    if options.pidfile:
        pid = os.getpid()
        # see tests/server/util.c function write_pidfile
        if os.name == "nt":
            pid += 65536
        with open(options.pidfile, "w") as f:
            f.write(str(pid))

    # Here we write a mini config for the server
    smb_config = configparser.ConfigParser()
    smb_config.add_section("global")
    smb_config.set("global", "server_name", "SERVICE")
    smb_config.set("global", "server_os", "UNIX")
    smb_config.set("global", "server_domain", "WORKGROUP")
    smb_config.set("global", "log_file", "")
    smb_config.set("global", "credentials_file", "")

    # We need a share which allows us to test that the server is running
    smb_config.add_section("SERVER")
    smb_config.set("SERVER", "comment", "server function")
    smb_config.set("SERVER", "read only", "yes")
    smb_config.set("SERVER", "share type", "0")
    smb_config.set("SERVER", "path", SERVER_MAGIC)

    # Have a share for tests.  These files will be autogenerated from the
    # test input.
    smb_config.add_section("TESTS")
    smb_config.set("TESTS", "comment", "tests")
    smb_config.set("TESTS", "read only", "yes")
    smb_config.set("TESTS", "share type", "0")
    smb_config.set("TESTS", "path", TESTS_MAGIC)

    if not options.srcdir or not os.path.isdir(options.srcdir):
        raise ScriptException("--srcdir is mandatory")

    test_data_dir = os.path.join(options.srcdir, "data")

    smb_server = TestSmbServer((options.host, options.port),
                               config_parser=smb_config,
                               test_data_directory=test_data_dir)
    log.info("[SMB] setting up SMB server on port %s", options.port)
    smb_server.processConfigFile()

    # Start a thread that cleanly shuts down the server on a signal
    with ShutdownHandler(smb_server):
        # This will block until smb_server.shutdown() is called
        smb_server.serve_forever()

    return 0

class TestSmbServer(imp_smbserver.SMBSERVER):
    """
    Test server for SMB which subclasses the impacket SMBSERVER and provides
    test functionality.
    """

    def __init__(self,
                 address,
                 config_parser=None,
                 test_data_directory=None):
        imp_smbserver.SMBSERVER.__init__(self,
                                         address,
                                         config_parser=config_parser)
        self.tmpfiles = []

        # Set up a test data object so we can get test data later.
        self.ctd = TestData(test_data_directory)

        # Override smbComNtCreateAndX so we can pretend to have files which
        # don't exist.
        self.hookSmbCommand(imp_smb.SMB.SMB_COM_NT_CREATE_ANDX,
                            self.create_and_x)

    def create_and_x(self, conn_id, smb_server, smb_command, recv_packet):
        """
        Our version of smbComNtCreateAndX looks for special test files and
        fools the rest of the framework into opening them as if they were
        normal files.
        """
        conn_data = smb_server.getConnectionData(conn_id)

        # Wrap processing in a try block which allows us to throw SmbException
        # to control the flow.
        try:
            ncax_parms = imp_smb.SMBNtCreateAndX_Parameters(
                smb_command["Parameters"])

            path = self.get_share_path(conn_data,
                                       ncax_parms["RootFid"],
                                       recv_packet["Tid"])
            log.info("[SMB] Requested share path: %s", path)

            disposition = ncax_parms["Disposition"]
            log.debug("[SMB] Requested disposition: %s", disposition)

            # Currently we only support reading files.
            if disposition != imp_smb.FILE_OPEN:
                raise SmbException(STATUS_ACCESS_DENIED,
                                   "Only support reading files")

            # Check to see if the path we were given is actually a
            # magic path which needs generating on the fly.
            if path not in [SERVER_MAGIC, TESTS_MAGIC]:
                # Pass the command onto the original handler.
                return imp_smbserver.SMBCommands.smbComNtCreateAndX(conn_id,
                                                                    smb_server,
                                                                    smb_command,
                                                                    recv_packet)

            flags2 = recv_packet["Flags2"]
            ncax_data = imp_smb.SMBNtCreateAndX_Data(flags=flags2,
                                                     data=smb_command[
                                                         "Data"])
            requested_file = imp_smbserver.decodeSMBString(
                flags2,
                ncax_data["FileName"])
            log.debug("[SMB] User requested file '%s'", requested_file)

            if path == SERVER_MAGIC:
                fid, full_path = self.get_server_path(requested_file)
            else:
                assert (path == TESTS_MAGIC)
                fid, full_path = self.get_test_path(requested_file)

            self.tmpfiles.append(full_path)

            resp_parms = imp_smb.SMBNtCreateAndXResponse_Parameters()
            resp_data = ""

            # Simple way to generate a fid
            if len(conn_data["OpenedFiles"]) == 0:
                fakefid = 1
            else:
                fakefid = conn_data["OpenedFiles"].keys()[-1] + 1
            resp_parms["Fid"] = fakefid
            resp_parms["CreateAction"] = disposition

            if os.path.isdir(path):
                resp_parms[
                    "FileAttributes"] = imp_smb.SMB_FILE_ATTRIBUTE_DIRECTORY
                resp_parms["IsDirectory"] = 1
            else:
                resp_parms["IsDirectory"] = 0
                resp_parms["FileAttributes"] = ncax_parms["FileAttributes"]

            # Get this file's information
            resp_info, error_code = imp_smbserver.queryPathInformation(
                os.path.dirname(full_path), os.path.basename(full_path),
                level=imp_smb.SMB_QUERY_FILE_ALL_INFO)

            if error_code != STATUS_SUCCESS:
                raise SmbException(error_code, "Failed to query path info")

            resp_parms["CreateTime"] = resp_info["CreationTime"]
            resp_parms["LastAccessTime"] = resp_info[
                "LastAccessTime"]
            resp_parms["LastWriteTime"] = resp_info["LastWriteTime"]
            resp_parms["LastChangeTime"] = resp_info[
                "LastChangeTime"]
            resp_parms["FileAttributes"] = resp_info[
                "ExtFileAttributes"]
            resp_parms["AllocationSize"] = resp_info[
                "AllocationSize"]
            resp_parms["EndOfFile"] = resp_info["EndOfFile"]

            # Let's store the fid for the connection
            # smbServer.log("Create file %s, mode:0x%x" % (pathName, mode))
            conn_data["OpenedFiles"][fakefid] = {}
            conn_data["OpenedFiles"][fakefid]["FileHandle"] = fid
            conn_data["OpenedFiles"][fakefid]["FileName"] = path
            conn_data["OpenedFiles"][fakefid]["DeleteOnClose"] = False

        except SmbException as s:
            log.debug("[SMB] SmbException hit: %s", s)
            error_code = s.error_code
            resp_parms = ""
            resp_data = ""

        resp_cmd = imp_smb.SMBCommand(imp_smb.SMB.SMB_COM_NT_CREATE_ANDX)
        resp_cmd["Parameters"] = resp_parms
        resp_cmd["Data"] = resp_data
        smb_server.setConnectionData(conn_id, conn_data)

        return [resp_cmd], None, error_code

    def get_share_path(self, conn_data, root_fid, tid):
        conn_shares = conn_data["ConnectedShares"]

        if tid in conn_shares:
            if root_fid > 0:
                # If we have a rootFid, the path is relative to that fid
                path = conn_data["OpenedFiles"][root_fid]["FileName"]
                log.debug("RootFid present %s!" % path)
            else:
                if "path" in conn_shares[tid]:
                    path = conn_shares[tid]["path"]
                else:
                    raise SmbException(STATUS_ACCESS_DENIED,
                                       "Connection share had no path")
        else:
            raise SmbException(imp_smbserver.STATUS_SMB_BAD_TID,
                               "TID was invalid")

        return path

    def get_server_path(self, requested_filename):
        log.debug("[SMB] Get server path '%s'", requested_filename)

        if requested_filename not in [VERIFIED_REQ]:
            raise SmbException(STATUS_NO_SUCH_FILE, "Couldn't find the file")

        fid, filename = tempfile.mkstemp()
        log.debug("[SMB] Created %s (%d) for storing '%s'",
                  filename, fid, requested_filename)

        contents = ""

        if requested_filename == VERIFIED_REQ:
            log.debug("[SMB] Verifying server is alive")
            pid = os.getpid()
            # see tests/server/util.c function write_pidfile
            if os.name == "nt":
                pid += 65536
            contents = VERIFIED_RSP.format(pid=pid).encode('utf-8')

        self.write_to_fid(fid, contents)
        return fid, filename

    def write_to_fid(self, fid, contents):
        # Write the contents to file descriptor
        os.write(fid, contents)
        os.fsync(fid)

        # Rewind the file to the beginning so a read gets us the contents
        os.lseek(fid, 0, os.SEEK_SET)

    def get_test_path(self, requested_filename):
        log.info("[SMB] Get reply data from 'test%s'", requested_filename)

        fid, filename = tempfile.mkstemp()
        log.debug("[SMB] Created %s (%d) for storing test '%s'",
                  filename, fid, requested_filename)

        try:
            contents = self.ctd.get_test_data(requested_filename).encode('utf-8')
            self.write_to_fid(fid, contents)
            return fid, filename

        except Exception:
            log.exception("Failed to make test file")
            raise SmbException(STATUS_NO_SUCH_FILE, "Failed to make test file")

class SmbException(Exception):
    def __init__(self, error_code, error_message):
        super(SmbException, self).__init__(error_message)
        self.error_code = error_code

class ScriptRC(object):
    """Enum for script return codes"""
    SUCCESS = 0
    FAILURE = 1
    EXCEPTION = 2

class ScriptException(Exception):
    pass

def get_options():
    parser = argparse.ArgumentParser()

    parser.add_argument("--port", action="store", default=9017,
                      type=int, help="port to listen on")
    parser.add_argument("--host", action="store", default="127.0.0.1",
                      help="host to listen on")
    parser.add_argument("--verbose", action="store", type=int, default=0,
                        help="verbose output")
    parser.add_argument("--pidfile", action="store",
                        help="file name for the PID")
    parser.add_argument("--logfile", action="store",
                        help="file name for the log")
    parser.add_argument("--srcdir", action="store", help="test directory")
    parser.add_argument("--id", action="store", help="server ID")
    parser.add_argument("--ipv4", action="store_true", default=0,
                        help="IPv4 flag")

    return parser.parse_args()

def setup_logging(options):
    """
    Set up logging from the command line options
    """
    root_logger = logging.getLogger()
    add_stdout = False

    formatter = logging.Formatter("%(asctime)s %(levelname)-5.5s %(message)s")

    # Write out to a logfile
    if options.logfile:
        handler = ClosingFileHandler(options.logfile)
        handler.setFormatter(formatter)
        handler.setLevel(logging.DEBUG)
        root_logger.addHandler(handler)
    else:
        # The logfile wasn't specified. Add a stdout logger.
        add_stdout = True

    if options.verbose:
        # Add a stdout logger as well in verbose mode
        root_logger.setLevel(logging.DEBUG)
        add_stdout = True
    else:
        root_logger.setLevel(logging.INFO)

    if add_stdout:
        stdout_handler = logging.StreamHandler(sys.stdout)
        stdout_handler.setFormatter(formatter)
        stdout_handler.setLevel(logging.DEBUG)
        root_logger.addHandler(stdout_handler)

if __name__ == '__main__':
    # Get the options from the user.
    options = get_options()

    # Setup logging using the user options
    setup_logging(options)

    # Run main script.
    try:
        rc = smbserver(options)
    except Exception as e:
        log.exception(e)
        rc = ScriptRC.EXCEPTION

    if options.pidfile and os.path.isfile(options.pidfile):
        os.unlink(options.pidfile)

    log.info("[SMB] Returning %d", rc)
    sys.exit(rc)


import json
import os

imgspath = r"F:\ZZC\Instance-check\image\check_guona"

#返回路径下的所有文件名， 返回[]
jsons = os.listdir(imgspath)

print(type(jsons))
print(jsons)
print(len(jsons))

count = 0
imgs_jsons = []
for fn in jsons:
    if fn.split('.')[1] == "json":
        count = count + 1
        imgs_jsons.append(fn)

'''
count_n = 0
imgs_jsons_n = []
for i in range(len(jsons)):
    if jsons[i][-4:] == "json":
        count_n = count_n + 1
        imgs_jsons_n.append(jsons[i])

print(imgs_jsons_n[-1])
print(count_n)
'''
print(len(imgs_jsons))

shapes = []
for fname in imgs_jsons:
    if fname != 'd.json':
        DICT = json.load(open(imgspath + '\\' + fname))
        print(type(DICT))
        L = len(DICT['shapes'])

        per_fname_labels = []
        for i in range(L):
            #DICT['shapes'][i]['label'] = DICT['shapes'][i]['label'].spilt('t')[0][-1]
            per_fname_labels.append(DICT['shapes'][i]['label'])
            #print(DICT['shapes'][i]['label'])

        for i in range(L):
            DICT['shapes'][i]['label'] = per_fname_labels[i].split("_")[0] + '-' + per_fname_labels[i].split("_")[1]
        print(DICT)
        with open(imgspath + '\\' + fname, "w") as dump_f:
            DICT_TEMP = json.dump(DICT, dump_f)
        print(type(DICT_TEMP))


import matplotlib.pyplot as plt
import numpy as np

x = 27

path = []

path.append(x)

while x != 1:
	if x % 2 != 0:
		x = 3 * x + 1
	else:
		x = x / 2
	path.append(x)

ypoints = [0]

for point in path:
	ypoints.append(point)

ypoints = np.array(ypoints)
plt.plot(ypoints)
plt.show()


"""
General database settings
"""
import contextlib
import logging

from sqlalchemy import create_engine, event, DateTime
from sqlalchemy.orm import sessionmaker

from hrsdb import utils

# Logging
logger = logging.getLogger(__name__)

# Defaults
DEFAULT_URL = 'sqlite:///hrsdb.db'

# Global database objects
engine = None
Session = None

def init_db(config=None):
    """Initialse the database

    :param db_url: Database URL to use. If None the config is loaded
    """

    global engine, Session

    if config is None:
        db_url = DEFAULT_URL
    else:
        db_url = config.get('database', 'url', fallback=DEFAULT_URL)

    logger.info("Database URL: %s" % db_url)

    # Cleanup if called multiple times
    if Session is not None:
        Session.close_all()
    if engine is not None:
        engine.dispose()

    engine = create_engine(db_url)
    Session = sessionmaker(bind=engine)

    # Create all database tables
    from hrsdb.db.models import Base, BiometricType
    Base.metadata.create_all(engine)

    # Build static entries
    with open_session() as session:
        BiometricType.create_static(session)

@contextlib.contextmanager
def open_session():
    """
    Handles connections to the database
    """
    global engine, Session

    logger.debug("DBHandler opened")
    session = Session()
    try:
        yield session
        session.commit()
    except Exception as error:
        session.rollback()
        logger.exception("Exception: %s" % str(error))
        raise
    finally:
        session.close()

def to_dict(record):
    """
    Converts a database record into a dictionary
    :param record:  Database record
    :return:        Dictionary key=column value=value
    """
    rdict = {}
    for column in record.__table__.columns:
        value = getattr(record, column.name)

        # Convert datetime string
        if type(column.type) is DateTime:
            value = utils.date2str(value)

        rdict[column.name] = value

    return rdict


from __future__ import division, absolute_import, print_function;
import sys, os, re, time, datetime, math, cmath, decimal, argparse;
from PIL import Image, ImageDraw, ImageFont;
if(__name__ == "__main__"):
 sys.tracebacklimit = 0;
__version_info__ = (1, 2, 8, "RC 1");
if(__version_info__[3]!=None):
 __version__ = str(__version_info__[0])+"."+str(__version_info__[1])+"."+str(__version_info__[2])+" "+str(__version_info__[3]);
if(__version_info__[3]==None):
 __version__ = str(__version_info__[0])+"."+str(__version_info__[1])+"."+str(__version_info__[2]);

def drawColorLine( ctx, x1, y1, x2, y2, color ):
 ctx.line((x1, y1, x2, y2), fill = color);
def drawColorRectangle( ctx, x1, y1, x2, y2, color ):
 ctx.rectangle([(x1, y1), (x2, y2)], fill = color);
#def drawColorText( ctx, size, x, y, text, color ):
# font = ImageFont.truetype(os.path.dirname(__file__)+os.sep+"OCRB.otf", size);
# text = str(text);
# ctx.text((x, y), text, font = font, fill = color);
# del(font);
def drawColorRectangleAlt( ctx, x1, y1, x2, y2, color ):
 ctx.rectangle([(x1, y1), (x2, y2)], outline = color);
def CalcRhythm(daysAlive, period, multi = 100):
 return decimal.Decimal(1 - math.sin((daysAlive % period) / period * 2 * math.pi) * multi);
def CalcRhythmAlt(daysAlive, period):
 return decimal.Decimal(1 - math.sin((daysAlive % period) / period * 2 * math.pi));
def CalcRoundRhythm(daysAlive, period, multi = 100):
 return decimal.Decimal(1 - math.sin((daysAlive % period) / period * 2 * math.pi) * multi).quantize(decimal.Decimal(1.0));
def CalcRoundRhythmAlt(daysAlive, period, multi = 100):
 return decimal.Decimal(1 - math.sin((daysAlive % period) / period * 2 * math.pi)).quantize(decimal.Decimal(1.0));
def csv(value):
 if(sys.version[0]=="2"):
  return map(str, value.split(","));
 if(sys.version[0]>="3"):
  return list(map(str, value.split(",")));
parser = argparse.ArgumentParser(conflict_handler = "resolve", add_help = True);
parser.add_argument("birthday", help = "enter your birthday in MM/DD/YYYY format");
parser.add_argument("-c", "--cdate", default = None, help = "enter center date");
parser.add_argument("-b", "--backward", default = 15, help = "number of days to show before center date");
parser.add_argument("-f", "--forward", default = 15, help = "number of days to show affter center date");
parser.add_argument("-x", "--scalex", default = 10, help = "number of pixels per periods");
parser.add_argument("-s", "--show", default = "emotional,physical,intellectual", type=csv, help = "show theses on chart in csv format");
parser.add_argument("-v", "--version", action = "version", version = __version__);
parser.add_argument("-V", "--verbose", action = "store_true", help = "print various debugging information");
parser.add_argument("-T", "--verbosetype", default = "text", help = "debugging information type");
parser.add_argument("-o", "--output", default = None, help = "input name of output image");
parser.add_argument("-d", "--display", action = "store_true", help = "display image");
getargs = parser.parse_args();
if(getargs.verbosetype=="string"):
 getargs.verbosetype = "text";
if(getargs.verbosetype!="text" and getargs.verbosetype!="csv"):
 getargs.verbosetype = "text";
try:
 bdayinfo = time.strptime(getargs.birthday, "%m/%d/%y");
except ValueError:
 bdayinfo = time.strptime(getargs.birthday, "%m/%d/%Y");
numdaysbackward = int(getargs.backward);
numdaysforward = int(getargs.forward);
pre_biorhythm = Image.new("RGB", (((numdaysbackward + numdaysforward) - 1) * int(getargs.scalex), 210));
biorhythm_img = ImageDraw.Draw(pre_biorhythm);
biorhythm_img.rectangle([(0, 0), (((numdaysbackward + numdaysforward) - 1) * int(getargs.scalex), 210)], fill = (255, 255, 255));
drawColorLine(biorhythm_img, 0, 105, ((numdaysbackward + numdaysforward) - 1) * int(getargs.scalex), 105, (0, 0, 0));
drawColorLine(biorhythm_img, ((numdaysbackward + numdaysforward) * int(getargs.scalex)) / 2, 0, ((numdaysbackward + numdaysforward) * int(getargs.scalex)) / 2, 210, (0, 0, 0));
startloop = 0;
endloop = numdaysbackward + numdaysforward;
while(startloop<endloop):
 if(startloop==(numdaysbackward - 1)):
  startloop = startloop + 1;
 drawColorLine(biorhythm_img, (startloop + 1) * int(getargs.scalex), 0, (startloop + 1) * int(getargs.scalex), 5, (0, 0, 0));
 drawColorLine(biorhythm_img, (startloop + 1) * int(getargs.scalex), 205, (startloop + 1) * int(getargs.scalex), 210, (0, 0, 0));
 startloop = startloop + 1;
startloop = 0;
endloop = 19;
while(startloop<endloop):
 if(startloop==6):
  startloop = startloop + 1;
 drawColorLine(biorhythm_img, 0, (startloop + 1) * 15, 5, (startloop + 1) * 15, (0, 0, 0));
 drawColorLine(biorhythm_img, (((numdaysbackward + numdaysforward) - 1) * int(getargs.scalex)) - 5, (startloop + 1) * 15, ((numdaysbackward + numdaysforward) - 1) * int(getargs.scalex), (startloop + 1) * 15, (0, 0, 0));
 startloop = startloop + 1;
if(not getargs.cdate==None):
 try:
  curdayinfo = time.strptime(getargs.cdate, "%m/%d/%y");
 except ValueError:
  curdayinfo = time.strptime(getargs.cdate, "%m/%d/%Y");
 currentdate = datetime.date(curdayinfo[0], curdayinfo[1], curdayinfo[2]);
if(getargs.cdate==None):
 currentdaytime = datetime.datetime.now();
 currentdate = datetime.date(currentdaytime.year, currentdaytime.month, currentdaytime.day);
birthdate = datetime.date(bdayinfo[0], bdayinfo[1], bdayinfo[2]);
startdate = currentdate - datetime.timedelta(days = numdaysbackward);
enddate = currentdate + datetime.timedelta(days = numdaysforward);
curdate = startdate;
curnum = 1;
if(getargs.verbose==True):
 if(getargs.verbosetype=="csv"):
  biorhythmone = ["number", "date"];
  biorhythmone = biorhythmone + list(getargs.show);
  print(", ".join(biorhythmone));
curyear=str(curdate.year);
curmonth=str(curdate.month);
if(len(curmonth)==1):
 curmonth = "0"+curmonth;
curday=str(curdate.day);
if(len(curmonth)==1):
 curday = "0"+curmonth;
endyear=str(enddate.year);
endmonth=str(enddate.month);
if(len(endmonth)==1):
 endmonth = "0"+endmonth;
endday=str(enddate.day);
if(len(endday)==1):
 endday = "0"+endday;
while(int(curyear+curmonth+curday)<int(endyear+endmonth+endday)):
 birthdays = abs((curdate-birthdate).days);
 if(getargs.verbose==True):
  if(getargs.verbosetype=="text"):
   print("number: "+str(curnum));
 curpos = (curnum-1) * int(getargs.scalex);
 if(getargs.verbose==True):
  if(getargs.verbosetype=="text"):
   print("date: "+str(curdate.month)+"/"+str(curdate.day)+"/"+str(curdate.year));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo = [str(curnum), str(curdate.month)+"/"+str(curdate.day)+"/"+str(curdate.year)];
 emotional = CalcRoundRhythm(birthdays, 28);
 if(curnum==1 and "emotional" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (emotional + 104), curpos, (emotional + 104), (51, 128, 51));
 if(curnum>1 and "emotional" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldemotional + 104), curpos, (emotional + 104), (51, 128, 51));
 if(getargs.verbose==True and "emotional" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("emotional: "+str(emotional));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(emotional));
 oldemotional = emotional;
 physical = CalcRoundRhythm(birthdays, 23);
 if(curnum==1 and "physical" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (physical + 104), curpos, (physical + 104), (153, 51, 51));
 if(curnum>1 and "physical" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldphysical + 104), curpos, (physical + 104), (153, 51, 51));
 if(getargs.verbose==True and "physical" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("physical: "+str(physical));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(physical));
 oldphysical = physical;
 intellectual = CalcRoundRhythm(birthdays, 33);
 if(curnum==1 and "intellectual" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (intellectual + 104), curpos, (intellectual + 104), (51, 51, 170));
 if(curnum>1 and "intellectual" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldintellectual + 104), curpos, (intellectual + 104), (51, 51, 170));
 if(getargs.verbose==True and "intellectual" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("intellectual: "+str(intellectual));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(intellectual));
 oldintellectual = intellectual;
 average = decimal.Decimal((physical + emotional + intellectual) / 3).quantize(decimal.Decimal(1.0));
 if(curnum==1 and "average" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (average + 104), curpos, (average + 104), (0, 0, 0));
 if(curnum>1 and "average" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldaverage + 104), curpos, (average + 104), (0, 0, 0));
 if(getargs.verbose==True and "average" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("average: "+str(average));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(average));
 oldaverage = average;
 spiritual = CalcRoundRhythm(birthdays, 53);
 if(curnum==1 and "spiritual" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (spiritual + 104), curpos, (spiritual + 104), (89, 51, 189));
 if(curnum>1 and "spiritual" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldspiritual + 104), curpos, (spiritual + 104), (89, 51, 189));
 if(getargs.verbose==True and "spiritual" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("spiritual: "+str(spiritual));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(spiritual));
 oldspiritual = spiritual;
 intuition = CalcRoundRhythm(birthdays, 38);
 if(curnum==1 and "intuition" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (intuition + 104), curpos, (intuition + 104), (100, 60, 51));
 if(curnum>1 and "intuition" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldintuition + 104), curpos, (intuition + 104), (100, 60, 51));
 if(getargs.verbose==True and "intuition" in getargs.show):
  print("intuition: "+str(intuition));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(intuition));
 oldintuition = intuition;
 awareness = CalcRoundRhythm(birthdays, 48);
 if(curnum==1 and "awareness" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (awareness + 104), curpos, (awareness + 104), (51, 138, 144));
 if(curnum>1 and "awareness" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldawareness + 104), curpos, (awareness + 104), (51, 138, 144));
 if(getargs.verbose==True and "awareness" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("awareness: "+str(awareness));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(awareness));
 oldawareness = awareness;
 aesthetic = CalcRoundRhythm(birthdays, 43);
 if(curnum==1 and "aesthetic" in getargs.show):
  drawColorLine(biorhythm_img, curpos, (aesthetic + 104), curpos, (aesthetic + 104), (171, 51, 141));
 if(curnum>1 and "aesthetic" in getargs.show):
  drawColorLine(biorhythm_img, oldpos, (oldaesthetic + 104), curpos, (aesthetic + 104), (171, 51, 141));
 if(getargs.verbose==True and "aesthetic" in getargs.show):
  if(getargs.verbosetype=="text"):
   print("aesthetic: "+str(aesthetic));
  if(getargs.verbosetype=="csv"):
   biorhythmtwo.append(str(aesthetic));
 oldaesthetic = aesthetic;
 oldpos = curpos;
 if(getargs.verbose==True):
  if(getargs.verbosetype=="text"):
   print("");
  if(getargs.verbosetype=="csv"):
   print(", ".join(biorhythmtwo));
 curdate = curdate + datetime.timedelta(days = 1);
 curyear=str(curdate.year);
 curmonth=str(curdate.month);
 if(len(curmonth)==1):
  curmonth = "0"+curmonth;
 curday=str(curdate.day);
 if(len(curmonth)==1):
  curday = "0"+curmonth;
 curnum = curnum + 1;
if(getargs.display==True):
 pre_biorhythm.show();
if(not getargs.output==None):
 pre_biorhythm.save(getargs.output);


import sys
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord

'''Concatenate sequences from all input files to create
a supercontig/gene.

Usage: concat-seq.py <seqid> <input files>

'''
seqid = sys.argv[1]
superseq = SeqRecord(seq='')  # empty sequence
numseq = 0
for infile in sys.argv[2:]:
    print >> sys.stderr, 'working on %s' % infile
    for rec in SeqIO.parse(infile, 'fasta'):
        superseq += rec
        numseq += 1

superseq.id = seqid
superseq.description = ''
SeqIO.write(superseq, sys.stdout, 'fasta')

print >> sys.stderr, 'Total sequences: %d' % numseq
print >> sys.stderr, 'Total length: %d bp' % len(superseq)


from typing import List
from itertools import combinations, product

def get_parsed_lines() -> List[int]:
    with open("input.txt") as f:
        return list(map(int, f.readlines()))

def find_first_error(numbers: List[int], preamble: int) -> int:
    for i in range(preamble, len(numbers)):
        if not any(sum(c) == numbers[i] for c in combinations(numbers[i-preamble:i], r=2)):
            return numbers[i]
    raise ValueError("Value not found")

def find_encryption_weakness(numbers: List[int], first_error: int) -> int:
    for i, j in product(range(len(numbers)), range(1, len(numbers) + 1)):
        if i >= j:
            continue
        slice = numbers[i:j]
        if sum(slice) == first_error:
            return min(slice) + max(slice)
    raise ValueError("Weakness not found")

def main():
    numbers = get_parsed_lines()
    first_error = find_first_error(numbers, 25)
    # part 1
    print(first_error)
    # part 2
    print(find_encryption_weakness(numbers, first_error))

if __name__ == "__main__":
    main()


import time
import numpy as np
import cv2
import argparse
import imutils
from imutils.video import VideoStream
from skimage.filters import threshold_local

"""
The follow 2 methods comes from
https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
"""
def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype = "float32")

    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    # return the ordered coordinates
    return rect

def four_point_transform(image, pts):
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
	[0, 0],
	[maxWidth - 1, 0],
	[maxWidth - 1, maxHeight - 1],
	[0, maxHeight - 1]], dtype = "float32")

    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    # return the warped image
    return warped

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-r", "--picamera", type=int, default=-1,
                help="whether or not the Raspberry Pi camera should be used")
args = vars(ap.parse_args())

print("[INFO] camera sensor warming up...")
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

while True:
    frame = vs.read()
    image = imutils.resize(frame, width=400)
    ratio = image.shape[0] / 500.0
    orig = image.copy()
    image = imutils.resize(image, height = 500)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 75, 200)

    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if imutils.is_cv2() else cnts[1]
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        if len(approx) == 4:
            shape_text = 'Rectangle'
        else:
            shape_text = 'Unknown'

        cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
        (x, y, w, h) = cv2.boundingRect(approx)
        cv2.putText(image, shape_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
		    0.5, (0, 255, 255), 2)

    cv2.imshow("Frame", image)

    # TODO: Trigger this from a external device that sends signal to PI
    # # apply the four point transform to obtain a top-down
    # # view of the original image
    # warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

    # # convert the warped image to grayscale, then threshold it
    # # to give it that 'black and white' paper effect
    # warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
    # T = threshold_local(warped, 11, offset = 10, method = "gaussian")
    # warped = (warped > T).astype("uint8") * 255

    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break

cv2.destroyAllWindows()
vs.stop()


from uuid import uuid4
from django.test import TestCase
from testfixtures.django import compare

from api.models.book_model import BookModel
from api.repositories.django_book_repository import DjangoBookRepository
from api.tests.factories.book_model_test_factory import BookModelTestFactory
from domain.entities.book.book import Book
from domain.entities.book.value_objects.book_id import BookId
from domain.entities.book.value_objects.book_name import BookName
from domain.exceptions.book.book_not_found_exception import BookNotFoundException
from domain.factories import BookFactory
from domain.tests.factories import BookIdTestFactory
from domain.tests.factories.book_test_factories.book_test_factory import BookTestFactory

class TestDjangoBookRepository(TestCase):
    def test_get_by_id_WHEN_called_THEN_retrieves_given_book_from_database(self) -> None:
        book_model: BookModel = BookModelTestFactory.create()
        book_id = BookIdTestFactory.build(value=book_model.id)
        django_book_repository = DjangoBookRepository()

        actual_book = django_book_repository.get_by_id(book_id)

        expected_book = BookFactory.build_existing(
            id_=book_model.id,
            name=book_model.name,
            edition=book_model.edition,
            publication_year=book_model.publication_year,
            authors=[author.id for author in book_model.authors.all()]
        )
        compare(expected_book, actual_book)

    def test_get_by_id_WHEN_book_does_not_exist_THEN_raises_book_not_found_exception(self) -> None:
        unexisting_book_id = BookId(uuid4())
        django_book_repository = DjangoBookRepository()

        with self.assertRaises(BookNotFoundException):
            django_book_repository.get_by_id(unexisting_book_id)

    def test_delete_WHEN_book_exists_THEN_deletes_given_book_model(self) -> None:
        book_model: BookModel = BookModelTestFactory.create()
        book_id = BookId(book_model.id)
        django_book_repository = DjangoBookRepository()

        django_book_repository.delete(book_id)

        book_exists = BookModel.objects.filter(pk=book_id.value).exists()
        self.assertFalse(book_exists)

    def test_delete_WHEN_book_does_not_exists_THEN_raises_book_not_found_exception(self) -> None:
        unexisting_book_id = BookId(uuid4())
        django_book_repository = DjangoBookRepository()

        with self.assertRaises(BookNotFoundException):
            django_book_repository.delete(unexisting_book_id)

    def test_filter_by_WHEN_filtering_by_name_THEN_returns_books_that_matches_name(self) -> None:
        book_name = BookName('Fake name')
        book_authors = []
        book_models = BookModelTestFactory.create_batch(1, name=book_name.value, authors=book_authors)
        django_book_repository = DjangoBookRepository()

        result_books = django_book_repository.filter_by(name=book_name)

        expected_books = [BookTestFactory.build(
            id__value=book_models[0].id,
            name__value=book_models[0].name,
            edition__value=book_models[0].edition,
            publication_year__value=book_models[0].publication_year,
            authors=book_authors,
        )]
        compare(expected_books, result_books)


from django.http import JsonResponse
from django.shortcuts import redirect, render
from django.urls import reverse_lazy
from django.views import generic

from static.script.search import search_script
from .forms import SignUpForm
from UserManagement.models import MyUser
from Cart.models import Payment
from Rings.models import Ring

import re

class SignUp(generic.CreateView):
    form_class = SignUpForm
    success_url = reverse_lazy('landing-page')
    template_name = 'signup.html'

def profile_page(request, **kwargs):
    if request.method == 'POST' and request.POST.__contains__('search_input'):
        # Check if POST req. comes from search form
        return search_script(request)

    user_id = kwargs['pk']
    this_user = MyUser.objects.filter(id=user_id).first()

    # return if no user found
    if this_user is None:
        return redirect('landing-page')

    if request.method == 'POST':
        if request.POST.__contains__('load_editpage'):
            return redirect('user_profile_edit', pk=user_id)

    # get payment informations
    bought_items = Payment.objects.filter(user_elem_id=user_id)
    ring_set = []
    quantity_set = []
    for item in bought_items:
        trimmed_items = item.items_id[:-1]  # removes last character from string (,)
        splitted_items = trimmed_items.split(", ")
        for s in splitted_items:
            item_id = re.split("id:| ", s)  # split for the id
            item_qt = re.split("qt:| ", s)  # split for the quantity of bought items
            ring_set.append(Ring.objects.get(id=item_id[1]).bezeichnung)
            quantity_set.append(item_qt[-1])
    # add item and quantity uniform to dictionary
    items_vals_dict = {}
    for i in range(len(ring_set)):
        items_vals_dict[ring_set[i]] = quantity_set[i]

    context = {'user': this_user,
               'payment_dict': items_vals_dict}
    return render(request, 'profile-page.html', context)

def profile_edit(request, **kwargs):
    if request.method == 'POST' and request.POST.__contains__('search_input'):
        # Check if POST req. comes from search form
        return search_script(request)

    user_id = kwargs['pk']
    profile = MyUser.objects.get(id=user_id)

    if request.method == 'POST':
        # return to profile page
        if request.POST.__contains__('discard_changes'):
            return redirect('user_profile', pk=user_id)

        # save changes of edited profile
        if request.POST.get("save_edited_profile") == "":
            if request.FILES.get("file") is not None:
                profile.profile_picture = request.FILES.get("file")

            profile.username = request.POST.get("username")
            profile.email = request.POST.get("email")
            profile.save()
            return JsonResponse({"profile_edit": True,
                                 "new_img_url": profile.profile_picture.url,
                                 "new_username": profile.username
                                 }, status=200)

    context = {'user': profile}
    return render(request, 'profile-edit.html', context)


import unittest

from solvers.central_difference_solver import CentralDifferenceSolver

from tests.utils import *

import numpy as np
from scipy import sparse

class TestCentralDifference(unittest.TestCase):
    def setUp(self):

        # example from bathe
        M = [[2, 0], [0, 1]]
        K = [[6, -2], [-2, 4]]
        C = [[0, 0], [0, 0]]
        F = np.zeros((2, 13))
        F[1, :] = 10
        self.M = sparse.csc_matrix(np.array(M))
        self.K = sparse.csc_matrix(np.array(K))
        self.C = sparse.csc_matrix(np.array(C))
        self.F = sparse.csc_matrix(np.array(F))

        self.u0 = np.zeros(2)
        self.v0 = np.zeros(2)

        self.n_steps = 12
        self.t_step = 0.28
        self.t_total = self.n_steps * self.t_step

        self.time = np.linspace(
            0, self.t_total, int(np.ceil((self.t_total - 0) / self.t_step)+1)
        )

        self.number_eq = 2
        return

    def run_central_difference_test(self, solver):
        res = solver()

        res.initialise(self.number_eq, self.time)
        res.calculate(self.M, self.C, self.K, self.F, 0, self.n_steps)
        # check solution
        np.testing.assert_array_almost_equal(
            np.round(res.u, 2),
            np.round(
                np.array(
                    [
                        [0, 0],
                        [0.000, 0.392],
                        [0.0307, 1.45],
                        [0.168, 2.83],
                        [0.487, 4.14],
                        [1.02, 5.02],
                        [1.7, 5.26],
                        [2.4, 4.9],
                        [2.91, 4.17],
                        [3.07, 3.37],
                        [2.77, 2.78],
                        [2.04, 2.54],
                        [1.02, 2.60],
                    ]
                ),
                2,
            ),
        )

    def test_nd_array_solver_central_difference(self):
        self.M, self.K, self.C, self.F = set_matrices_as_np_array(self.M, self.K, self.C, self.F)
        # set_matrices_as_sparse()
        self.run_central_difference_test(CentralDifferenceSolver)

    def test_sparse_solver_central_difference(self):
        self.M, self.K, self.C, self.F = set_matrices_as_sparse(self.M, self.K, self.C, self.F)
        # set_matrices_as_sparse()
        self.run_central_difference_test(CentralDifferenceSolver)


# 2623
from collections import deque

n, m = map(int, input().split())
arr = []
for i in range(m):
    arr.append(list(map(int, input().split()))[1:])

graph = [[] for _ in range(n+1)]
indegree = [0] * (n+1)

for i in range(m):
    for j in range(1, len(arr[i])):
        a, b = arr[i][j-1], arr[i][j]
        graph[a].append(b)
        indegree[b] += 1

result = []
def topology():
    q = deque()
    for i in range(1, n+1):
        if indegree[i] == 0:
            q.append(i)
            result.append(i)

    while q:
        now = q.popleft()
        for j in graph[now]:
            indegree[j] -= 1
            if indegree[j] == 0:
                q.append(j)
                result.append(j)
topology()

available = True
for i in range(n):
    if indegree[i] >= 1:
        available = False

if available:
    for i in range(n):
        print(result[i])
else:
    print(0)


#!/usr/bin/env python3
#
# Converter from list of Varaible and output of NN to JSON
import json
import argparse

"""
must provide two files:
variables_file:
['var_0','var_1',...,'var_n']
classes_file:
['prediction_1',...,'prediction_n']
"""

def _get_args():
    parser = argparse.ArgumentParser(
        description="Converter from Variable.log to Variable.JSON",
        epilog=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('variables_file', help='list of variable names in list')
    parser.add_argument('classes_file', help='list of predicted variables names in list')
    parser.add_argument('--mean', help='mean value to offset the input', default=0, type=int)
    parser.add_argument('--scale', help='scale the input', default=1,type=int)
    return parser.parse_args()

def _run():
    """Top level routine"""
    args = _get_args()

    inVars=[]
    with open(args.variables_file,'r') as filehandle:
        for line in filehandle:
            currentPlace = line
            var_lines =  currentPlace.replace("['","").replace("']","").replace("','"," ")
            inVars = list(var_lines.split())

    outVars=[]
    with open(args.classes_file,'r') as outhandle:
        for line in outhandle:
            currentPlace = line
            var_lines =  currentPlace.replace("['","").replace("']","").replace("','"," ")
            outVars = list(var_lines.split())

    inputs = build_inputs(inVars, args.mean, args.scale)
    out_dict = {
        'inputs': inputs,
        'class_labels':outVars
    }

    print(json.dumps(out_dict,indent=2))

def build_inputs(input_items, mean, scale):
    inputs=[]
    for inVar in input_items:
        the_input = {
            'name':inVar,
            'offset':mean,
            'scale':scale
        }
        inputs.append(the_input)
    return inputs

if __name__ == '__main__':
    _run()


import tqdm
import json
import pathlib
from collections import OrderedDict

import click
import librosa
import numpy as np
import soundfile

@click.command(help='Merge clips into segments of similar length')
@click.argument('input_wavs', metavar='INPUT_WAVS')
@click.argument('output_wavs', metavar='OUTPUT_WAVS')
@click.option('--length', type=int, required=False, default=240, metavar='SECONDS')
@click.option('--sr', type=int, required=False, default=16000)
def merge_wavs(
        input_wavs, output_wavs, length, sr
):
    input_wavs = pathlib.Path(input_wavs).resolve()
    assert input_wavs.exists(), 'The input directory does not exist.'
    output_wavs = pathlib.Path(output_wavs).resolve()
    assert not output_wavs.exists() or all(False for _ in output_wavs.iterdir()), \
        'The output directory is not empty.'

    output_wavs.mkdir(parents=True, exist_ok=True)
    tags = OrderedDict()
    count = 0
    cache: list[tuple[str, np.ndarray]] = []
    cache_len = 0.

    def save_cache():
        nonlocal tags, count, cache, cache_len
        waveform_merged = np.concatenate(tuple(c[1] for c in cache))
        filename = (output_wavs / str(count).zfill(8)).with_suffix('.wav')
        soundfile.write(
            str(filename),
            waveform_merged, sr, format='WAV'
        )
        tags[str(filename.stem)] = [
            {
                'filename': c[0],
                'duration': c[1].shape[0] / sr
            }
            for c in cache
        ]
        cache.clear()
        cache_len = 0.
        count += 1

    for wav in tqdm.tqdm(input_wavs.iterdir()):
        if not wav.is_file() or wav.suffix != '.wav':
            continue
        y, _ = librosa.load(wav, sr=sr, mono=True)
        cur_len = y.shape[0] / sr
        if len(cache) > 0 and cache_len + cur_len >= length:
            save_cache()
        cache.append((wav.stem, y))
        cache_len += cur_len
    if len(cache) > 0:
        save_cache()

    tags_path = output_wavs / 'tags.json'
    with open(tags_path, 'w', encoding='utf8') as f:
        json.dump(tags, f, ensure_ascii=False, indent=2)
        print(f'Timestamps saved to {tags_path}')

if __name__ == '__main__':
    merge_wavs()


import os

import pytest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

import dotenv

dotenv.load_dotenv()

desired_cap = {"browser": "Chrome", "name": "Chrome random Safe Search Tests"}
#    'browser_version': '62.0',
#    'os': 'Windows',
#    'os_version': '10',
#    'resolution': '1024x768',

# __file__ = os.getcwd() + "/"  # paste this if by hand
opt = webdriver.chrome.options.Options()
opt.add_extension(
    extension=os.path.join(os.path.dirname(__file__), "..", "..", "safe.zip")
)
desired_cap.update(opt.to_capabilities())

@pytest.fixture(scope="session")
def secret():
    """string in the form key:token for login to browserstack"""
    return os.environ["BROWSERSTACK_LOGIN"]

@pytest.fixture(scope="session")
def driver(secret):
    url = "http://{}@hub.browserstack.com:80/wd/hub".format(secret)
    driver = webdriver.Remote(command_executor=url, desired_capabilities=desired_cap)
    yield driver
    driver.quit()


'''
        Para o r/brasil

        Pesquisa, análise e gráficos por:
        u/Drunpy

            • Estrutura do código:
                • Apresentação
                • Imports
                • Separação dos dados
                • Gráficos

'''

    #IMPORTS

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

pesquisa = pd.read_csv("Pesquisa T.csv")
df = pd.DataFrame(pesquisa)

    #SEPARAÇÃO DOS DADOS

#Renomeando as colunas para facilitar a leitura
df.columns = ["data", "estado", "idade", "sexo", "renda", "estadocivil", "jatraiu", "jafoitraido", "comoseusamigosencaramisso", "comovcencaraisso", "nafamilia", "qmtraimais", "Sugestoesoucomentarios"]

#Lista com todos os estado para serem usados na separação dos dados
estadosunico = ['MG', 'SC', 'RS', 'SP', 'RJ', 'PE', 'GO', 'DF', 'BA', 'CE', 'PR', 'AL', 'ES', 'RN', 'SE', 'PB', 'TO', 'PI', 'PA', 'RO', 'MT', 'MA', 'AM', 'RR', 'MS', 'AP']

#Abaixo todos os códigos tem intenção de filtrar os dados

estado_x_pessoas = {}
estados_validos = {}

for i in estadosunico:
    qnt = np.count_nonzero(df.estado == i)
    estado_x_pessoas[i] = int(qnt)

#Quantidade de participantes >=19 para o estado ser válido
for k,v in estado_x_pessoas.items():
    if v >= 19:
        estados_validos[k] = v

#Separando qnt de traidores(pessoas) por estado
estado_x_jatraiu = {}
for i in estados_validos:
    qntjatraiu = np.count_nonzero((df.estado == i) & (df.jatraiu == 'Não'))
    estado_x_jatraiu[i] = int(qntjatraiu)

#Extraindo opcoes de idade
idadeops = []
for i in df.idade:
    if i not in idadeops:
        idadeops.append(i)
#correlacionando idades + traidores
idade_traiuqnt = {}
for i in idadeops:
    qntraidores = np.count_nonzero((df.idade == i) & (df.jatraiu == 'Sim'))
    idade_traiuqnt[i] = int(qntraidores)

#Total de pessoas por idade
total_poridade = {}
for i in idadeops:
    cont = np.count_nonzero(df.idade == i)
    total_poridade[i] = cont

#Correlacionando participantes e renda
rendas_un = []
total_prenda ={}
for i in df.renda:
    if i not in rendas_un:
        rendas_un.append(i)

#^GAMBIARRA para trocar o nome do Xticks no gráfico
total_prenda = {'<1000': 78, 'Entre 1000 e 2000': 71, '8000+': 46, 'Entre 3000 e 5000': 49, 'Entre 2000 e 3000': 46, 'Entre 5000 e 8000': 28}

#Quem trai mais
traimais = {'Mulher': 0 , 'Homem': 0}
for i in df.qmtraimais:
    mulher = np.count_nonzero(df.qmtraimais == 'Mulher')
    homem = np.count_nonzero(df.qmtraimais == 'Homem')
    traimais['Mulher'] = mulher
    traimais['Homem'] = homem

#Correlação sexo + Trai (qual dos sexos traiu mais)
sexo_traicao_sim = {}
for i in df.sexo:
    ses = np.count_nonzero((df.sexo == i) & (df.jatraiu == 'Sim'))
    if ses != 0:
        sexo_traicao_sim[i] = ses

sexo_traicao_nao ={}
for i in df.sexo:
    sen = np.count_nonzero((df.sexo == i) & (df.jatraiu == 'Não'))
    if sen != 0:
        sexo_traicao_nao[i] = sen

#O que você acha sobre traicao ?
opn_g = {}
for i in df.comovcencaraisso:
    como = np.count_nonzero(df.comovcencaraisso == i)
    opn_g[i] = como

'''
    #Muitos dados para um intel pentium... SKIPED
#Correl sexo/jafoitraido
sexo_jafoitraido = {}
for sexo in df.sexo:
    for i in df.jafoitraido:
        jafoi = np.count_nonzero((df.sexo == sexo) & (df.jafoitraido == i))
        if jafoi != 0:
            sexo_jafoitraido[sexo] = jafoi
'''

    #GRAFICOS
def total_pessoas():
    exp = pd.DataFrame.from_dict(estados_validos, orient='index')

    exp.plot(kind='pie',
             subplots=True,
             shadow=True,
             explode=[0,0 ,0.05 ,0 , 0],
             labeldistance=1.1,
             autopct='%.1f%%')

    plt.title('Participações validadas', fontsize=15)
    plt.suptitle('Não foram obtidos dados significativos\n de outros estados.', y=0.88, fontsize=6)
    plt.show()

def mulhertraida():
    dfmulhertraida = pd.DataFrame.from_dict(sexo_jafoitraido, orient='index')

def opn_geral():
    dfopng = pd.DataFrame.from_dict(opn_g, orient='index')

    dfopng.plot(kind='pie',
                subplots=True,
                shadow=True,
                explode=[0.05, 0 ,0 ,0 , 0],
                autopct = '%.1f%%')

    plt.title('O que as pessoas acham sobre traição ?', size=20)
    plt.suptitle('**Do total de respostas.', y=0.88, size=12)
    plt.show()

def sexotraicaosim():
    dfsts = pd.DataFrame.from_dict(sexo_traicao_sim, orient='index')

    dfsts.plot(kind='pie',
               subplots=True,
               shadow=True,
               explode= [0, 0.1, 0,0],
               autopct='%.1f%%')

    plt.title('Os que já trairam são: ', size=20)
    plt.suptitle('**Ou opção sexual', y=0.88, size=9)
    plt.show()

def traidores_estado():
    dfestadosvalidos = pd.DataFrame.from_dict(estados_validos, orient='index')
    dfestadosvalidos.columns = ['estado']
    dfestado = pd.DataFrame.from_dict(estado_x_jatraiu, orient='index')
    dfestado.columns = ['estado']

    ax = dfestadosvalidos.plot(color='#3366ff',
                               kind='bar',
                               zorder=2)

    dfestado.plot(kind='bar',
                  ax=ax,
                  label=True,
                  color='#ff3333',
                  title="Participantes que já trairam, por estado.",
                  zorder=2).legend(['Total','Já trairam'])

    plt.xticks(rotation=0)
    plt.grid(axis='y')
    plt.xlabel("Estado")
    plt.ylabel("Participantes")
    plt.show()

def participantes_estado():

    x_axis = ['MG', 'SC', 'SP', 'RJ', 'PR']
    y_axis = estados_validos.values()

    ind = np.arange(len(y_axis))

    plt.title("Quantidade de participantes por estado")

    plt.bar(ind, y_axis, align='center')
    plt.xticks(ind, x_axis)
    plt.grid('on', axis='y')
    plt.show()

def traicao_idade():
    x_axis = total_poridade.values()
    y_axis = total_poridade.keys()
    x2_axis =  idade_traiuqnt.values()

    ind = np.arange(len(y_axis))
    ind_inverted = np.arange(len(y_axis))[::-1]

    plt.barh(ind-0.3,
             list(x_axis)[::-1],
             height=-0.3,
             align='center',
             zorder=2)

    plt.barh(ind-0.6,
             list(x2_axis)[::-1],
             height=-0.3,
             align='center')

    plt.yticks(ind_inverted-0.45, y_axis)
    plt.grid('on', axis='x')
    plt.xlabel('Participantes')
    plt.ylabel('Faixa etária')
    plt.legend(('Total por idade','Já trairam'), loc=1)
    plt.title('Traição por faixa etária', fontsize=20)
    plt.show()

def traicao_renda():
    dfrenda = pd.DataFrame.from_dict(total_prenda, orient='index')
    dfrenda.columns = ['total']
    dfrenda.set_index(dfrenda.columns[0])

    dfrenda.plot(kind='bar',
                 rot=60,
                 width=1,
                 zorder=2,
                 legend=False,
                 color=[plt.cm.Paired(np.arange(len(dfrenda)))])

    plt.grid('on', axis='y')
    plt.ylabel('Total', fontsize=15)
    plt.title('Traição por renda mensal', fontsize=20)
    plt.show()

def qmtraimais():
    dftraimais = pd.DataFrame.from_dict(traimais, orient='index')

    dftraimais.plot(kind='pie',
                    shadow=True,
                    explode=[0, 0.1],
                    subplots=True,
                    autopct='%.1f%%')

    plt.title('Quem trai mais ?', fontsize=20)
    plt.show()

Pesquisa Infidelidade 23042018/Pesquisa T Code.py