# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import pytest
from packaging.version import (
    Version,
)

from landoapi.phabricator import (
    PhabricatorClient,
)
from landoapi.stacks import (
    build_stack_graph,
)
from landoapi.uplift import (
    add_original_revision_line_if_needed,
    create_uplift_bug_update_payload,
    get_latest_non_commit_diff,
    get_revisions_without_bugs,
    parse_milestone_version,
    strip_depends_on_from_commit_message,
)

MILESTONE_TEST_CONTENTS_1 = """
# Holds the current milestone.
# Should be in the format of
#
#    x.x.x
#    x.x.x.x
#    x.x.x+
#
# Referenced by build/moz.configure/init.configure.
# Hopefully I'll be able to automate replacement of *all*
# hardcoded milestones in the tree from these two files.
#--------------------------------------------------------

84.0a1
"""

MILESTONE_TEST_CONTENTS_2 = """
# Holds the current milestone.
# Should be in the format of
#
#    x.x.x
#    x.x.x.x
#    x.x.x+
#
# Referenced by build/moz.configure/init.configure.
# Hopefully I'll be able to automate replacement of *all*
# hardcoded milestones in the tree from these two files.
#--------------------------------------------------------

105.0
"""

def test_parse_milestone_version():
    assert parse_milestone_version(MILESTONE_TEST_CONTENTS_1) == Version(
        "84.0a1"
    ), "Test milestone file 1 should have 84 as major milestone version."

    assert parse_milestone_version(MILESTONE_TEST_CONTENTS_2) == Version(
        "105.0"
    ), "Test milestone file 2 should have 84 as major milestone version."

    bad_milestone_contents = "blahblahblah"
    with pytest.raises(ValueError, match=bad_milestone_contents):
        parse_milestone_version(bad_milestone_contents)

DEPENDS_ON_MESSAGE = """
bug 123: testing r?sheehan

Something something Depends on D1234

Differential Revision: http://phab.test/D234

Depends on D567
""".strip()

def test_strip_depends_on_from_commit_message():
    assert strip_depends_on_from_commit_message(DEPENDS_ON_MESSAGE) == (
        "bug 123: testing r?sheehan\n"
        "\n"
        "Something something Depends on D1234\n"
        "\n"
        "Differential Revision: http://phab.test/D234\n"
    ), "`Depends on` line should be stripped from commit message."

@pytest.mark.xfail
def test_uplift_creation(
    db,
    monkeypatch,
    phabdouble,
    client,
    auth0_mock,
    mock_repo_config,
    release_management_project,
):
    def _call_conduit(client, method, **kwargs):
        if method == "differential.revision.edit":
            # Load transactions
            transactions = kwargs.get("transactions")
            assert transactions is not None
            transactions = {t["type"]: t["value"] for t in transactions}

            # Check the state of the added transactions is valid for the first uplift.
            if transactions["update"] == "PHID-DIFF-1":
                # Check the expected transactions
                expected = {
                    "update": "PHID-DIFF-1",
                    "title": "Add feature XXX",
                    "summary": (
                        "some really complex stuff\n"
                        "\n"
                        "Original Revision: http://phabricator.test/D1"
                    ),
                    "bugzilla.bug-id": "",
                    "reviewers.add": ["blocking(PHID-PROJ-0)"],
                }
                for key in expected:
                    assert (
                        transactions[key] == expected[key]
                    ), f"key does not match: {key}"

            depends_on = []
            if "parents.set" in transactions:
                depends_on.append({"phid": transactions["parents.set"][0]})

            # Create a new revision
            new_rev = phabdouble.revision(
                title=transactions["title"],
                summary=transactions["summary"],
                depends_on=depends_on,
            )
            return {
                "object": {"id": new_rev["id"], "phid": new_rev["phid"]},
                "transactions": [
                    {"phid": "PHID-XACT-DREV-fakeplaceholder"} for t in transactions
                ],
            }

        else:
            # Every other request fall back in phabdouble
            return phabdouble.call_conduit(method, **kwargs)

    # Intercept the revision creation to avoid transactions support in phabdouble
    monkeypatch.setattr(PhabricatorClient, "call_conduit", _call_conduit)

    diff = phabdouble.diff()
    revision = phabdouble.revision(
        title="Add feature XXX",
        summary=(
            "some really complex stuff\n"
            "\n"
            "Differential Revision: http://phabricator.test/D1"
        ),
        diff=diff,
    )
    repo_mc = phabdouble.repo()
    user = phabdouble.user(username="JohnDoe")
    repo_uplift = phabdouble.repo(name="mozilla-uplift")

    payload = {
        "landing_path": [
            {"revision_id": f"D{revision['id']}", "diff_id": diff["id"]},
        ],
        "repository": repo_mc["shortName"],
    }

    # No auth
    response = client.post("/uplift", json=payload)
    assert response.json["title"] == "X-Phabricator-API-Key Required"
    assert response.status_code == 401

    # API key but no auth0
    headers = {"X-Phabricator-API-Key": user["apiKey"]}
    response = client.post("/uplift", json=payload, headers=headers)
    assert response.status_code == 401
    assert response.json["title"] == "Authorization Header Required"

    # Invalid repository (not uplift)
    headers.update(auth0_mock.mock_headers)
    response = client.post("/uplift", json=payload, headers=headers)
    assert response.status_code == 400
    assert (
        response.json["title"]
        == "Repository mozilla-central is not an uplift repository."
    )

    # Only one revision at first
    assert len(phabdouble._revisions) == 1

    # Valid uplift repository
    payload["repository"] = repo_uplift["shortName"]
    response = client.post("/uplift", json=payload, headers=headers)
    assert response.status_code == 201
    assert response.json == {
        "PHID-DREV-1": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 2,
            "diff_phid": "PHID-DIFF-1",
            "revision_id": 2,
            "revision_phid": "PHID-DREV-1",
            "url": "http://phabricator.test/D2",
        },
        "tip_differential": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 2,
            "diff_phid": "PHID-DIFF-1",
            "revision_id": 2,
            "revision_phid": "PHID-DREV-1",
            "url": "http://phabricator.test/D2",
        },
    }

    # Now we have a new uplift revision on Phabricator
    assert len(phabdouble._revisions) == 2
    new_rev = phabdouble._revisions[-1]
    assert new_rev["title"] == "Add feature XXX"
    assert (
        new_rev["summary"]
        == "some really complex stuff\n\nOriginal Revision: http://phabricator.test/D1"
    )

    # Add some more revisions to test uplifting a stack.
    diff2 = phabdouble.diff()
    rev2 = phabdouble.revision(
        title="bug 1: xxx r?reviewer",
        summary=("summary info.\n\nDifferential Revision: http://phabricator.test/D3"),
        diff=diff2,
    )
    diff3 = phabdouble.diff()
    rev3 = phabdouble.revision(
        title="bug 1: yyy r?reviewer",
        summary=("summary two.\n\nDifferential Revision: http://phabricator.test/D4"),
        depends_on=[rev2],
        diff=diff3,
    )
    diff4 = phabdouble.diff()
    rev4 = phabdouble.revision(
        title="bug 1: yyy r?reviewer",
        summary=("summary two.\n\nDifferential Revision: http://phabricator.test/D4"),
        depends_on=[rev3],
        diff=diff4,
    )

    # Send an uplift request for a stack.
    payload["landing_path"] = [
        {"revision_id": f"D{rev2['id']}", "diff_id": diff2["id"]},
        {"revision_id": f"D{rev3['id']}", "diff_id": diff3["id"]},
        {"revision_id": f"D{rev4['id']}", "diff_id": diff4["id"]},
    ]
    response = client.post("/uplift", json=payload, headers=headers)
    assert response.status_code == 201, "Response should have status code 201."
    assert len(response.json) == 4, "API call should have created 3 revisions."
    assert response.json == {
        "PHID-DREV-5": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 7,
            "diff_phid": "PHID-DIFF-6",
            "revision_id": 6,
            "revision_phid": "PHID-DREV-5",
            "url": "http://phabricator.test/D6",
        },
        "PHID-DREV-6": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 9,
            "diff_phid": "PHID-DIFF-8",
            "revision_id": 7,
            "revision_phid": "PHID-DREV-6",
            "url": "http://phabricator.test/D7",
        },
        "PHID-DREV-7": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 11,
            "diff_phid": "PHID-DIFF-10",
            "revision_id": 8,
            "revision_phid": "PHID-DREV-7",
            "url": "http://phabricator.test/D8",
        },
        "tip_differential": {
            "mode": "uplift",
            "repository": "mozilla-uplift",
            "diff_id": 11,
            "diff_phid": "PHID-DIFF-10",
            "revision_id": 8,
            "revision_phid": "PHID-DREV-7",
            "url": "http://phabricator.test/D8",
        },
    }, "Response JSON does not match expected."

    # Check that parent-child relationships are preserved.
    phab = phabdouble.get_phabricator_client()
    last_phid = response.json["tip_differential"]["revision_phid"]
    _nodes, edges = build_stack_graph(phab, last_phid)

    assert edges == {
        ("PHID-DREV-7", "PHID-DREV-6"),
        ("PHID-DREV-6", "PHID-DREV-5"),
    }, "Uplift does not preserve parent/child relationships."
    # We still have the same revision
    assert len(phabdouble._revisions) == 1
    new_rev = phabdouble._revisions[0]
    assert new_rev["title"] == "my test revision title"

def test_create_uplift_bug_update_payload():
    bug = {
        "cf_status_firefox100": "---",
        "id": 123,
        "keywords": [],
        "whiteboard": "[checkin-needed-beta]",
    }
    payload = create_uplift_bug_update_payload(
        bug, "beta", 100, "cf_status_firefox{milestone}"
    )

    assert payload["ids"] == [123], "Passed bug ID should be present in the payload."
    assert (
        payload["whiteboard"] == ""
    ), "checkin-needed flag should be removed from whiteboard."
    assert (
        payload["cf_status_firefox100"] == "fixed"
    ), "Custom tracking flag should be set to `fixed`."

    bug = {
        "cf_status_firefox100": "---",
        "id": 123,
        "keywords": ["leave-open"],
        "whiteboard": "[checkin-needed-beta]",
    }
    payload = create_uplift_bug_update_payload(
        bug, "beta", 100, "cf_status_firefox{milestone}"
    )

    assert (
        "cf_status_firefox100" not in payload
    ), "Status should not have been set with `leave-open` keyword on bug."

def test_add_original_revision_line_if_needed():
    uri = "http://phabricator.test/D123"

    summary_no_original = "Bug 123: test summary r?sheehan"
    summary_with_original = (
        "Bug 123: test summary r?sheehan\n"
        "\n"
        "Original Revision: http://phabricator.test/D123"
    )

    assert (
        add_original_revision_line_if_needed(summary_no_original, uri)
        == summary_with_original
    ), "Passing summary without `Original Revision` should return with line added."

    assert (
        add_original_revision_line_if_needed(summary_with_original, uri)
        == summary_with_original
    ), "Passing summary with `Original Revision` should return the input."

def test_get_revisions_without_bugs(phabdouble):
    phab = phabdouble.get_phabricator_client()

    rev1 = phabdouble.revision(bug_id=123)
    revs = phabdouble.differential_revision_search(
        constraints={"phids": [rev1["phid"]]},
    )
    revisions = phab.expect(revs, "data")

    assert (
        get_revisions_without_bugs(phab, revisions) == set()
    ), "Empty set should be returned if all revisions have bugs."

    rev2 = phabdouble.revision()
    revs = phabdouble.differential_revision_search(
        constraints={"phids": [rev1["phid"], rev2["phid"]]},
    )
    revisions = phab.expect(revs, "data")

    assert get_revisions_without_bugs(phab, revisions) == {
        rev2["id"]
    }, "Revision without associated bug should be returned."

def test_get_latest_non_commit_diff():
    test_data = [
        {"creationMethod": "commit", "id": 3},
        {"creationMethod": "moz-phab-hg", "id": 1},
        {"creationMethod": "commit", "id": 4},
        {"creationMethod": "moz-phab-hg", "id": 2},
        {"creationMethod": "commit", "id": 5},
    ]

    diff = get_latest_non_commit_diff(test_data)

    assert (
        diff["id"] == 2
    ), "Returned diff should have the highest diff ID without `commit`."
    assert (
        diff["creationMethod"] != "commit"
    ), "Diffs with a `creationMethod` of `commit` should be skipped."


import sys
import os
import os.path as osp
import numpy as np
import pandas as pd
from tqdm import tqdm
from PIL import Image

def get_images_and_masks(classes_id, min_pixels, partition, path_annot, mode=""):
    df = pd.read_csv(osp.join(path_annot, 'validation', 'validation-annotations-object-segmentation.csv'))
    kept = df.loc[df['LabelName'].isin(classes_id)]
    mv = [osp.join('validation/masks', e) for e in kept['MaskPath'].tolist()]
    iv = [osp.join('validation', e+'.jpg') for e in kept['ImageID'].tolist()]

    df = pd.read_csv(osp.join(path_annot, 'test', 'test-annotations-object-segmentation.csv'))
    kept = df.loc[df['LabelName'].isin(classes_id)]
    mte = [osp.join('test/masks', e) for e in kept['MaskPath'].tolist()]
    ite = [osp.join('test', e+'.jpg') for e in kept['ImageID'].tolist()]

    maskpaths = mv + mte
    impaths = iv + ite

    if partition != "val" and mode != "lite":
        df = pd.read_csv(osp.join(path_annot, 'train', 'train-annotations-object-segmentation.csv'))
        kept = df.loc[df['LabelName'].isin(classes_id)]
        mtr = [osp.join('train/masks', e) for e in kept['MaskPath'].tolist()]
        itr = [osp.join('train', e+'.jpg') for e in kept['ImageID'].tolist()]

        maskpaths += mtr
        impaths += itr

    if mode == "default":
        mode_flag = ""
    else:
        mode_flag = "_" + mode

    masksfile = "{}_masks{}.txt".format(partition, mode_flag)
    imagesfile = "{}_images{}.txt".format(partition, mode_flag)

    fm = open(osp.join(path_annot, masksfile), 'w')
    fi = open(osp.join(path_annot, imagesfile), 'w')
    for i in tqdm(range(len(maskpaths))):
        m = maskpaths[i]
        im = impaths[i]
        mask = np.array(Image.open(osp.join(path_annot, m)))

        pos = np.where(mask)
        if len(pos[0]) == 0:
            continue
        xmin = np.min(pos[1])
        xmax = np.max(pos[1])
        ymin = np.min(pos[0])
        ymax = np.max(pos[0])
        w = xmax - xmin
        h = ymax - ymin
        if np.min((h, w)) < min_pixels or h*w < min_pixels*80:
            continue
        fm.write('{}\n'.format(m))
        fi.write('{}\n'.format(im))
    fm.close()
    fi.close()

if len(sys.argv) > 1:
    mode = sys.argv[1]
else:
    mode = ""

if mode == "lite":
    min_pixels = 80
else:
    min_pixels = 50

root = osp.join(os.environ['DB_ROOT'], 'OpenImages')
path_images = osp.join(root, 'images')
path_annot = osp.join(root, 'annotations')

all_classes = pd.read_csv(osp.join(root, 'metadata/class-descriptions-boxable.csv'), names=['ID', 'name'])

val_classes = ['Lion', 'Duck', 'Bus', 'Croissant', 'Panda', 'Parrot', 'Hammer', 'Airplane', 'Apple', 'Ambulance', 'Harmonica', 'Washing machine', 'Lighthouse', 'Hot dog', 'Hamburger']

val_classes_id = set(all_classes.loc[all_classes['name'].isin(val_classes)]['ID'].tolist())
train_classes_id = set(all_classes.loc[~all_classes['name'].isin(val_classes)]['ID'].tolist())

get_images_and_masks(val_classes_id, min_pixels, 'val', path_annot, mode)
get_images_and_masks(train_classes_id, min_pixels, 'train', path_annot, mode)


from tournapy.core.ruleset import RulesetEnum
from tournapy.tournament import Tournament

class TournamentManager:

    def __init__(self):
        self.tourneys_dict: dict[str, Tournament] = {}

    def is_admin(self, tournament_name: str, user_id: str) -> bool:
        if self.exists(tournament_name):
            return self.get_tournament(tournament_name).is_admin(user_id)

    def exists(self, tournament_name: str):
        return tournament_name in self.tourneys_dict.keys()

    def create_tournament(self, tournament_name: str, team_size: int, user_id: str, logo_url: str) -> bool:
        if not self.exists(tournament_name):
            tourney = Tournament()
            tourney.setup(user_id, tournament_name, team_size)
            tourney.logo_url = logo_url
            self.tourneys_dict[tournament_name] = tourney
            return True
        else:
            return False

    def delete_tournament(self, tournament_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                del self.tourneys_dict[tournament_name]
                return True, f'Tournament {tournament_name} deleted.'
            else:
                return False, f'Cannot delete tournament {tournament_name}. Missing admin rights.'
        else:
            return False, f'No tournament {tournament_name} existing.'

    def add_phase(self, tournament_name: str, phase_name: str, rules_name: str, pool_size: int, bo: int,
                  user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                ruleset = RulesetEnum(rules_name).get_ruleset(
                    phase_name, pool_size, bo)
                t: Tournament = self.tourneys_dict[tournament_name]
                t.add_phase(len(t.stages_dict), ruleset)
                print(f'team.stages_dict={t.stages_dict}')
                return True, f'{phase_name} phase added to {tournament_name} tournament.'
            else:
                return False, f'{phase_name} phase cannot be added to {tournament_name}. Missing admin rights.'
        else:
            return False, f'{tournament_name} does not exist.'

    def start_next_phase(self, tournament_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                t: Tournament = self.tourneys_dict[tournament_name]
                # Retrieve list of teams eligible for next phase:
                # 1st case: no phase performed yet, every team should be added
                if t.current_phase_idx == 0:
                    teams_names = t.df_teams()['name']
                # 2nd case: get list of teams sorted by their rankings.
                else:
                    previous_phase = t.get_phase(t.current_phase_idx - 1)
                    # TODO implement & use tournament standings
                    teams_sorted_ga = sorted(t.teams_dict.values(),
                                             key=lambda team: (team.goals_scored - team.goals_taken),
                                             reverse=True)
                    teams_sorted = sorted(teams_sorted_ga, key=lambda team: team.points, reverse=True)
                    teams_names = list(map(lambda team: team.name, teams_sorted))
                next_phase = t.get_current_phase()
                # adding previously retrieved list of teams. Will be added following team rank from previous phase.
                # will not accept team if next phase pool is complete.
                for team_name in teams_names:
                    # next_phase.add_team(team.get_team(team_name))
                    next_phase.add_team(t.teams_dict[team_name])
                if not next_phase.running:
                    next_phase.init_bracket()
                    next_phase.start()
                    # TODO find a way to increment stage automatically at end of previous stage
                    # t.current_phase_idx += 1
                    return True, f'{next_phase.name} phase started.'
                else:
                    return False, f'{next_phase.name} already running.'
            else:
                return False, f'Cannot start next phase of {tournament_name}. Missing admin rights.'
        else:
            return False, f'{tournament_name} does not exist.'

    def add_player(self, tournament_name: str, player_name: str, player_elo: int, user_id: str) -> (bool, str):
        # user_id = user.id of admin or user.name of player
        if self.exists(tournament_name):
            # players can self register. Admins can register anyone
            if self.is_admin(tournament_name, user_id) or player_name == user_id:
                t: Tournament = self.tourneys_dict[tournament_name]
                if t.add_player(player_name, player_elo):
                    return True, f'{player_name} player registered to {tournament_name}'
                else:
                    return False, f'{player_name} already registered to {tournament_name}'
            else:
                return False, f'{player_name} player cannot be registered to {tournament_name}. Missing admin rights'
        else:
            return (
                False, f'{player_name} player cannot be registered to {tournament_name}. Tournament does not exists')

    def remove_player(self, tournament_name: str, player_name: str, user_id: str) -> (bool, str):
        # user_id = user.id of admin or user.name of player
        if self.exists(tournament_name):
            # players can self unregister. Admins can unregister anyone
            if self.is_admin(tournament_name, user_id) or player_name == user_id:
                t: Tournament = self.tourneys_dict[tournament_name]
                t.remove_player(player_name)
                return True, f'{player_name} player unregistered from {tournament_name}'
            else:
                return (
                    False, f'{player_name} player cannot be unregistered from {tournament_name}. Missing admin rights')
        else:
            return (
                False,
                f'{player_name} player cannot be unregistered from {tournament_name}. Tournament does not exists')

    def add_team(self, tournament_name: str, team_name: str, players_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id) or user_id in players_name:
                t: Tournament = self.tourneys_dict[tournament_name]
                success, feedback = t.add_to_team(team_name, players_name)
                return success, feedback
            else:
                return False, f'Cannot form a team. Missing admin rights'
        else:
            return False, f'Tournament {tournament_name} does not exists'

    def remove_team(self, tournament_name: str, team_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                t: Tournament = self.tourneys_dict[tournament_name]
                success, feedback = t.remove_team(team_name)
                return success, feedback
            else:
                return False, f'Cannot remove a team. Missing admin rights'
        else:
            return False, f'Tournament {tournament_name} does not exists'

    def clean_teams(self, tournament_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                t: Tournament = self.tourneys_dict[tournament_name]
                success, feedback = t.clear_teams()
                return success, feedback
            else:
                return False, f'Cannot clear teams. Missing admin rights'
        else:
            return False, f'Tournament {tournament_name} does not exists'

    def generate_teams(self, tournament_name: str, user_id: str) -> (bool, str):
        if self.exists(tournament_name):
            if self.is_admin(tournament_name, user_id):
                t: Tournament = self.tourneys_dict[tournament_name]
                success, feedback = t.generate_teams()
                return success, feedback
            else:
                return False, f'Cannot generate teams. Missing admin rights'
        else:
            return False, f'Tournament {tournament_name} does not exists'

    def get_tournaments_list(self) -> []:
        return self.tourneys_dict.keys()

    def get_tournament(self, tournament_name: str) -> Tournament:
        return self.tourneys_dict[tournament_name]


import re

from scrapy import Request
from scrapy.spiders import Spider

from ycombinator.items import JobItem
from ycombinator.mysql.job_helper import JobHelper

class JobsSpider(Spider):
    name = 'ycombinator'
    NAME_REGEX = r'(.*\))|(.*)\sis|(.*)\s\W|(.*?\s.*?\s)'
    LOCATION_REGEX = r'\sin\s(.*)|\sat\s(.*)'
    POSITION_REGEX = r'ing\s(.*)\sin|ing\s(.*)'

    start_urls = [
        'https://news.ycombinator.com/jobs',
    ]

    job_helper = JobHelper()
    job_ids = job_helper.get_ids()
    print(job_ids)

    def parse(self, response):
        new_jobs = []
        job_ids = response.css('.athing::attr(id)').getall()
        raw_jobs = response.css('.storylink::text').getall()

        for job_id, raw_job in zip(job_ids, raw_jobs):
            if (job_id,) in self.job_ids:
                continue
            job = JobItem()
            job['job_id'] = job_id
            job['name'] = self.get_company_name(raw_job)
            job['location'] = self.get_location(raw_job)
            job['position'] = self.get_position(raw_job)

            new_jobs.append(job)
            yield job

        self.job_helper.insert_db(new_jobs)

        urls_s = response.css('.morelink')
        yield from [response.follow(url_s, callback=self.parse) for url_s in urls_s]

        # urls = response.css('.morelink::attr(href)').getall()
        # for url in urls:
            # yield Request('/'+url, callback=self.parse)

    def get_company_name(self, text):
        return self.clean(re.search(self.NAME_REGEX, text, flags=re.I).groups(''))

    def get_location(self, text):
        return self.clean(re.findall(self.LOCATION_REGEX, text, flags=re.I))

    def get_position(self, text):
        return self.clean(re.findall(self.POSITION_REGEX, text, flags=re.I))

    def clean(self, data):
        if isinstance(data, tuple):
            return [e.strip() for e in data if e and e.strip()][0]

        if isinstance(data, list):
            data = [e.strip() for seq in data for e in seq if e and e.strip()]
            return data[0] if data else ''

        if isinstance(data, str):
            return data.strip()


from ._utils import get_jwt_token_data
from .._models import Authentication
from .._exceptions import NotAuthenticatedError
from ...enums import AuthTypeEnum

async def _get_db_subject(*args, type_: AuthTypeEnum) -> Authentication:
	subject = get_jwt_token_data(*args)
	async with Authentication(subject, type_) as subject_db:
		if not subject_db.exists:
			raise NotAuthenticatedError()
		return subject_db

# здесь некоторое повторение кода, ибо может потребоваться добавление какой-то особенной обработки
# при авторизации в том или ином приложении

async def site(*args, type_):
	return await _get_db_subject(*args, type_=type_)

async def tgAdmin(*args, type_):
	return await _get_db_subject(*args, type_=type_)

async def CRM(*args, type_):
	return await _get_db_subject(*args, type_=type_)


#!/usr/bin/python3
'''make file with batch commands to run dap-g scan for 1Mb SNPs within each gene'''
import os
import argparse
import sys
def check_arg(args=None):
    parser = argparse.ArgumentParser(description='Run dap-g scan for 1Mb SNPs within each gene')
    parser.add_argument('-b', '--pop',
                        help='group/pop id for group_id column of .dat file',
                        required='True'
                        )
    return parser.parse_args(args)
args = check_arg(sys.argv[1:])
pop = args.pop
datlist = os.listdir(path=pop+'_all1Mb_sbams')

outfile = open(pop+"_all1Mb_batch_scan.cmd","w")
for f in datlist:
    ens = f[:-4]
    outfile.write("/usr/local/bin/dap-g -d "+pop+"_all1Mb_sbams/" + f + " --scan > "+pop+"_all1Mb_scan_out/" + ens + ".bf\n")
outfile.close()


import math
t = int(input())

from collections import defaultdict
def subarrayXor(arr, n, m):
    HashTable=defaultdict(bool)
    HashTable[0]=1
    count=0
    curSum=0
    for i in arr:
        curSum^=i
        if HashTable[curSum^m]:
            count+=HashTable[curSum^m]
        HashTable[curSum]+=1
    return(count)

for x in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	count2=0
	for x in range(int(math.sqrt(2*n))):
		print(n,subarrayXor(a,n,x**2),x)
		count2+=subarrayXor(a,n,x**2)

	print((n*(n+1))//2 - count2)

	# count=0
	# for j in range(n-1,-1,-1):
	# 	xor=0
	# 	k=j
	# 	while k>=0:
	# 		xor = xor^a[k]
	# 		if math.sqrt(xor)!=int(math.sqrt(xor)):
	# 			count+=1
	# 		k-=1
	# print(count)


from rest_framework import serializers, fields
from ..models.insurance import Insurance, TYPE_INSURANCE
from datetime import date
from rest_framework import fields
from ..models.car import Car

class InsuranceSerializer(serializers.ModelSerializer):
    type_of_insurance = fields.MultipleChoiceField(choices=TYPE_INSURANCE)
    end_date = fields.DateField(input_formats=['%Y-%m-%d', 'iso-8601',])

    class Meta:
        model = Insurance

        fields = ['insurance_status', 'insurance_company', 'end_date', 'type_of_insurance',
                  'worth_of_insurance', 'insurance_number', 'car']
        read_only_fields = ['end_date']

    def validate_type_of_insurance(self, value):

        """
        Validation of type of insurance.
        Checks if the car has compulsory OC insurance.
        """

        if 'OC' not in value:
            raise serializers.ValidationError("The car must have OC")
        return value

    def validate(self, data):

        """
        Validation of car data and insurance data relation.
        Checks that if the car is leased, the car has a full insurance too.
        """

        car_id = data['car'].id
        leasing = 'L'
        insurance = data['type_of_insurance']
        car_qs = Car.objects.filter(id=car_id)
        if not car_qs.exists():
            raise serializers.ValidationError('This car not exist...!')

        car = car_qs[0]
        car.status = False
        if car.way_of_purchase == leasing and insurance != {'AC', 'OC', 'NWW'}:
            raise serializers.ValidationError("If you bought a car in leasing, you must have full insurance.")

        if data['end_date'] < date.today() and data['insurance_status'] == True:
            raise serializers.ValidationError("Are you sure this car has insurance?")

        return data


from plone.app.content.interfaces import INameFromTitle
from plone.app.dexterity import _ as _PMF
from plone.app.textfield import RichText
from plone.autoform.directives import read_permission
from plone.dexterity.content import Item
from plone.namedfile.field import NamedBlobImage
from plone.rfc822.interfaces import IPrimaryField
from plone.supermodel.directives import fieldset
from plone.supermodel.model import Schema
from ploneorg import _
from ploneorg.vocabularies import org_size_vocabulary
from ploneorg.vocabularies import payment_currency_vocabulary
from ploneorg.vocabularies import payment_frequency_vocabulary
from ploneorg.vocabularies import payment_method_vocabulary
from zope import schema
from zope.interface import alsoProvides
from zope.interface import implementer
from zope.interface import Invalid

import re

# TODO: add workflow state Waiting for Initial Payment
# TODO: display effective and expiry date in view template
# TODO: display status in view template
# TODO: use logo as lead image
# TODO: add button to send an email to the primary contact
# TODO: set effective and expiry dates from payment start and end dates
# TODO: make payment date not required (e.g. waiting for payment)
# TODO: enable commenting/discussion instead of commenting with change comments?

# email re w/o leading '^'
EMAIL_RE = "([0-9a-zA-Z_&.'+-]+!)*[0-9a-zA-Z_&.'+-]+@(([0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-z-A-Z])?\.)+[a-zA-Z]{2,}|([0-9]{1,3}\.){3}[0-9]{1,3})$"  # noQA

def isEmail(value):
    prog = re.compile("^" + EMAIL_RE)
    result = prog.match(value)
    if result is None:
        raise Invalid(_PMF("is not a valid email address."))
    return True

def isHTTP(value):
    if not value.startswith("http://") and not value.startswith("https://"):
        raise Invalid(_PMF("web address must start with http:// or https://"))
    return True

class IFoundationSponsor(Schema):
    """A Foundation sponsor"""

    fieldset(
        "Contacts",
        label="Contacts",
        fields=[
            "fname",
            "lname",
            "email",
            "alt_fname",
            "alt_lname",
            "alt_email",
        ],
    )

    fieldset(
        "Address",
        label="Address",
        fields=[
            "address",
            "address2",
            "city",
            "state",
            "postalCode",
            "country",
        ],
    )

    fieldset(
        "Payment",
        label="Payment",
        fields=[
            "payment_frequency",
            "payment_method",
            "payment_amount",
            "payment_currency",
            "payment_date",
        ],
    )

    fieldset(
        "Status",
        label="Status",
        fields=[
            "start_date",
            "end_date",
            "last_verified_date",
            "notes",
        ],
    )

    title = schema.TextLine(
        title=_PMF("Organization name", default="Organization name"), required=True
    )

    logo = NamedBlobImage(
        title=_("Logo"),
        required=False,
    )

    sponsorship_type = schema.Choice(
        title=_PMF("Sponsor Type", default="Sponsor Type"),
        vocabulary="ploneorg.vocabulary.sponsorship_types",
        required=True,
    )

    read_permission(orgsize="ploneorg.foundationsponsor.view")
    orgsize = schema.Choice(
        title=_("Organization size"),
        description=_("Number of people in your organization. It's fine to estimate."),
        vocabulary=org_size_vocabulary,
        required=True,
    )

    is_provider = schema.Bool(
        title=_PMF("Is a Plone provider", default="Is a Plone provider"),
        required=False,
        default=False,
    )

    website = schema.URI(
        title=_PMF("Web Site", default="Web Site"),
        description=_("Enter a http:// or https:// web address"),
        required=False,
        constraint=isHTTP,
    )

    provider_listing_url = schema.URI(
        title=_PMF("Plone.com provider listing", default="Plone.com provider listing"),
        description=_("Enter a http:// or https:// web address"),
        required=False,
        constraint=isHTTP,
    )

    read_permission(fname="ploneorg.foundationsponsor.view")
    fname = schema.TextLine(
        title=_PMF("Contact first name", default="Contact first name"), required=True
    )

    read_permission(lname="ploneorg.foundationsponsor.view")
    lname = schema.TextLine(
        title=_PMF("Contact last name", default="Contact last name"), required=True
    )

    read_permission(email="ploneorg.foundationsponsor.view")
    email = schema.TextLine(
        title=_PMF("Email", default="Email"), constraint=isEmail, required=True
    )

    read_permission(address="ploneorg.foundationsponsor.view")
    address = schema.TextLine(title=_PMF("Address", default="Address"), required=False)

    read_permission(address2="ploneorg.foundationsponsor.view")
    address2 = schema.TextLine(
        title=_PMF("Address 2", default="Address 2"), required=False
    )

    city = schema.TextLine(title=_PMF("City", default="City"), required=True)

    read_permission(state="ploneorg.foundationsponsor.view")
    state = schema.TextLine(title=_PMF("State", default="State"), required=False)

    read_permission(postalCode="ploneorg.foundationsponsor.view")
    postalCode = schema.TextLine(
        title=_PMF("Postal code", default="Postal code"), required=False
    )

    country = schema.Choice(
        title=_PMF("Country", default="Country"),
        vocabulary="ploneorg.vocabulary.countries",
        required=True,
    )

    read_permission(alt_fname="ploneorg.foundationsponsor.view")
    alt_fname = schema.TextLine(
        title=_PMF(
            "Alternate contact first name", default="Alternate contact first name"
        ),
        required=False,
    )

    read_permission(alt_lname="ploneorg.foundationsponsor.view")
    alt_lname = schema.TextLine(
        title=_PMF(
            "Alternate contact last name", default="Alternate contact last name"
        ),
        required=False,
    )

    read_permission(alt_email="ploneorg.foundationsponsor.view")
    alt_email = schema.TextLine(
        title=_PMF("Alternate email", default="Alternate email"),
        constraint=isEmail,
        required=False,
    )

    twitter = schema.TextLine(
        title=_PMF("Twitter account", default="Twitter account"),
        description=_PMF(
            "(without the leading " "@" ")", default="(without the leading " "@" ")"
        ),
        required=False,
    )

    read_permission(connection_to_plone="ploneorg.foundationsponsor.view")
    connection_to_plone = RichText(
        title=_PMF("Connection to Plone", default="Connection to Plone"),
        description=_(
            "What is your connection to Plone? How is Plone used by your organization?"
        ),
        required=False,
    )

    read_permission(payment_frequency="ploneorg.foundationsponsor.view")
    payment_frequency = schema.Choice(
        title=_PMF("Payment frequency", default="Payment frequency"),
        vocabulary=payment_frequency_vocabulary,
    )

    read_permission(payment_method="ploneorg.foundationsponsor.view")
    payment_method = schema.Choice(
        title=_PMF("Payment method", default="Payment method"),
        vocabulary=payment_method_vocabulary,
        default="PayPal",
        required=True,
    )

    read_permission(payment_amount="ploneorg.foundationsponsor.view")
    payment_amount = schema.Float(
        title=_PMF("Payment Amount", default="Payment Amount"), min=0.0, required=True
    )

    read_permission(payment_currency="ploneorg.foundationsponsor.view")
    payment_currency = schema.Choice(
        title=_PMF("Currency", default="Currency"),
        vocabulary=payment_currency_vocabulary,
        default="USD",
        required=True,
    )

    start_date = schema.Date(
        title=_PMF("Start Date", default="Start Date"), required=False
    )

    end_date = schema.Date(title=_PMF("End Date", default="End Date"), required=False)

    read_permission(payment_date="ploneorg.foundationsponsor.view")
    payment_date = schema.Date(
        title=_PMF("Payment Date", default="Payment Date"), required=False
    )

    read_permission(last_verified_date="ploneorg.foundationsponsor.view")
    last_verified_date = schema.Date(
        title=_PMF("Status last verified date", default="Status last verified date"),
        required=False,
    )

    read_permission(notes="ploneorg.foundationsponsor.view")
    notes = RichText(
        title=_PMF("Private notes", default="Private notes"), required=False
    )

    public_notes = RichText(
        title=_PMF("Public notes", default="Public notes"), required=False
    )

alsoProvides(IFoundationSponsor["logo"], IPrimaryField)

@implementer(IFoundationSponsor)
class FoundationSponsor(Item):
    def get_full_name(self):
        names = [
            self.title,
            self.fname,
            self.lname,
        ]
        return " ".join([name for name in names if name])

    @property
    def preview_image(self):
        return self.logo

    @preview_image.setter
    def preview_image(self, value):
        self.logo = value

    @property
    def preview_caption(self):
        return self.title

    @preview_caption.setter
    def preview_caption(self, value):
        pass

    @property
    def remoteUrl(self):
        return self.website

    @remoteUrl.setter
    def remoteUrl(self, value):
        self.website = value

    def toXML(self, schematas=["contact", "survey"]):
        """To XML for Paul ;)"""

        out = ""
        out += '<foundationsponsor id="%s">' % self.getId()
        fields = [
            f
            for f in self.Schema().fields()
            if (f.schemata in schematas) and f.getName() != "id"
        ]
        for f in fields:
            out += "<%s>%s</%s>" % (
                f.getName(),
                getattr(self, f.accessor)(),
                f.getName(),
            )
        out += "</foundationsponsor>"
        return out

class INameFromPersonNames(INameFromTitle):
    def title():
        """Return a processed title"""

@implementer(INameFromPersonNames)
class NameFromPersonNames(object):
    def __init__(self, context):
        self.context = context

    @property
    def title(self):
        return self.context.title


import json, string

with open("words.txt") as file:
	words = json.loads(file.read())

triangles = {0.5*i*(i+1) for i in range(1, 1000)}

def sumletter(word):
	return sum(string.ascii_uppercase.index(char)+1 for char in word)

count = 0
for word in words:
	if sumletter(word) in triangles:
		count += 1

print(count)


import os

from telegram.ext import (
    Application,
    CommandHandler,
    ChatMemberHandler,
    MessageHandler,
    filters,
)

import commands
import handlers
import runtime_index as rn_index
import bot_general as bg

import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.DEBUG)
logger = logging.getLogger(__name__)

def main():

    BOT_TOKEN = os.environ.get('BOT_TOKEN')
    v_path = os.environ.get('VOLUME_PATH')

    if BOT_TOKEN == None:
        logger.error("Can't read bot token env")
        return

    if v_path != None:
        handlers.update_VOLUME_PATH(v_path)

    bg.create_general_db()
    rn_index.init_runtime_chat_indexes()

    app = Application.builder().token(BOT_TOKEN).build()
    app.add_handler(CommandHandler("tits", commands.tits))
    app.add_handler(MessageHandler(filters.PHOTO, handlers.receive_tits_or_cats_v2))
    app.add_handler(ChatMemberHandler(handlers.track_chats, ChatMemberHandler.MY_CHAT_MEMBER))

    app.run_polling()

if __name__ == "__main__":
    main()


import random

from django.shortcuts import render, get_object_or_404

from basketapp.models import Basket
from .models import Product, Category

def index(request):
    products = Product.objects.all()
    basket = get_basket(request.user)
    context = {
        'head_title': 'главная',
        'products': products,
        'basket': basket,
    }
    return render(request, 'mainapp/index.html', context)

def catalog(request, pk=None):
    categories_link = Category.objects.all()
    basket = get_basket(request.user)
    hot_product = get_hot_product()
    same_products = get_same_products(hot_product)
    if request.user.is_authenticated:
        basket = Basket.objects.filter(user=request.user)
    if pk is not None:
        if pk == 0:
            products = Product.objects.all().order_by('price')
            category = {'name': 'Все'}
        else:
            category = get_object_or_404(Category, pk=pk)
            products = Product.objects.filter(category_name__pk=pk).order_by('price')
        context = {
            'head_title': 'каталог',
            'products': products,
            'categories_link': categories_link,
            'category': category,
            'basket': basket,
            'hot_product': hot_product,
            'same_products': same_products,

        }
        return render(request, 'mainapp/catalog.html', context)
    context = {
        'head_title': 'каталог',
        'same_products': same_products,
        'categories_link': categories_link,
        'hot_product':  hot_product,
        'basket': basket,

    }
    return render(request, 'mainapp/catalog.html', context)

def contact(request):
    basket = get_basket(request.user)
    context = {
        'head_title': 'контакты',
        'basket': basket,
    }
    return render(request, 'mainapp/contacts.html', context)

def card(request, pk=None):
    product = get_object_or_404(Product, pk=pk)
    basket = get_basket(request.user)
    context = {
        'head_title': 'продукт',
        'product': product,
        'basket': basket,
    }
    return render(request, 'mainapp/card_product.html', context)
# Create your views here.

def get_basket(user):
    if user.is_authenticated:
        return Basket.objects.filter(user=user)
    else:
        return []

def get_hot_product():
    products = Product.objects.all()

    return random.sample(list(products), 1)[0]

def get_same_products(hot_product):
    same_products = Product.objects.filter(category_name=hot_product.category_name).exclude(pk=hot_product.pk)[:3]

    return same_products


import functools
import operator
import sys

chars = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?:;'

alphanum = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

def get_page_num(levels):
    x = 0
    for l in levels:
        if l[2] > l[1]: raise Exception('value out of range')
        x *= l[1]
        x += l[2]
    return x

def get_page_num_rev(x, levels):

    for l in reversed(levels):
        n = l[1]

        y = x % n
        x -= y
        x //= n

        l[2] = y

def get_page_rev(string):
    x = 0
    for s in reversed(string):
        x *= len(chars)
        x += chars.index(s)
    return x

def get_page(x, n):
    s = ''
    for i in range(n):
        n = len(chars)

        y = x % n
        x -= y
        x //= n

        s += chars[y]
    return s

def int_to_alphanum(x):
    s = ''
    while True:
        m = len(alphanum) + 1

        y = x % m
        x -= y
        x //= m

        s += alphanum[y-1]

        if x == 0: break
    return s

def alphanum_to_int(string):
    x = 0
    for s in reversed(string):
        y = alphanum.index(s) + 1
        x *= len(alphanum) + 1
        x += y
    return x

def block_print(s, w, h):
    s = get_page(x, 64)
    print('+'+'-'*w+'+')
    for i in range(h):
        print('|'+s[i*h:i*h+w]+'|')
    print('+'+'-'*w+'+')

######################################################33

#x = alphanum_to_int('abcd')
#s = int_to_alphanum(x)
#print(x,s)
#sys.exit(0)

levels = [
    ['room',     36**100,   0],
    ['bookcase',       4,   0],
    ['shelf',          5,   0],
    ['volume',        32,   0],
    ['page',         410,   0]
    ]

get_page_num_rev(get_page_rev(' hello'), levels)

n = functools.reduce(operator.mul, [l[1] for l in levels], 1)

x = get_page_num(levels)

w = 8
h = 8

s = get_page(x, w*h)

block_print(s, w, h)


import os

from django.contrib import admin
from django.urls import reverse
from django.utils import timezone
from django.utils.dateparse import parse_datetime
from django.utils.html import format_html
from .models import Order, Coupon
from order.task import send_order_status_to_email

@admin.register(Coupon)
class CouponModelAdmin(admin.ModelAdmin):
    list_display = ('code', 'discount', 'expired')
    list_filter = ('expired',)
    list_editable = ('discount', 'expired')

def approve_order(modeladmin, request, queryset):
    queryset.update(approved=True)
    drone = queryset[0].Drone
    drone.available = False
    drone.save()
    approve_order.short_description = 'Approve order'

def finish_order(modeladmin, request, queryset):
    order_end_date = parse_datetime(queryset[0].end_date)
    is_paid = queryset[0].paid
    now = timezone.now()
    if is_paid and now >= order_end_date:
        queryset.update(finished=True)
        drone = queryset[0].Drone
        drone.available = True
        drone.save()
        pk = queryset[0].pk
        send_order_status_to_email.delay(pk, status='finished')
    else:
        if os.name == 'posix':
            os.system('notify-send "Error Notification!" "You can\'t finish this Order yet."')
        else:
            from win10toast import ToastNotifier
            toaster = ToastNotifier()
            toaster.show_toast(title, message, duration=3)
    finish_order.short_description = 'Finish order'

@admin.register(Order)
class OrderModelAdmin(admin.ModelAdmin):
    def send_pdf_order_detail_to_email(self, order):
        return format_html('Send Email',
                           reverse('order:send_pdf_to_email', kwargs={'pk': order.pk}))

    send_pdf_order_detail_to_email.short_description = 'Send PDF Order detail to email'

    list_display = ('start_date', 'end_date', 'user', 'drone', 'approved', 'canceled',
                    'finished', 'paid', 'rate', 'send_pdf_order_detail_to_email')
    search_fields = ('user__username', 'drone__model__name', 'drone__name')
    list_filter = ('approved', 'canceled', 'finished', 'rate')
    list_editable = ('approved', 'canceled', 'finished', 'paid')
    actions = (approve_order, finish_order)


def zhuan(AB, D):
    result = ''
    while True:
        shang = AB // D
        yu = AB % D
        result = result + str(yu)
        if shang == 0:
            break
        else:
            AB = shang
    return int(result[::-1])

A, B, D = [int(x) for x in input().split()[:3]]
AB = A + B
print(zhuan(AB, D))


#!/usr/bin/env python3

import sys
import numpy as np
import cv2
# import dlib
import face_recognition
import time
import os

cvXMLPath = '/usr/share/opencv4/haarcascades/'

# Load the cascade # set the correct path
face_cascade = cv2.CascadeClassifier(cvXMLPath + 'haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

def prepare_data():
    known_face_encodings_map = {}
    files = os.listdir('images')
    for f in files:
        image  = face_recognition.load_image_file("images/{}".format(f))
        image_encoding = face_recognition.face_encodings(image)[0]
        f = f[ : f.find('.')]
        f = f.replace("_", " ")
        known_face_encodings_map.update({f: image_encoding})
    return known_face_encodings_map

def main():

    known_face_encodings_map = prepare_data()

    while(True):
        # Here we store data about recognized faces
        recognized_faces = {}

        # Capture frame-by-frame
        ret, frame = cap.read()

        # get it into the correct format
        rgb_frame = frame[:, :, ::-1]

        # Detect the faces
        min_face_distances = 1
        min_face_distances_name = ""
        faces = face_cascade.detectMultiScale(rgb_frame)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
            # print ("Rectangle coords: {} {} {} {}".format(x, y, x+w, y+h))

            cropped = frame[y: y+h, x: x+w]
            rgb_cropped = cropped[:, :, ::-1]
            face_image = rgb_cropped

            # cv2.imshow("face image", face_image)
            face_encoding_list = face_recognition.face_encodings(face_image)

            if len(face_encoding_list) < 1:
                # print ("No face recognized")
                break

            # continue
            face_encoding = face_encoding_list[0]
            # print ("len (face_encoding) = {}".format(len (face_encoding)))

            min_face_distances = 1
            for name, face in known_face_encodings_map.items():
                face_distances = face_recognition.face_distance([face], face_encoding)
                if min_face_distances > face_distances:
                    min_face_distances = face_distances
                    min_face_distances_name = name

            recognized_faces[min_face_distances_name] = {'distance':min_face_distances, 'coords':(x, y, w, h)}
            # print(recognized_faces)

        # Display
        font = cv2.FONT_HERSHEY_SIMPLEX #cv2.FONT_HERSHEY_DUPLEX
        for name, data in recognized_faces.items():
            distance = data['distance']
            # print ("name '{}' distance '{}'".format(name, distance))
            if name != "" and distance < 1:
                (x, y, w, h) = data['coords']
                cv2.putText(frame, "{} - {}".format(name, distance), (x + 6, y - 6), font, 0.5, (255, 255, 255), 1)
        cv2.imshow('face detector', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        time.sleep(0.1)

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


from typing import List, Tuple

class Curso:

    def __init__(self, curso: str = "Curso Padrão", carga_horaria: int = 45) -> None:
        self.__curso = curso
        self.__carga_horaria = carga_horaria

# Polimorfismo
curso1: Curso = Curso()
curso2: Curso = Curso("Padrões de Projeto em Python")
curso3: Curso = Curso("Python para Data Science", 40)

# print(curso1.__dict__)
# print(curso2.__dict__)
# print(curso3.__dict__)

nome: str = "Geek University"
tupla: Tuple = (1, 3, 5, 7, 9)
lista: List = [2, 4, 6, 8, 10]

# print(nome[:4], tupla[:4], lista[:4])

class Pessoa:

    def __init__(self: object, nome: str) -> None:
        self.__nome = nome

    def andar(self: object) -> str:
        print(f"{self.__nome} está andando")

class Aluno(Pessoa):

    def __init__(self: object, nome: str, matricula: str) -> None:
        super().__init__(nome)
        self.__matricula = matricula

felicity = Aluno('Felicity Jones', '1234')
# felicity.andar()

# Abstração
def gerar_fibonacci(qtd: int) -> None:
    if qtd <= 0:
        print("Quantidade deve ser maior que 0")
    else:
        print(f'A sequência de Fibonacci para {qtd} termo(s) é: ')
        contador: int = 0
        aux1: int = 0
        aux2: int = 1
        while contador < qtd:
            print(aux1, end=', ')
            proximo = aux1 + aux2
            aux1 = aux2
            aux2 = proximo
            contador += 1

# gerar_fibonacci(10)

# Composição
class Motor:

    def ligar(self) -> None:
        print("Motor está ligado")

class Pneu:

    def encher(self: object) -> None:
        print("Pneu está cheio")

class Carro:

    def __init__(self: object) -> None:
        self.__motor = Motor()
        self.__pneu = Pneu()


# Author: Ronaldlee Ejalu
# Course: DSC 450
# HomeWork Assignment 5 Module part 2
import os
import csv
import urllib.request
import sqlite3

os.chdir("C:/Users/rejalu1/OneDrive - Henry Ford Health System/DSC450/Assignments/Week5")

createtbl = """

CREATE TABLE Chauffeurs
(
    LicenseNumber NUMBER,
    Renewed VARCHAR2(50),
    Status VARCHAR2(50),
    StatusDate DATE,
    DriverType VARCHAR2(25),
    LicenseType VARCHAR2(25),
    OriginalIssue DATE,
    CName VARCHAR2(100),
    Sex VARCHAR2(10),
    ChauffeurCity VARCHAR2(50),
    ChauffeurState VARCHAR2(50),
    RecordNumber VARCHAR(50)--,
    --CONSTRAINT Chauffeurs_PK PRIMARY KEY (LicenseNumber)
);
"""

insertScript = """
INSERT  INTO Chauffeurs VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')
"""
conn = sqlite3.connect('dsc450.db')                                                                             # open the connection
cur = conn.cursor()                                                                                # instanciate a cursor object

# Drop the table if it exist
cur.execute("DROP TABLE IF EXISTS Chauffeurs;")

# execute the ddl to cretate the respective tables.
cur.execute(createtbl)

url_link = "http://dbgroup.cdm.depaul.edu/DSC450/Public_Chauffeurs_Short_hw3.csv"
response = urllib.request.urlopen(url_link)

# decode urlopen's response, which is in bytes, into a valid local encoding
responseLines = [row.decode('utf-8') for row in response.readlines()]
csvRow = csv.reader(responseLines)                                          # use the reader method to return a reader object which we iterate over lines in the given csv file

# extracting the field names
fields = next(csvRow)

for row in csvRow:
    # if row[7] == 'CORONA,  JUAN M' or row[7] == 'HUSSAIN, TARIQ':                                                  # This is for debugging purposes
    if len(row[7].split(', ')) == 2:
        # print("The length of %s is %s" %(row[7].split(', '), len(row[7].split(', '))))                             # This is for debugging purposes
        cleansedRow = row[7].split(', ')
        # print(cleansedRow)                                                                                # This is for debugging purposes
        cName = cleansedRow[1] + ' '+ cleansedRow[0]
        # print(cName)
        cur.execute(insertScript %(int(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], cName, row[8], row[9], row[10], row[11]))

    # elif len(row[7].split(', ')) == 1 and row[7] == "PATRICK J. O'BRIEN":                                                                  # This is for debugging purposes
    #     cur.execute(insertScript %(int(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], row[7].replace("'",""), row[8], row[9], row[10], row[11]))
    else:
        cur.execute(insertScript %(int(row[0]), row[1], row[2], row[3], row[4], row[5], row[6], row[7].replace("'","''"), row[8], row[9], row[10], row[11]))

outPutRes = cur.execute("SELECT COUNT(*) FROM Chauffeurs;")
outPutResIObj = outPutRes.fetchall()

print("SELECT COUNT(*) FROM Chauffeurs is \n %s" %(outPutResIObj))

conn.commit()
conn.close()


import media

import fresh_tomatoes

seven_samurai = media.Movie(
    "Seven Samurai", "Seven Samurai help a village against bandit",
    "http://2.bp.blogspot.com/-tbI9Z7tqdmw/T0PUbMFBROI/AAAAAAAABIY/Ip28mqmZaUQ/s1600/Shichinin-no-samurai+1.jpg",  # noqa
    "https://www.youtube.com/watch?v=wJ1TOratCTo&disable_polymer=true",
    "100")

sanjuro = media.Movie("Sanjuro", "A lonely samurai helps a family", "https://s3.amazonaws.com/criterion-production/release_boxshots/1792-c9b9b35636a0769bf266c5376a1c5692/Sanjuro_wrap-1_original.jpg",  # noqa
    "https://www.youtube.com/watch?v=ZHIRcbAMFHo", "100")

high_low = media.Movie(
    "High and Low",
    "Toshiro Mifune stars as a wealthy industrialist whose family becomes\
     the target of a ruthless kidnapper",
    "https://upload.wikimedia.org/wikipedia/en/6/6a/HIGH_AND_LOW_JP_.jpg",
    "https://www.youtube.com/watch?v=LV3z2Ytxu90", "94")

casablanca = media.Movie("Casablanca", "Rick Blaine who owns a nightclub in \
    Casablanca, discovers his old flame Ilsa is in town with her husband, \
    Victor Laszlo. Laszlo is a famed rebel, and with Germans on his tail,\
    Ilsa knows Rick can help them get out of the country.",
    "https://m.media-amazon.com/images/M/MV5BY2IzZGY2YmEtYzljNS00NTM5LTgwMzUtMzM1NjQ4NGI0OTk0XkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_UX182_CR0,0,182,268_AL_.jpg",  # noqa
    "https://www.youtube.com/watch?v=PD9HDwMcfRE&disable_polymer=true", "97")

toy_story = media.Movie(
    "Toy Story",
    "A story about a kid and his toys that come to life",
    "https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-m8zzyx_5670999f.jpeg?region=0,0,300,450",  # noqa
    "https://youtu.be/v-PjgYDrg70&disable_polymer=true", "100"
)

akira = media.Movie(
    "Akira",
    "Shotaro Kaneda, a leader of a local biker gang who must stop\
     his friend Tetsuo Shima from using his newly awakened superpowers \
     to release the titular esper",
    "http://t3.gstatic.com/images?q=tbn:ANd9GcS6hoa1ePkqSULxJOKdO3O1PriRxrl5bOYT5owZJsh7FDwCPDrW",  # noqa,
    "https://youtu.be/nA8KmHC2Z-g", "87")

paprika = media.Movie(
    "Paprika",
    'Dr. Atsuko Chiba works as a scientist by day and, under the code name\
     "Paprika" is a dream detective at night',
    "https://upload.wikimedia.org/wikipedia/en/1/16/Paprikaposter.jpg",
    "https://www.youtube.com/watch?v=yn7U1KIGeuQ", "84")

up = media.Movie(
    "UP",
    "A 78-year-old balloon salesman, is about to fulfill a lifelong dream",
    "http://static.rogerebert.com/uploads/movie/movie_poster/up-2009/large_zh9DXJhBdHVVaWiDURTipADamcK.jpg",  # noqa
    "https://youtu.be/ZE_V0g9q4g0&disable_polymer=true", "98")

parasite = media.Movie(
    "Parasite",
    "Greed and class discrimination threaten the newly formed symbiotic \
      relationship between the wealthy Park family and the destitute Kim clan.",
    "https://upload.wikimedia.org/wikipedia/en/5/53/Parasite_%282019_film%29.png",
    "https://youtu.be/isOGD_7hNIY", "99")

# This array holds all the movie objects
list_movies = [
    seven_samurai,
    sanjuro,
    high_low,
    casablanca,
    toy_story,
    up,
    akira,
    paprika,
    parasite
]

# call Media and the list of movies from the object
fresh_tomatoes.open_movies_page(list_movies)


#!/usr/bin/env python
# ian.heywood@physics.ox.ac.uk

import os.path as o
import pickle
import sys
sys.path.append(o.abspath(o.join(o.dirname(sys.modules[__name__].__file__), "..")))

from oxkat import generate_jobs as gen

def main():

    CWD = gen.CWD
    OXKAT = gen.OXKAT
    PARSETS = gen.PARSETS
    SCRIPTS = gen.SCRIPTS
    LOGS = gen.LOGS
    DDFACET_CONTAINER = gen.DDFACET_CONTAINER

    submit_file = 'submit_3GC_jobs.sh'
    kill_file = 'kill_3GC_jobs.sh'

    gen.setup_dir(SCRIPTS)
    gen.setup_dir(LOGS)

    with open('project_info.p','rb') as f:
        project_info = pickle.load(f,encoding='latin1')

    targets = project_info['target_list']

    f = open(submit_file,'w')

    for target in targets:

        fieldname = target[0]
        code = fieldname[-3:]
        myms = target[2].rstrip('/')

        pcal_prefix = 'img_'+myms+'_pcal'
        ddf1_prefix = 'img_'+myms+'_DDF_corr'

        # ------------------------------------------------------------------------------
        # Make FITS mask

        slurmfile = SCRIPTS+'/slurm_makemask1_'+code+'.sh'
        logfile = LOGS+'/slurm_makemask1_'+code+'.log'

        syscall,fitsmask = gen.generate_syscall_makemask(pcal_prefix)
        syscall = 'singularity exec '+DDFACET_CONTAINER+' '+syscall

        gen.write_slurm(opfile=slurmfile,
                    jobname=code+'mask1',
                    logfile=logfile,
                    syscall=syscall)

        job_id_makemask1 = 'MAKEMASK1_'+code
        syscall = job_id_makemask1+"=`sbatch "+slurmfile+" | awk '{print $4}'`"
        f.write(syscall+'\n')

        # ------------------------------------------------------------------------------
        # DDFacet

        slurmfile = SCRIPTS+'/slurm_ddf_corr_'+code+'.sh'
        logfile = LOGS+'/slurm_ddf_corr_'+code+'.log'

        syscall = 'singularity exec '+DDFACET_CONTAINER+' '
        syscall += gen.generate_syscall_ddfacet(mspattern=myms,
                    imgname=ddf1_prefix,
                    chunkhours=2,
                    mask=fitsmask)

        gen.write_slurm(opfile=slurmfile,
                    jobname=code+'ddfac',
                    partition='HighMem',
                    logfile=logfile,
                    syscall=syscall)

        job_id_ddf1 = 'DDF1_'+code
        syscall = job_id_ddf1+"=`sbatch -d afterok:${"+job_id_makemask1+"} "+slurmfile+" | awk '{print $4}'`"
        f.write(syscall+'\n')

        # ------------------------------------------------------------------------------

    kill = 'echo "scancel "$'+job_id_ddf1+' > '+kill_file

    f.write(kill+'\n')

    f.close()

if __name__ == "__main__":

    main()


import numpy
from sklearn import tree
import theFeatures
import theLabels
import Chordss

def main():

    chords=["A", "C","G","G"] #                 INPUT
    interval = Chordss.startParse(chords) #translate chords to major scale

    keyType = 1 #1 is major, 0 is minor         INPUT
    keyStartProgression = 0 #1 is yes, 0 is no  INPUT

    x = similar(interval) #begins to process interval into 8 character array containg match case identifiers
    current = []
    current.append(keyType)
    current.append(keyStartProgression)
    for i in x:
        current.append(i)

    features = theFeatures.getFeatures() #text file of features retrieval
    labels = theLabels.getLabels()       #text file of the corresponing labels
    print(current)

    guess = processData(current, features, labels) #translates guess to word format
    binGuess = guessTrans(guess) #translates guess to numerical format
    response = input("Is this a " + guess + " song? (y/n) ") #user verifies if song perdiction is correct or incorrect
    if(response == "n"):
        r = input("what genre is this song? ")
        while(True):
            if(r == "country"):
                theLabels.setLabels(0) #setting the courect label in text file
                break;
            if(r == "hip-hop"):
                theLabels.setLabels(1)
                break;
            if(r == "pop"):
                theLabels.setLabels(2)
                break;
            else:
                r =input("invalid entry, try again ")
    if(response=="y"):
        theLabels.setLabels(binGuess)

    theFeatures.setFeatures(current)#setting 10 character binary list to text file

def guessTrans(guess):
    result=0
    if(guess == "country"):
        result = 0
    if(guess == "hip-hop"):
        result = 1
    if(guess == "pop"):
        result = 2
    return(result)

def processData(current, features, labels):
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(features, labels)
    #where constraints will go about the song
    output = (clf.predict([current]))
    result = ""
    if(output == 0):
        result = "country"
    if(output == 1):
        result = "hip-hop"
    if(output == 2):
        result = "pop"
    return(result)

def similar(interval): # finds if matches exist by assigning value of one
    matchData = []
    length = len(interval)
    endPos = length-1
    count = 0
    for i in interval:
        while(True):
            if(count != endPos): #proceed if positions are not comparing with themselves
                if(i == interval[endPos]):#if values match
                    matchData.append(1)
                    endPos-=1 #re-adjust for next comparision with i
                else:
                    matchData.append(0)
                    endPos-=1
            else:
                count+=1
                endPos = length-1
                break;
    print(matchData)
    m=isMatching(matchData, interval)
    return(m)

def isMatching(data, interval): #for similar values, if match exists, finds the type of match

    pairData = [9]
    length = len(interval)  #amount of chords
    dataLength = len(data) - 1 # amount of data points to determine matches
    gap = 2*(length-2)
    pos = 0
    for i in data:
        #print(pos)
        if(i==1):
            while(True):
                if(pos == 0 and data[pos]==1):
                    #print("same note starts and resolved progression.......Locations") #represent first and last, value 0
                    pairData.append(0)
                    pos+=1
                    break
                if(pos == len(interval)-2 ):
                    #print("beginning 2 chords are the same chords match")#represent first 2, value 1
                    pairData.append(1)
                    pos+=1
                    break
                if(pos == dataLength):
                    #print("ending 2 chords are the same") #representing last 2, value 2
                    pairData.append(2)
                    pos+=1
                    break
                if(pos== gap or pos== gap+2 or pos== gap+3 or pos== gap+4):
                    #print("matchiong chords side by side in middle of pogression") #represent other adjacent chord outcomes between start and end, value 3
                    pairData.append(3)
                    pos+=1
                    break
                else:
                    #print("spaced match") # any other chord repetition not as pair, nor beginning and end match, value 4
                    pairData.append(4)
                    pos+=1
                    break;
        else:
            #print("match test is negative")
            pos+=1
    print(pairData)

    featuresSequence =assignMatchData(pairData)
    return(featuresSequence)

def assignMatchData(pair): #assign importance if multiple match occur or other outcomes with many matches
    data = [0,0,0,0,0,0,0,0]
    while(True):
        if len(pair)==1:
            break;
        else:
            data.pop(0)
            data.insert(0,1)
        for i in pair:
            if(i==0): #first and last simlilar, is detected first
                data.pop(1)
                data.insert(1,1)
                data.pop(3)
                data.insert(3,1)
            if(i==1): #begin pair
                data.pop(2)
                data.insert(2,1)
                data.pop(5)
                data.insert(5,1)
            if(i==2): #end chords the same
                data.pop(2)
                data.insert(2,1)
                data.pop(6)
                data.insert(6,1)
            if(i==3): # middle chord pair
                data.pop(2)
                data.insert(2,1)
                data.pop(7)
                data.insert(7,1)
            if(i==4): #any other spaced pair
                data.pop(1)
                data.insert(1,1)
                data.pop(4)
                data.insert(4,1)

        break;

    #print(data)
    return(data)

def startsInKey(chords, key): #takes in original chords and the key to see if firat chord is the key
    dataOfKey = ["A","A#","Bb","B","C","C#","Db","D","D#","Eb","E","F","F#","Gb","G","G#","Ab"]
    #output 1 means they match, output 0 is they don't match
    matches=0
    if(key == chords[0]):
        matches = 1
    return(matches)

main()


import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft, ifft
from scipy.signal import lfilter,freqz

def signal_function(t):
	# t is a list of numbers.
	return np.cos(2*np.pi*10*t) + np.cos(2*np.pi*100*t) + np.cos(2*np.pi*200*t)

def fft_plots(t, v, signal_xt, abs_fft_out, ifft_out):
	###########################
	### FFT and IFTTT PLOTS ###
	###########################

	# Set up three subplots.
	f, xarr = plt.subplots(3)

	# Change the color of the plots.
	plt.style.use('seaborn-dark') 	#print(plt.style.available)

	# Add space between the subplots.
	plt.subplots_adjust(hspace = 1.25)

	# NOISY SIGNAL
	xarr[0].set_title("Noisy Signal") 	# subplot[index]
	xarr[0].set_xlabel("Time t")
	xarr[0].set_ylabel("Volts V")
	xarr[0].plot(t, signal_xt)

	# FFT SIGNAL
	xarr[1].set_title("FFT Amplitude Spectrum")
	xarr[1].set_xlabel("Frequency")
	xarr[1].set_ylabel("Amplitude")
	xarr[1].plot(v[0:499], abs_fft_out[0:499])

	# IFFT SIGNAL
	xarr[2].set_title("Inverse FFT Amplitude Spectrum")
	xarr[2].set_xlabel("Time t")
	xarr[2].set_ylabel("Volts V")
	xarr[2].plot(t, ifft_out)

	return 0

def time_domain_filtering_plots(t, signal_xt, b, bn, filter_out):
	###################################
	### TIME DOMAIN FILTERING PLOTS ###
	###################################

	# TDFP = Time Domain Filtering Plots
	# Set up a column of subplots.
	f, tdfp = plt.subplots(3)

	# Change the style of the plots.
	plt.style.use("seaborn-dark")

	# Add space between the subplots.
	plt.subplots_adjust(hspace = 1.25)

	# NOISY SIGNAL
	tdfp[0].set_title("Noisy Signal") 	# subplot[index]
	tdfp[0].set_xlabel("Time t")
	tdfp[0].set_ylabel("Volts V")
	tdfp[0].plot(t, signal_xt)

	# FILTER IMPULSE
	tdfp[1].set_title("Filter Impulse (YOU CAN IGNORE THIS SUBPLOT)") 	# subplot[index]
	tdfp[1].set_xlabel("Time t")
	tdfp[1].set_ylabel("Volts V")
	tdfp[1].stem(bn, b)

	# FILTER OUTPUT (CONVOLUTION)
	tdfp[2].set_title("Filter Output (Convolution)") 	# subplot[index]
	tdfp[2].set_xlabel("Time t")
	tdfp[2].set_ylabel("Volts V")
	tdfp[2].plot(t, filter_out)

	return 0

def frequency_domain_filtering_plots(v, abs_fft_out, hzTrue, H, w_filter_out):
	########################################
	### FREQUENCY DOMAIN FILTERING PLOTS ###
	########################################

	# fdf = Frequency Domain Filtering
	f, fdf = plt.subplots(3)

	# Change the style of the plots.
	plt.style.use("seaborn-dark")

	# Add space between the subplots.
	plt.subplots_adjust(hspace = 1.25)

	# Amplitude Spectrum (Real Half)
	fdf[0].set_title("Amplitude Spectrum (Real Half)") 	# subplot[index]
	fdf[0].set_xlabel("Frequency Hz")
	fdf[0].set_ylabel("Amplitude")
	fdf[0].plot(v[0:499], abs_fft_out[0:499])

	# Frequency Response of Filter
	fdf[1].set_title("Frequency Response of Filter") 	# subplot[index]
	fdf[1].set_xlabel("Frequency Hz")
	fdf[1].set_ylabel("Amplitude")
	fdf[1].plot(hzTrue[0:499], H[0:499])

	# Filter Output (Fast Fourier Transform)
	fdf[2].set_title("Filter Output (fft)") 	# subplot[index]
	fdf[2].set_xlabel("Frequency Hz")
	fdf[2].set_ylabel("Amplitude")
	fdf[2].plot(v[0:499], w_filter_out[0:499])

	return 0

def final_plot(t, v, filter_out, filter_out_fft_out, w_filter_out, ifft_tdf):
	# fp = Final Plot
	f, fp = plt.subplots(4)

	# Change the style of the plots.
	plt.style.use("seaborn-dark")

	# Add space between the subplots.
	plt.subplots_adjust(hspace = 1.25)

	# FILTER OUTPUT (CONVOLUTION)
	fp[0].set_title("Filter Output (Convolution)") 	# subplot[index]
	fp[0].set_xlabel("Time t")
	fp[0].set_ylabel("Volts V")
	fp[0].plot(t, filter_out)

	# FFT of Convolution
	fp[1].set_title("FFT of Time Domain Filtering (Convolution)") 	# subplot[index]
	fp[1].set_xlabel("Time t")
	fp[1].set_ylabel("Amplitude")
	fp[1].plot(v[0:499], filter_out_fft_out[0:499])

	# Filter Output of Frequency Domain Filtering (Fast Fourier Transform)
	fp[2].set_title("Filter Output of Frequency Domain Filter (fft)") 	# subplot[index]
	fp[2].set_xlabel("Frequency Hz")
	fp[2].set_ylabel("Amplitude")
	fp[2].plot(v[0:499], w_filter_out[0:499])

	# The Inverse Fast Fourier Transform of the output of the filtered waveform from FFT.
	fp[3].set_title("IFFT of FFT of Frequency Domain Filtering") 	# subplot[index]
	fp[3].set_xlabel("Time t")
	fp[3].set_ylabel("Amplitude")
	#fp[3].plot(v[0:499], ifft_tdf[0:499])
	fp[3].plot(t, ifft_tdf)
	return 0

if __name__ == "__main__":
	#####################
	### FFT and IFTTT ###
	#####################

	# t is of length 1000. Numbers range from 0 to 2.
	t = np.linspace(0, 2, 1000)

	# Making a list of numbers which will make the list of frequencies.
	n = np.linspace(0, 1, 1000) # n goes from 0 to 1 with 1000 points in-between.

	# Create the signal xt
	signal_xt = signal_function(t)

	# T is the sample period
	# T = (endTime - startTime)/(signalLength)
	T = (2-0)/len(signal_xt) # T = 0.002

	# v is the sample frequency multiplies by the length of the function to make an array of frequencies.
	v = n/T # v ranges from 0 to 500 with 1000 samples in between.

	# Obtain the absolute value of the fast Fourier transform of the signal xt
	fft_out = fft(signal_xt)
	abs_fft_out = abs(fft_out)

	# Get the inverse Fourier transform from the fast Fourier transform.
	ifft_out = ifft(fft_out)

	#############################
	### TIME DOMAIN FILTERING ###
	#############################

	# 15 point moving average filter
	N = 15

	# Make list of 15 ones, and divide every single one by 15.
	b = (1/N)*np.ones(N)

	# Make 15 different points for graphing.
	bn = np.arange(15)

	# Generate a filtered output of the signal_xt, This convolves the sequence b, with the noisy data.
	filter_out = lfilter(b, 1, signal_xt)

	##################################
	### FREQUENCY DOMAIN FILTERING ###
	##################################

	# The rate at which the frequency is sampled.
	sample_rate = 1/T # sample_rate = 500

	# H is a representation of our moving average filter called the frequency response.
	hz, H = freqz(b, 1, worN = v*2*np.pi/sample_rate)
	hzTrue = hz*sample_rate/(2*np.pi)

	# Filter the fast Fourier transform of the signal xt
	w_filter_out = H*fft_out

	##################
	### FINAL PLOT ###
	##################

	# From the Time Domain Filtering.
	filter_out_fft_out = fft(filter_out) # Plot amplitude spectrum.

	# Take the Inverse Fast Fourier Transform of the FFT of the Time Domain Filtering.
	ifft_tdf = ifft(w_filter_out)

	#################
	### ALL PLOTS ###
 	#################

	# FFT and IFFT Filtering Plots
	fft_plots(t, v, signal_xt, abs_fft_out, ifft_out)

	# Time Domain Filtering Plots
	time_domain_filtering_plots(t, signal_xt, b, bn, filter_out)

	# Frequency Domain Filtering Plots
	frequency_domain_filtering_plots(v, abs_fft_out, hzTrue, H, w_filter_out)

	# Final Plots
	final_plot(t, v, filter_out, filter_out_fft_out, w_filter_out, ifft_tdf)

	# Show all the plots.
	plt.show()


import argparse
import os
from pathlib import Path

import tensorflow as tf
from tensorflow.keras import layers, models

import flwr as fl
import csv
from datetime import datetime
import numpy as np

from models import load_model
import time

# Make TensorFlow logs less verbose
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"

# Define Flower client
class CustomClient(fl.client.NumPyClient):
    def __init__(self, model, x_train, y_train, x_test, y_test, id):
        self.model = model
        self.x_train, self.y_train = x_train, y_train
        self.x_test, self.y_test = x_test, y_test
        self.id = id
        self.round = 0
        self.create_result_file()

    def create_result_file(self):
        row = "round & loss & accuracy & precision & time \\\\ \hline \n"
        now = datetime.now()
        postfix = now.strftime('%Y%m%d%H%M')

        file_path = "./results/client0" + str(self.id) + "-" + postfix

        self.result_file = file_path

        # with open(self.result_file, 'w') as f:
        #     f.write(row)

    def get_properties(self, config):
        """Get properties of client."""
        raise Exception("Not implemented")

    def get_parameters(self, config):
        """Get parameters of the local model."""
        raise Exception("Not implemented (server-side parameter initialization)")

    def fit(self, parameters, config):
        """Train parameters on the locally held training set."""
        start = time.time()
        # Update local model parameters
        self.model.set_weights(parameters)

        # Get hyperparameters for this round
        batch_size: int = config["batch_size"]
        epochs: int = config["local_epochs"]

        print("tamanho da base que estou treinando: ", len(self.x_train))

        # Train the model using hyperparameters from config
        history = self.model.fit(
            self.x_train,
            self.y_train,
            batch_size,
            epochs,
            validation_split=0.1,
        )

        # Return updated model parameters and results
        parameters_prime = self.model.get_weights()
        num_examples_train = len(self.x_train)
        results = {
            "loss": history.history["loss"][0],
            "accuracy": history.history["accuracy"][0],
            "val_loss": history.history["val_loss"][0],
            "val_accuracy": history.history["val_accuracy"][0],
        }

        end = time.time()
        self.elapse = end - start

        return parameters_prime, num_examples_train, results

    def evaluate(self, parameters, config):
        """Evaluate parameters on the locally held test set."""

        # Update local model with global parameters
        self.model.set_weights(parameters)

        # Get config values
        steps: int = config["val_steps"]

        # Evaluate global model parameters on the local test data and return results
        loss, accuracy, pre = self.model.evaluate(self.x_test, self.y_test, 32, steps=steps)

        num_examples_test = len(self.x_test)

        # row = " & " + str(self.round) + " & " + str(round(loss,3)) + " & " + str(round(accuracy,2)) + " & " + str(round(pre,2)) + " & " + str(int(self.elapse)) + " \\\\ \n"
        # with open(self.result_file, 'a') as f:
        #     f.write(row)

        self.round = self.round + 1

        return loss, num_examples_test, {"accuracy": accuracy}

def main() -> None:
    # Parse command line argument `partition`
    parser = argparse.ArgumentParser(description="Flower")
    parser.add_argument("--partition", type=int, choices=range(0, 5), required=True)
    parser.add_argument("--epochs", type=int, choices=range(1, 30), required=True)
    parser.add_argument("--clients", type=int, choices=range(1, 6), required=True)
    args = parser.parse_args()

    # Load and compile Keras model
    model = load_model("efinet", (45, 45, 3), 1)

    # Load a subset of dataset to simulate the local data partition
    (x_train, y_train), (x_test, y_test) = load_partition(args.partition, args.clients)

    # Start Flower client
    client = CustomClient(model, x_train, y_train, x_test, y_test, args.partition)

    fl.client.start_numpy_client(
        server_address="127.0.0.1:8080",
        client=client,
        root_certificates=Path(".cache/certificates/ca.crt").read_bytes(),
    )

def load_partition(idx: int, clients: int):
    """Load 1/10th of the training and test data to simulate a partition."""
    assert idx in range(clients)

    print("CLIENT %s --- Lendo dados para treino", idx)
    # read dataset
    train_ds = tf.keras.utils.image_dataset_from_directory(
        f'./datasource/hospital_detector_seed123/train',
        shuffle=True,
        batch_size=None,
        image_size=(45, 45)
    )

    test_ds = tf.keras.utils.image_dataset_from_directory(
        f'./datasource/hospital_detector_seed123/test',
        shuffle=True,
        batch_size=None,
        image_size=(45, 45)
    )

    print("CLIENT %s --- Fazendo treino", idx)

    x_train, y_train = tuple(zip(*train_ds))
    x_train, y_train = np.array(x_train), np.array(y_train)

    x_test, y_test = tuple(zip(*test_ds))
    x_test, y_test = np.array(x_test), np.array(y_test)

    # s_client_data_train_0 = int(len(x_train)*0.15)
    # s_client_data_train_1 = int(len(x_train)*0.10)
    # s_client_data_train_2 = int(len(x_train)*0.20)
    # s_client_data_train_3 = int(len(x_train)*0.30)
    # s_client_data_train_4 = int(len(x_train)*0.25)

    s_client_data_train = int(len(x_train)/clients)

    # print("----------- data size: ", str(s_client_data_train), " ------------------")
    # s_client_data_test = int(len(x_test)/clients)
    #x_test, y_test = tf.keras.datasets.cifar10.load_data()

    # start = 0
    # end = s_client_data_train_0

    # if idx == 1:
    #     start = s_client_data_train_0
    #     end = s_client_data_train_0 + s_client_data_train_1
    # elif idx == 2:
    #     start = s_client_data_train_0 + s_client_data_train_1
    #     end = s_client_data_train_0 + s_client_data_train_1 + s_client_data_train_2
    # elif idx == 3:
    #     start = s_client_data_train_0 + s_client_data_train_1 + s_client_data_train_2
    #     end = s_client_data_train_0 + s_client_data_train_1 + s_client_data_train_2 + s_client_data_train_3
    # elif idx == 4:
    #     start = s_client_data_train_0 + s_client_data_train_1 + s_client_data_train_2 + s_client_data_train_3
    #     end = len(x_train)

    # print("Start: ", start, "End: ", end)

    return (
        # x_train[start : end],
        # y_train[start : end],
        x_train[idx * s_client_data_train : (idx + 1) * s_client_data_train],
        y_train[idx * s_client_data_train : (idx + 1) * s_client_data_train],
    ), (
        # x_test[idx * s_client_data_test : (idx + 1) * s_client_data_test],
        # y_test[idx * s_client_data_test : (idx + 1) * s_client_data_test],
        x_test, y_test
    )

if __name__ == "__main__":
    main()


import random

def make_matrix(edges, vertices):
    matrix = []
    for i in range(vertices):
        matrix.append([])
        for j in range(vertices):
            matrix[i].append(0)

    for i in range(len(edges)):
        matrix[edges[i][0]][edges[i][1]] = edges[i][2]
        matrix[edges[i][1]][edges[i][0]] = edges[i][2]
    return matrix

def get_degree(matrix, vertices):
    degrees = []
    for i in range(vertices):
        degree = 0
        for j in range(vertices):
            if matrix[i][j] != 0:
                degree += 1
        degrees.append(degree)
    return degrees

def find_odd(degrees):
    odd = []
    for i in range(len(degrees)):
        if degrees[i] % 2 != 0:
            odd.append(i)
    return odd

def bfs(graph, start, end):
    if start == end:
        return 0, []
    queue: list[list[int]] = [[start]]
    stop = False
    while not stop and queue:
        stop = True
        for path in queue:
            if path[-1] != end:
                stop = False
        if stop:
            break
        new_queue = []
        for path in queue:
            nodes = graph[path[-1]]
            if end in path:
                new_queue.append(path)
            else:
                for i in range(len(nodes)):
                    if nodes[i] != 0 and i not in path:
                        path_copy = path.copy()
                        path_copy.append(i)
                        new_queue.append(path_copy.copy())
        queue = new_queue
    weights = []
    for path in queue:
        weight = 0
        for i in range(len(path) - 1):
            weight += graph[path[i]][path[i + 1]]
        weights.append(weight)

    min_weight = min(weights)
    min_index = weights.index(min_weight)
    return min_weight, queue[min_index]

def gen_pairs(odds):
    pairs = []
    for i in range(len(odds) - 1):
        for j in range(i + 1, len(odds)):
            pairs.append([odds[i], odds[j]])
    return pairs

def gen_edges_to_add(pairs, graph, odds: list[int]):
    pairs = pairs.copy()
    odds = odds.copy()
    weights = []
    paths = []

    for pair in pairs:
        weight, path = bfs(graph, pair[0], pair[1])
        weights.append(weight)
        paths.append(path)

    edges_to_add = []

    while len(odds) != 0:
        min_weight = min(weights)
        min_index = weights.index(min_weight)
        path = paths[min_index]
        if pairs[min_index][0] in odds and pairs[min_index][1] in odds:
            for i in range(len(path) - 1):
                edges_to_add.append((min(path[i], path[i + 1]), max(path[i], path[i + 1]), graph[path[i]][path[i + 1]]))
            odds.remove(pairs[min_index][0])
            odds.remove(pairs[min_index][1])
        weights.remove(min_weight)
        paths.remove(path)
        pairs.remove(pairs[min_index])

    return edges_to_add

def find_min_euler_cycle(edges, start):
    stack = []
    ce = []
    stack.append(start)
    ce.append(start)
    while stack:
        v = stack[-1]
        for i in range(len(edges)):
            if edges[i][0] == v:
                stack.append(edges[i][1])
                ce.append(edges[i][1])
                edges.pop(i)
                break
            elif edges[i][1] == v:
                stack.append(edges[i][0])
                ce.append(edges[i][0])
                edges.pop(i)
                break
        if stack[-1] == v:
            stack.pop()
    return ce

# ---

def is_cycle(edge, mst):
    start = edge[0]
    go_to = edge[1]
    old_goto = ""
    to_return = False
    while True:
        for x in mst:
            old_goto = go_to
            if go_to == x[1]:
                go_to = x[0]
                mst.remove(x)
                break
            if go_to == x[0]:
                go_to = x[1]
                mst.remove(x)
                break
        if old_goto == go_to:
            break
        if go_to == start:
            to_return = True
            break
        if len(mst) < 2:
            break
    return to_return

def kruskal(edges, vertices):
    mst = []
    all_edges = edges.copy()
    all_edges.sort(key=lambda x: x[2])
    while True:
        try:
            edge = all_edges.pop(0)
        except:
            print("I can't find a tree")
            exit(0)
        if not is_cycle(edge, mst.copy()):
            mst.append(edge)
        if len(mst) == vertices - 1:
            break
    return mst

def check_if_connected(edges, vertices: int):
    visited = {}
    for x in edges:
        visited[x[0]] = False
        visited[x[1]] = False

    stack = [edges[0][0]]

    while stack:
        v = stack.pop()
        if not visited[v]:
            visited[v] = True
            for edge in edges:
                if edge[0] == v:
                    stack.append(edge[1])
                elif edge[1] == v:
                    stack.append(edge[0])
    return all(visited.items())

def find_vertices_amount(edges):
    stack = set()
    for edge in edges:
        stack.add(edge[0])
        stack.add(edge[1])
    return len(stack)

def find_euler_cycle(edges, start=0):
    print(edges)
    ce = [start]
    w = 2*len(edges)
    while len(edges) > 0 and w > 0:
        v = ce[-1]
        w -= 1
        print(ce)
        for i in range(len(edges)):
            if edges[i][0] == v or edges[i][1] == v:
                edge = edges[i]
                edges_coppy = edges.copy()
                edges_coppy.pop(i)

                if check_if_connected(edges, find_vertices_amount(edges_coppy)):
                    ce.append(edge[0] if edge[1] == v else edge[1])
                    edges.pop(i)
                    break
    return ce

def main():
    # edges = [(0, 1, 2), (0, 7, 1), (1, 2, 2), (1, 8, 2), (2, 3, 3), (3, 4, 4), (3, 8, 3), (3, 9, 1),
    #          (4, 5, 3), (4, 9, 2), (5, 6, 5), (5, 9, 2), (6, 7, 1), (6, 8, 2), (6, 9, 4), (7, 8, 4)]
    # vertices = 10

    edges = [(0, 1, 7), (0, 3, 5), (1, 2, 8), (1, 3, 9), (1, 4, 7), (2, 4, 5), (3, 4, 15), (3, 5, 6), (4, 5, 8),
             (4, 6, 9), (5, 6, 11)]
    vertices = 7

    mst = kruskal(edges, vertices)
    graph = make_matrix(mst, vertices)
    degrees = get_degree(graph, vertices)
    odds = find_odd(degrees)
    pairs = gen_pairs(odds)
    edges_to_add = gen_edges_to_add(pairs, graph, odds)
    for x in edges_to_add:
        mst.append(x)

    # random.shuffle(mst)

    euler_cycle = find_euler_cycle(mst)
    hamilton = []
    for x in euler_cycle:
        if x not in hamilton:
            hamilton.append(x)
    print(hamilton)

if __name__ == '__main__':
    main()


#!/usr/bin/python3
import os
import sys
import argparse
import gettext
import registering

gettext.install('gooroom-client-server-register', '/usr/share/gooroom/locale')

example_regkey = """>v1.0)gooroom-client-server-register noninteractive-regkey -d gkm.gooroom.kr
                                            [-C /usr/local/share/ca-certificates/server.crt]
                                            [ -r 2] #0:create 1:update 2:create or update
                                            [ -m ipaddress]
                                            [-u gooroom]
                                            [-t Default]
                                             -k registration key
                                            [-e 2020-01-01]
                                            [-c 2F ooo]"""

autoregisteration_help ="""To automatically register your client to Gooroom Platform Management Server(GPMS) on boot, configure /etc/gooroom/gooroom-client-server-register/gcsr_auto.conf accordingly."""

def usage():
    print('ex)gooroom-client-server-register gui\n')
    print('ex)gooroom-client-server-register cli\n')
    print(example_regkey)
    print('\n')
    print(autoregisteration_help)

def argument_parser():
    clientip = os.popen('hostname --all-ip-addresses').read().split(' ')[0]
    parser = argparse.ArgumentParser(description=_('Register certificate of gooroom root CA & gooroom platform management server'))

    subparsers = parser.add_subparsers(dest='cmd', help=_('commands'))
    subparsers.required = True
    gui_parser = subparsers.add_parser('gui', help=_('Run as gtk graphical user interface'))
    cli_parser = subparsers.add_parser('cli', help=_('Run as command line interface'))
    ni_parser = subparsers.add_parser('noninteractive',
                                      help=_('Run as Noninteractive with shell.'),
                                      formatter_class=argparse.RawTextHelpFormatter)
    ni_help = subparsers.add_parser('noninteractive --help', help=_('Print help on the noninteractive command'))
    ni_parser.add_argument('-d', '--domain', required=True, help=_('Key management server hostname'))
    ni_parser.add_argument('-C', '--CAfile', help=_('(Option)PEM format file of gooroom root CA certificate'), nargs='?')
    ni_parser.add_argument('-n', '--cn', help=_('Unique CN to use for the client certificate'))
    ni_parser.add_argument('-m', '--name', help=_('(Option)Client name to distinguish from others:default=ip'), default=clientip)
    ni_parser.add_argument('-u', '--unit', help=_('Client organizational unit to use for the client certificate'))
    ni_parser.add_argument('-t', '--password-system-type', help=_('Password system type to use for the password hashing.'), default='Default', nargs='?')
    ni_parser.add_argument('-i', '--id', help=_('GPMS admin ID'))
    ni_parser.add_argument('-p', '--password', help=_('GPMS admin password'))
    ni_parser.add_argument('-e', '--expiration-date', help=_('(Option)Certificates expiration date(format:YYYY-MM-DD)'), default='', nargs='?')
    ni_parser.add_argument('-c', '--comment', help=_('(Option)Description of the certificate'), nargs='?')
    ni_parser.add_argument('-r', '--cert-reg-type', help=_('(Option)Certificate Registration Type(0:create 1:update 2:create or update) default=create or update'), default='2', nargs='?')

    ni_regkey_parser = subparsers.add_parser('noninteractive-regkey', description=example_regkey,
                                      help=_('Run as NoninteractiveRegKey with shell using regstration key but id/pwd.'),
                                      formatter_class=argparse.RawTextHelpFormatter)
    ni_regkey_help = subparsers.add_parser('noninteractive-regkey --help', help=_('Print help on the noninteractive-regkey command'))
    ni_regkey_parser.add_argument('-d', '--domain', required=True, help=_('Key management server hostname'))
    ni_regkey_parser.add_argument('-I', '--IP', help=_('Key management server IP'))
    ni_regkey_parser.add_argument('-C', '--CAfile', help=_('(Option)PEM format file of gooroom root CA certificate'), nargs='?')
    ni_regkey_parser.add_argument('-m', '--name', help=_('Option)Client name to distinguish from others:default=ip'), default=clientip)
    ni_regkey_parser.add_argument('-u', '--unit', help=_('Client organizational unit to use for the client certificate'))
    ni_regkey_parser.add_argument('-t', '--password-system-type', help=_('Password system type to use for the password hashing.'), default='Default', nargs='?')
    ni_regkey_parser.add_argument('-e', '--expiration-date', help=_('(Option)Certificates expiration date(format:YYYY-MM-DD)'), default='', nargs='?')
    ni_regkey_parser.add_argument('-c', '--comment', help=_('(Option)Description of the certificate'), nargs='?')
    ni_regkey_parser.add_argument('-k', '--regkey', required=True, help=_('GPMS registration key'))
    ni_regkey_parser.add_argument('-r', '--cert-reg-type', help=_('(Option)Certificate Registration Type(0:create 1:update 2:create or update) default=create or update'), default='2', nargs='?')

    arguments = parser.parse_args()
    return arguments

if __name__ == '__main__':
    '''
    main
    '''

    if len(sys.argv) == 1:
        usage()
        exit(1)

    args = argument_parser()
    if os.getuid():
        print(_("Permission denied."))
        exit(1)

    if args.cmd == 'gui':
        registering.GUIRegistering()
    else:
        shell_register = registering.ShellRegistering()
        shell_register.run(args)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys
import time
import argparse
import numpy as np
import cv2

from tensorflow.lite.python.interpreter import Interpreter

def mkdir(path):
    path=path.rstrip("\\")
    isExists=os.path.exists(path)
    if not isExists:
        os.makedirs(path)
        return True
    else:
        return False

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '-i',
      '--image',
      default='/tmp/grace_hopper.bmp',
      help='image to be classified')
  parser.add_argument(
      '-m',
      '--model_file',
      default='/tmp/mobilenet_v1_1.0_224_quant.tflite',
      help='.tflite model to be executed')
  parser.add_argument(
      '--input_mean',
      default=127.5, type=float,
      help='input_mean')
  parser.add_argument(
      '--input_std',
      default=0.0078125, type=float,
      help='input standard deviation')
  args = parser.parse_args()
  print(args.model_file)
  print(args.image)
  print(args.input_mean)
  print(args.input_std)
  interpreter = Interpreter(model_path=args.model_file)
  interpreter.allocate_tensors()

  input_details = interpreter.get_input_details()
  print(input_details)
  output_details = interpreter.get_output_details()
  print(output_details)
  # check the type of the input tensor
  floating_model = input_details[0]['dtype'] == np.float32

  # NxHxWxC, H:1, W:2
  height = input_details[0]['shape'][1]
  width = input_details[0]['shape'][2]
  #print(height)
  #print(width)
  img_list=[]
  with open(args.image, 'r') as f:
      for line in f:
          img_list.append(line.strip())
  #print(img_list)
  i = 0
  for img_path in img_list:
    print(img_path)
    out_img_path = img_path.replace("/Users/leejohnnie/nfs/lfw-matlab-112x112", "./result")
    #out_img_path = img_path.replace("/Users/leejohnnie/dataset", "./result")
    print(out_img_path)
    out_img_path = os.path.splitext(out_img_path)
    print(out_img_path)
    newname = out_img_path[0] + ".bin"
    print(newname)
    mkdir(os.path.dirname(newname))
    start = time.time()
    img = cv2.imread(img_path, 0)
    input_data = np.expand_dims(img, axis=2)
    input_data = np.expand_dims(input_data, axis=0)
    print(input_data.shape)
    print("===========================")
    i = i + 1
    print(i)

    if floating_model:
      input_data = (np.float32(input_data) - args.input_mean) * args.input_std
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()
    output_data = interpreter.get_tensor(output_details[0]['index'])
    output_data.astype('float32').tofile(newname)
    stop = time.time()
    print("time(s):", stop-start)