import functools
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import IO, Any, Callable, List, Optional, Union

HAS_TTY = sys.stdout.isatty()
ROOT = Path(os.path.dirname(os.path.realpath(__file__)))

def color_text(code: int, file: Optional[IO[Any]] = None) -> Callable[[str], None]:
    def wrapper(text: str) -> None:
        if HAS_TTY:
            print(f"\x1b[{code}m{text}\x1b[0m", file=file)
        else:
            print(text, file=file)

    return wrapper

warn = color_text(31, file=sys.stderr)
info = color_text(32)
link = color_text(34)

def sh(
    command: List[str], cwd: Optional[Union[Path, str]] = None, check: bool = True
) -> "subprocess.CompletedProcess[str]":
    info("$ " + " ".join(command))
    return subprocess.run(command, cwd=cwd, check=check, text=True)

def verify_commit_hash(commit: str) -> str:
    cmd = ["git", "rev-parse", "--verify", commit]
    proc = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, text=True)
    return proc.stdout.strip()

def escape_attr(attr: str) -> str:
    attr_parts = attr.split(".")
    first = attr_parts[0]
    rest = [f'"{item}"' for item in attr_parts[1:]]
    return ".".join([first, *rest])

@functools.lru_cache(maxsize=1)
def current_system() -> str:
    system = subprocess.run(
        [
            "nix",
            "--extra-experimental-features",
            "nix-command",
            "eval",
            "--impure",
            "--raw",
            "--expr",
            "builtins.currentSystem",
        ],
        check=True,
        stdout=subprocess.PIPE,
        text=True,
    )
    return system.stdout

def nix_nom_tool() -> str:
    "Return `nom` if found in $PATH"
    if shutil.which("nom"):
        return "nom"
    else:
        return "nix"


# Imports the required dependencies
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

# create the Example class, which will create the window object
class Example(QWidget):       # name declaration, will inherit stuff from the QWidget class
    def __init__(self):       # constructor for the inherited class, so will construct the QWidget class
        super().__init__()    # super() is to call the class above it, so line is constructing the QWidget class
        self.initUI()         # this line constructs the actual window, and is the function call for the next definition

    def initUI(self):                         # this function of the Example class will create the actual window
        self.setGeometry(300, 300, 300, 220)  # setGeometry will resize and move the created window
        self.setWindowTitle('Icon')           # will set the title of the window
        self.setWindowIcon(QIcon('web.png'))  # will locate an image and set it as the window icon
        self.show()                           # actually creates the window

if __name__ == '__main__':
    app = QApplication(sys.argv)   # required starting of the application

    ex = Example()                 # creating the Example and assigning the name ex
    sys.exit(app.exec_())          # will exit the application when triggered


import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D,Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from constants import PATH
from pathlib import Path

# set train human and horse directory
train_human_dir = Path(PATH).joinpath("humans")
train_horses_dir = Path(PATH).joinpath("horses")
# verify all file names in train human and horses folder
train_human_file_names = [entry.name for entry in train_human_dir.iterdir() if entry.is_file()]
train_horses_file_names = [entry.name for entry in train_horses_dir.iterdir() if entry.is_file()]

print("training human images:::==>",len(train_human_file_names))
print("training horse images:::==>", len(train_horses_file_names))

model = Sequential([Conv2D(16, (3,3), activation="relu",input_shape=(300,300,3)),
                    MaxPooling2D(2,2),
                    Conv2D(32, (3,3),activation="relu"),
                    MaxPooling2D(2,2),
                    Conv2D(64, (3,3), activation="relu"),
                    MaxPooling2D(2,2),
                    Conv2D(64, (3,3), activation="relu"),
                    MaxPooling2D(2,2),
                    Conv2D(64, (3,3), activation="relu"),
                    MaxPooling2D(2,2),
                    Flatten(),
                    Dense(512, activation="relu"),
                    Dense(1, activation="sigmoid")
                ]
            )
model.summary()
# compile the model
model.compile(loss="binary_crossentropy",
              optimizer=RMSprop(learning_rate=0.001),
              metrics=["accuracy"]
              )

# create a image data generator
train_data_gen= ImageDataGenerator(rescale=1./255)
train_data_generator = train_data_gen.flow_from_directory(
    Path(PATH),
    target_size=(300,300),
    batch_size=128,
    class_mode="binary",
)
history = model.fit(
    train_data_generator,
    steps_per_epoch=8,
    epochs=15,
    verbose=1,
)

# # validation_data_gen= ImageDataGenerator(rescale=1./255)
# # vaildation_data_generator = validation_data_gen.flow_from_directory(
# #     validation_dir,
# #     target_size=(300,300),
# #     batch_size=128,
# #     class_mode="binary",

# # )


import glob
import pandas as pd
import os
from xml.etree import ElementTree as ET

def convert_annots(annotations_path: str) -> pd.DataFrame:
    """
    Function to convert xml of annotations to a dataframe for easier processing
    Args:
        annots: a list of string file paths
        image_dir: path to the image folder

    Returns: a daframe containing xy, height, width, and label class
    """

    annotations = glob.glob(os.path.join(annotations_path, '*.xml'))
    data = {
        'image_path': [],
        'xmin': [],
        'ymin': [],
        'xmax': [],
        'ymax': [],
        'labels': []
    }

    for annot in annotations:
        tree = ET.parse(annot)
        root = tree.getroot()
        file_name = root.find('filename').text
        image_path = glob.glob(os.path.join(annotations_path, file_name))[0]

        for obj in root.findall('object'):
            data['image_path'].append(image_path)
            data['labels'].append(obj.find('name').text)
            for bb in obj.find('bndbox'):
                data[bb.tag].append(int(bb.text))

    return pd.DataFrame(data)


import json
import os
import re
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

from c6ep import Parser

# Ideally, the export file should have a number at the end
# This function matches that number and returns it
def gameNumber(file):
    return re.search(r'([0-9]+)(?=\.)', file).group()

# Parse the files
def parse():
    directory = 'exports'   # Directory where json exports are stored

    for file in os.listdir(directory):
        with open(f'{directory}/{file}', "r", encoding='utf-8') as read_file:
            data = json.load(read_file)

        parse = Parser(data, gameNumber(file))
        print(parse.count_moments())
        print(parse.count_players(False))

# Loads the parsed data into the Firebase DB
def pushToFirebase():
    # Fetch the service account key JSON file contents
    cred = credentials.Certificate('key/serviceAccountKey.json')

    # Initialize the app with a service account, granting admin privileges
    firebase_admin.initialize_app(cred, {
        'databaseURL': open('key/databaseURL.txt', 'r').read()
    })

    # As an admin, the app has access to read and write all data, regardless of Security Rules
    ref = db.reference('/statsForEvents')

    # Directories where the parsed data json files are stored
    civDirectory = 'output/civilizations'
    eventDirectory = 'output/events'

    # Dictionary that will contain combined data
    game = {}

    for file in os.listdir(civDirectory):
        with open(f'{civDirectory}/{file}', 'r', encoding='utf-8') as read_file:
            data = json.load(read_file)
        game_number = gameNumber(file)
        game[f"game{game_number}"] = {'game_number': int(game_number), 'leaders': data}

    # Refreshing the increment
    incr = 0
    for file in os.listdir(eventDirectory):
        with open(f'{eventDirectory}/{file}', 'r', encoding='utf-8') as read_file:
            data = json.load(read_file)
        events = {}
        events["events"] = data
        game_number = gameNumber(file)
        game[f"game{game_number}"] = {**game[f"game{game_number}"], **events}

        ref.set(game)
        incr = incr + 1

print('Expecting input:\n"parse" to parse the file,\n"push" to push the parsed data to Firebase DB.')
while True:
    input_action = input()
    if input_action == "parse":
        parse()
        break
    elif input_action == "push":
        pushToFirebase()
        break
    else:
        print('Wrong input')
        continue


from itertools import combinations
from math import ceil
from collections import defaultdict

def multi2(N, m):
    N2 = N
    a = list(range(m, N + m))
    while N2 > 1:
        N1 = N2 % 2; N2 = N2 // 2 + N1
        for i in range(N2 - N1):
            a[i] *= a[N2 + i]
        a = a[0: N2]
    return a[0]

def C(m, n):
	ret = multi2(n, m - n + 1) // multi2(n, 1)
	return ret % 12345787

def makeList(r, g, b, number):
	ret = defaultdict(list)
	for colors in combinations([r, g, b], 2):
		c1, c2 = colors
		for n in range(0, number):
			cnt = 0
			for num1 in range(1, n // c1 + 1):
				for num2 in range(1, n // c2 + 1):
					if n - c1 * num1 - c2 * num2 < 0:
						continue
					bnum = C(n - c1 * num1 - c2 * num2 + num1 + num2, num1 + num2)
					cnum = C(num1 + num2, num1)
					cnt += bnum * cnum
			ret[colors].append(cnt)
	return ret

def Function(n, r, g, b):
	cnt = 0
	for colors in combinations([r, g, b], 2):
		c1, c2 = colors
		for num1 in range(1, n // c1 + 1):
			for num2 in range(1, n // c2 + 1):
				if n - c1 * num1 - c2 * num2 < 0:
					continue
				bnum = C(n - c1 * num1 - c2 * num2 + num1 + num2, num1 + num2) % 12345787
				cnum = C(num1 + num2, num1) % 12345787
				cnt += bnum * cnum
	return cnt % 12345787

def insane_tri_bicolor_tiling(n, r, g, b):
	'''number = 30
	chooseList = makeList(r, g, b, number)
	cnt = 0
	for cs in combinations([r, g, b], 2):
		cnt += f(n, cs, number, chooseList)
	return cnt % 12345787'''
	return Function(n, r, g, b)

print(insane_tri_bicolor_tiling(100, 2, 3, 4))


"""Test the users.

## Domain

*   Users as in `conftest`, under *players*
*   Clean slate, see `starters`
*   The user table

## Acts

Getting to know all users.

`test_users`
:   **office** checks whether all users in the database have a corresponding
    client fixture.

`test_identity`
:   We check whether all users work as their own identity.
    This is a test on the web-app as well a test on the mechanics of the test
    framework.

`test_readEmail`
:   All users try to read the email address of **auth**, but only some succeed
"""

import pytest

import magic  # noqa
from control.utils import E, serverprint
from conftest import USER_LIST, NAMED_USERS, POWER_USERS
from example import AUTH, AUTH_EMAIL, EMAIL, PUBLIC, USER
from helpers import viewField
from starters import start
from subtest import assertFieldValue, assertStatus

startInfo = {}

@pytest.mark.usefixtures("db")
def test_start(clientOffice):
    startInfo.update(start(clientOffice=clientOffice, users=True))

def test_users(clientOffice):
    assert len(USER_LIST) == 11
    valueTables = startInfo["valueTables"]

    users = valueTables[USER]
    for user in USER_LIST:
        if user == PUBLIC:
            assert user not in users
        else:
            assert user in users

def test_identity(clients):
    for (user, cl) in clients.items():
        response = cl.get("/whoami")
        actualUser = response.get_data(as_text=True)
        serverprint(f"{user} says: I am {actualUser}")
        assert user == actualUser

def test_login(clients, clientPublic):
    for user in sorted(NAMED_USERS) + [E, PUBLIC, "xxxxxx"]:
        isNamed = user in NAMED_USERS
        expect = 302 if isNamed else 303
        serverprint(f"LOGIN {user}")
        assertStatus(clientPublic, f"/login?eppn={user}", expect)
        serverprint(f"LOGOUT {user}")
        if user in clients:
            assertStatus(clients[user], "/logout", expect)

def test_readEmail(clients):
    valueTables = startInfo["valueTables"]

    eid = valueTables[USER][AUTH]
    for (user, client) in clients.items():
        (text, fields) = viewField(client, USER, eid, EMAIL)
        if user in POWER_USERS:
            assertFieldValue(fields, EMAIL, AUTH_EMAIL)
        else:
            assertFieldValue(fields, EMAIL, None)


import urllib.request
from bs4 import BeautifulSoup
import re
import os
import requests
import subprocess
import sys
import inquirer

ost_title_list = []
ost_dict = {}
ost_name = ""
dir_path = "./test/"
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) '\
'AppleWebKit/537.36 (KHTML, like Gecko) '\
'Chrome/55.0.2883.95 Safari/537.36 '

def fetch_ost_dict():
    query = sys.argv[1].replace(" ", "+")
    base_url = 'https://downloads.khinsider.com/'
    search_url = base_url +'/search?search=' + query
    req = urllib.request.Request(search_url, headers={'User-Agent': ua})
    html = urllib.request.urlopen(req)
    soup = BeautifulSoup(html, "html.parser")
    mp3s = soup.find_all(href=re.compile("downloads.khinsider.com/game-soundtracks/album/"))
    for mp3 in mp3s:
        title = mp3.get_text()
        url = mp3.get('href')
        ost_dict[title] = url
        ost_title_list.append(title)
    return(ost_dict)

def select_ost(ost_dict):
    ost_candidate_list = []
    global ost_name
    ost_name = ""
    for title in ost_dict:
        ost_candidate_list.append(title)
    ost_candidates = [
        inquirer.List('selected',
            message="What OST do you want download?",
            choices=ost_candidate_list,
        ),
    ]

    requires = inquirer.prompt(ost_candidates)
    requires_list = list(requires.values())
    ost_name = requires_list[0]
    return(ost_name)

def get_mp3(ost_name, ost_dict):
    mp3_url_list = []
    ost_url = ost_dict[ost_name]
    req = urllib.request.Request(ost_url, headers={'User-Agent': ua})
    html = urllib.request.urlopen(req)
    soup = BeautifulSoup(html, "html.parser")
    mp3s = soup.find_all(href=re.compile(".mp3"))
    for mp3 in mp3s:
        mp3_url = mp3.get('href')
        if mp3_url not in mp3_url_list:
            mp3_url_list.append(mp3_url)
    for mp3_url in mp3_url_list:
        base_url = 'https://downloads.khinsider.com'
        url = base_url + mp3_url
        dir_path = url.split('/')[5]
        try:
            os.mkdir(dir_path)
        except FileExistsError:
            pass
        req = urllib.request.Request(url, headers={'User-Agent': ua})
        html = urllib.request.urlopen(req)
        soup = BeautifulSoup(html, "html.parser")
        audio_url = soup.find('audio').get('src')
        subprocess.call('wget -P %s %s' % (dir_path, audio_url), shell=True)

def main():
    global ost_title_list
    global ost_name
    fetch_ost_dict()
    select_ost(ost_dict)
    get_mp3(ost_name, ost_dict)

if __name__ == "__main__":
    main()


from collections.abc import Iterable
from contextlib import suppress
import copy
from enum import IntEnum
from functools import partial
import sys
import weakref
from weakref import WeakKeyDictionary, WeakSet

from matplotlib.axes import Axes
from matplotlib.container import Container
from matplotlib.figure import Figure
import numpy as np

from . import _pick_info

_default_bindings = dict(
    select=1,
    deselect=3,
    left="shift+left",
    right="shift+right",
    up="shift+up",
    down="shift+down",
    toggle_enabled="e",
    toggle_visible="v",
)
_default_annotation_kwargs = dict(
    bbox=dict(
        boxstyle="round,pad=.5",
        fc="yellow",
        alpha=.5,
        ec="k",
    ),
    arrowprops=dict(
        arrowstyle="->",
        connectionstyle="arc3",
        shrinkB=0,
        ec="k",
    ),
)
_default_annotation_positions = [
    dict(position=(-15, 15), anncoords="offset points",
         horizontalalignment="right", verticalalignment="bottom"),
    dict(position=(15, 15), anncoords="offset points",
         horizontalalignment="left", verticalalignment="bottom"),
    dict(position=(15, -15), anncoords="offset points",
         horizontalalignment="left", verticalalignment="top"),
    dict(position=(-15, -15), anncoords="offset points",
         horizontalalignment="right", verticalalignment="top"),
]
_default_highlight_kwargs = dict(
    # Only the kwargs corresponding to properties of the artist will be passed.
    # Line2D.
    color="yellow",
    markeredgecolor="yellow",
    linewidth=3,
    markeredgewidth=3,
    # PathCollection.
    facecolor="yellow",
    edgecolor="yellow",
)

class _MarkedStr(str):
    """A string subclass solely for marking purposes."""

def _mouse_event_matches(event, spec):
    """
    Return whether a mouse event "matches" an event spec, which is either a
    single mouse button, or a mapping matched against ``vars(event)``, e.g.
    ``{"button": 1, "key": "control"}``.
    """
    if isinstance(spec, int):
        spec = {"button": spec}
    return all(getattr(event, k) == v for k, v in spec.items())

def _get_rounded_intersection_area(bbox_1, bbox_2):
    """Compute the intersection area between two bboxes rounded to 8 digits."""
    # The rounding allows sorting areas without floating point issues.
    bbox = bbox_1.intersection(bbox_1, bbox_2)
    return round(bbox.width * bbox.height, 8) if bbox else 0

def _iter_axes_subartists(ax):
    r"""Yield all child `Artist`\s (*not* `Container`\s) of *ax*."""
    yield from ax.collections
    yield from ax.images
    yield from ax.lines
    yield from ax.patches
    yield from ax.texts

def _is_alive(artist):
    """Check whether *artist* is still present on its parent axes."""
    return bool(artist
                and artist.axes
                and (artist.container in artist.axes.containers
                     if isinstance(artist, _pick_info.ContainerArtist) else
                     artist in _iter_axes_subartists(artist.axes)))

def _reassigned_axes_event(event, ax):
    """Reassign *event* to *ax*."""
    event = copy.copy(event)
    event.xdata, event.ydata = (
        ax.transData.inverted().transform((event.x, event.y)))
    return event

class HoverMode(IntEnum):
    NoHover, Persistent, Transient = range(3)

class Cursor:
    """
    A cursor for selecting Matplotlib artists.

    Attributes
    ----------
    bindings : dict
        See the *bindings* keyword argument to the constructor.
    annotation_kwargs : dict
        See the *annotation_kwargs* keyword argument to the constructor.
    annotation_positions : dict
        See the *annotation_positions* keyword argument to the constructor.
    highlight_kwargs : dict
        See the *highlight_kwargs* keyword argument to the constructor.
    """

    _keep_alive = WeakKeyDictionary()

    def __init__(self,
                 artists,
                 *,
                 multiple=False,
                 highlight=False,
                 hover=False,
                 bindings=None,
                 annotation_kwargs=None,
                 annotation_positions=None,
                 highlight_kwargs=None):
        """
        Construct a cursor.

        Parameters
        ----------

        artists : List[Artist]
            A list of artists that can be selected by this cursor.

        multiple : bool, default: False
            Whether multiple artists can be "on" at the same time.  If on,
            cursor dragging is disabled (so that one does not end up with many
            cursors on top of one another).

        highlight : bool, default: False
            Whether to also highlight the selected artist.  If so,
            "highlighter" artists will be placed as the first item in the
            :attr:`extras` attribute of the `Selection`.

        hover : `HoverMode`, default: False
            Whether to select artists upon hovering instead of by clicking.
            (Hovering over an artist while a button is pressed will not trigger
            a selection; right clicking on an annotation will still remove it.)
            Possible values are

            - False, alias `HoverMode.NoHover`: hovering is inactive.
            - True, alias `HoverMode.Persistent`: hovering is active;
              annotations remain in place even after the mouse moves away from
              the artist (until another artist is selected, if *multiple* is
              False).
            - 2, alias `HoverMode.Transient`: hovering is active; annotations
              are removed as soon as the mouse moves away from the artist.

        bindings : dict, optional
            A mapping of actions to button and keybindings.  Valid keys are:

            ================ ==================================================
            'select'         mouse button to select an artist
                             (default: :data:`.MouseButton.LEFT`)
            'deselect'       mouse button to deselect an artist
                             (default: :data:`.MouseButton.RIGHT`)
            'left'           move to the previous point in the selected path,
                             or to the left in the selected image
                             (default: shift+left)
            'right'          move to the next point in the selected path, or to
                             the right in the selected image
                             (default: shift+right)
            'up'             move up in the selected image
                             (default: shift+up)
            'down'           move down in the selected image
                             (default: shift+down)
            'toggle_enabled' toggle whether the cursor is active
                             (default: e)
            'toggle_visible' toggle default cursor visibility and apply it to
                             all cursors (default: v)
            ================ ==================================================

            Missing entries will be set to the defaults.  In order to not
            assign any binding to an action, set it to ``None``.  Modifier keys
            (or other event properties) can be set for mouse button bindings by
            passing them as e.g. ``{"button": 1, "key": "control"}``.

        annotation_kwargs : dict, default: {}
            Keyword argments passed to the `annotate
            <matplotlib.axes.Axes.annotate>` call.

        annotation_positions : List[dict], optional
            List of positions tried by the annotation positioning algorithm.
            The default is to try four positions, 15 points to the NW, NE, SE,
            and SW from the selected point; annotations that stay within the
            axes are preferred.

        highlight_kwargs : dict, default: {}
            Keyword arguments used to create a highlighted artist.
        """

        artists = [*artists]
        # Be careful with GC.
        self._artists = [weakref.ref(artist) for artist in artists]

        for artist in artists:
            type(self)._keep_alive.setdefault(artist, set()).add(self)

        self._multiple = multiple
        self._highlight = highlight

        self._visible = True
        self._enabled = True
        self._selections = []
        self._selection_stack = []
        self._last_auto_position = None
        self._callbacks = {"add": [], "remove": []}
        self._hover = hover

        self._suppressed_events = WeakSet()
        connect_pairs = [
            ("pick_event", self._on_pick),
            ("key_press_event", self._on_key_press),
        ]
        if hover:
            connect_pairs += [
                ("motion_notify_event", self._on_hover_motion_notify),
                ("button_press_event", self._on_hover_button_press),
            ]
        else:
            connect_pairs += [
                ("button_press_event", self._on_nonhover_button_press),
            ]
            if not self._multiple:
                connect_pairs.append(
                    ("motion_notify_event", self._on_nonhover_button_press))
        self._disconnectors = [
            partial(canvas.mpl_disconnect, canvas.mpl_connect(*pair))
            for pair in connect_pairs
            for canvas in {artist.figure.canvas for artist in artists}]

        bindings = {**_default_bindings,
                    **(bindings if bindings is not None else {})}
        unknown_bindings = {*bindings} - {*_default_bindings}
        if unknown_bindings:
            raise ValueError("Unknown binding(s): {}".format(
                ", ".join(sorted(unknown_bindings))))
        bindings_items = list(bindings.items())
        for i in range(len(bindings)):
            action, key = bindings_items[i]
            for j in range(i):
                other_action, other_key = bindings_items[j]
                if key == other_key:
                    raise ValueError(
                        f"Duplicate bindings: {key} is used for "
                        f"{other_action} and for {action}")
        self.bindings = bindings

        self.annotation_kwargs = (
            annotation_kwargs if annotation_kwargs is not None
            else copy.deepcopy(_default_annotation_kwargs))
        self.annotation_positions = (
            annotation_positions if annotation_positions is not None
            else copy.deepcopy(_default_annotation_positions))
        self.highlight_kwargs = (
            highlight_kwargs if highlight_kwargs is not None
            else copy.deepcopy(_default_highlight_kwargs))

    @property
    def artists(self):
        """The tuple of selectable artists."""
        # Work around matplotlib/matplotlib#6982: `cla()` does not clear
        # `.axes`.
        return tuple(filter(_is_alive, (ref() for ref in self._artists)))

    @property
    def enabled(self):
        """Whether clicks are registered for picking and unpicking events."""
        return self._enabled

    @enabled.setter
    def enabled(self, value):
        self._enabled = value

    @property
    def selections(self):
        r"""The tuple of current `Selection`\s."""
        for sel in self._selections:
            if sel.annotation.axes is None:
                raise RuntimeError("Annotation unexpectedly removed; "
                                   "use 'cursor.remove_selection' instead")
        return tuple(self._selections)

    @property
    def visible(self):
        """
        Whether selections are visible by default.

        Setting this property also updates the visibility status of current
        selections.
        """
        return self._visible

    @visible.setter
    def visible(self, value):
        self._visible = value
        for sel in self.selections:
            sel.annotation.set_visible(value)
            sel.annotation.figure.canvas.draw_idle()

    def _get_figure(self, aoc):
        """Return the parent figure of artist-or-container *aoc*."""
        if isinstance(aoc, Container):
            try:
                ca, = {artist for artist in (ref() for ref in self._artists)
                       if isinstance(artist, _pick_info.ContainerArtist)
                          and artist.container is aoc}
            except ValueError:
                raise ValueError(f"Cannot find parent figure of {aoc}")
            return ca.figure
        else:
            return aoc.figure

    def _get_axes(self, aoc):
        """Return the parent axes of artist-or-container *aoc*."""
        if isinstance(aoc, Container):
            try:
                ca, = {artist for artist in (ref() for ref in self._artists)
                       if isinstance(artist, _pick_info.ContainerArtist)
                          and artist.container is aoc}
            except ValueError:
                raise ValueError(f"Cannot find parent axes of {aoc}")
            return ca.axes
        else:
            return aoc.axes

    def add_selection(self, pi):
        """
        Create an annotation for a `Selection` and register it.

        Returns a new `Selection`, that has been registered by the `Cursor`,
        with the added annotation set in the :attr:`annotation` field and, if
        applicable, the highlighting artist in the :attr:`extras` field.

        Emits the ``"add"`` event with the new `Selection` as argument.  When
        the event is emitted, the position of the annotation is temporarily
        set to ``(nan, nan)``; if this position is not explicitly set by a
        callback, then a suitable position will be automatically computed.

        Likewise, if the text alignment is not explicitly set but the position
        is, then a suitable alignment will be automatically computed.
        """
        # pi: "pick_info", i.e. an incomplete selection.
        # Pre-fetch the figure and axes, as callbacks may actually unset them.
        figure = self._get_figure(pi.artist)
        axes = self._get_axes(pi.artist)
        get_cached_renderer = (
            figure.canvas.get_renderer
            if hasattr(figure.canvas, "get_renderer")
            else axes.get_renderer_cache)  # matplotlib <3.6.
        renderer = get_cached_renderer()
        if renderer is None:
            figure.canvas.draw()  # Needed below anyways.
            renderer = get_cached_renderer()
        ann = axes.annotate(
            _pick_info.get_ann_text(*pi), xy=pi.target,
            xytext=(np.nan, np.nan),
            horizontalalignment=_MarkedStr("center"),
            verticalalignment=_MarkedStr("center"),
            visible=self.visible,
            zorder=np.inf,
            **self.annotation_kwargs)
        # Move the Annotation's ownership from the Axes to the Figure, so that
        # it gets drawn even above twinned axes.  But ann.axes must stay set,
        # so that e.g. unit converters get correctly applied.
        ann.remove()
        ann.axes = axes
        figure.add_artist(ann)
        ann.draggable(use_blit=not self._multiple)
        extras = []
        if self._highlight:
            hl = self.add_highlight(*pi)
            if hl:
                extras.append(hl)
        sel = pi._replace(annotation=ann, extras=extras)
        self._selections.append(sel)
        self._selection_stack.append(sel)
        for cb in self._callbacks["add"]:
            cb(sel)

        # Check that `ann.axes` is still set, as callbacks may have removed the
        # annotation.
        if ann.axes and ann.xyann == (np.nan, np.nan):
            fig_bbox = figure.get_window_extent()
            ax_bbox = axes.get_window_extent()
            overlaps = []
            for idx, annotation_position in enumerate(
                    self.annotation_positions):
                ann.set(**annotation_position)
                # Work around matplotlib/matplotlib#7614: position update is
                # missing.
                ann.update_positions(renderer)
                bbox = ann.get_window_extent(renderer)
                overlaps.append(
                    (_get_rounded_intersection_area(fig_bbox, bbox),
                     _get_rounded_intersection_area(ax_bbox, bbox),
                     # Avoid needlessly jumping around by breaking ties using
                     # the last used position as default.
                     idx == self._last_auto_position))
            auto_position = max(range(len(overlaps)), key=overlaps.__getitem__)
            ann.set(**self.annotation_positions[auto_position])
            self._last_auto_position = auto_position
        else:
            if isinstance(ann.get_horizontalalignment(), _MarkedStr):
                ann.set_horizontalalignment(
                    {-1: "right", 0: "center", 1: "left"}[
                        np.sign(np.nan_to_num(ann.xyann[0]))])
            if isinstance(ann.get_verticalalignment(), _MarkedStr):
                ann.set_verticalalignment(
                    {-1: "top", 0: "center", 1: "bottom"}[
                        np.sign(np.nan_to_num(ann.xyann[1]))])

        if (extras
                or len(self.selections) > 1 and not self._multiple
                or not figure.canvas.supports_blit):
            # Either:
            #  - there may be more things to draw, or
            #  - annotation removal will make a full redraw necessary, or
            #  - blitting is not (yet) supported.
            figure.canvas.draw_idle()
        elif ann.axes:
            # Fast path, only needed if the annotation has not been immediately
            # removed.
            ann.draw(renderer)
            figure.canvas.blit()
        # Removal comes after addition so that the fast blitting path works.
        if not self._multiple:
            for sel in self.selections[:-1]:
                self.remove_selection(sel)
        return sel

    def add_highlight(self, artist, *args, **kwargs):
        """
        Create, add, and return a highlighting artist.

        This method is should be called with an "unpacked" `Selection`,
        possibly with some fields set to None.

        It is up to the caller to register the artist with the proper
        `Selection` (by calling ``sel.extras.append`` on the result of this
        method) in order to ensure cleanup upon deselection.
        """
        hl = _pick_info.make_highlight(
            artist, *args,
            **{"highlight_kwargs": self.highlight_kwargs, **kwargs})
        if hl:
            artist.axes.add_artist(hl)
            return hl

    def connect(self, event, func=None):
        """
        Connect a callback to a `Cursor` event; return the callback.

        Two events can be connected to:

        - callbacks connected to the ``"add"`` event are called when a
          `Selection` is added, with that selection as only argument;
        - callbacks connected to the ``"remove"`` event are called when a
          `Selection` is removed, with that selection as only argument.

        This method can also be used as a decorator::

            @cursor.connect("add")
            def on_add(sel):
                ...

        Examples of callbacks::

            # Change the annotation text and alignment:
            lambda sel: sel.annotation.set(
                text=sel.artist.get_label(),  # or use e.g. sel.index
                ha="center", va="bottom")

            # Make label non-draggable:
            lambda sel: sel.draggable(False)

        Note that when a single event causes both the removal of an "old"
        selection and the addition of a "new" one (typically, clicking on an
        artist when another one is selected, or hovering -- both assuming that
        ``multiple=False``), the "add" callback is called *first*.  This allows
        it, in particular, to "cancel" the addition (by immediately removing
        the "new" selection) and thus avoid removing the "old" selection.
        However, this call order may change in a future release.
        """
        if event not in self._callbacks:
            raise ValueError(f"{event!r} is not a valid cursor event")
        if func is None:
            return partial(self.connect, event)
        self._callbacks[event].append(func)
        return func

    def disconnect(self, event, cb):
        """
        Disconnect a previously connected callback.

        If a callback is connected multiple times, only one connection is
        removed.
        """
        try:
            self._callbacks[event].remove(cb)
        except KeyError:
            raise ValueError(f"{event!r} is not a valid cursor event")
        except ValueError:
            raise ValueError(f"Callback {cb} is not registered to {event}")

    def remove(self):
        """
        Remove a cursor.

        Remove all `Selection`\\s, disconnect all callbacks, and allow the
        cursor to be garbage collected.
        """
        for disconnectors in self._disconnectors:
            disconnectors()
        for sel in self.selections:
            self.remove_selection(sel)
        for s in type(self)._keep_alive.values():
            with suppress(KeyError):
                s.remove(self)

    def _on_pick(self, event):
        # Avoid creating a new annotation when dragging a preexisting
        # annotation (if multiple = True).  To do so, rely on the fact that
        # pick_events (which are used to implement dragging) trigger first (via
        # Figure's button_press_event, which is registered first); when one of
        # our annotations is picked, registed the corresponding mouse event as
        # "suppressed".  This can be done via a WeakSet as Matplotlib will keep
        # the event alive while being propagated through the callbacks.
        # Additionally, also rely on this mechanism to update the "current"
        # selection.
        for sel in self._selections:
            if event.artist is sel.annotation:
                self._suppressed_events.add(event.mouseevent)
                self._selection_stack.remove(sel)
                self._selection_stack.append(sel)
                break

    def _on_nonhover_button_press(self, event):
        if _mouse_event_matches(event, self.bindings["select"]):
            self._on_select_event(event)
        if _mouse_event_matches(event, self.bindings["deselect"]):
            self._on_deselect_event(event)

    def _on_hover_motion_notify(self, event):
        if event.button is None:
            # Filter away events where the mouse is pressed, in particular to
            # avoid conflicts between hover and draggable.
            self._on_select_event(event)

    def _on_hover_button_press(self, event):
        if _mouse_event_matches(event, self.bindings["deselect"]):
            # Still allow removing the annotation by right clicking.
            self._on_deselect_event(event)

    def _filter_mouse_event(self, event):
        # Accept the event iff we are enabled, and either
        # - no other widget is active, and this is not the second click of a
        #   double click (to prevent double selection), or
        # - another widget is active, and this is a double click (to bypass
        #   the widget lock), or
        # - hovering is active (in which case this is a motion_notify_event
        #   anyways).
        return (self.enabled
                and (event.canvas.widgetlock.locked() == event.dblclick
                     or self._hover))

    def _on_select_event(self, event):
        if (not self._filter_mouse_event(event)
                # See _on_pick.  (We only suppress selects, not deselects.)
                or event in self._suppressed_events):
            return
        # Work around lack of support for twinned axes.
        per_axes_event = {ax: _reassigned_axes_event(event, ax)
                          for ax in {artist.axes for artist in self.artists}}
        pis = []
        for artist in self.artists:
            if (artist.axes is None  # Removed or figure-level artist.
                    or event.canvas is not artist.figure.canvas
                    or not artist.get_visible()
                    or not artist.axes.contains(event)[0]):  # Cropped by axes.
                continue
            pi = _pick_info.compute_pick(artist, per_axes_event[artist.axes])
            if pi:
                pis.append(pi)
        # The any() check avoids picking an already selected artist at the same
        # point, as likely the user is just dragging it.  We check this here
        # rather than not adding the pick_info to pis at all, because in
        # transient hover mode, selections should be cleared out only when no
        # candidate picks (including such duplicates) exist at all.
        pi = min((pi for pi in pis
                  if not any((pi.artist, tuple(pi.target))
                             == (other.artist, tuple(other.target))
                             for other in self._selections)),
                 key=lambda pi: pi.dist, default=None)
        if pi:
            self.add_selection(pi)
        elif not pis and self._hover == HoverMode.Transient:
            for sel in self.selections:
                if event.canvas is sel.annotation.figure.canvas:
                    self.remove_selection(sel)

    def _on_deselect_event(self, event):
        if not self._filter_mouse_event(event):
            return
        for sel in self.selections[::-1]:  # LIFO.
            ann = sel.annotation
            if event.canvas is not ann.figure.canvas:
                continue
            if ann.contains(event)[0]:
                self.remove_selection(sel)
                break
        else:
            if self._highlight:
                for sel in self.selections[::-1]:
                    if any(extra.contains(event)[0] for extra in sel.extras):
                        self.remove_selection(sel)
                        break

    def _on_key_press(self, event):
        if event.key == self.bindings["toggle_enabled"]:
            self.enabled = not self.enabled
        elif event.key == self.bindings["toggle_visible"]:
            self.visible = not self.visible
        if not self._selections or not self.enabled:
            return
        sel = self._selection_stack[-1]
        for key in ["left", "right", "up", "down"]:
            if event.key == self.bindings[key]:
                self.remove_selection(sel)
                self.add_selection(_pick_info.move(*sel, key=key))
                break

    def remove_selection(self, sel):
        """Remove a `Selection`."""
        self._selections.remove(sel)
        self._selection_stack.remove(sel)
        # <artist>.figure will be unset so we save them first.
        figures = {artist.figure for artist in [sel.annotation] + sel.extras}
        # ValueError is raised if the artist has already been removed.
        with suppress(ValueError):
            sel.annotation.remove()
        for artist in sel.extras:
            with suppress(ValueError):
                artist.remove()
        for cb in self._callbacks["remove"]:
            cb(sel)
        for figure in figures:
            figure.canvas.draw_idle()

def cursor(pickables=None, **kwargs):
    """
    Create a `Cursor` for a list of artists, containers, and axes.

    Parameters
    ----------

    pickables : Optional[List[Union[Artist, Container, Axes, Figure]]]
        All artists and containers in the list or on any of the axes or
        figures passed in the list are selectable by the constructed `Cursor`.
        Defaults to all artists and containers on any of the figures that
        :mod:`~matplotlib.pyplot` is tracking.  Note that the latter will only
        work when relying on pyplot, not when figures are directly instantiated
        (e.g., when manually embedding Matplotlib in a GUI toolkit).

    **kwargs
        Keyword arguments are passed to the `Cursor` constructor.
    """

    # Explicit check to avoid a confusing
    # "TypeError: Cursor.__init__() got multiple values for argument 'artists'"
    if "artists" in kwargs:
        raise TypeError(
            "cursor() got an unexpected keyword argument 'artists'")

    if pickables is None:
        # Do not import pyplot ourselves to avoid forcing the backend.
        plt = sys.modules.get("matplotlib.pyplot")
        pickables = [
            plt.figure(num) for num in plt.get_fignums()] if plt else []
    elif (isinstance(pickables, Container)
          or not isinstance(pickables, Iterable)):
        pickables = [pickables]

    def iter_unpack_figures(pickables):
        for entry in pickables:
            if isinstance(entry, Figure):
                yield from entry.axes
            else:
                yield entry

    def iter_unpack_axes(pickables):
        for entry in pickables:
            if isinstance(entry, Axes):
                yield from _iter_axes_subartists(entry)
                containers.extend(entry.containers)
            elif isinstance(entry, Container):
                containers.append(entry)
            else:
                yield entry

    containers = []
    artists = [*iter_unpack_axes(iter_unpack_figures(pickables))]
    for container in containers:
        contained = [*filter(None, container.get_children())]
        for artist in contained:
            with suppress(ValueError):
                artists.remove(artist)
        if contained:
            artists.append(_pick_info.ContainerArtist(container))

    return Cursor(artists, **kwargs)


from __future__ import print_function

import os
from subprocess import check_call, check_output
import sys
from pprint import pprint
import distutils
import distutils.sysconfig
from distutils.version import LooseVersion

from . import escape_path
from .env import (IS_64BIT, IS_DARWIN, IS_WINDOWS,
                  DEBUG, REL_WITH_DEB_INFO, USE_MKLDNN,
                  check_env_flag, check_negative_env_flag)
from .cuda import USE_CUDA
from .dist_check import USE_DISTRIBUTED, USE_GLOO_IBVERBS
from .nccl import (USE_SYSTEM_NCCL, NCCL_INCLUDE_DIR, NCCL_ROOT_DIR,
                   NCCL_SYSTEM_LIB, USE_NCCL)
from .numpy_ import USE_NUMPY, NUMPY_INCLUDE_DIR
from .rocm import USE_ROCM
from .nnpack import USE_NNPACK
from .qnnpack import USE_QNNPACK

def _which(thefile):
    path = os.environ.get("PATH", os.defpath).split(os.pathsep)
    for d in path:
        fname = os.path.join(d, thefile)
        fnames = [fname]
        if IS_WINDOWS:
            exts = os.environ.get('PATHEXT', '').split(os.pathsep)
            fnames += [fname + ext for ext in exts]
        for name in fnames:
            if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name):
                return name
    return None

def _mkdir_p(d):
    try:
        os.makedirs(d)
    except OSError:
        pass

# Ninja
# Use ninja if it is on the PATH. Previous version of PyTorch required the
# ninja python package, but we no longer use it, so we do not have to import it
USE_NINJA = (not check_negative_env_flag('USE_NINJA') and
             _which('ninja') is not None)

def get_version(cmd):
    "Returns cmake version."

    for line in check_output([cmd, '--version']).decode('utf-8').split('\n'):
        if 'version' in line:
            return LooseVersion(line.strip().split(' ')[2])
    raise Exception('no version found')

def get_command():
    "Returns cmake command."

    cmake_command = 'cmake'
    if IS_WINDOWS:
        return cmake_command
    cmake3 = _which('cmake3')
    if cmake3 is not None:
        cmake = _which('cmake')
        if cmake is not None:
            bare_version = get_version(cmake)
            if (bare_version < LooseVersion("3.5.0") and
                    get_version(cmake3) > bare_version):
                cmake_command = 'cmake3'
    return cmake_command

def defines(lst, **kwargs):
    "Add definitions to cmake."
    for key, value in sorted(kwargs.items()):
        if value is not None:
            lst.append('-D{}={}'.format(key, value))

def get_build_type():
    "Get the build type."
    if DEBUG:
        return "Debug"
    elif REL_WITH_DEB_INFO:
        return "RelWithDebInfo"
    else:
        return "Release"

def run(version,
        cmake_python_library,
        build_python,
        build_test,
        build_dir,
        my_env):
    "Run cmake."

    cmake_args = [
        get_command()
    ]
    if USE_NINJA:
        cmake_args.append('-GNinja')
    elif IS_WINDOWS:
        generator = os.getenv('CMAKE_GENERATOR', 'Visual Studio 15 2017')
        supported = ['Visual Studio 15 2017', 'Visual Studio 16 2019']
        if generator not in supported:
            print('Unsupported `CMAKE_GENERATOR`: ' + generator)
            print('Please set it to one of the following values: ')
            print('\n'.join(supported))
            exit(1)
        cmake_args.append('-G' + generator)
        toolset_dict = {}
        toolset_version = os.getenv('CMAKE_GENERATOR_TOOLSET_VERSION')
        if toolset_version is not None:
            toolset_dict['version'] = toolset_version
            curr_toolset = os.getenv('VCToolsVersion')
            if curr_toolset is None:
                print('When you specify `CMAKE_GENERATOR_TOOLSET_VERSION`, you must also '
                      'activate the vs environment of this version. Please read the notes '
                      'in the build steps carefully.')
                exit(1)
        if IS_64BIT:
            cmake_args.append('-Ax64')
            toolset_dict['host'] = 'x64'
        if toolset_dict:
            toolset_expr = ','.join(["{}={}".format(k, v) for k, v in toolset_dict.items()])
            cmake_args.append('-T' + toolset_expr)

    cflags = os.getenv('CFLAGS', "") + " " + os.getenv('CPPFLAGS', "")
    ldflags = os.getenv('LDFLAGS', "")
    if IS_WINDOWS:
        defines(cmake_args,
                MSVC_Z7_OVERRIDE=os.getenv('MSVC_Z7_OVERRIDE', "ON"))
        cflags += " /EHa"

    base_dir = os.path.dirname(os.path.dirname(os.path.dirname(
        os.path.abspath(__file__))))
    install_dir = os.path.join(base_dir, "torch")

    _mkdir_p(install_dir)
    _mkdir_p(build_dir)

    defines(
        cmake_args,
        PYTHON_EXECUTABLE=escape_path(sys.executable),
        PYTHON_LIBRARY=escape_path(cmake_python_library),
        PYTHON_INCLUDE_DIR=escape_path(distutils.sysconfig.get_python_inc()),
        BUILDING_WITH_TORCH_LIBS=os.getenv("BUILDING_WITH_TORCH_LIBS", "ON"),
        TORCH_BUILD_VERSION=version,
        CMAKE_BUILD_TYPE=get_build_type(),
        BUILD_PYTHON=build_python,
        BUILD_SHARED_LIBS=os.getenv("BUILD_SHARED_LIBS", "ON"),
        BUILD_BINARY=check_env_flag('BUILD_BINARY'),
        BUILD_TEST=build_test,
        INSTALL_TEST=build_test,
        BUILD_CAFFE2_OPS=not check_negative_env_flag('BUILD_CAFFE2_OPS'),
        ONNX_NAMESPACE=os.getenv("ONNX_NAMESPACE", "onnx_torch"),
        ONNX_ML=not check_negative_env_flag("ONNX_ML"),
        USE_CUDA=USE_CUDA,
        USE_DISTRIBUTED=USE_DISTRIBUTED,
        USE_FBGEMM=not (check_env_flag('NO_FBGEMM') or
                        check_negative_env_flag('USE_FBGEMM')),
        NAMEDTENSOR_ENABLED=(check_env_flag('USE_NAMEDTENSOR') or
                             check_negative_env_flag('NO_NAMEDTENSOR')),
        USE_NUMPY=USE_NUMPY,
        NUMPY_INCLUDE_DIR=escape_path(NUMPY_INCLUDE_DIR),
        USE_SYSTEM_NCCL=USE_SYSTEM_NCCL,
        NCCL_INCLUDE_DIR=NCCL_INCLUDE_DIR,
        NCCL_ROOT_DIR=NCCL_ROOT_DIR,
        NCCL_SYSTEM_LIB=NCCL_SYSTEM_LIB,
        CAFFE2_STATIC_LINK_CUDA=check_env_flag('USE_CUDA_STATIC_LINK'),
        USE_ROCM=USE_ROCM,
        USE_NNPACK=USE_NNPACK,
        USE_LEVELDB=check_env_flag('USE_LEVELDB'),
        USE_LMDB=check_env_flag('USE_LMDB'),
        USE_OPENCV=check_env_flag('USE_OPENCV'),
        USE_QNNPACK=USE_QNNPACK,
        USE_TENSORRT=check_env_flag('USE_TENSORRT'),
        USE_FFMPEG=check_env_flag('USE_FFMPEG'),
        USE_SYSTEM_EIGEN_INSTALL="OFF",
        USE_MKLDNN=USE_MKLDNN,
        USE_NCCL=USE_NCCL,
        NCCL_EXTERNAL=USE_NCCL,
        CMAKE_INSTALL_PREFIX=install_dir,
        CMAKE_C_FLAGS=cflags,
        CMAKE_CXX_FLAGS=cflags,
        CMAKE_EXE_LINKER_FLAGS=ldflags,
        CMAKE_SHARED_LINKER_FLAGS=ldflags,
        THD_SO_VERSION="1",
        CMAKE_PREFIX_PATH=(os.getenv('CMAKE_PREFIX_PATH') or
                           distutils.sysconfig.get_python_lib()),
        BLAS=os.getenv('BLAS'),
        CUDA_NVCC_EXECUTABLE=escape_path(os.getenv('CUDA_NVCC_EXECUTABLE')),
        USE_REDIS=os.getenv('USE_REDIS'),
        USE_GLOG=os.getenv('USE_GLOG'),
        USE_GFLAGS=os.getenv('USE_GFLAGS'),
        USE_ASAN=check_env_flag('USE_ASAN'),
        WERROR=os.getenv('WERROR'))

    if os.getenv('_GLIBCXX_USE_CXX11_ABI'):
        defines(cmake_args, GLIBCXX_USE_CXX11_ABI=os.getenv('_GLIBCXX_USE_CXX11_ABI'))

    if os.getenv('USE_OPENMP'):
        defines(cmake_args, USE_OPENMP=check_env_flag('USE_OPENMP'))

    if os.getenv('USE_TBB'):
        defines(cmake_args, USE_TBB=check_env_flag('USE_TBB'))

    if os.getenv('MKL_SEQ'):
        defines(cmake_args, INTEL_MKL_SEQUENTIAL=check_env_flag('MKL_SEQ'))

    if os.getenv('MKL_TBB'):
        defines(cmake_args, INTEL_MKL_TBB=check_env_flag('MKL_TBB'))

    mkldnn_threading = os.getenv('MKLDNN_THREADING')
    if mkldnn_threading:
        defines(cmake_args, MKLDNN_THREADING=mkldnn_threading)

    parallel_backend = os.getenv('PARALLEL_BACKEND')
    if parallel_backend:
        defines(cmake_args, PARALLEL_BACKEND=parallel_backend)

    if USE_GLOO_IBVERBS:
        defines(cmake_args, USE_IBVERBS="1", USE_GLOO_IBVERBS="1")

    if USE_MKLDNN:
        defines(cmake_args, MKLDNN_ENABLE_CONCURRENT_EXEC="ON")

    expected_wrapper = '/usr/local/opt/ccache/libexec'
    if IS_DARWIN and os.path.exists(expected_wrapper):
        defines(cmake_args,
                CMAKE_C_COMPILER="{}/gcc".format(expected_wrapper),
                CMAKE_CXX_COMPILER="{}/g++".format(expected_wrapper))
    for env_var_name in my_env:
        if env_var_name.startswith('gh'):
            # github env vars use utf-8, on windows, non-ascii code may
            # cause problem, so encode first
            try:
                my_env[env_var_name] = str(my_env[env_var_name].encode("utf-8"))
            except UnicodeDecodeError as e:
                shex = ':'.join('{:02x}'.format(ord(c)) for c in my_env[env_var_name])
                print('Invalid ENV[{}] = {}'.format(env_var_name, shex),
                      file=sys.stderr)
                print(e, file=sys.stderr)
    # According to the CMake manual, we should pass the arguments first,
    # and put the directory as the last element. Otherwise, these flags
    # may not be passed correctly.
    # Reference:
    # 1. https://cmake.org/cmake/help/latest/manual/cmake.1.html#synopsis
    # 2. https://stackoverflow.com/a/27169347
    cmake_args.append(base_dir)
    pprint(cmake_args)
    check_call(cmake_args, cwd=build_dir, env=my_env)


import socket
import sys
import time
import json
from argparse import ArgumentParser
import cv2
import numpy as np

def build(method='POST', path='/security/camera/alert', msg=None):
    method = method
    path = path
    data = dict()
    data['time'] = time.ctime()
    data['epoch'] = int(time.time() * 1000)
    if msg is None:
        data['message'] = 'Motion Detected'
    else:
        data['message'] = msg

    return (method + ' ' + path + '\r\n\r\n' + json.dumps(data)).encode()

def alert(host_ip=None, hostname=None, port=None, data=None):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print("Socket successfully created")
    except socket.error as err:
        print("socket creation failed with error {}".format(err))

    if host_ip is None:
        if hostname is None:
            print("Error: Need to include either a hostname or host ip")
            sys.exit()
        else:
            try:
                host_ip = socket.gethostbyname(hostname)
            except socket.gaierror:
                # this means could not resolve the host
                print("there was an error resolving the host")
                sys.exit()
    if port is None:
        port = 80

    try:
        s.connect((host_ip, port))

        print("the socket has successfully connected to {}".format(host_ip))

        if data is None:
            data = build()

        s.send(data)

        print(data)

        s.close()

        print("Successfully sent message")
    except ConnectionRefusedError as e:
        print("[ERROR] Unable to connect to server")
        s.close()

def main():
    parser = ArgumentParser()
    parser.add_argument("--host-name", "-hn", required=False, type=str)
    parser.add_argument("--port", "-p", required=False, type=int)
    args = parser.parse_args()
    cv2.namedWindow("TEST")
    vc = cv2.VideoCapture(0)
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        rval = False

    while rval:
        cv2.imshow("TEST", frame)
        rval, frame = vc.read()
        key = cv2.waitKey(20)
        if key == 27:
            break

    cv2.destroyWindow("TEST")
    vc.release()
    i = 0
    while i < 5:
        msg = input("Input message")
        alert(hostname=args.host_name, port=args.port, data=build(msg=msg))
        i += 1

    # alert(host_ip='192.168.0.190')

if __name__ == '__main__':
    main()


import numpy as np
from collections import namedtuple
from scipy.stats import norm

# local
import estimator
from misc import *

TestResult = namedtuple("TestResult", ["test_val", "res_joint", "res_sep"])
EPOCHS = 10000
EPOCHS_BOOTSTRAP = 500

def cheat_estimation(train_func, is_separated, *args, **kwargs):
    tau = .1
    quantile = norm.ppf(tau)

    theta1 = dict_of_pars(np.array([[0.0, 0.2], [0.2, 0.0]]) * quantile,
                          np.array([[0.5, 0.0], [0.0, 0.5]]),
                          np.array([[0.5, 0.5]]) * quantile)
    theta2 = dict_of_pars(np.array([[0.0, 0.2], [0.2, 0.0]]) * quantile,
                          np.array([[-0.5, 0.0], [0.0, 0.5]]),
                          np.array([[0.5, 0.5]]) * quantile)
    if not is_separated:
        cheat_pars = [theta1, theta2]
    else:
        cheat_pars = [pars_left_right(theta1, theta1), pars_left_right(theta1, theta2),
                      pars_left_right(theta2, theta2)]

    ress = [train_func(*args, **kwargs, init_pars=pars) for pars in cheat_pars]
    _, idx = min([(res.loss, i) for (i, res) in enumerate(ress)])
    return ress[idx]

def _train_mvcaviar(*args, **kwargs):
    return cheat_estimation(estimator.train_mvcaviar, *args, **kwargs)

def _train_mvcaviar_w_shift(*args, **kwargs):
    return cheat_estimation(estimator.train_mvcaviar_w_shift, *args, **kwargs)

def _train_mvcaviar_separated(*args, **kwargs):
    return cheat_estimation(estimator.train_mvcaviar_separated2, *args, **kwargs)

class _CPTestWrapper:

    def __init__(self, Y, tau, breaks, message="X", prev_quantile=None):
        self.Y = Y
        self.breaks = breaks
        self.tau = tau
        self.message = message
        self.counter = 0
        (self.tmax, self.n) = np.shape(Y)
        self.prev_quantile = prev_quantile

        self.do_test()

    def do_test(self):
        self.res_joint = _train_mvcaviar(self.Y, self.tau, epochs=EPOCHS, prev_quantile=self.prev_quantile)
        self.res_breaks = [
            _train_mvcaviar_separated(self.Y, self.tau, break_point, epochs=EPOCHS,
                                               #init_pars=pars_left_right(self.res_joint.pars, self.res_joint.pars),
                                               prev_quantile=self.prev_quantile)
            for break_point in self.breaks
        ]
        self.test_val = self.res_joint.loss - min([res.loss for res in self.res_breaks])

        print("Task {}: original test done".format(self.message))

    def __call__(self, weights):
        vals = []

        for i, break_point in enumerate(self.breaks):
            shift = pars_diff(get_right_pars(self.res_breaks[i].pars), get_left_pars(self.res_breaks[i].pars))
            res_joint = estimator.train_mvcaviar_w_shift(
                self.Y, self.tau, break_point, shift,
                prev_quantile=self.res_breaks[i].prev_quantile,
                init_pars=get_left_pars(self.res_breaks[i].pars),
                sample_weights=weights, epochs=EPOCHS_BOOTSTRAP
            )
            res_break = estimator.train_mvcaviar_separated(
                self.Y, self.tau, break_point,
                prev_quantile=self.res_breaks[i].prev_quantile,
                init_pars=pars_left_right(res_joint.pars, pars_sum(res_joint.pars, shift)),
                sample_weights=weights, epochs=EPOCHS_BOOTSTRAP
            )
            vals.append(res_joint.loss - res_break.loss)

        self.counter += 1
        print("Task {}: did {} bootstrap tests".format(self.message, self.counter))
        return max(vals)

def bstrap_cp_test(Y, tau, breaks, SIM_NUM, message="X", prev_quantile=None, cheat_pars=None):
    assert(len(breaks) > 0)
    wrapper = _CPTestWrapper(Y, tau, breaks, message=message, prev_quantile=prev_quantile)

    np.random.seed(1)

    weights_list = [generate_random_weights(np.shape(Y)[0]-1) for x in range(SIM_NUM)]
    btest_vals = list(map(wrapper, weights_list))
    return wrapper.test_val, btest_vals

def select_homogeneity_interval_with_single_break(Y, tau, lengths, SIM_NUM=400):
    return


# -*- encoding: utf-8 -*-
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, InvalidPage

from aplication.core.utils.url_gen import urlGen
from aplication.news_feed.models import NewsFeedModels, FeedModels
from aplication.news_feed.utils import split_news

def posts_feed_blog(request, slug):

    feed = get_object_or_404(FeedModels, slug=slug)
    news_list = NewsFeedModels.objects.filter(feed=feed)
    news_list = split_news(news_list)
    news_paginator = Paginator(news_list, 12)

    try:
        page = int(request.REQUEST.get('page', '1'))
    except ValueError:
        page = 1

    try:
        news = news_paginator.page(page)
    except (EmptyPage, InvalidPage):
        # If page is out of range (e.g. 9999), deliver last page of results.
        news = news_paginator.page(news_paginator.num_pages)

    url = urlGen()
    pageURI = url.generate('page', request.GET)
    items = len(news_list)

    context = {
        'title_page': 'Blog - %s' % (feed.title),
        'prefix_sub_title_page': 'Notícas',
        'sub_title_page': feed.title,
        'news_list': news,
        'total_item': items,
        'pageURI': pageURI,
        'current': page,
    }
    return render(request, 'news_feed/blog.html', context)


import ast

from typing import List, Tuple, Union

from nagini_translation.lib import silver_nodes as sil
from nagini_translation.lib.context import Context
from nagini_translation.lib.jvmaccess import JVM
from nagini_translation.lib.program_nodes import (
    PythonMethod,
    PythonVar,
)
from nagini_translation.lib.typedefs import (
    Expr,
    Info,
    Field,
    Method,
    Position,
    Predicate,
    Stmt,
    StmtsAndExpr,
    VarDecl,
)
from nagini_translation.lib.typeinfo import TypeInfo
from nagini_translation.lib.util import (
    get_func_name,
    InvalidProgramException,
    UnsupportedException,
)
from nagini_translation.lib.viper_ast import ViperAST
from nagini_translation.translators.abstract import TranslatorConfig
from nagini_translation.translators.common import CommonTranslator
from nagini_translation.translators.obligation.loop import (
    LoopObligationTranslator,
)
from nagini_translation.translators.obligation.manager import (
    ObligationManager,
)
from nagini_translation.translators.obligation.method import (
    MethodObligationTranslator,
)
from nagini_translation.translators.obligation.obligation_info import (
    PythonMethodObligationInfo,
)
from nagini_translation.translators.obligation.waitlevel import (
    WaitLevelTranslator,
)

class ObligationTranslator(CommonTranslator):
    """Translator for obligations."""

    def __init__(self, config: TranslatorConfig, jvm: JVM, source_file: str,
                 type_info: TypeInfo, viper_ast: ViperAST) -> None:
        super().__init__(config, jvm, source_file, type_info, viper_ast)
        self._obligation_manager = ObligationManager()
        self._method_translator = MethodObligationTranslator(
            config, jvm, source_file, type_info, viper_ast,
            self._obligation_manager)
        self._loop_translator = LoopObligationTranslator(
            config, jvm, source_file, type_info, viper_ast,
            self._obligation_manager)
        self._wait_level_translator = WaitLevelTranslator(
            config, jvm, source_file, type_info, viper_ast,
            self._obligation_manager)

    def enter_loop_translation(
            self, node: Union[ast.While, ast.For], ctx: Context,
            err_var: PythonVar = None) -> None:
        """Update context with info needed to translate loop."""
        self._loop_translator.enter_loop_translation(node, ctx, err_var)

    def leave_loop_translation(self, ctx: Context) -> None:
        """Remove loop translation info from context."""
        self._loop_translator.leave_loop_translation(ctx)

    def create_while_node(
            self, ctx: Context, cond: Expr,
            invariants: List[Expr],
            local_vars: List[VarDecl],
            body: Stmt, node: Union[ast.While, ast.For]) -> List[Stmt]:
        """Construct a while loop AST node with obligation stuff."""
        return self._loop_translator.create_while_node(
            ctx, cond, invariants, local_vars, body, node)

    def translate_obligation_contractfunc_call(
            self, node: ast.Call, ctx: Context, impure: bool) -> StmtsAndExpr:
        """Translate a call to obligation contract function."""
        func_name = get_func_name(node)
        if func_name == 'MustTerminate':
            if not impure:
                raise InvalidProgramException(node, 'invalid.contract.position')
            return self._translate_must_terminate(node, ctx)
        elif func_name == 'MustRelease':
            if not impure:
                raise InvalidProgramException(node, 'invalid.contract.position')
            return self._translate_must_release(node, ctx)
        elif func_name == 'WaitLevel':
            raise InvalidProgramException(
                node, 'invalid.wait_level.use')
        elif func_name == 'Level':
            raise InvalidProgramException(
                node, 'invalid.level.use')
        else:
            raise UnsupportedException(
                node, 'Unsupported contract function.')

    def translate_must_invoke_token(
            self, node: ast.Call, ctx: Context) -> StmtsAndExpr:
        """Translate a call to ``token``."""
        if ctx.obligation_context.is_translating_loop():
            return self._loop_translator.translate_must_invoke(node, ctx)
        else:
            return self._method_translator.translate_must_invoke(node, ctx)

    def translate_must_invoke_ctoken(
            self, node: ast.Call, ctx: Context) -> StmtsAndExpr:
        """Translate a call to ``ctoken``."""
        if ctx.obligation_context.is_translating_loop():
            return self._loop_translator.translate_may_invoke(
                node, ctx)
        elif ctx.obligation_context.is_translating_posts:
            if ctx.actual_function.name != 'Gap':
                raise InvalidProgramException(
                    node, 'invalid.postcondition.ctoken_not_allowed')
            else:
                return self._method_translator.translate_may_invoke(
                    node, ctx)
        else:
            return self._method_translator.translate_may_invoke(
                node, ctx)

    def is_wait_level_comparison(self, node: ast.Compare,
                                 ctx: Context) -> bool:
        """Check if we are comparing with ``WaitLevel``."""
        return self._wait_level_translator.is_wait_level_comparison(
            node, ctx)

    def translate_wait_level_comparison(self, node: ast.Compare,
                                        ctx: Context) -> StmtsAndExpr:
        """Translate comparison with ``WaitLevel``."""
        return self._wait_level_translator.translate_wait_level_comparison(
            node, ctx)

    def create_level_call(self, expr: sil.RefExpression) -> sil.PermExpression:
        return self._wait_level_translator.create_level_call(expr)

    def create_level_below(
            self, expr: sil.PermExpression,
            residue_level_var: sil.PermExpression,
            ctx: Context) -> sil.BoolExpression:
        return self._wait_level_translator.create_level_below(
            expr, residue_level_var, ctx)

    def initialize_current_wait_level(
            self, current_wait_level: sil.PermExpression,
            residue_level_var: sil.PermExpression,
            ctx: Context) -> sil.BoolExpression:
        return self._wait_level_translator.initialize_current_wait_level(
            current_wait_level, residue_level_var, ctx)

    def _translate_must_terminate(
            self, node: ast.Call, ctx: Context) -> StmtsAndExpr:
        """Translate a call to ``MustTerminate``."""
        if ctx.obligation_context.is_translating_loop():
            return self._loop_translator.translate_must_terminate(node, ctx)
        elif ctx.obligation_context.is_translating_posts:
            raise InvalidProgramException(
                node, 'obligation.must_terminate.in_postcondition')
        else:
            if ctx.current_function and ctx.current_function.pure:
                raise InvalidProgramException(node, 'invalid.contract.position')
            return self._method_translator.translate_must_terminate(node, ctx)

    def _translate_pure_must_terminate(
            self, node: ast.Call, ctx: Context) -> StmtsAndExpr:
        stmts, measure = self.translate_expr(node.args[0], ctx, target_type=self.viper.Int)
        if stmts:
            raise InvalidProgramException(node, "purity.violated")
        pos = self.to_position(node, ctx)
        info = self.no_info(ctx)
        obligation_info = self._method_translator._get_obligation_info(ctx)
        guarded_obligation_instance = obligation_info.get_instance(node)
        if guarded_obligation_instance.guard:
            guards = []
            for g in guarded_obligation_instance.guard:
                stmts, new_guard = self.translate_expr(g, ctx, target_type=self.viper.Bool)
                if stmts:
                    raise InvalidProgramException(g, "purity.violated")
                guards.append(new_guard)
            guard_exp = self._conjoin(guards, pos, info)
        else:
            guard_exp = None
        decreases = self.viper.DecreasesTuple([measure], guard_exp, pos, info)
        return [], decreases

    def _translate_must_release(
            self, node: ast.Call, ctx: Context) -> StmtsAndExpr:
        """Translate a call to ``MustRelease``."""
        if ctx.obligation_context.is_translating_loop():
            return self._loop_translator.translate_must_release(node, ctx)
        else:
            return self._method_translator.translate_must_release(node, ctx)

    def get_obligation_preamble(
            self,
            ctx: Context) -> Tuple[List[Predicate], List[Field]]:
        """Construct obligation preamble.

        To track each obligation we use predicates which are defined in
        this preamble.
        """
        predicates = self._obligation_manager.create_predicates(self)
        fields = self._obligation_manager.create_fields(self)
        return predicates, fields

    def create_method_node(     # pylint: disable=too-many-arguments
            self, ctx: Context, name: str,
            args: List[VarDecl], returns: List[VarDecl],
            pres: List[Expr], posts: List[Expr],
            local_vars: List[VarDecl], body: List[Stmt],
            position: Position, info: Info,
            method: PythonMethod = None,
            overriding_check: bool = False) -> Method:
        """Construct method AST node with additional obligation stuff."""
        return self._method_translator.create_method_node(
            ctx, name, args, returns, pres, posts, local_vars, body,
            position, info, method, overriding_check)

    def create_method_call_node(
            self, ctx: Context, method_name: str, args: List[Expr],
            targets: List[Expr], position: Position, info: Info,
            target_method: PythonMethod = None,
            target_node: ast.Call = None) -> List[Stmt]:
        """Construct a method call AST node with obligation stuff."""
        return self._method_translator.create_method_call_node(
            ctx, method_name, args, targets, position, info,
            target_method, target_node)

    def create_method_fork(self, ctx: Context, targets, thread: Expr,
                           position: Position, info: Info,
                           target_node: ast.Call = None) -> List[Stmt]:
        return self._method_translator.create_method_fork(
            ctx, targets, thread, position, info, target_node)

    def create_obligation_info(self, method: PythonMethod) -> object:
        """Create obligation info for the method."""
        info = PythonMethodObligationInfo(
            self._obligation_manager, method, self)
        info.traverse_contract()
        return info


import unittest
import json
from src.loader import Loader

class TestLoader(unittest.TestCase):
    def setUp(self):
        self.loader = Loader(db_uri='mongodb://localhost:27017/', db_name="test_fever_unittest")
        self.coll_purchase = "purchase_detail"
        self.coll_events = "events_info"

    def tearDown(self):
        self.loader.delete_many(self.coll_purchase, {})
        self.loader.delete_many(self.coll_events, {})

    def test_insert_many_events(self):
        with open('/opt/repos/plan-test/test/out/events.json') as f:
            data = json.load(f)

        self.loader.insert_many(self.coll_events, data)

    def test_insert_many_purchase(self):
        with open('/opt/repos/plan-test/test/out/purchase.json') as f:
            data = json.load(f)

        self.loader.insert_many(self.coll_events, data)

    def test_insert_one(self):
        num_before = self.loader.count_elems(self.coll_events)

        self.loader.insert_one(self.coll_events,
                               {"city": "MAD", "event": "end_app_session", "os": "ios", "plan_id": "6846",
                                "time": 1530057600000, "user_id": 611248618,
                                "_id": "611248618|nan|2018-06-27 00:00:00|end_app_session|ios"})

        num_after = self.loader.count_elems(self.coll_events)

        self.assertEqual(num_after, num_before + 1)

    # def test_insert_many_one_by_one(self):
    #     num_before = self.loader.count_elems(self.coll_events)
    #
    #     with open('/opt/repos/plan-test/test/out/events.json') as f:
    #         data = json.load(f)
    #
    #     for json_elem in data:
    #         self.loader.insert_one(self.coll_events, json_elem)
    #
    #     num_after = self.loader.count_elems(self.coll_events)
    #
    #     self.assertEqual(num_after, num_before + len(data))

    # def test_upsert_many_one_by_one(self):
    #     jsons = [{"city": "MAD", "event": "end_app_session", "os": "ios", "plan_id": "6846",
    #      "time": 1530057600000, "user_id": 611248618,
    #      "_id": "611248618|nan|2018-06-27 00:00:00|end_app_session|ios"},
    #
    #     {"city": "MAD", "event": "end_app_session", "os": "ios", "plan_id": "6846",
    #      "time": 1530057600000, "user_id": 611248618,
    #      "_id": "ABC|nan|2018-06-27 00:00:00|end_app_session|ios"}]
    #
    #     collection = self.loader.get_collection(self.coll_events)
    #     for document in jsons:
    #         id_mongo = document['_id']
    #         collection.update_one(filter={'_id': id_mongo}, update={"$set": document},upsert=True)

if __name__ == '__main__':
    unittest.main()


from copy import deepcopy
import boto3

DEFAULT_RESPONSE = {
    'statusCode': 500
}

class SSMHelper:

    def __init__(self, logger):
        self.client = boto3.client('ssm')
        self.logger = logger
        self.parameter_name = '/GerenciaSolo/SNSTopicARN'

    def get_parameter(self) -> dict:
        final_response = deepcopy(DEFAULT_RESPONSE)
        try:
            response = self.client.get_parameter(Name=self.parameter_name)
            message = f'Parametro {self.parameter_name} obtido!'
            final_response['statusCode'] = 200
            final_response['topicArn'] = response.get('Parameter').get('Value')
        except Exception as e:
            final_response['topicArn'] = ''
            message = f'Erro ao obter parametro .:. Exception: {e}'

        self.logger.info(message)
        final_response['message'] = message
        return final_response


import pendulum
from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator

with DAG(
    dag_id="jinja-example",
    schedule_interval="@daily",
    start_date=pendulum.datetime(2022, 1, 1, tz="Europe/Brussels"),
) as dag:
    revenue = PostgresOperator(
        task_id="query_revenue",
        postgres_conn_id="postgres_default",
        sql="sql/daily_revenue.sql",
        params={"table": "SALES"},
    )


from falcon import HTTPUnauthorized

from .models import Token
from ..utils import utcnow_datetime_aware, Session_Maker

# Authenticates Request
def authenticate_request(req, Api_Session):
    value = req.get_header('Authorization')

    # Check existence of token
    if value is None:
        msg = 'Authorization required'
        raise HTTPUnauthorized('Unauthorized', msg)
    else:
        value = value.split(' ')[1]

    # Verify validity of token
    sm = Session_Maker(Api_Session)
    with sm as session:
        token = session.query(Token).filter(Token.value == value).first()

        # Check existence of token
        if token is None:
            msg = "Invalid token."
            raise HTTPUnauthorized("Invalid token", msg)

        # Check expiration of token
        if utcnow_datetime_aware() > token.expires_at:
            msg = "Expired token."
            raise HTTPUnauthorized("Expired token", msg)


import cv2 as cv

img = cv.imread('Photos/lady.jpg')
cv.imshow('Lady', img)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Graysclae', gray)

haar_cascade = cv.CascadeClassifier('haar_face.xml') # - reads
# cv.CascadeClassifier (xml file) reads the xml trained data

faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3 )
print(f'number of faces found = {len(faces_rect)}')
# number of faces found = 1

# - draw a rectangle
for (x,y,w,h) in faces_rect:
    cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)

cv.imshow('detected face', img)

# group 2
group =  cv.imread('Photos/group 2.jpg')
gray_group = cv.cvtColor(group, cv.COLOR_BGR2GRAY)
cv.imshow('Gray group 2', gray_group)
# faces_rect = haar_cascade.detectMultiScale(gray_group, scaleFactor=1.1, minNeighbors=3 )

# shows 7 ()additionally stomachs as faces
#  but true val is 5, it is because of the noise in the image
# -- so we increase min neighbours to 7
faces_rect = haar_cascade.detectMultiScale(gray_group, scaleFactor=1.1, minNeighbors=6)
print(f'number of faces found = {len(faces_rect)}')
# ---
for (x,y,w,h) in faces_rect:
    cv.rectangle(group, (x,y), (x+w,y+h), (0,255,0), thickness=2)

cv.imshow('Group', group)

# group 1
group1 =  cv.imread('Photos/group 1.jpg')
gray_group1 = cv.cvtColor(group1, cv.COLOR_BGR2GRAY)
faces_rect = haar_cascade.detectMultiScale(gray_group1, scaleFactor=1.1, minNeighbors=1)
print(f'number of faces found = {len(faces_rect)}')
# number of faces found = 14 with k=3
# number of faces found = 19 with k=1
# --- so by minimizing k, we are more prone to noise
for (x,y,w,h) in faces_rect:
    cv.rectangle(group1, (x,y), (x+w,y+h), (0,255,0), thickness=2)
    # some faces are not chosen bc they are not perpendicular to the camera

cv.imshow('Group 1', group1)

cv.waitKey(0)


import os
import re
import shutil
import subprocess

from config import I2B2_PATH

def space_tokenizer(text):
    tokens = re.split('\ +', text)

    spans = []
    start = 0
    for token in tokens:
        if token == '':
            spans.append((start, start))
        else:

            this_span = re.search(re.escape(token), text[start:])
            assert this_span is not None

            this_span = this_span.span()
            spans.append((this_span[0] + start, this_span[1] + start))

            start += this_span[1]

    return tokens, spans

def convert_i2b2_format(all_iob_sequences, output_path, sort_files=True,
        test_texts_path=os.path.join(I2B2_PATH, '2010/test/texts/')):

    os.makedirs(output_path, exist_ok=True)

    txt_filenames = os.listdir(test_texts_path)
    if sort_files:
        txt_filenames.sort()

    for filename in txt_filenames:

        text = open(test_texts_path + filename, 'r', encoding='utf-8-sig').read()

        sentences = text.split('\n')
        token_sequences_with_spans = [space_tokenizer(sentence) for sentence in sentences]

        sentence_id = 0
        annotations = []
        for token_sequence, span_sequence in token_sequences_with_spans:

            iob_sequence = all_iob_sequences.pop(0)
            iob_sequence = list(iob_sequence)
            if len(iob_sequence) != len(token_sequence):
                if len(iob_sequence) + 1 == len(token_sequence) and token_sequence[-1] == '':
                    iob_sequence += ['O']
                elif len(iob_sequence) == len(token_sequence) + 1 and iob_sequence[-1] == 'O':
                    iob_sequence.pop(-1)
                else:
                    print('Lengths don\'t match:')
                    print(iob_sequence, token_sequence)
                    raise ValueError

            last_iob_tag_type = None
            last_iob_tag_position = None

            last_span_start = 0
            last_span_end = 0
            last_id_start = 0
            last_id_end = 0

            token_id = 0
            for token, span in zip(token_sequence, span_sequence):

                iob_tag = iob_sequence.pop(0)
                if iob_tag == 'O':
                    iob_tag_position = ''
                    iob_tag_type = 'O'
                else:
                    iob_tag_position = iob_tag.split('-')[0]
                    iob_tag_type = iob_tag.split('-')[1]

                if last_iob_tag_type != iob_tag_type \
                    or (last_iob_tag_position == 'B' and iob_tag_position == 'B'):

                    if last_iob_tag_type != 'O' and last_iob_tag_type is not None:
                        annotations.append(
                            [sentence_id,
                             last_span_start, last_span_end,
                             last_id_start, last_id_end,
                             last_iob_tag_type])

                    last_iob_tag_type = iob_tag_type
                    last_iob_tag_position = iob_tag_position

                    last_span_start = span[0]
                    last_span_end = span[1]
                    last_id_start = token_id
                    last_id_end = token_id
                    #last_concept_id = concept_id
                else:
                    last_span_end = span[1]
                    last_id_end = token_id

                token_id += 1

            if last_iob_tag_type != 'O' and last_iob_tag_type is not None:
                annotations.append(
                    [sentence_id,
                     last_span_start, last_span_end,
                     last_id_start, last_id_end,
                     last_iob_tag_type])

            sentence_id += 1

        with open(os.path.join(output_path, filename[:-3] + 'con'), 'w') as writer:
            for ann in annotations:
                if ann[5]:
                    text = 'c="'
                    text += re.sub('\ +', ' ', sentences[ann[0]][ann[1]:ann[2]]).lower()
                    text += '" '
                    text += str(ann[0] + 1) + ':' + str(ann[3]) + ' ' + str(ann[0] + 1) + ':' + str(ann[4])
                    text += '||t="'
                    text += ann[5]
                    text += '"\n'

                    # print(text)
                    writer.write(text)

def i2b2_evaluation(all_iob_sequences, output_path, logger=None, verbose=True, sort_files=True,
        i2b2_path=I2B2_PATH):

    test_concepts_path = i2b2_path + '2010/test/concepts/'
    test_texts_path = i2b2_path + '2010/test/texts/'
    eval_jar = 'i2b2va-eval.jar'

    convert_i2b2_format(all_iob_sequences, output_path=output_path,
                        test_texts_path=test_texts_path, sort_files=sort_files)

    cmd = 'java -jar %s -rcp %s -scp %s -ft con -ex all' % (eval_jar, test_concepts_path, output_path)
    status = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE)
    result = status.stdout.decode()

    if verbose:
        if logger:
            logger.info(result)
            logger.info("----====----\n\n")
            logger.info(result.split('\n')[7])
        else:
            print(result)
            print("----====----\n\n")
            print(result.split('\n')[7])

    shutil.rmtree(output_path)

    return result.split('\n')[7].split()[-3:]


"""
Application Loader
"""
from .receiver import Receiver as ReceiverApp
from .cli import CommandLine as CommandLineApp
from .default import Default as DefaultApp
from .log_parser import LogParser as LogParserApp
from .. import VERSION
from ..framework.constants import APP_TYPE

class Loader:
    '''Bootstrap Factory
    '''
    @staticmethod
    def create(platform, options):
        '''Initial bootstrap instance
        '''
        print("[Info] Python driver version: {0} ".format(VERSION))

        active_app = None
        if platform == APP_TYPE.DEFAULT:
            active_app = DefaultApp(**options)

        if platform == APP_TYPE.CLI:
            active_app = CommandLineApp(**options)

        if platform == APP_TYPE.RECEIVER:
            active_app = ReceiverApp(**options)

        if platform == APP_TYPE.LOG_PARSER:
            active_app = LogParserApp(**options)

        if active_app is None:
            raise ValueError('no matched bootstrap')

        return active_app


from django.contrib import admin
from .models import Candidate
from .forms import CandidateForm
from django.utils.html import format_html

class CandidateAdmin(admin.ModelAdmin):
  radio_fields = {"smoker": admin.HORIZONTAL}
  form = CandidateForm
  exclude = ['status']
  list_filter = ['Situation']
  list_display = ['name', 'job', 'email', 'created_at', 'status', '_']
  search_fields = ['firstname', 'lastname', 'email', 'Situation']
  list_per_page = 10

  #Read Only field
  readonly_fields = ['experience', 'gender', 'firstname', 'lastname', 'job', 'email',
  'phone', 'salary', 'birth', 'personality', 'smoker', 'file', 'image', 'frameworks',
  'languages', 'databases', 'libraries', 'mobile', 'others', 'message', 'status_course',
  'started_course', 'finished_course', 'course', 'institution', 'about_course', 'started_job',
  'finished_job', 'company', 'position', 'about_job', 'employed', 'remote', 'travel']

  #Fieldset
  fieldsets = [
    # HR Operations
    ("HR OPERATIONS", {"fields": ['Situation', 'company_note']}),
    # Personal
    ("Personal", {"fields": ['experience', 'gender', 'job', 'email', 'phone',
    'salary', 'birth', 'personality', 'smoker', 'file', 'image', 'message']}),
    #Skills
    ("Skills", {"fields": ['frameworks', 'languages', 'databases', 'libraries', 'mobile', 'others']}),
    #Education
    ("Education", {"fields": ['status_course', 'started_course', 'finished_course',
    'course', 'institution', 'about_course']}),
    #Professional
    ("Professional", {"fields": ['started_job', 'finished_job', 'company', 'position',
    'about_job']}),
    #Note
    ("Note", {"fields": ['employed', 'remote', 'travel']}),
  ]

  #func to hide name
  def get_fields(self, request, obj = None):
    fields = super().get_fields(request, obj)
    if obj:
      fields.remove('firstname')
      fields.remove('lastname')
    return fields

  # Function to change the icon
  def _(self, obj):
    if obj.Situation == 'Accepted':
      return True
    elif obj.Situation == 'Pending':
      return None
    else:
      return False
  _.boolean = True

  # Func to color the text
  def status(self, obj):
    if obj.Situation == 'Accepted':
      color = '#28a745'
    elif obj.Situation == 'Pending':
      color = '#fea95e'
    else:
      color = 'red'
    return format_html('
{}
'.format(color, obj.Situation))
  status.allow_tags = True

admin.site.register(Candidate, CandidateAdmin)


from flask import Flask, render_template,request,jsonify, make_response
import requests
import numpy as np
import bs4 as bs
import urllib.request
import re
import nltk

app = Flask(__name__)

def get_content(url,lines):
    scraped_data = urllib.request.urlopen(url)
    article = scraped_data.read()

    parsed_article = bs.BeautifulSoup(article,'lxml')

    paragraphs = parsed_article.find_all('p')

    article_text = ""

    for p in paragraphs:
        article_text += p.text

    # Removing Square Brackets and Extra Spaces
    article_text = re.sub(r'\[[0-9]*\]', ' ', article_text)
    article_text = re.sub(r'\s+', ' ', article_text)

    # Removing special characters and digits
    formatted_article_text = re.sub('[^a-zA-Z]', ' ', article_text )
    formatted_article_text = re.sub(r'\s+', ' ', formatted_article_text)
    sentence_list = nltk.sent_tokenize(article_text)
    stopwords = nltk.corpus.stopwords.words('english')

    word_frequencies = {}

    for word in nltk.word_tokenize(formatted_article_text):
        if word not in stopwords:
            if word not in word_frequencies.keys():
                word_frequencies[word] = 1
            else:
                word_frequencies[word] += 1
    maximum_frequncy = max(word_frequencies.values())

    for word in word_frequencies.keys():
        word_frequencies[word] = (word_frequencies[word]/maximum_frequncy)
    sentence_scores = {}

    for sent in sentence_list:
        for word in nltk.word_tokenize(sent.lower()):
            if word in word_frequencies.keys():
                if len(sent.split(' ')) < 30:
                    if sent not in sentence_scores.keys():
                        sentence_scores[sent] = word_frequencies[word]
                    else:
                        sentence_scores[sent] += word_frequencies[word]

    import heapq
    summary_sentences = heapq.nlargest(int(lines), sentence_scores, key=sentence_scores.get)

    summary = ' '.join(summary_sentences)
    return(summary)

@app.route("/")

def test():
    return render_template("files.html")

def func():
    return "Task completed"

@app.route("/create", methods = ["POST"])

def create_entry():
    req = request.get_json()
    print(req)

    name = req["name"]
    lines = req["message"]
    print(name)

    url = (name)
    lines = (lines)

    url_content = get_content(url,lines)

    res=make_response(jsonify({"message": url_content}),200)

    return res

if __name__ == "__main__":
    app.run(debug=True)


import logging
import re
import tornado.escape
import tornado.web
import tornado.websocket
import uuid
from .main import BaseHandler
from tornado.web import authenticated
from utils.account import func

class RoomHandler(BaseHandler):
    """
    聊天室
    """

    @authenticated
    def get(self):
        ret = func(self.current_user)
        self.render('room.html', messages=ChatSocketHandler.cache, ret=ret)

class ChatSocketHandler(tornado.websocket.WebSocketHandler, BaseHandler):
    waiters = set()  # 等待接收消息的用户
    cache = []  # 存放消息
    cache_size = 200  # 消息列表的大小

    def get_compression_options(self):
        """
        非None的返回值开启压缩
        :return:
        """
        return {}

    def open(self):
        """
        新的WebSocke连接打开
        :return:
        """
        logging.info("new connection %s" % self)
        ChatSocketHandler.waiters.add(self)

    def on_close(self):
        """
        WebSocket连接断开
        :return:
        """
        return ChatSocketHandler.waiters.remove(self)

    @classmethod
    def update_cache(cls, chat):
        """
        跟新消息列表，加入新的消息
        :param chat:
        :return:
        """
        cls. \
            cache.append(chat)
        if len(cls.cache) > cls.cache_size:
            cls.cache = cls.cache[-cls.cache_size:]

    @classmethod
    def send_updates(cls, chat):
        """
        给每个等待接收的用户发新的消息
        :param chat:
        :return:
        """
        logging.info("sending message to %d waiters", len(cls.waiters))
        for waiter in cls.waiters:
            try:
                waiter.write_message(chat)
            except:
                logging.error("Error sending message", exc_info=True)

    def on_message(self, message):
        """WebSocket 服务器端接收到消息"""
        logging.info("got message %r", message)
        parsed = tornado.escape.json_decode(message)
        if parsed["body"] == '':
            return
        if re.findall(r'^http://.*.jpg$', parsed["body"]) != []:
            img_url = 'http://127.0.0.1:8000/save?url={}&from=room&user={}'.format(parsed["body"], self.current_user)
            from tornado.httpclient import AsyncHTTPClient
            from tornado.ioloop import IOLoop
            c = AsyncHTTPClient()
            IOLoop.current().spawn_callback(c.fetch, img_url)
            chat = {
                "id": str(uuid.uuid4()),
                "username": '--admin',
                "body": '{},url is processsing:{}'.format(self.current_user, parsed["body"]),
                "img": None,
            }
            chat["html"] = tornado.escape.to_basestring(self.render_string("message.html", message=chat))
            self.write_message(chat)
        else:
            chat = {
                "id": str(uuid.uuid4()),
                "body": parsed["body"],
                'username': '--{}'.format(self.current_user),
                "img": None,
            }
            chat["html"] = tornado.escape.to_basestring(self.render_string("message.html", message=chat))
            print(chat["html"])
            ChatSocketHandler.update_cache(chat)
            ChatSocketHandler.send_updates(chat)

{'AsyncHTTPClient': 'tornado.httpclient.AsyncHTTPClient', 'IOLoop': 'tornado.ioloop.IOLoop'}.update_cache

{'AsyncHTTPClient': 'tornado.httpclient.AsyncHTTPClient', 'IOLoop': 'tornado.ioloop.IOLoop'}.send_updates


import streamlit as st
import requests
from datetime import datetime
import logging

default_max_tokens=100
default_temperature=0.9
default_top_p=1.0
default_presence_penalty=0.0
default_frequency_penalty=0.0

def f(prompt,
        name,
        max_tokens=default_max_tokens,
        temperature=default_temperature,
        top_p=default_top_p,
        presence_penalty=default_presence_penalty,
        frequency_penalty=default_frequency_penalty):
    headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + st.secrets["API_KEY"],
    }
    data = '{\n  "prompt": "' + prompt + '",\n  "max_tokens": 100\n}'
    data = '{\n' + \
            f'"prompt": "{prompt}",\n' + \
            f'"max_tokens": {max_tokens},\n' + \
            f'"temperature": {temperature},\n' + \
            f'"top_p": {top_p},\n' + \
            f'"presence_penalty": {presence_penalty},\n' + \
            f'"frequency_penalty": {frequency_penalty},\n' + \
            f'"echo": true\n' + \
            '}'
    response = requests.post('https://api.openai.com/v1/engines/davinci/completions', headers=headers, data=data)
    if name != "":
        st.header(name + "!")
    else:
        name = 'guest'
    response_text = response.json()['choices'][0]['text'].replace("\n", " ")
    st.subheader(response_text)
    logging.error(f"{name},{response_text}")

advanced = st.sidebar.checkbox("Advanced settings", False)

st.write("# I will tell your fortune")

name = st.text_input("How do they call you?")

if advanced:
    prompt = st.text_input("Write anything here, I will continue", "Once upon a time, ")
    temprerature = st.slider("temperature (higher the temperature = crazyer fortune)", 0.0, 1.0, default_temperature, 0.01)
    max_tokens = st.slider("max_tokens", 1, 100, default_max_tokens, 1)
    top_p=st.slider("top_p", 0.0, 1.0, default_top_p, 0.01)
    presence_penalty=st.slider("presence_penalty", -2.0, 2.0, default_presence_penalty, 0.01)
    frequency_penalty=st.slider("frequency_penalty", -2.0, 2.0, default_frequency_penalty, 0.01)
    st.button("Tell fortune", on_click=f, args=(prompt, name, max_tokens, temprerature, top_p, presence_penalty, frequency_penalty))
else:
    prompt = st.selectbox(
        'What do you seek?',
        ('When I look into your palm, the future reveals itself to me. Next week you will',
        'The tarot cards never lie... I can cleary see that next year you will'))
    st.button("Tell fortune", on_click=f, args=(prompt, name))

st.image("https://s3-eu-west-1.amazonaws.com/dailyartartwork/img-2017031258c5aa9390ee2_ipad")