#
# loss.py
# loss functions for RGBD-GAN
#

import torch
import torch.nn.functional as F
import numpy as np

from util.warp import warp, inv_warp, bilinear_sampling

def loss_gen_bce(y_fake):
    """
    loss function with binary cross entropy on generator
    """
    label_real = torch.ones_like(y_fake)
    loss = F.binary_cross_entropy_with_logits(y_fake, label_real, reduction='none')
    return loss

def loss_gen_wasserstein(y_fake):
    """
    loss function with wasserstein distance on generator
    """
    return -torch.mean(y_fake)

def loss_dis_bce(y_fake, y_real):
    """
    loss function with binary cross entropy on discriminator
    """
    label_fake = torch.zeros_like(y_fake)
    label_real = torch.ones_like(y_real)
    fake_loss = F.binary_cross_entropy_with_logits(y_fake, label_fake, reduction='none')
    real_loss = F.binary_cross_entropy_with_logits(y_real, label_real)

    return (torch.mean(fake_loss) + real_loss) / 2, real_loss, fake_loss

def loss_dis_wasserstein(y_fake, y_real):
    return (torch.mean(y_fake) + torch.mean(-y_real)) / 2

class Rotate3dLoss:
    """
    class for calculating 3d loss for two camera parameters' images
    """
    def __init__(self, K=None, norm='l1', lambda_geometric=3, device=torch.device('cuda')):
        self.size = None
        self.K = K
        self.inv_K = None
        self.norm = norm
        self.lambda_geometric = lambda_geometric
        self.p = None
        self.device = device

    def init_params(self, size=4):
        """
        initialize camera intrinsic parameter and image pixel homogeneous coordinate
        """
        # camera intrinsic parameter and inverse (3, 3)
        if self.size is None:
            if self.K is not None:
                self.K = torch.tensor(self.K[:3, :3], dtype=torch.float32)
                self.K[:2] *= size / self.K[0, 2] / 2
                self.size = size
            else:
                self.size = size
                self.K = torch.tensor([[size * 2, 0, size / 2], [0, size * 2, size / 2], [0, 0, 1]], dtype=torch.float32)
        else:
            self.size = size
            self.K[:2] *= size / self.K[0, 2] / 2

        self.K = self.K.to(self.device)
        self.inv_K = torch.linalg.inv(self.K)

        # homogeneous grid of image pixels (3, h * w)
        self.p = torch.tensor(np.asarray(
            np.meshgrid(np.arange(size), np.arange(size)) + [np.ones((size, size))]
        ).reshape(3, -1), dtype=torch.float32).to(self.device)

    def __call__(self, img_1, theta_1, img_2, theta_2, occlusion_aware=False, debug=False, max_depth=None, min_depth=None):
        """
        calculate 3d loss of image and warped image
        img : (b, 4, h, w) -> rgb and depth
        theta : (b, 3, 4) -> rotation and translation
        img_1 : theta_1 camera extrinsic parameter image
        img_2 : theta_2 camera extrinsic parameter image
        """
        # check image size
        if self.size != img_1.shape[-1]:
            self.init_params(size=img_1.shape[-1])

        # get depth from images
        # (b, 1, h, w) -> (b, 1, h * w)
        d_1 = img_1[:, -1:].reshape(img_1.shape[0], 1, -1)
        d_2 = img_2[:, -1:].reshape(img_2.shape[0], 1, -1)

        # if theta is not tensor type, change type to tensor
        if not isinstance(theta_1, torch.Tensor):
            theta_1 = torch.tensor(theta_1).to(self.device)
        if not isinstance(theta_2, torch.Tensor):
            theta_2 = torch.tensor(theta_2).to(self.device)

        # split rotation and translation from theta
        R1 = theta_1[:, :3, :3]
        R2 = theta_2[:, :3, :3]
        t1 = theta_1[:, :3, -1:]
        t2 = theta_2[:, :3, -1:]

        # calculate R and t between img_1 and img_2
        R = torch.matmul(R2.transpose(2, 1), R1)
        inv_R = R.transpose(2, 1)
        t = torch.matmul(R1.transpose(2, 1), t2 - t1)

        # img_1's depth * pixels projected from c2
        new_dp_1 = warp(self.K, self.inv_K, R, t, d_1, self.p)

        # img_2's depth * pixels projected from c1
        new_dp_2 = inv_warp(self.K, self.inv_K, inv_R, t, d_2, self.p)

        # use new_dp and image for bilinear sampling to warp RGBD image
        # warp img_2 to c1 using new_dp_1
        warped_1, not_out_1 = bilinear_sampling(img_2, new_dp_1)
        # warp img_1 to c2 using new_dp_2
        warped_2, not_out_2 = bilinear_sampling(img_1, new_dp_2)

        # make original target to calculate loss with warped one
        warped_1_target = torch.cat([img_1[:, :-1].permute(0, 2, 3, 1).reshape(-1, img_1.shape[1] - 1),
                                     new_dp_1[:, :, 2].reshape(-1, 1)], dim=1) * not_out_1[:, None]
        warped_2_target = torch.cat([img_2[:, :-1].permute(0, 2, 3, 1).reshape(-1, img_2.shape[1] - 1),
                                     new_dp_2[:, :, 2].reshape(-1, 1)], dim=1) * not_out_2[:, None]

        if occlusion_aware:
            # calculate occlusion
            not_occluded_1 = warped_1[:, -1:] > new_dp_1[:, :, 2].reshape(-1, 1)
            not_occluded_2 = warped_2[:, -1:] > new_dp_2[:, :, 2].reshape(-1, 1)
            warped_1 = warped_1 * not_occluded_1
            warped_2 = warped_2 * not_occluded_2
            warped_1_target = warped_1_target * not_occluded_1
            warped_2_target = warped_2_target * not_occluded_2

        if max_depth is not None:
            pass

        if min_depth is not None:
            pass

        # select loss type (l1 or l2)
        if self.norm == 'l1':
            criteria = F.l1_loss
        else:
            criteria = F.mse_loss

        # calculate 3d loss
        # loss between original rgb and warped rgb
        loss = criteria(warped_1[:, :-1], warped_1_target[:, :-1]) + criteria(warped_2[:, :-1], warped_2_target[:, :-1])
        # loss between projected depth and warped depth
        # hyperparameter as lambda_geometric
        loss += (criteria(warped_1[:, -1], warped_1_target[:, -1]) + criteria(warped_2[:, -1], warped_2_target[:, -1])) * self.lambda_geometric

        return loss, torch.cat([new_dp_1, new_dp_2], dim=0)


from django.shortcuts import render_to_response
from django.template import RequestContext
from apps.producto.models import producto
from apps.producto.forms import productoForm
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

@login_required(login_url='/personal/login')
def view_add(request):
	if request.method == "POST":
		form = productoForm(request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect("/productos")
		else:
			form  = productoForm()
			msg="Revisa bien los datos"
			ctx = {'form':form,'msg':msg}
			return render_to_response('producto/add.html',ctx,
				context_instance=RequestContext(request))

	else:
		form = productoForm()
		msg="Ingresa los datos"
		ctx = {'form':form,'msg':msg}
		return render_to_response('producto/add.html',ctx,
				context_instance=RequestContext(request))

@login_required(login_url='/personal/login')
def view_edit(request,id):
	p = producto.objects.get(pk=id)
	if request.method == "POST":
		form = productoForm(request.POST,instance=p)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect("/productos/")
		else:
			form = productoForm(p)
			msg="Revisa bien los datos"
			ctx = {'form':form,'msg':msg}
			return render_to_response('producto/edit.html',ctx,
				context_instance=RequestContext(request))

	else:
		form = productoForm(instance=p)
		msg="Ingresa los datos"
		ctx = {'form':form,'msg':msg}
		return render_to_response('producto/edit.html',ctx,
				context_instance=RequestContext(request))

@login_required(login_url='/personal/login')
def view_del(request,id):
	p = producto.objects.get(pk=id)
	p.delete()
	return HttpResponseRedirect("/productos/")

@login_required(login_url='/personal/login')
def view_lista(request):
    contact_list = producto.objects.all()
    paginator = Paginator(contact_list, 5) # Show 25 contacts per page

    page = request.GET.get('page')
    try:
        productos = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        productos = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        productos = paginator.page(paginator.num_pages)

    return render_to_response('producto/lista.html', {"lista": productos},
				context_instance=RequestContext(request))


import os
import sys
import re
from functools import partial
from operator import methodcaller
from collections import defaultdict

import argparse
import parsel
import parmap

from animeu.common.func_helpers import compose
from animeu.common.file_helpers import JSONListStream, open_transcoded
from animeu.spiders.xpath_helpers import get_all_text

MALE_PATTERNS = [r"\bhe\b", r"\bhis\b"]

def strip_field_name(text):
    """Strip the field name section from a piece of text.

    For example 'aka: bob' -> 'bob' or 'hair color: green' -> 'green'.
    """
    if not text:
        return None
    return re.sub(r"[^:]+:\s*", "", text)

def select_name(sel):
    """Select the element containing the characters name."""
    # pylint: disable=line-too-long
    return sel.xpath("//div[contains(@class, 'breadcrumb')]/following-sibling::div[1]")

def extract_en_jp_name(sel):
    """Extract the character's full name."""
    name_sel = select_name(sel)
    english_name = name_sel.css("::text").get().strip()
    japanese_name = get_all_text(name_sel.xpath("span")).strip("()")
    return {"en": [english_name], "jp": [japanese_name]}

def extract_info_fields_and_description(sel):
    """Extract the name-value info fields of the character."""
    name_sel = select_name(sel)
    # pylint: disable=line-too-long
    text_fragments = list(map(compose(methodcaller("strip"), methodcaller("get")),
                              name_sel.xpath("following-sibling::text() | "
                                             "following-sibling::b/text()")))
    text_fragments = list(filter(bool, text_fragments))
    info_key_to_fragments = defaultdict(list)
    current_key = None
    description_fragments = []
    # pylint: disable=invalid-name
    MAX_INFO_FIELD_FRAGMENT_LENGTH = 40
    MAX_WRITTEN_BY_LENGTH = 40
    for text_fragment in text_fragments:
        # pylint: disable=line-too-long
        if re.search(r"\b(written|from|source|author)\b", text_fragment, flags=re.I) and \
                len(text_fragment) <= MAX_WRITTEN_BY_LENGTH:
            continue
        if description_fragments and \
                len(text_fragment) > MAX_INFO_FIELD_FRAGMENT_LENGTH:
            description_fragments.append(text_fragment.strip())
            continue
        match = re.match(r"^(?P<key>[^:]{1,30}):\s*(?P<value>.*$)",
                         text_fragment)
        if not match and len(text_fragment) > MAX_INFO_FIELD_FRAGMENT_LENGTH:
            description_fragments.append(text_fragment.strip())
            continue
        if match:
            current_key = match.group("key").strip()
        if current_key:
            info_key_to_fragments[current_key].append(text_fragment.strip())
    return (
        [{"key": k, "value": "\n".join(v).strip()}
         for k, v in info_key_to_fragments.items()],
        "\n".join(description_fragments).strip() or None
    )

def extract_main_display_picture(sel):
    """Extract the main display picture URL of the character."""
    return sel.xpath("//a[contains(@href, 'pictures')]/img").attrib["src"]

def extract_anime_roles(sel):
    """Extract the animes the character appeared in."""
    anime_table = \
        sel.xpath("//div[text() = 'Animeography']/following-sibling::table[1]")
    roles = []
    for row in anime_table.xpath(".//tr"):
        image_sel, text_sel = row.xpath(".//td")
        picture = image_sel.xpath(".//img").attrib["src"]
        title = get_all_text(text_sel.xpath("./a"))
        role = get_all_text(text_sel.xpath("./div"))
        if re.search(r"main", role, flags=re.IGNORECASE):
            role = "main"
        elif re.search(r"support(ing)?|secondary", role, flags=re.IGNORECASE):
            role = "secondary"
        roles.append({"name": title, "picture": picture, "role": role})
    return roles

def extract_manga_roles(sel):
    """Extract the mangas the character appeared in."""
    anime_table = \
        sel.xpath("//div[text() = 'Mangaography']/following-sibling::table[1]")
    roles = []
    for row in anime_table.xpath(".//tr"):
        image_sel, text_sel = row.xpath(".//td")
        picture = image_sel.xpath(".//img").attrib["src"]
        title = get_all_text(text_sel.xpath("./a"))
        role = get_all_text(text_sel.xpath("./div"))
        if re.search(r"main", role, flags=re.IGNORECASE):
            role = "main"
        elif re.search(r"support(ing)?|secondary", role, flags=re.IGNORECASE):
            role = "secondary"
        roles.append({"name": title, "picture": picture, "role": role})
    return roles

def extract_gallery_pictures(sel):
    """Extract the pictures of the character from the gallery."""
    return sel.xpath("//div[@class = 'picSurround']//img/@src").getall()

def extract_metadata_from_file(filename):
    """Extract the metadata from a file."""
    # pylint: disable=broad-except
    try:
        metadata = {
            "sources": ["myanimelist"],
            "filenames": [filename],
            "names": {"en": [], "jp": []},
            "descriptions": [],
            "nicknames": {"en": [], "jp": []},
            "info_fields": [],
            "rankings": [],
            "tags": [],
            "anime_roles": [],
            "manga_roles": [],
            "pictures": {
                "display": [],
                "gallery": []
            }
        }
        profile_filename = filename
        pictures_filename = filename.replace(".html", ".pictures.html")
        if os.path.exists(profile_filename):
            with open_transcoded(filename, "r") as file_obj:
                profile_sel = parsel.Selector(text=file_obj.read().decode("utf8"))
            metadata["names"] = extract_en_jp_name(profile_sel)
            metadata["pictures"]["display"].append(
                extract_main_display_picture(profile_sel)
            )
            metadata["anime_roles"].extend(extract_anime_roles(profile_sel))
            metadata["manga_roles"].extend(extract_manga_roles(profile_sel))
            info_fields, maybe_description = \
                extract_info_fields_and_description(profile_sel)
            metadata["info_fields"].extend(info_fields)
            if maybe_description:
                metadata["descriptions"].append(maybe_description)
        if os.path.exists(pictures_filename):
            with open_transcoded(pictures_filename, "r") as file_obj:
                pictures_sel = parsel.Selector(text=file_obj.read().decode("utf8"))
            metadata["pictures"]["gallery"].extend(
                extract_gallery_pictures(pictures_sel)
            )
        return metadata
    except Exception as ex:
        print(f"Error occured processing: {filename}: {ex}", file=sys.stderr)
        return None

def test_is_male_character(metadata):
    """Test if a character is male."""
    # pylint: disable=invalid-name
    for info_field in metadata["info_fields"]:
        if re.search(r"sex", info_field["key"], flags=re.I) and \
                re.search(r"\b(male|men|man)\b", info_field["value"], flags=re.I):
            return True
    return False

def main(argv=None):
    """Entry point to the myanimelist extractor."""
    argv = argv or sys.argv[1:]
    parser = argparse.ArgumentParser("""Extract content from anime pages.""")
    parser.add_argument("--pages-directory",
                        metavar="PAGES",
                        type=str,
                        required=False)
    parser.add_argument("--output",
                        metavar="OUTPUT",
                        type=argparse.FileType("w", encoding="utf8"),
                        default=sys.stdout)
    parser.add_argument("--no-parallel",
                        action="store_true",
                        help="""Disable parallel processing.""")
    result = parser.parse_args(argv)
    basenames = \
        set(f for f in os.listdir(result.pages_directory) if
            not (f.endswith(".anime.html") or f.endswith(".pictures.html")))
    filenames = \
        list(map(partial(os.path.join, result.pages_directory), basenames))
    with JSONListStream(result.output) as extract_file:
        for metadata in parmap.map(extract_metadata_from_file,
                                   filenames,
                                   pm_pbar=True,
                                   pm_parallel=not result.no_parallel,
                                   pm_chunksize=10):
            if not metadata:
                continue
            if test_is_male_character(metadata):
                print(f"Skipping {metadata['filenames']} as it is a male "
                      f"character.",
                      file=sys.stderr)
                continue
            extract_file.write(metadata)
            result.output.flush()


import cv2
import numpy as np

image = cv2.imread('nature.jpg', 1)
blur = cv2.blur(image, (5, 5))
gaussian = cv2.GaussianBlur(image, (5, 5), 0)
cv2.imshow('org', image)
cv2.imshow('blur', blur)
cv2.imshow('gaussian', gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()


from web.models import Area, Client, User, Feature
from web.helpers.seed_static_data import users, areas, clients, features

"""
"""
class DataSeedHelper():

    def seed_demo_site(self):
        self.create_users()
        self.create_areas()
        self.create_clients()
        self.create_features()
        return users, areas, clients, features

    def create_areas(self):
        for a in areas:
            if Area.is_unique(id=0, name=a):
                area = Area(a)
                area.add(area)

    def create_clients(self):
        for c in clients:
            if Client.is_unique(id=0, name=c):
                client = Client(c)
                client.add(client)

    def create_users(self):
        for key, value in users.items():
            existing_user = User.query.filter_by(name=key).first()
            if existing_user is None:
                user = User(name=key)
                user.check_password_strength_and_hash_if_ok(value)
                user.add(user)

    def create_features(self):
        for f in features:
            title = f['title']
            existing = Feature.query.filter_by(title=title).first()
            if existing is None:
                client_name = f['client']
                client = Client.query.filter_by(name=client_name).first()
                area_name = f['area']
                area = Area.query.filter_by(name=area_name).first()
                feature = Feature(
                    title=title,
                    description=f['description'],
                    target_date=f['target_date'],
                    client_priority=f['client_priority'],
                    client=client,
                    area=area)
                feature.add(feature)


import docker
import pytest
from docker.errors import NotFound

from srsran_controller.common.docker.entity import Entity

TEST_IMAGE_DOCKER = 'ubuntu'
TEST_COMMAND = '/bin/yes'

@pytest.fixture(scope='function')
def container():
    client = docker.from_env()
    return client.containers.run(TEST_IMAGE_DOCKER, TEST_COMMAND, auto_remove=True, detach=True)

def test_shutting_down(container):
    client = docker.from_env()
    entity = Entity(container)
    entity.shutdown()
    with pytest.raises(NotFound):
        assert client.containers.get(container.id)

def test_shutting_down_removed_container(container):
    client = docker.from_env()
    entity = Entity(container)
    container.kill()
    try:
        container.wait(condition='removed')
    except NotFound:
        pass
    entity.shutdown()
    with pytest.raises(NotFound):
        assert client.containers.get(container.id)


#!/usr/bin/env python3
"""Answer for Advent of Code 2019, Day 1."""

from math import floor

def part1():
    """Process list of masses and return total fuel required."""
    fuel = 0
    with open('./py2019/day1/input.txt', 'r') as file:
        for mass in file:
            fuel += floor(int(mass)/3) - 2
    print(fuel)

def part2():
    """Process list of masses and return total fuel required."""
    total_fuel = 0
    with open('./py2019/day1/input.txt', 'r') as file:
        for mass in file:
            module_fuel = floor(int(mass)/3) - 2
            while module_fuel > 0:
                total_fuel += module_fuel
                module_fuel = floor(int(module_fuel)/3) - 2
    print(total_fuel)


import json
import os
from pathlib import Path

import pytest

from never_worse_reduction.mdp import MDP
from never_worse_reduction.mdp_deserialise import read_mdp_from_simple_format_json

def get_test_root() -> Path:
    root_path = Path(__file__).parent

    return root_path

def get_test_input_mdp_by_name(mdp_name: str) -> MDP:
    """
        Given the name of a test input MDP, this function will return that MDP. The name of the MDP corresponds to the
        filename of the MDP json file in the "test_input_mdp" folder, without the .json extension.

    Parameters
    ----------
    mdp_name
        The name of the MDP

    Returns
    -------
    MDP
        The MDP.
    """

    return read_mdp_from_simple_format_json(get_test_root() / Path("test_files/test_input_mdp") / (mdp_name + ".json"))

def get_test_case_by_name(test_case_name: str) -> dict:
    """
        Retrieve the test case with the specified name from the "test_cases" folder.

    Parameters
    ----------
    test_case_name
        This is the filename of the JSON file without the '.json' extension.
    Returns
    -------
        A dict with the contents of the test case JSON file.

    """
    with open(get_test_root() / Path("test_files/test_cases") / (test_case_name + ".json"), 'r') as in_file:
        return json.load(fp=in_file)

def read_json_file(path: Path) -> dict:
    """
        Read the JSON file at the specified path.
    """
    with open(path, 'r') as in_file:
        return json.load(fp=in_file)

def get_labelled_states_from_mdp(mdp: MDP):
    """
        Retrieve the set of states of the specified MDP. Each state is encoded as a string label.
    """

    return mdp.get_state_labels_for_set(state_set=mdp.get_states())

def get_labelled_choices_from_mdp(mdp: MDP):
    """
        Retrieve the set of choices of the specified MDP. Each choice is encoded as a string label.
    """
    return mdp.get_choice_labels_for_set(choice_set=mdp.get_choices())

def get_labelled_forward_transitions_from_mdp(mdp: MDP) -> dict[str, set]:
    """
        Retrieve the forward transitions of the specified MDP. Each state or choice is encoded as a string label. The
        return value is a dict that maps each state or choice to its successors.
    """

    mdp_forward_transitions = dict()

    for state in mdp.get_states():
        state_label = mdp.get_state_label(state_id=state)

        labelled_choices = mdp.get_choice_labels_for_set(mdp.get_choices_for_state(state_id=state))

        mdp_forward_transitions[state_label] = labelled_choices

    for choice in mdp.get_choices():
        choice_label = mdp.get_choice_label(choice_id=choice)

        labelled_states = mdp.get_state_labels_for_set(mdp.get_successors_for_choice(choice_id=choice))

        mdp_forward_transitions[choice_label] = labelled_states

    return mdp_forward_transitions

def get_all_vertices_except_final_and_fail(mdp: MDP) -> set[int]:
    return set(mdp.get_vertices().difference({mdp.get_final_state(), mdp.get_fail_state()}))

def get_names_of_all_test_input_mdp_files() -> list[str]:
    """
        Retrieve a list of all MDPs in the "test_input_mdp" folder.
    """
    return [
        "almost_surely_reach_disconnected_graph",
        "almost_surely_reach_redundant_goals",
        "essential_set_disconnected_never_reaching",
        "essential_set_never_almost_surely_reaching",
        "essential_set_sometimes_almost_surely_reaching",
        "essential_singleton_nontrivial",
        "essential_singleton_trivial_all_nodes_are_essential",
        "extremal_and_mec_collapse_scenario_1_trivial",
        "extremal_and_mec_collapse_scenario_2_complex",
        "extremal_and_mec_collapse_scenario_3_complex",
        "extremal_five_iter_chain",
        "extremal_four_iter_chain",
        "extremal_one_iter_complex",
        "extremal_one_iter_simple_chain",
        "extremal_three_iter_chain",
        "extremal_three_iter_chain_double_connection",
        "extremal_two_iter_complex",
        "inference_almost_surely_reach",
        "inference_as_reach_2",
        "inference_fixpoint_non_trivial_1",
        "inference_fixpoint_trivial",
        "inference_old_essential",
        "inference_old_union_rule",
        "mdp_choice_prune_test_1",
        "mdp_choice_prune_test_2",
        "mdp_choice_prune_test_3",
        "mdp_choice_prune_test_4",
        "mdp_choice_prune_test_5",
        "mdp_choice_prune_test_6",
        "mdp_read_complex",
        "mec_get_scc_multiple",
        "mec_get_scc_no",
        "mec_get_scc_single",
        "mec_illegal_choices_all_illegal",
        "mec_illegal_choices_no_illegal",
        "mec_illegal_choices_some_illegal",
        "mec_multiple_iterations",
        "mec_simple_1_ashokEtAl",
        "mec_simple_2_ashokEtAl_altered",
        "mec_simple_3",
        "mec_simple_4",
        "under_approx_init_essential",
        "under_approx_init_trivial",
    ]


"""
    Module containing tests for the `Pipeline` class
"""

import pathlib

import pytest

from micone.pipelines import Pipeline

@pytest.mark.usefixtures("example_pipelines")
class TestPipeline:
    """ Tests for the `Pipeline` class """

    def test_pipeline_init(self, example_pipeline_files):
        user_settings = example_pipeline_files["grouptaxa_sparcc_json"]
        assert Pipeline(user_settings, profile="local")

    def test_pipeline_len_getitem(self, example_pipeline_files):
        user_settings = example_pipeline_files["grouptaxa_sparcc_json"]
        pipeline = Pipeline(user_settings, profile="local")
        assert len(pipeline) == 6
        process = pipeline["network_inference.network.make_network_with_pvalue.1"]
        assert process == "network_inference.network.make_network_with_pvalue.1"

    @pytest.mark.filterwarnings("ignore::UserWarning")
    def test_pipeline_run(self, example_pipeline_files, tmpdir):
        user_settings = example_pipeline_files["grouptaxa_sparcc_json"]
        pipeline_dir = tmpdir.mkdir("test_pipeline")
        input_dir = pathlib.Path.cwd() / "tests/data"
        pipeline = Pipeline(
            user_settings,
            profile="local",
            output_location=pipeline_dir,
            base_dir=input_dir,
        )
        for process in pipeline.run():
            process.wait()
            process.log()
            assert process.status == "success"


'''
UDP 用户数据报协议 无连接 简单 面向数据报的运输层协议
    不提供可靠性，只是把应用程序传给ip层的数据报飞洒送出去，但并不能保证他们到达目的地
    不需要建立链接，没有超时重发，传输速率很快

UDP特点：udp是面向无连接的通讯协议，udp数据包包括目的端口号和源端口号信息，由于通讯不需要建立连接，所以可以实现广播发送。
    UDP传输数据时有大小限制，必须在64KB之内。
    UDP是一个不可靠的协议发送方锁发送的数据报并不一定以相同的次序达到接收方

UDP适用情况：UDP是面向消息的协议，通信时不需要简历连金，数据的传输自然是不可靠的，UDP一般用于多点通通信和时时业务
    比如：语音广播 视频 qq
        tftp 简单文件传输
        snmp 简单网络管理
        rip 路由信息协议
        dns 域名解释
'''
'''
socket: 操作系统留给编程人员的接口，实现网络通信
获得一个套接字对象：
socket.socket( AF, type ):
    AF:表示链接类型 AF_INET： 互联网通信
                    AF_UNIX： 本机通信
    typ表示类型：   SOCK_DGRAM 数据包服务 udp协议
                    SOCK_STREAM 流式  tcp协
'''
'''
udp服务器：
'''
import socket
# 1 获取一个套接字 互联网通信 udp协议
socket = socket.socket( socket.AF_INET , socket.SOCK_DGRAM )
# 2 为服务器绑定ip和端口
# ip空代表任何ip都可以访问我的服务器 端口号需要指定，在动态地址空间
address = ( "", 8080 )
socket.bind(address)
while True:
    # 3 阻塞等待客户端传来的消息
    # recvfrom() 参数指定一次接收多大数据，返回二元组 数据 和 对面地址
    data , addr = socket.recvfrom(1024)
    if not data :
        break

    print(addr , ":" , data.decode("utf-8"))

    # 4 利用sendto 可以发送消息 传入 数据和 地址
    socket.sendto( data , addr )
socket.close()

01 高级语法、系统、网络、正则/03 python网络编程/01 UDP/00复习/udp.py


"""Convert TFrecords for the Montalbano gesture dataset.

This is used to deal with the Montalbano gesture dataset.
http://gesture.chalearn.org/2013-multi-modal-challenge/
data-2013-challengedataset.

I read the segmentation annotation data from .mat files and segment
the color and depth videos according to this.
I also resize and resample the videos to have smaller size for latter use.

However, the dataset is too large (~40G) for this script to work.
On one hand it's very slow (it takes several hours) and on the other hand
python will run out of memory. Therefore to do the conversion we may
need to do it in several times separately.
"""

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

import os
import sys
import math
import random

import scipy.io
import imageio

import numpy as np
import scipy.misc
import scipy.signal

import tensorflow as tf
from data import dataset_utils

def process_session(basename, num_frames=40,
                    width=100, height=100, grayscale=True):
    """Preprocess the datafiles of one session.

    This includes the segmentation and resampling of intensity
    and depth videos.

    Args:
        basename: The basename of the session.
        num_frames: The number of times frames contained in the
            output videos.
        width: The width of output videos.
        height: The height of output videos.
        grayscale: To return grayscale video or not.

    Returns:
        A list of triples (color video, depth video, label) representing
        information extrated from the files.
        Each color/depth video is a numpy array of dimension 4
        [Height, Width, Time frames, Channels].
    """
    if not os.path.exists(basename + '_data.mat'):
        return None

    metadata = scipy.io.loadmat(basename + '_data.mat')
    label_data = metadata['Video'][0, 0]['Labels']

    if label_data.shape[1] == 0:
        return None

    color_video = imageio.get_reader(basename + '_color.mp4', 'ffmpeg')
    depth_video = imageio.get_reader(basename + '_depth.mp4', 'ffmpeg')
    segmented_data = []

    for i, seg_info in enumerate(label_data[0]):
        label, start_frame, end_frame = seg_info
        extracted_color_video = extract_video(
            color_video, start_frame[0, 0]-1, end_frame[0, 0]-1,
            width, height, grayscale)
        extracted_depth_video = extract_video(
            depth_video, start_frame[0, 0]-1, end_frame[0, 0]-1,
            width, height, grayscale)
        segmented_data.append([
            scipy.signal.resample(extracted_color_video, num_frames, axis=2),
            scipy.signal.resample(extracted_depth_video, num_frames, axis=2),
            label[0],
        ])
    return segmented_data

def extract_video(video_data, start_frame, end_frame,
                  width=100, height=100, grayscale=True):
    """Extract video data from a given frame range.

    Args:
        video_data: An imageio ffmpeg reader instance that contains
            the video data.
        start_frame: The starting frame of the extracted video.
        end_frame: The final frame of the extracted video.
        width: The width of output video.
        height: The height of output video.
        grayscale: To return grayscale video or not.

    Returns:
        A numpy array of dimension 4 [Height, Width, Time frames, Channels]
    """
    channels = video_data.get_data(0).shape[2]
    extracted_video = np.empty(
        (width, height, end_frame-start_frame+1, channels))
    for i in range(start_frame, end_frame+1):
        extracted_video[:, :, i-start_frame, :] = \
            scipy.misc.imresize(video_data.get_data(i), (100, 100))
    if grayscale and channels == 3:
        extracted_video = (
            0.299 * extracted_video[:, :, :, 0]
            + 0.587 * extracted_video[:, :, :, 1]
            + 0.114 * extracted_video[:, :, :, 2])
    return extracted_video

class_names = [
    'vattene', 'vieniqui', 'perfetto', 'furbo', 'cheduepalle',
    'chevuoi', 'daccordo', 'seipazzo', 'combinato', 'freganiente',
    'ok', 'cosatifarei', 'basta', 'prendere', 'noncenepiu',
    'fame', 'tantotempo', 'buonissimo', 'messidaccordo', 'sonostufo',
]

class_names_to_ids = dict(zip(class_names, range(1, 21)))
labels_to_class_names = dict(zip(range(1, 21), class_names))

def to_tfexample(color_video, depth_video, class_id):
    return tf.train.Example(features=tf.train.Features(feature={
        'video/color/data': dataset_utils.float_feature(list(color_video)),
        'video/color/shape': dataset_utils.float_feature(color_video.shape),
        'video/depth/data': dataset_utils.float_feature(list(depth_video)),
        'video/depth/shape': dataset_utils.float_feature(depth_video.shape),
        'video/label': dataset_utils.int64_feature(class_id),
    }))

def get_tfrecord_filename(split_name, tfrecord_dir, shard_id, num_shards):
    output_filename = 'montalbano_%s_%d-of-%d.tfrecord' % (
        split_name, shard_id, num_shards)
    return os.path.join(tfrecord_dir, output_filename)

def convert_dataset(split_name,
                    data_triples,
                    tfrecord_dir,
                    num_shards=10):

    num_per_shard = int(math.ceil(len(data_triples)/float(num_shards)))

    with tf.Graph().as_default():

        for shard_id in range(num_shards):
            output_filename = get_tfrecord_filename(
                split_name, tfrecord_dir, shard_id, num_shards)

            with tf.python_io.TFRecordWriter(output_filename)\
                    as tfrecord_writer:
                start_ndx = shard_id * num_per_shard
                end_ndx = min((shard_id+1)*num_per_shard, len(data_triples))
                for i in range(start_ndx, end_ndx):
                    sys.stdout.write(
                        '\r>> Converting data instance %d/%d shard %s %d' % (
                            i+1, len(data_triples), split_name, shard_id))
                    sys.stdout.flush()

                    color_video, depth_video, class_name = data_triples[i]
                    class_id = class_names_to_ids[class_name]

                    example = to_tfexample(color_video, depth_video, class_id)
                    tfrecord_writer.write(example.SerializeToString())

    sys.stdout.write('\n')
    sys.stdout.flush()

def convert_montalbano(dataset_dir_train,
                       dataset_dir_validation,
                       tfrecord_dir,
                       num_shards=10,
                       width=100,
                       height=100,
                       num_frames=40, grayscale=True):

    if not tf.gfile.Exists(tfrecord_dir):
        tf.gfile.MakeDirs(tfrecord_dir)

    training_data = []

    for i in range(1, 404):
        basename = os.path.join(
            dataset_dir_train,
            'Sample' + '{:5d}'.format(i).replace(' ', '0'))
        sys.stdout.write('\r >> Processing training session %d/403' % i)
        sys.stdout.flush()
        processed_data = process_session(
            basename, num_frames, width, height, grayscale)
        if processed_data is not None:
            training_data.extend(processed_data)
    sys.stdout.write('\n>> Shuffling training session data\n')
    sys.stdout.flush()
    random.shuffle(training_data)
    convert_dataset('train', training_data, tfrecord_dir, num_shards)

    validation_data = []

    for i in range(410, 711):
        basename = os.path.join(
            dataset_dir_validation,
            'Sample' + '{:5d}'.format(i).replace(' ', '0'))
        sys.stdout.write('\r >> Processing validation session %d/301' % i-410)
        sys.stdout.flush()
        processed_data = process_session(
            basename, num_frames, width, height)
        if processed_data is not None:
            validation_data.extend(processed_data)
    sys.stdout.write('\n>> Shuffling validation_data session data\n')
    sys.stdout.flush()
    random.shuffle(validation_data)
    convert_dataset('validation', validation_data, tfrecord_dir, num_shards)

    dataset_utils.write_label_file(labels_to_class_names, tfrecord_dir)
    print('\nFinished converting dataset!')


from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django.forms.models import ModelForm
from django.utils.translation import gettext as _

from .models import Snippet

class SnippetCreateForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'crispy-form-item'
            visible.field.widget.attrs['spellcheck'] = 'false'

        self.fields['body'].widget.attrs['class'] += ' code-snippet'

    class Meta:
        model = Snippet
        fields = ('title', 'description', 'body', 'lang')

    helper = FormHelper()
    helper.add_input(Submit('submit', _('Create'),
                     css_class='btn-primary crispy-form-item'))


import json
import os
from typing import Union

__all__ = ["Settings"]

class SettingsClass:
	def __init__(self,file_location:Union[str,os.PathLike]) -> None:
		self._file_location = file_location
		self._autosave = True
		try:
			open(self._file_location,"x")
			open(self._file_location,"w").write("{}")
		except FileExistsError: pass
		# self._file_handler = open(self._file_location,"r+") # we don't want it getting fucked with while we're working with it
		try:
			with open(self._file_location,"r") as file: settings:dict = json.load(file)
		except json.JSONDecodeError:
			open(self._file_location,"w").write("{}")
			settings = {}
		self._autosave = False
		self.redirect_uri:str = settings.get("redirect_uri","http://127.0.0.1:9090")
		self.client_id:str = settings.get("client_id","")
		self.client_secret:str = settings.get("client_secret","")
		self.added_songs:int = settings.get("added_songs",90)
		self._autosave = True

	def __setattr__(self, __name: str, __value) -> None:
		super().__setattr__(__name,__value)
		if __name[0] == "_" or not self._autosave:
			return
		with open(self._file_location,"r") as file: settings:dict = json.load(file)
		settings.update({k:v for k,v in self.__dict__.items() if k[0] != "_"})
		settings[__name] = __value
		with open(self._file_location,"w") as file: json.dump(settings,file,indent=4)

	def __repr__(self) -> str:
		repr_str = f"<SettingsClass: "
		for key in self:
			repr_str += f"{key}={repr(self.__dict__[key])}, "
		repr_str += f"_file_location={repr(self._file_location)}>"
		return repr_str

	def __iter__(self):
		for i in self.__dict__.keys():
			if i[0] == "_": continue
			yield i

	def __getitem__(self,item):
		if item[0] != "_":
			return self.__dict__[item]
		raise KeyError(repr(item))

	def __setitem__(self,key,value):
		self.__setattr__(key,value)

Settings = SettingsClass("settings.json")

def assert_default_settings(settings:SettingsClass):
	assert settings.added_songs == 90
	assert settings.client_id == ""
	assert settings.client_secret == ""
	assert settings.redirect_uri == "http://127.0.0.1:9090"

def main():
	# i wrote this function because I wanted to feel productive while doing nothing productive at all
	# it did end up making the code a little less shit but not in any meaningful way
	"""
	resets your settings and performs some code tests
	"""
	f = "settings.json"

	# test __init__ from a corrupted state
	with open(f,"w") as file: file.write("{")
	Settings = SettingsClass(f)
	assert_default_settings(Settings)

	# test __init__ from a non-blank state
	Settings = SettingsClass(f)
	assert_default_settings(Settings)

	# test __init__ from a blank state
	os.remove(f)
	Settings = SettingsClass(f)
	assert_default_settings(Settings)

	# test __setattr__
	Settings.added_songs = 45
	assert Settings.added_songs == 45 # seems stupid but considering we do fuck with the __setattr__ function I believe it is a good test

	# test __iter__
	assert 'added_songs' in Settings
	assert '_file_location' not in Settings

	# test __repr__
	print(Settings) # tell me if it looks good, that's about all I can say

	# test __getitem__
	assert Settings['added_songs'] == 45
	try: # can't assert that an error will happen but we can error if it does
		Settings['_file_location']
		raise AssertionError
	except KeyError: pass

	Settings.added_songs = 90 # assuming everything else went well this will set it back to the default value

if __name__ == "__main__":
	main()


class Graph:
    def __init__(self, row, col, g):
        self.graph = g
        self.row = row
        self.col = col

    def isSafe(self, i, j, visited):
        return (i >= 0 and i< self.row and j >= 0 and j < self.col and not visited[i][j] and self.graph[i][j])

    def dfs(self, i, j, visited):
        newrow = [-1,-1,-1,0,0,1,1,1]
        newcol = [-1,0,1,-1,1,-1,0,1]
        visited[i][j] = True
        for k in range(8):
            if self.isSafe(i+newrow[k], j+newcol[k], visited):
                self.dfs(i+newrow[k], j+newcol[k], visited)

    def numsofIslands(self):
        visited = [[False for i in range(self.col)] for j in range(self.row)]
        count = 0
        for i in range(self.row):
            for j in range(self.col):
                if visited[i][j] == False and self.graph[i][j] == 1:
                    self.dfs(i,j, visited)
                    count += 1
        return count
# grid = [[0,1],[1,0],[1,1],[1,0]]
grid = [[1, 1, 0, 0, 0],
        [0, 1, 0, 0, 1],
        [1, 0, 0, 1, 1],
        [0, 0, 0, 0, 0],
        [1, 0, 1, 0, 1]]

graph = Graph(len(grid), len(grid[0]),grid)
print(graph.numsofIslands())


from decimal import Decimal

from leather.scales.base import Scale
from leather.ticks.score import ScoreTicker

class Linear(Scale):
    """
    A scale that linearly maps values from a domain to a range.

    :param domain_min:
        The minimum value of the input domain.
    :param domain_max:
        The maximum value of the input domain.
    """
    def __init__(self, domain_min, domain_max):
        if domain_min > domain_max:
            raise ValueError('Inverted domains are not currently supported.')
        elif domain_min == domain_max:
            # Default to unit scale
            self._data_min = Decimal(0)
            self._data_max = Decimal(1)
        else:
            self._data_min = Decimal(domain_min)
            self._data_max = Decimal(domain_max)

        self._ticker = ScoreTicker(self._data_min, self._data_max)

    def contains(self, v):
        """
        Return :code:`True` if a given value is contained within this scale's
        domain.
        """
        return self._data_min <= v <= self._data_max

    def project(self, value, range_min, range_max):
        """
        Project a value in this scale's domain to a target range.
        """
        value = Decimal(value)
        range_min = Decimal(range_min)
        range_max = Decimal(range_max)

        pos = (value - self._ticker.min) / (self._ticker.max - self._ticker.min)

        return ((range_max - range_min) * pos) + range_min

    def project_interval(self, value, range_min, range_max):
        """
        Project a value in this scale's domain to an interval in the target
        range. This is used for places :class:`.Bars` and :class:`.Columns`.
        """
        raise NotImplementedError

    def ticks(self):
        """
        Generate a series of ticks for this scale.
        """
        return self._ticker.ticks


# 정사각형 방

def BFS(s):
    q = [(s[0], s[1])]
    room[arr[s[0]][s[1]]] = 0
    cnt = 1
    while q:
        i, j = q.pop(0)
        for di, dj in zip([-1, 1, 0, 0], [0, 0, -1, 1]):
            ni, nj = i + di, j + dj
            if 0 <= ni < N and 0 <= nj < N and arr[i][j] + 1 == arr[ni][nj]:
                q.append((ni, nj))
                room[arr[ni][nj]] = 0
                cnt += 1
    # 방문한 방 횟수
    return cnt

for t in range(int(input())):
    N = int(input())
    arr = [list(map(int, input().split())) for _ in range(N)]
    room = [0] * (N ** 2 + 1)
    for i in range(N):
        for j in range(N):
            room[arr[i][j]] = (i, j)
    num = N * N
    sol = 0
    for i in range(1, len(room)):
        if room[i]:
            temp = BFS(room[i])
            if sol < temp:
                sol = temp
                num = i

    print(f'#{t + 1} {num} {sol}')

'''
# 시간초과 풀이
# 최대 수행 횟수가 10^6 이기 때문에 중복 방문 예외처리 안 해주면 시간 초과

def bfs(i, j):
    global vst
    q.append((i, j))
    vst = [[0] * N for _ in range(N)]
    vst[i][j] = 1
    sol = 0
    while q:
        i, j = q.pop(0)
        for di, dj in zip([-1, 1, 0, 0], [0, 0, -1, 1]):
            ni, nj = i + di, j + dj
            if 0 <= ni < N and 0 <= nj < N and not vst[ni][nj]:
                if arr[ni][nj] - arr[i][j] == 1:
                    q.append((ni, nj))
                    vst[ni][nj] = vst[i][j] + 1
    return vst[i][j]

for t in range(int(input())):
    N = int(input())
    arr = [[0] * N for _ in range(N)]
    q = []
    sols = []
    for i in range(N):
        lst = list(map(int, input().split()))
        for j in range(N):
            arr[i][j] = lst[j]
    # 모든 방에서 bfs 탐색
    for i in range(N):
        for j in range(N):
            sols.append(bfs(i, j))
    # 그 중 방문횟수 최댓값을 sol1에 저장
    sol1 = max(sols)
    # 방문횟수 최댓값이 여러개라면
    if sols.count(sol1) > 1:
        temp1 = []
        for n in range(len(sols)):
            if sols[n] == sol1:
                temp1.append(n)
        sol2 = 10 ** 10
        for n in temp1:
            sol2 = min(sol2, arr[n//N][n%N])
    # 방문 최댓값이 한개라면 바로 방번호 sol2에 저장
    else:
        idx = sols.index(sol1)
        ii = idx // N
        jj = idx % N
        sol2 = arr[ii][jj]

    print(f'#{t + 1} {sol2} {sol1}')
'''


import json
import re
import time
import sys
import datetime
from rmaker_admin_lib import configmanager
from rmaker_admin_lib.exceptions import SSLError,\
    NetworkError,\
    RequestTimeoutError,\
    InvalidJSONError
from rmaker_admin_lib.logger import log
try:
    import requests
    from requests.exceptions import RequestException
except ImportError as err:
    log.error('{}\nPlease run `pip install -r '
              'requirements.txt`\n'.format(err))
    sys.exit(1)

from rmaker_admin_lib import constants

MAX_HTTP_CONNECTION_RETRIES = 5

class Node_Mfg:
    """
    Node_Mfg class used to instantiate instances to perform various
    node manufacturing operations.

    :param token: User token
    :type token: str
    """

    def __init__(self, token):
        """
        Instantiate node_mfg with user token (session)
        """
        self.request_header = {'content-type': 'application/json',
                               'Authorization': token}
        log.debug("Node Mfg request header: {}".format(self.request_header))

    def get_node_id_req(self, node_cnt, expected_resp=None):
        '''
        Request to get list of node ids

        :param node_cnt: Number of node ids to get
        :type node_cnt: int

        :param expected_resp: Data expected to be present in response
        :type expected_resp: str
        '''
        try:
            backslash = '/'
            retry_cnt = MAX_HTTP_CONNECTION_RETRIES
            log.debug("Retry count set to: {}".format(retry_cnt))
            err_status_key = "status"
            err_description_key = "description"

            path = 'admin/node_ids'
            request_url = constants.HOST.rstrip(backslash) + backslash + path
            request_body = {
                'node_count': node_cnt
            }
            log.debug('Request parameters set: request_url: {} '
                      'request_body: {}'.format(
                          request_url,
                          request_body))

            while retry_cnt > 0:
                try:
                    log.debug("Retry Count: {}".format(retry_cnt))
                    # Send HTTP POST Request
                    log.debug('Sending HTTP {} request - url: {} data: {} '
                              'headers: {}'.format(
                                  'post',
                                  request_url,
                                  json.dumps(request_body),
                                  self.request_header))
                    response = requests.post(url=request_url,
                                             data=json.dumps(request_body),
                                             headers=self.request_header,
                                             verify=configmanager.CERT_FILE,
                                             timeout=(5.0, 5.0))
                    log.debug('Response status code '
                              'received: {}'.format(response.status_code))
                    response = json.loads(response.text)
                    log.debug("HTTP Response received: {}".format(response))

                    if expected_resp in response:
                        log.debug('Expected response {} exists '
                                  'in response'.format(expected_resp))
                        return response[expected_resp]
                    elif err_status_key in response:
                        log.debug('Request failed: '
                                  'response received: {}'.format(response))
                        log.error("Status: {} \n{}".format(
                            response[err_status_key],
                            response[err_description_key]))
                        return None
                except NetworkError as net_err:
                    log.info(net_err)
                except RequestTimeoutError as req_err:
                    log.info(req_err)

                log.info('Retrying...No of retries left: {}'.format(
                    str(retry_cnt - 1))
                )
                retry_cnt -= 1
                time.sleep(5)
            return None
        except SSLError as ssl_err:
            log.error(ssl_err)
        except Exception as err:
            raise Exception(err)

    def gen_node_id(self, node_count):
        '''
        Generate list of node ids

        :param node_cnt: Number of node ids to get
        :type node_cnt: int
        '''
        try:
            backslash = '/'
            req_id = None
            log.info("Sending request for generating node ids: {}".format(
                node_count)
            )
            expected_resp = 'request_id'
            log.debug('Sending request for node ids for number of nodes: {}. '
                      'Expected response: {}'.format(
                          node_count,
                          expected_resp))
            req_id = self.get_node_id_req(
                node_count,
                expected_resp=expected_resp)
            if not req_id:
                log.debug("Expected response: {} not found.".format(
                    expected_resp))
                return False
            log.info('Request for generating '
                     'node ids: {} is successful'.format(node_count))

            retry_cnt = MAX_HTTP_CONNECTION_RETRIES
            expected_key_in_resp = "url"

            path = 'admin/node_ids'
            query_params = {'request_id': req_id}
            request_url = constants.HOST.rstrip(backslash) + backslash + path

            log.debug('Sending GET HTTP Request - request url: {} '
                      'query params: {}'.format(request_url, query_params))

            log.info('Sending request for getting {} '
                     'for node ids file'.format(expected_key_in_resp))

            while retry_cnt > 0:
                try:
                    log.debug('Sending HTTP {} request - url: {} params: {} '
                              'headers: {}'.format(
                                  'get',
                                  request_url,
                                  query_params,
                                  self.request_header))
                    # Send query params in HTTP GET Request
                    response = requests.get(url=request_url,
                                            params=query_params,
                                            headers=self.request_header,
                                            verify=configmanager.CERT_FILE,
                                            timeout=(5.0, 5.0))
                    log.debug("Response status code received: {}".format(
                        response.status_code))
                    response = json.loads(response.text)
                    log.debug("Response received: {}".format(response))

                    curr_time = time.time()
                    timestamp = datetime.datetime.fromtimestamp(
                        curr_time).strftime('%H:%M:%S')
                    log.info("[{:<6}] Current status: {:<3}".format(
                        timestamp,
                        response['status']))
                    if 'success' in response['status']:
                        log.debug("Response status: {}".format(
                            response['status']))
                        if expected_key_in_resp not in response:
                            raise InvalidJSONError
                        log.info("Node ids file url received successfully")
                        log.debug("URL received: {}".format(
                            response[expected_key_in_resp]))
                        return response[expected_key_in_resp]
                    elif 'in_progress' in response['status']:
                        time.sleep(5)
                        continue
                    elif 'failure' in response['status']:
                        log.debug('Request failed: response '
                                  'received: {}'.format(response))
                        log.error("Status: {} \n {}".format(
                            response['status'],
                            response['description']))
                        return None

                except NetworkError as net_err:
                    log.error(net_err)
                except RequestTimeoutError as req_err:
                    log.error(req_err)

                log.info('Retrying...No of retries left: {}'.format(
                    str(retry_cnt - 1)))
                retry_cnt -= 1
                time.sleep(5)

            return None

        except InvalidJSONError as json_err:
            log.error(json_err)
        except SSLError as ssl_err:
            log.error(ssl_err)
        except Exception as err:
            raise Exception(err)

    def get_cert_upload_url(self, cert_filename):
        '''
        Get URL to upload device certificates

        :param cert_filename: Filename having certifcates
        :type cert_filename: str
        '''
        try:
            backslash = '/'
            log.info("Get URL for uploading certificates file")
            retry_cnt = MAX_HTTP_CONNECTION_RETRIES
            expected_key_in_resp = "url"
            err_status_key = "status"

            path = 'admin/node_certificates/register'
            query_params = {'file_name': cert_filename}
            request_url = constants.HOST.rstrip(backslash) + backslash + path

            log.debug('Sending request to get URL '
                      'for uploading certificates file '
                      '- url: {} params: {}'.format(request_url, query_params))

            while retry_cnt > 0:
                try:
                    log.debug('Sending HTTP {} request - url: {} params: {} '
                              'headers: {}'.format(
                                  'get',
                                  request_url,
                                  query_params,
                                  self.request_header))
                    # Send query params in HTTP GET Request
                    response = requests.get(url=request_url,
                                            params=query_params,
                                            headers=self.request_header,
                                            verify=configmanager.CERT_FILE,
                                            timeout=(5.0, 5.0))
                    log.debug("Response status code received: {}".format(
                        response.status_code))
                    response = json.loads(response.text)
                    log.debug("Response received: {}".format(response))

                    if expected_key_in_resp in response:
                        log.debug('Expected key: {} present in '
                                  'response: {}'.format(
                                      expected_key_in_resp,
                                      response))
                        log.info('URL for uploading certificates file '
                                 'received successfully')
                        log.debug("URL received: {}".format(
                            response[expected_key_in_resp]))
                        return response[expected_key_in_resp]
                    elif err_status_key in response:
                        log.debug('Request failed: response '
                                  'received: {}'.format(response))
                        log.error("Status: {} \n {}".format(
                            response[err_status_key],
                            response['description']))
                        return None

                except NetworkError as net_err:
                    log.error(net_err)
                except RequestTimeoutError as req_err:
                    log.error(req_err)

                log.info('Retrying...No of retries '
                         'left: {}'.format(str(retry_cnt - 1)))
                retry_cnt -= 1
                time.sleep(5)

            return None

        except SSLError as ssl_err:
            log.error(ssl_err)
        except Exception as req_exc_err:
            raise Exception(req_exc_err)

    def validate_groupnames(self, group_name, parent_group_id):
        '''
        API to validate the following things:
        Parent group (if provided) is level 0 group, if it exists.
        Group is level 0 group if no parent group is provided.

        :param group_name: Group name to be validated
        :type group_name: str
        :param parent_group_id: parent_group_id
        :type parent_group_id: str
        return values:
        is_present: str
        group_id: str
        '''
        try:
            backslash = '/'
            log.info("Validating Group Names")

            path = 'admin/node_group'
            query_params = {'group_name': group_name}
            request_url = constants.HOST.rstrip(backslash) + backslash + path
            log.debug("Get admin node group - url: {} params: {}".format(
                request_url,
                query_params))

            log.debug('Sending HTTP {} request - url: {} params: {} '
                      'headers: {}'.format(
                          'get',
                          request_url,
                          query_params,
                          self.request_header))
            # Send query params in HTTP GET Request
            response = requests.get(url=request_url,
                                    params=query_params,
                                    headers=self.request_header,
                                    verify=configmanager.CERT_FILE,
                                    timeout=(5.0, 5.0))

            log.debug("Response status code received: {}".format(
                response.status_code))
            response = json.loads(response.text)
            log.debug("Response received: {}".format(response))

            if "status" in response:
                if 'failure' in response['status']:
                    log.debug('Request failed: '
                              'Response received: {}'.format(response))
                    return False, ""

            is_present = False
            group_id = ""
            subGroupId = ""
            for group_response in response:
                is_present = True
                if "parent_group_id" in group_response:
                    parentId = group_response["parent_group_id"]
                    if parent_group_id and parent_group_id == parentId:
                        subGroupId = group_response["group_id"]
                elif "group_id" in group_response:
                    group_id = group_response["group_id"]
            if subGroupId:
                 group_id = subGroupId
            return is_present, group_id
        except SSLError as ssl_err:
            log.error(ssl_err)
        except NetworkError as net_err:
            log.error(net_err)
        except RequestTimeoutError as req_err:
            log.error(req_err)
        except Exception as req_exc_err:
            raise Exception(req_exc_err)

    def register_cert_req(self, filename, md5_checksum, refresh_token, node_type, model, group_name,parent_group_id,parent_group_name,subtype,tags,
                          expected_resp='request_id'):
        '''
        Request to register device certificates

        :param filename: Filename of uploaded device certificates file
        :type filename: str

        :param md5_checksum: MD5 Checksum of file
        :type md5_checksum: str

        :param expected_resp: Data expected to be present in response
        :type expected_resp: str
        '''
        try:
            backslash = '/'
            log.debug("Sending request to register certificate")
            retry_cnt = MAX_HTTP_CONNECTION_RETRIES
            err_status_key = "status"

            path = 'admin/node_certificates/register'
            request_url = constants.HOST.rstrip(backslash) + backslash + path
            request_body = {
                'file_name': filename,
                'file_md5': md5_checksum,
                'refresh_token': refresh_token,
                'group_name': group_name,
                'type': node_type,
                'model': model,
                'parent_group_id':parent_group_id,
                'parent_group_name':parent_group_name,
                'subtype':subtype,
                'tags':tags,
            }
            log.debug('Register Certificate Request - url: {} '
                      'req_body: {}'.format(request_url, request_body))

            while retry_cnt > 0:
                try:
                    # Send HTTP POST Request
                    log.debug('Sending HTTP {} request - url: {} data: {} '
                              'headers: {}'.format(
                                  'post',
                                  request_url,
                                  json.dumps(request_body),
                                  self.request_header))
                    response = requests.post(url=request_url,
                                             data=json.dumps(request_body),
                                             headers=self.request_header,
                                             verify=configmanager.CERT_FILE,
                                             timeout=(5.0, 5.0))
                    log.debug('Response status code received: {}'.format(
                        response.status_code))
                    response = json.loads(response.text)
                    log.debug("Response received: {}".format(response))

                    if 'status' in response:
                        log.debug("Response status: {}".format(
                            response['status']))
                        if 'success' in response['status']:
                            log.debug('Expected response {} '
                                      'received in response: {}'.format(
                                          expected_resp,
                                          response))
                            log.debug("Expected response: {}".format(
                                response[expected_resp]))
                            return response[expected_resp]
                        elif 'failure' in response['status']:
                            log.debug('Request failed: '
                                      'Response received: {}'.format(
                                          response))
                            log.error('Status: {} \n {}'.format(
                                response[err_status_key],
                                response['description']))
                            return False

                except NetworkError as net_err:
                    log.error(net_err)
                except RequestTimeoutError as req_err:
                    log.error(req_err)

                log.info("Current status: {}".format(response['status']))
                log.debug("Retrying...No of retries left: {}".format(
                    str(retry_cnt - 1)))
                retry_cnt -= 1
                time.sleep(5)

            return None

        except SSLError as ssl_err:
            log.error(ssl_err)
        except Exception as req_exc_err:
            raise Exception(req_exc_err)

    def get_register_cert_status(self, request_id):
        '''
        Register device certificates

        :param request_id: Request Id of device
                           certificate registration request
        :type request_id: str
        '''
        try:
            backslash = '/'
            log.info("Getting device certificate registration status")

            path = 'admin/node_certificates/register'
            query_params = {'request_id': request_id}
            request_url = constants.HOST.rstrip(backslash) + backslash + path
            log.debug("Register Certificate - url: {} params: {}".format(
                request_url,
                query_params))

            log.debug('Sending HTTP {} request - url: {} params: {} '
                      'headers: {}'.format(
                          'get',
                          request_url,
                          query_params,
                          self.request_header))
            # Send query params in HTTP GET Request
            response = requests.get(url=request_url,
                                    params=query_params,
                                    headers=self.request_header,
                                    verify=configmanager.CERT_FILE,
                                    timeout=(5.0, 5.0))
            log.debug("Response status code received: {}".format(
                response.status_code))
            response = json.loads(response.text)
            log.debug("Response received: {}".format(response))

            log.debug("Current status: {:<3}".format(response['status']))

            return response['status']

        except SSLError as ssl_err:
            log.error(ssl_err)
        except NetworkError as net_err:
            log.error(net_err)
        except RequestTimeoutError as req_err:
            log.error(req_err)
        except Exception as req_exc_err:
            raise Exception(req_exc_err)

    def get_mqtthostname(self,is_local):
        '''
        Get MQTT Hostname
        '''
        try:
            backslash = '/'
            log.info("Sending request to get mqtt endpoint")
            retry_cnt = MAX_HTTP_CONNECTION_RETRIES
            #If is_local is true or input node_ids.cs file is given, we get MQTT endpoint via unauthenticated API.
            if is_local:
                path = 'mqtt_host'
                host = constants.API_URL
            else:
                path = 'admin/mqtt_host'
                host = constants.HOST

            request_url = host.rstrip(backslash) + backslash + path
            log.debug("Get MQTT hostname - url: {}".format(request_url))

            while retry_cnt > 0:
                log.debug('Sending HTTP {} request - url: {} '
                          'headers: {}'.format(
                              'get',
                              request_url,
                              self.request_header))
                response = requests.get(url=request_url,
                                        headers=self.request_header,
                                        verify=configmanager.CERT_FILE,
                                        timeout=(5.0, 5.0))
                log.debug("Response status code received: {}".format(
                    response.status_code))
                response = json.loads(response.text)
                log.debug("Response received: {}".format(response))

                if 'mqtt_host' in response:
                    log.debug("Response mqtt hostname: {}".format(
                        response['mqtt_host']))
                    return response['mqtt_host']

                log.info("Retrying...No of retries left: {}".format(
                    str(retry_cnt - 1)))
                retry_cnt -= 1
                time.sleep(5)

            return False

        except SSLError as ssl_err:
            log.error(ssl_err)
        except NetworkError as net_err:
            log.error(net_err)
        except RequestTimeoutError as req_err:
            log.error(req_err)
        except RequestException as err:
            log.error(err)
        except Exception as req_exc_err:
            raise Exception(req_exc_err)


from flask import Flask, render_template, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

from config import Config
from models import db

from log_req import logined

app = Flask(__name__)
app.config.from_object(Config)

db.init_app(app)
migrate = Migrate(app, db)

from api import bp as api_bp

@app.route('/')
@logined
def game():
	return render_template('index.html', name = session.get('username'))

@app.route('/login', methods = ['GET', 'POST'])
def login():
	if request.method == 'POST':

		name = request.json.get('name')
		session['log'] = True
		session['username'] = name
	return render_template('login.html')

app.register_blueprint(api_bp, url_prefix = '/api')

if __name__ == '__main__':
	app.run(debug = True, port=4000)


## https://leetcode.com/problems/range-sum-query-2d-mutable/

from typing import List

class NumMatrix:
    matrix = []
    R, C = 0, 0

    def __init__(self, matrix: List[List[int]]):
        self.matrix = matrix
        self.R, self.C = len(matrix), len(matrix[0])

        self.partial_sum = [[] for _ in range(self.R)]
        for row in range(self.R):
            for col in range(self.C):
                if col == 0:
                    self.partial_sum[row].append(matrix[row][0])
                else:
                    self.partial_sum[row].append(self.partial_sum[row][-1] + matrix[row][col])

        # for row in range(self.R):
        #     print(self.partial_sum[row])

    def update(self, row: int, col: int, val: int) -> None:
        pre_val = self.matrix[row][col]
        self.matrix[row][col] = val
        diff = val - pre_val
        for c in range(col, self.C):
            self.partial_sum[row][c] += diff

        # for r in range(self.R):
        #     print(self.partial_sum[r])

        return

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        result = 0
        for r in range(row1, row2 + 1):
            if col1 == 0:
                result += self.partial_sum[r][col2]
            else:
                result += self.partial_sum[r][col2] - self.partial_sum[r][col1 - 1]

        # print(result)

        return result

# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# obj.update(row,col,val)
# param_2 = obj.sumRegion(row1,col1,row2,col2)

obj = NumMatrix([[3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5], [4, 1, 0, 1, 7], [1, 0, 3, 0, 5]])
obj.sumRegion(2,1,4,3)
obj.update(3,2,2)
obj.sumRegion(2,1,4,3)


'''
[PGS] 영어가 싫어요/03/오슬기

0부터 9까지 담은 arr을 만든 뒤 문자열과 그 숫자에 해당하는
arr의 인덱스를 치환

'''

def solution(numbers):
    arr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    for i in range(len(arr)):
        numbers = numbers.replace(arr[i], str(arr.index(arr[i])))
    return int(numbers)


from ..core import magnitude, Timer
from ..subcommand import SubcommandABC
from ..log import getLogger, setLogFile

_logger = getLogger(__name__)

class Result():
    def __init__(self, analysis_name, field_name, energy_data, emissions_data, error=None):
        self.analysis_name = analysis_name
        self.field_name = field_name
        self.energy = energy_data
        self.emissions = emissions_data
        self.error = error

    def __str__(self):
        return f"<Result analysis:{self.analysis_name} field:{self.field_name} error:{self.error}>"

def total_emissions(proc, gwp):
    rates = proc.emissions.rates(gwp)
    total = rates.loc["GHG"].sum()
    return magnitude(total)

def energy_and_emissions(field, gwp):
    import pandas as pd

    procs = field.processes()
    energy_by_proc = {proc.name: magnitude(proc.energy.rates().sum()) for proc in procs}
    energy_data = pd.Series(energy_by_proc, name=field.name)

    emissions_by_proc = {proc.name: total_emissions(proc, gwp) for proc in procs}
    emissions_data = pd.Series(emissions_by_proc, name=field.name)
    return energy_data, emissions_data

def run_field(analysis_name, field_name, xml_string):
    from ..config import setParam
    from ..model_file import ModelFile

    setParam('OPGEE.XmlSavePathname', '')  # avoid writing /tmp/final.xml since no need

    try:
        mf = ModelFile.from_xml_string(xml_string, add_stream_components=False,
                                       use_class_path=False,
                                       use_default_model=True,
                                       analysis_names=[analysis_name],
                                       field_names=[field_name])

        analysis = mf.model.get_analysis(analysis_name)
        field = analysis.get_field(field_name)

        field.run(analysis)
        energy_data, emissions_data = energy_and_emissions(field, analysis.gwp)
        result = Result(analysis_name, field_name, energy_data, emissions_data)

    except Exception as e:
        result = Result(analysis_name, field_name, None, None, error=str(e))

    return result

def run_parallel(model_xml_file, analysis_name, field_names, max_results=None):
    from dask.distributed import as_completed

    from ..model_file import extracted_model
    from ..mcs.distributed_mcs_dask import Manager

    mgr = Manager(cluster_type='local')

    timer = Timer('run_parallel').start()

    # Put the log for the monitor process in the simulation directory.
    # Workers will set the log file to within the directory for the
    # field it's currently running.
    # log_file = f"{sim_dir}/opgee-mcs.log"
    # setLogFile(log_file, remove_old_file=True)

    # N.B. start_cluster saves client in self.client and returns it as well
    client = mgr.start_cluster()

    futures = []

    # Submit all the fields to run on worker tasks
    for field_name, xml_string in extracted_model(model_xml_file, analysis_name,
                                                  field_names=field_names,
                                                  as_string=True):

        future = client.submit(run_field, analysis_name, field_name, xml_string)
        futures.append(future)

    energy_cols = []
    emission_cols = []
    errors = []

    count = 0   # used if we're returning results in batches

    # Wait for and process results
    for future, result in as_completed(futures, with_results=True):
        if max_results and count == 0:
            energy_cols.clear()
            emission_cols.clear()
            errors.clear()

        if result.error:
            _logger.error(f"Failed: {result}")
            errors.append(result)
        else:
            _logger.debug(f"Succeeded: {result}")

            energy_cols.append(result.energy)
            emission_cols.append(result.emissions)

        if max_results:
            if count == max_results:
                count = 0
                # return the next batch
                yield energy_cols, emission_cols, errors
            else:
                count += 1

    _logger.debug("Workers finished")

    mgr.stop_cluster()
    _logger.info(timer.stop())

    if count:
        yield energy_cols, emission_cols, errors
    else:
        return energy_cols, emission_cols, errors

def run_serial(model_xml_file, analysis_name, field_names):
    from ..model_file import extracted_model

    timer = Timer('run_serial').start()

    energy_cols = []
    emission_cols = []
    errors = []

    for field_name, xml_string in extracted_model(model_xml_file, analysis_name,
                                                  field_names=field_names,
                                                  as_string=True):
        result = run_field(analysis_name, field_name, xml_string)
        if result.error:
            _logger.error(f"Failed: {result}")
            errors.append(result)
        else:
            energy_cols.append(result.energy)
            emission_cols.append(result.emissions)

    _logger.info(timer.stop())
    return energy_cols, emission_cols, errors

class RunManyCommand(SubcommandABC):

    def __init__(self, subparsers):
        kwargs = {'help' : '''Run thousands of fields in parallel using dask.'''}
        super().__init__('runmany', subparsers, kwargs, group='project')

    def addArgs(self, parser):
        from ..utils import ParseCommaList

        parser.add_argument('-a', '--analysis',
                            help='''The name to give the <Analysis> element. Default is the file basename
                            with the extension removed. Default is the first analyses found in the given
                            model XML file.''')

        parser.add_argument('-b', '--batch-start', default=0, type=int,
                            help='''The value to use to start numbering batch result files.
                            Default is zero. Ignored unless -N/--save-after is specified.''')

        parser.add_argument('-f', '--fields', action=ParseCommaList,
                            help=f'''A comma-delimited list of fields to run. Default is to
                            run all fields indicated in the named analysis.''')

        parser.add_argument('-m', '--model', required=True,
                            help=f'''[Required] The pathname of a model XML file to process.''')

        parser.add_argument('-N', '--save-after', type=int,
                            help='''Write a results to a new file after the given number of results are
                            returned. Implies --parallel.''')

        parser.add_argument('-n', '--count', type=int, default=0,
                            help='''The number of fields to run from the named analysis.
                            Default is 0, which means run all fields.''')

        parser.add_argument('-o', '--output', required=True,
                            help='''[Required] The pathname of the CSV files to create containing energy and
                            emissions results for each field. This argument is used as a basename,
                            with the suffix '.csv' replaced by '-energy.csv' and '-emissions.csv' to
                            store the results. Each file has fields in columns and processes in rows.''')

        parser.add_argument('-p', '--parallel', action='store_true',
                            help='''Run the fields in parallel locally using dask.''')

        parser.add_argument('-S', '--start-with',
                            help='''The name of a field to start with. Use this to resume a run after a failure.
                            Can be combined with -n/--count to run a large number of fields in smaller batches.''')

        parser.add_argument('-s', '--skip-fields', action=ParseCommaList,
                            help='''Comma-delimited list of field names to exclude from analysis''')

        return parser

    def run(self, args, tool):
        import os
        import pandas as pd
        from ..config import getParam, setParam, pathjoin
        from ..error import CommandlineError
        from ..model_file import analysis_names, fields_for_analysis

        setParam('OPGEE.XmlSavePathname', '')   # avoid writing /tmp/final.xml since no need

        model_xml_file = args.model

        analysis_name = args.analysis or analysis_names(model_xml_file)[0]
        all_fields = fields_for_analysis(model_xml_file, analysis_name)

        field_names = [name.strip() for name in args.fields] if args.fields else None
        if field_names:
            unknown = set(field_names) - set(all_fields)
            if unknown:
                raise CommandlineError(f"Fields not found in {model_xml_file}: {unknown}")
        else:
            field_names = all_fields

        if args.start_with:
            # skip all before the named field
            i = field_names.index(args.start_with)
            field_names = field_names[i:]

        if args.count:
            field_names = field_names[:args.count]

        skip = args.skip_fields
        if skip:
            field_names = [name.strip() for name in field_names if name not in skip]

        def _save_cols(columns, csvpath):
            df = pd.concat(columns, axis='columns')
            df.index.name = 'process'
            df.sort_index(axis='rows', inplace=True)

            print(f"Writing '{csvpath}'")
            df.to_csv(csvpath)

        def _save_errors(errors, csvpath):
            """Save a description of all field run errors"""
            with open(csvpath, 'w') as f:
                f.write('analysis,field,error\n')
                for result in errors:
                    f.write(f"{result.analysis_name},{result.field_name},{result.error}\n")

        temp_dir = getParam('OPGEE.TempDir')
        dir_name, filename = os.path.split(args.output)
        subdir = pathjoin(temp_dir, dir_name)
        basename, ext = os.path.splitext(filename)

        save_after = args.save_after
        if save_after:
            batch = args.batch_start   # used in naming result files

            for vectors in run_parallel(model_xml_file, analysis_name, field_names,
                                        max_results=save_after):

                energy_cols, emissions_cols, errors = vectors

                _save_cols(energy_cols,    pathjoin(subdir, f"{basename}-energy-{batch}{ext}"))
                _save_cols(emissions_cols, pathjoin(subdir, f"{basename}-emissions-{batch}{ext}"))
                _save_errors(errors,       pathjoin(subdir, f"{basename}-errors-{batch}.csv"))

                batch += 1

        else:
            # If not running in batches, save all results at the end
            run_func = run_parallel if args.parallel else run_serial
            energy_cols, emissions_cols, errors = run_func(model_xml_file, analysis_name,
                                                           field_names)

            # Insert "-energy" or "-emissions" between basename and extension
            _save_cols(energy_cols,    pathjoin(subdir, f"{basename}-energy{ext}"))
            _save_cols(emissions_cols, pathjoin(subdir, f"{basename}-emissions{ext}"))
            _save_errors(errors,       pathjoin(subdir, f"{basename}-errors.csv"))


from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

base = declarative_base()

# Defining the structure of our Movie object
class Movie(base):
    __tablename__ = 'movies' # Tell SQL Alchemy what the table name will be
    # Here we define columns for the table movies
    id = Column(Integer, primary_key=True, autoincrement=True, unique=True)
    title = Column(String(250), nullable=False)
    poster_url = Column(String(250), nullable=False)
    trailer_url = Column(String(250), nullable=False)

# Retrieves all movies from the database
def get_movies():
    session = dbsession()

    movies = session.query(Movie).all()

    return movies

# Adds a new movie to the database
def create_movie(title, poster_url, trailer_url):
    session = dbsession()
    session.add(Movie(title=title, poster_url=poster_url, trailer_url=trailer_url))
    session.commit()

# Opens a new session to the database. Extracted to its own function to keep things DRY.
def dbsession():
    engine = create_engine('sqlite:///movies.sqlite')
    base.metadata.bind = engine

    session = sessionmaker()
    session.bind = engine
    session = session()

    return session

# Create an engine that stores data in the local directory's
# movies.sqlite file.
engine = create_engine('sqlite:///movies.sqlite')

# Create all tables in the engine.
base.metadata.create_all(engine)


# testing files from murtaza
import cv2
import numpy as np

cap = cv2.VideoCapture(1)

while True:
    _, img = cap.read()
    print(img.shape)
    imgRes1 = cv2.resize(img, (500, 200))
    imgRes2 = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    #print(imgRes2.shape)
    imgCrop = img[100:200, 200:300]

    cv2.imshow("Image1",imgRes1)
    cv2.imshow("Image2",imgRes2)
    cv2.imshow("Crop", imgCrop)

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

cv2.destroyAllWindows()


import cv2

class QuestaoMultiplaEscolha:
    def __init__(self, dados):
        self.questao = dados[0]
        self.alternativa_1 = dados[1]
        self.alternativa_2 = dados[2]
        self.alternativa_3 = dados[3]
        self.alternativa_4 = dados[4]
        self.resposta = int(dados[5])
        self.resposta_usuario = None

    def atualizar(self, cursor, caixas_delimitadoras, imagem):
        for x, caixa_delimitadora in enumerate(caixas_delimitadoras):
            x1, y1, x2, y2 = caixa_delimitadora
            if x1 < cursor[0] < x2 and y1 < cursor[1] < y2:
                self.resposta_usuario = x+1
                cv2.rectangle(imagem, (x1, y1), (x2, y2), (0, 255, 0), cv2.FILLED)


# built in
import os

# 3rd party modules
import cv2

'''

    probably needs serious rework, the scales/cropings seem super specific to the sample p&id

'''
def crop_image ( file_path , cut_fldr ) :
    files = os.listdir ( file_path ) # list all the files ' names
    counter = 0
    for file in files :
        # print ( file )
        original_image_name = os.path.join ( file_path , file )
        # dest_image_name = os. path.join ( cut_fldr , file )
        dest_image_name = cut_fldr + str( counter ) + ".png"
        if os.path.isfile ( original_image_name ) :
            #print ( original_image_name )
            original_image = cv2.imread ( original_image_name )
            shape = original_image.shape
            h = shape [0] # Y ( height ) = 1786
            w = shape [1] # X ( width ) = 2526
            #print ("h = ", h )
            #print ("w = ", w )
            cropped_y_start = int( h * 0.1) # y start
            cropped_y_end = int( h * 0.95) # y end
            cropped_x_start = int( w * 0.05) # x start
            cropped_x_end = int( w * 0.85) # x end
            cropped_img = original_image [ cropped_y_start : cropped_y_end ,
                                           cropped_x_start : cropped_x_end ] # crop
            # print ( dest_image_name )
            # cv2. imshow ( ' cropped ', cropped_img )
            # cv2. waitKey (0)

            cv2.imwrite(dest_image_name,cropped_img ) #
            counter +=1
            # print ( dest_image_name )


import tkinter
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox
import pyaudio
import wave

from audio_processing import process_audio_sample
from consts import CLASS_NAMES, TEST_DATA_ROOT
from ai_utils import analyze

class App:
    def __init__(self, models=CLASS_NAMES, data_path=TEST_DATA_ROOT, chunk=3024, frmat=pyaudio.paInt16, channels=1,
                 rate=44100, py=pyaudio.PyAudio()):
        self.main = tkinter.Tk()
        self.collections = []
        self.main.geometry('900x800')
        self.main.title('Record')
        self.CHUNK = chunk
        self.FORMAT = frmat
        self.CHANNELS = channels
        self.RATE = rate
        self.p = py
        self.frames = []
        self.st = 1
        self.stream = self.p.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.RATE, input=True,
                                  frames_per_buffer=self.CHUNK)
        self.results = []
        self.listOfModels = models

        self.audio_file = None
        self.model_path = None
        self.data_path = data_path

        self.buttons = tkinter.Frame(self.main, padx=50, pady=20)

        self.buttons.pack(fill=tk.BOTH)

        self.listbox = tkinter.Listbox(self.buttons, width=30, height=5, selectmode=tkinter.SINGLE)
        self.listbox.grid(row=0, column=0, columnspan=1, padx=10, pady=5)

        for item in self.listOfModels:
            self.listbox.insert(tkinter.END, item)

        self.line = tkinter.Label(self.buttons, text='_____________________________________________________________')

        self.strt_rec = tkinter.Button(self.buttons, width=10, padx=10, pady=5, text='Start Recording',
                                       command=lambda: self.start_record())
        self.strt_rec.grid(row=1, column=0, padx=50, pady=5)
        self.stop_rec = tkinter.Button(self.buttons, width=10, padx=10, pady=5, text='Stop Recording',
                                       command=lambda: self.stop())
        self.stop_rec.grid(row=2, column=0, columnspan=1, padx=50, pady=5)
        self.select_file = tk.Button(self.buttons, width=10, padx=10, pady=5, text="Select Audio File",
                                     command=lambda: self.select_audio_file())
        self.select_file.grid(row=3, column=0, columnspan=1, padx=50, pady=5)
        self.select_model = tk.Button(self.buttons, width=10, padx=10, pady=5, text="Select Model File",
                                      command=lambda: self.select_model_file())
        self.select_model.grid(row=4, column=0, columnspan=1, padx=50, pady=5)

        self.file_string = tkinter.StringVar()
        self.file_string.set('No file selected')
        self.file_label = tkinter.Label(self.buttons, textvariable=self.file_string)
        self.file_label.grid(row=5, column=0, columnspan=1, padx=50, pady=5)

        self.model_string = tkinter.StringVar()
        self.model_string.set('No file selected')
        self.model_label = tkinter.Label(self.buttons, textvariable=self.model_string)
        self.model_label.grid(row=6, column=0, columnspan=1, padx=50, pady=5)

        self.label = tkinter.Label(self.buttons,
                                   text='Press Process Audio File and then Analyze '
                                        'to start authentication process on selected audio file')
        self.label.grid(row=7, column=0, columnspan=1, padx=50, pady=5)

        self.create_spectrograms = tk.Button(self.buttons, width=10, padx=10, pady=5, text="Process Audio File",
                                             command=lambda: self.process_audio())
        self.create_spectrograms.grid(row=8, column=0, columnspan=1, padx=50, pady=5)

        self.analyze = tkinter.Button(self.buttons, width=10, padx=10, pady=5, text='Analyze',
                                      command=lambda: self.analyze_audio_file())
        self.analyze.grid(row=9, column=0, columnspan=1, padx=50, pady=5)

        self.message_box = tkinter.Text(self.buttons, width=100, height=20, padx=0, pady=5)
        self.message_box.grid(row=10, column=0, columnspan=1, padx=50, pady=5)

        self.line = tkinter.Label(self.buttons, text='_____________________________________________________________')

        self.main.mainloop()

    def select_model_file(self):
        self.model_path = filedialog.askopenfilename(initialdir="../", title="Select Trained Model",
                                                     filetypes=(("pt files", "*.pt"), ("all files", "*.*")))
        self.model_string.set(self.model_path)
        self.message_box.insert(tkinter.END, 'Model selected: ' + self.model_path + ' \n\n')

    def select_audio_file(self):
        self.audio_file = filedialog.askopenfilename(initialdir="../", title="Select Audio File",
                                                     filetypes=(("wav files", "*.wav"), ("all files", "*.*")))
        self.file_string.set(self.audio_file)
        self.message_box.insert(tkinter.END, 'File selected: ' + self.audio_file+ ' \n\n')

    def process_audio(self):
        process_audio_sample(self.audio_file, self.data_path)
        self.message_box.insert(tkinter.END, 'Spectrograms created! \n\n')

    def analyze_audio_file(self):
        self.check_data()
        self.results = analyze(self.model_path, self.data_path)
        self.show_results()

    def check_data(self):
        if self.model_path is None or self.audio_file is None:
            self.message_box.insert(tkinter.END, 'No model or audio selected! \n\n')

    def show_results(self):
        for line in self.results:
            self.message_box.insert(tkinter.END, line)
        self.message_box.insert(tkinter.END, '\n\n')

    def start_record(self):
        self.st = 1
        self.frames = []
        stream = self.p.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.RATE, input=True,
                             frames_per_buffer=self.CHUNK)
        self.message_box.insert(tkinter.END, 'Recording started! \n')

        while self.st == 1:
            data = stream.read(self.CHUNK)
            self.frames.append(data)
            print("* recording")
            self.main.update()

        stream.close()

        wf = wave.open('../data/tests/test_recording.wav', 'wb')
        wf.setnchannels(self.CHANNELS)
        wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
        wf.setframerate(self.RATE)
        wf.writeframes(b''.join(self.frames))
        wf.close()
        self.audio_file = '../data/tests/test_recording.wav'
        self.file_string.set(self.audio_file)

    def stop(self):
        self.message_box.insert(tkinter.END, 'Recording stopped!  \n\n')
        self.st = 0

app = App()


#!/usr/bin/python

# Play the Animal Crossing: New Leaf soundtrack in your command
# line according to the date, weather, and time of day!

import argparse
import datetime
import os
import requests
import subprocess
import time

# Add your OpenWeatherMap API key here!
WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather?q=NewYork&APPID='
WEATHER_API_KEY = ''

class AcPlayer:
    def __init__(self):
        self.song = ''
        self.time = None
        self.weather = ''
        # Paths for songs depending on weather
        self.clear_path = os.getcwd() + '/assets/songs/time/clear/'
        self.rainy_path = os.getcwd() + '/assets/songs/time/rainy/'
        self.snowy_path = os.getcwd() + '/assets/songs/time/snowy/'
        self.get_weather()

    def display_info(self):
        print('It is currently {} o\'clock and {}.'.format(self.time, self.weather))

    def get_weather(self):
        response = requests.get(WEATHER_URL + WEATHER_API_KEY).json()
        self.weather = response['weather'][0]['main']

    def play(self):
        playing = subprocess.call(['mplayer', self.song, '/dev/null'], stdout=subprocess.PIPE)

    def update(self):
        time = datetime.datetime.now().hour

        if time != self.time:
            self.time = time
            if self.weather == 'Rain':
                self.song = self.rainy_path + str(self.time) + '.mp3'
            elif self.weather == 'Snow':
                self.song = self.snowy_path + str(self.time) + '.mp3'
            else:
                self.song = self.clear_path + str(self.time) + '.mp3'
            self.display_info()

def main():
    parser = argparse.ArgumentParser(description='Play the Animal Crossing: New Leaf soundtrack according to time & weather',
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-a', '--api', choices=[0,1], default=None, type=str, help='OpenWeatherMap API key')

    if WEATHER_API_KEY == '':
        print('AC PLAYER: You must add an OpenWeatherMap API key before starting.')
        exit(1)

    print('ANIMAL CROSSING: NEW LEAF PLAYER')
    print( '****** command line edition!')
    player = AcPlayer()

    while 1:
        player.update()
        player.play()

if __name__ == '__main__':
    main()


input_string = '''2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5
'''

# input_string = '''1,1,1
# 2,1,1
# '''

# with open('input.txt', 'r') as file:
#     input_string = file.read()

max_n = 5
min_n = 5
n = 25
all_blocks = set()
grid = [[[0 for z in range(-1, n)] for y in range(-1, n)]
        for x in range(-1, n)]
for line in input_string.strip().split('\n'):
    x, y, z = (int(i) for i in line.split(','))
    max_n = max(x, y, z, max_n)
    min_n = min(x, y, z, min_n)
    grid[x][y][z] = 1
    all_blocks.add((x, y, z))
# print(min_n, max_n)

area = 0
dim = ((0, 0, 1), (0, 0, -1), (0, 1, 0), (0, -1, 0), (1, 0, 0), (-1, 0, 0))
for x, y, z in all_blocks:
    for dx, dy, dz in dim:
        if x+dx < -1 or x+dx >= n or y+dy < -1 or y+dy >= n or z+dz < -1 or z+dz >= n:
            continue
        if grid[x+dx][y+dy][z+dz] == 0:
            area += 1

print(area)


#!/usr/bin/python
import sys
import os

#stdout, stderr i stdin
sys.stdout.write("hello world!\n")
sys.stdout.flush()
if len(sys.argv)<=1:
    inp = sys.stdin.read()
    print(inp,file=sys.stderr)

#sys.stdout.write(inp)
#modul argparse
print(sys.argv)

#zmienne srodowiskowe
print(os.environ)

#exit code/errorlevel
sys.exit(3)


import uvicorn
from fastapi import Depends, FastAPI, HTTPException, Security
from fastapi.responses import RedirectResponse
from fastapi.security.api_key import APIKeyHeader

# ---------------------------------------------------------------------------------------------------------
# Пример реализации самой простой аутентификации. Не рекомендуется использовать в нелокальных проектах т.к.
# ключ незахеширован и остается в header'е запроса
# ---------------------------------------------------------------------------------------------------------

app = FastAPI()
api_key_header = APIKeyHeader(name="Authorization", auto_error=True)

SECRET_KEY = "api_secret_key"  # Секретный ключ

async def get_api_key(api_key: str = Security(api_key_header)):
    """
    Функция проверки секретного ключа, получаемого в header запроса

    :param api_key: api-ключ
    :return:
    """
    if api_key == SECRET_KEY:
        return api_key
    else:
        raise HTTPException(status_code=401, detail="Невалидный API ключ")

# Тестовый эндпоинт
@app.get("/test", dependencies=[Depends(get_api_key)])
async def test(name: str):
    return f"Hello, {name}!"

# Эндпоинт документации
@app.get("/", response_class=RedirectResponse, include_in_schema=False)
async def redirect_to_docs():
    return RedirectResponse(url="/docs")

if __name__ == '__main__':
    uvicorn.run(app)