#! /usr/bin/env python
# -*- coding:utf8 -*-
from functools import lru_cache
import time

from framework.localconcurrent import ProcessExcecutor

@lru_cache
def my_func(num):
    print('start to call my function')
    time.sleep(5)
    print('complete calling my function')
    return 'done'

if __name__ == '__main__':
    #单进程
    # for i in range(1, 5, 1):
    #     print(my_func())

    #多进程下缓存无效
    results = ProcessExcecutor(2).execute(my_func, list(range(1, 6, 1)))
    for result in results:
        print(result)


import cv2
import numpy as np

image1 = cv2.imread("./weine.jpg")
image2 = cv2.imread("./FotoJet.jpg")

M = np.ones(image1.shape,dtype="uint8")*50
added = cv2.add(image1,image2)             # 將圖像image與M相減
subtracted = cv2.subtract(image1,image2)   # 將圖像image與M相減
multiplied = cv2.multiply(image1,image2)
divided = cv2.divide(image1,image2)
cv2.imshow("weine",image1)     #示原圖
cv2.imshow("tiger",image2)     #示原圖
cv2.imshow("add",added)            # 加運算圖
cv2.imshow("sub",subtracted)  # 減運算圖
cv2.imshow("mul",multiplied)#*
cv2.imshow("div",divided)#/

cv2.waitKey(0)
cv2.destroyAllWindows()


import csv
import time

import pytest

from conftest import botocore_client, custom_config_global

def iam_users():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_users"
    return (
        botocore_client.get("iam", "list_users", [], {})
        .extract_key("Users")
        .flatten()
        .values()
    )

def iam_admin_users():
    return [
        user for user in iam_users_with_policies_and_groups() if user_is_admin(user)
    ]

def iam_inline_policies(username):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_user_policies"
    return (
        botocore_client.get("iam", "list_user_policies", [], {"UserName": username})
        .extract_key("PolicyNames")
        .flatten()
        .values()
    )

def iam_managed_policies(username):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_attached_user_policies"
    return (
        botocore_client.get(
            "iam", "list_attached_user_policies", [], {"UserName": username}
        )
        .extract_key("AttachedPolicies")
        .flatten()
        .values()
    )

def iam_user_groups(username):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_groups_for_user"
    return (
        botocore_client.get("iam", "list_groups_for_user", [], {"UserName": username})
        .extract_key("Groups")
        .flatten()
        .values()
    )

def iam_user_group_inline_policies(username):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_group_policies"
    return [
        botocore_client.get(
            "iam", "list_group_policies", [], {"GroupName": group["GroupName"]}
        )
        .extract_key("PolicyNames")
        .flatten()
        .values()
        for group in iam_user_groups(username)
    ]

def iam_user_group_managed_policies(username):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_attached_group_policies"
    return [
        botocore_client.get(
            "iam", "list_attached_group_policies", [], {"GroupName": group["GroupName"]}
        )
        .extract_key("AttachedPolicies")
        .flatten()
        .values()
        for group in iam_user_groups(username)
    ]

def iam_all_user_policies(username):
    """
    Gets all policies that can be attached to a user. This includes:
        - Inline policies on the user
        - Managed policies on the user
        - Inline policies on the group that the user is in
        - Managed policies on the group that the user is in

    Inline policy API calls just return the name of the policy, so we create a single key dictionary to
    allow for standard access to the policy name ({'PolicyName': policy_name})
    """
    inline = []
    inline_policies = [
        iam_inline_policies(username=username)
        + iam_user_group_inline_policies(username=username)
    ]
    for policies in inline_policies:
        for policy_name in policies:
            if isinstance(policy_name, str):
                inline += {"PolicyName": policy_name}

    managed = [
        policy
        for policies in iam_managed_policies(username=username)
        + iam_user_group_managed_policies(username=username)
        for policy in policies
    ]

    return inline + managed

def iam_users_with_policies():
    return [
        {**{"Policies": iam_all_user_policies(username=user["UserName"])}, **user}
        for user in iam_users()
    ]

def iam_users_with_policies_and_groups():
    """Users with their associated Policies and Groups"""
    return [
        {**{"Groups": iam_user_groups(username=user["UserName"])}, **user}
        for user in iam_users_with_policies()
    ]

def iam_admin_login_profiles():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_login_profile"
    return iam_login_profiles(iam_admin_users())

def iam_admin_mfa_devices():
    "https://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_mfa_devices"
    return iam_mfa_devices(iam_admin_users())

def iam_user_login_profiles():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_login_profile"
    return iam_login_profiles(iam_users())

def iam_user_mfa_devices():
    "https://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_mfa_devices"
    return iam_mfa_devices(iam_users())

def iam_login_profiles(users):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_login_profile"
    return [
        botocore_client.get(
            "iam",
            "get_login_profile",
            [],
            {"UserName": user["UserName"]},
            result_from_error=lambda error, call: {"LoginProfile": None},
        )
        .extract_key("LoginProfile")
        .values()[0]
        for user in users
    ]

def iam_mfa_devices(users):
    "https://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_mfa_devices"
    return [
        botocore_client.get(
            "iam", "list_mfa_devices", [], {"UserName": user["UserName"]}
        )
        .extract_key("MFADevices")
        .values()[0]
        for user in users
    ]

def iam_roles():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_roles"
    return (
        botocore_client.get("iam", "list_roles", [], {})
        .extract_key("Roles")
        .flatten()
        .values()
    )

def iam_all_role_policies(rolename):
    return [
        {"PolicyName": policy_name}
        for policy_name in iam_role_inline_policies(rolename=rolename)
    ] + iam_role_managed_policies(rolename=rolename)

def iam_roles_with_policies():
    return [
        {**{"Policies": iam_all_role_policies(rolename=role["RoleName"])}, **role}
        for role in iam_roles()
    ]

def iam_role_inline_policies(rolename):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_role_policies"
    return (
        botocore_client.get("iam", "list_role_policies", [], {"RoleName": rolename})
        .extract_key("PolicyNames")
        .flatten()
        .values()
    )

def iam_role_managed_policies(rolename):
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_attached_role_policies"
    return (
        botocore_client.get(
            "iam", "list_attached_role_policies", [], {"RoleName": rolename}
        )
        .extract_key("AttachedPolicies")
        .flatten()
        .values()
    )

def iam_admin_roles():
    return [role for role in iam_roles_with_policies() if user_is_admin(role)]

def iam_access_keys_for_user(username):
    "https://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_access_keys"
    return (
        botocore_client.get("iam", "list_access_keys", [], {"UserName": username})
        .extract_key("AccessKeyMetadata")
        .flatten()
        .values()
    )

def iam_get_all_access_keys():
    return sum(
        [iam_access_keys_for_user(username=user["UserName"]) for user in iam_users()],
        [],
    )

def iam_generate_credential_report():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.generate_credential_report"
    results = botocore_client.get(
        "iam", "generate_credential_report", [], {}, do_not_cache=True
    ).results
    if len(results):
        return results[0].get("State")
    return ""

def iam_get_credential_report():
    "http://botocore.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_credential_report"
    # the story of the wack api
    while True:
        cred_report_state = iam_generate_credential_report()
        if cred_report_state not in ["STARTED", "INPROGRESS"]:
            break
        time.sleep(2)

    # We want this to blow up if it can't get the "Content"
    results = botocore_client.get(
        "iam", "get_credential_report", [], {}, do_not_cache=True
    ).results
    if not len(results):
        return []
    content = results[0]["Content"]
    decoded_content = content.decode("utf-8")
    return list(csv.DictReader(decoded_content.split("\n")))

# (ajvb) I'm not a big fan of this, but it seems to be the easiest way to
# only have to call `get_credential_report` once since it is not easily cacheable.
def iam_admin_users_with_credential_report():
    """Returns all "admin" users with an additional "CredentialReport" key,
    which is a dict containing their row in the Credentials Report.
    """
    admins = iam_admin_users()
    credential_report = iam_get_credential_report()

    for admin in admins:
        for user in credential_report:
            if admin["UserName"] == user["user"]:
                admin["CredentialReport"] = user
                break

    return admins

def user_is_admin(user):
    for policy in user["Policies"]:
        if isinstance(policy, dict):
            if policy.get("PolicyName", "") in custom_config_global.aws.admin_policies:
                return True

    for group in user.get("Groups", []):
        if isinstance(group, dict):
            if group.get("GroupName", "") in custom_config_global.aws.admin_groups:
                return True

    return False

def get_all_users_that_can_access_aws_account():
    """
    Returns users with console or API access to an AWS account.
    """
    profile_usernames = [
        profile["UserName"]
        for profile in iam_user_login_profiles()
        if profile is not None
    ]
    access_key_usernames = [
        akey["UserName"]
        for akey in iam_get_all_access_keys()
        if akey["Status"] == "Active"
    ]
    return set(profile_usernames + access_key_usernames)


from collections import deque
class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        r=deque()
        d=deque()
        n=len(senate)
        for i in range(n):
            if senate[i]=="R":
                r.append(i)
            else:
                d.append(i)
        j=0
        while(r and d):
            pos1=r.popleft()
            pos2=d.popleft()
            if pos1<pos2:
                r.append(n+j)
            else:
                d.append(n+j)
            j+=1
        if r:
            return 'Radiant'
        else:
            return 'Dire'


import networkx as nx
import newick

def parse_graphml_file_newick_format(filename: str, digraph=True):
    """
    Parses a graphml file and return the networkx graph. Forces each node to be in the newick.Node format.
    :param filename: str; full path of the to be parsed file
    :param digraph: Bool; is the graph a digraph
    :return: nx.Graph()
    """
    graphml_graph = nx.read_graphml(filename, node_type=newick.Node)
    if digraph:
        graphml_graph = graphml_graph.to_directed()

    for current_node in graphml_graph.nodes:
        graphml_graph.add_node(current_node, name=current_node, child_position=0)

    return graphml_graph

def parse_graphml_file(filename: str, digraph=True):
    """
    Parses a graphml file and return the networkx graph.
    :param filename: str; full path of the to be parsed file
    :param digraph: Bool; is the graph a digraph
    :return: nx.Graph()
    """
    graphml_graph = nx.read_graphml(filename)
    if digraph:
        graphml_graph = graphml_graph.to_directed()

    return graphml_graph

def parse_newick_file(filename: str, digraph=True):
    """
    Parses a newick file and returns the networkx graph.
    :param filename: str; full path of the to be parsed file
    :param digraph: Bool; is the graph a digraph
    :return: nx.Graph()
    """
    tree = newick.read(filename)

    if digraph:
        graph_newick = nx.DiGraph()
    else:
        graph_newick = nx.Graph

    none_counter = 1

    # Adding root node
    graph_newick.add_node(tree[0], child_position=0)

    while tree:
        tree_node = tree[0]
        tree_node, none_counter = rename_none_node(tree_node, none_counter)
        graph_newick, descendants, none_counter = add_newick_node_and_edge(graph_newick, tree_node, none_counter)
        tree += descendants
        tree.remove(tree_node)

    return graph_newick

def add_newick_node_and_edge(nx_graph: nx.Graph, current_node, none_counter: int):
    """
    Adding the descendants of the current node to the graph and adding the according edge.
    Rename a node if the name is None.
    :param nx_graph: the network x graph to which node and edge should be added
    :param current_node: the current node from the newick graph
    :param none_counter: an integer representing the amount of None node
    :return: (graph, descendants, none_counter)
    """
    descendants = current_node.descendants
    for child_pos in range(len(descendants)):
        descendants[child_pos], none_counter = rename_none_node(descendants[child_pos], none_counter)
        nx_graph.add_node(descendants[child_pos], child_position=child_pos)
        nx_graph.add_edge(current_node, descendants[child_pos])

    return nx_graph, descendants, none_counter

def rename_none_node(node_to_rename: newick.Node, counter):
    """
    Renaming node with no name to differ from other not named node.
    :param node_to_rename: node to be checked
    :param counter: int; counter for none nodes
    :return: (Node, int)
    """
    if node_to_rename.name is None:
        node_to_rename.name = str(node_to_rename.name) + "_" + str(counter)
        counter += 1
    return node_to_rename, counter


from abc import ABC
import inspect

def sig_to_param(signature):
    return {
        k: v.default
        for k, v in signature.parameters.items()
        if v.default is not inspect.Parameter.empty
    }

class BasePipe(ABC):
    pass

    @property
    def default_param(self):
        """Get the default parameters of the model.

        Returns
        -------
        dict:
            Dictionary with parameter: default value
        """
        cur_class = self.__class__
        default_parameters = sig_to_param(inspect.signature(self.__init__))
        while cur_class != BasePipe:
            signature = inspect.signature(super(cur_class, self).__init__)
            new_parameters = sig_to_param(signature)
            default_parameters.update(new_parameters)
            cur_class = cur_class.__bases__[0]
        return default_parameters


import string
from collections import defaultdict, Counter

# Average letter frequencies found in English (from Wikipedia)
letter_frequencies = [
  ('A', 0.08167),
  ('B', 0.01492),
  ('C', 0.02782),
  ('D', 0.04253),
  ('E', 0.12702),
  ('F', 0.02228),
  ('G', 0.02015),
  ('H', 0.06094),
  ('I', 0.06966),
  ('J', 0.00153),
  ('K', 0.00772),
  ('L', 0.04025),
  ('M', 0.02406),
  ('N', 0.06749),
  ('O', 0.07507),
  ('P', 0.01929),
  ('Q', 0.00095),
  ('R', 0.05987),
  ('S', 0.06327),
  ('T', 0.09056),
  ('U', 0.02758),
  ('V', 0.00978),
  ('W', 0.02360),
  ('X', 0.00150),
  ('Y', 0.01974),
  ('Z', 0.00074),
]

def all_substrings(text, size):
  '''Returns a list of all substrings with length of size in text'''
  length = len(text)
  return [text[i:i+size] for i in range(length) if i+size<=length]

def rotate_letter(letter, shift):
  '''Rotates the letter clockwise by the given shift value.'''
  rotated_letter = ord(letter) + shift
  if rotated_letter < ord('A'):
    rotated_letter = ord('Z') - (ord('A') - rotated_letter) + 1
  if rotated_letter > ord('Z'):
    rotated_letter = ord('A') + (rotated_letter - ord('Z')) - 1
  return chr(rotated_letter)

def calculate_distances(string_list):
  '''
  Takes in a list of strings and tells how far away matching elements are from
  one another. ['a','a'] has a distance of 1.
  Returns list((string, distance)).
  '''
  distances = []
  length = len(string_list)
  for i in range(length):
    for x in range(length):
      if string_list[i] == string_list[x] and i != x and i-x > 0:
        distances.append((string_list[i], i-x))
  return distances

def mod_counts(numbers, max_mod):
  '''
  Takes in a list(int). Calculates how many of the int%x==0 for 1<x<max_mod .
  Returns a defaultdict{mod, count} showing how many of the int%mod==0 .
  '''
  counts = defaultdict(int)
  for i in range(2, max_mod):
    for num in numbers:
      if num%i == 0:
        counts[i] += 1
  return counts

def find_shift_value(shift_column):
  '''
  Takes in a list of letters. Finds the most common occurrances.
  Uses these common occurrances to estimate how much the text was shifted.
  Returns a probable integer shift value.
  '''
  count = Counter(shift_column)
  # Ensure counts of 0 appear
  unfound_letters = [l for l in string.uppercase if l not in count]
  for l in unfound_letters: count[l] = 0
  total_letters = 0.0
  for l,c in count.most_common(): total_letters += c
  # Try to find the smallest difference between actual and expected frequencies
  differences = defaultdict(float)
  # Try shifting through every combination
  for r in range(len(letter_frequencies)):
    for l,f in letter_frequencies:
      rotated = rotate_letter(l, r)
      differences[r] += abs(f - count[rotated]/total_letters)
  # The smallest difference is most likely the shift value
  smallest = 0
  for s,d in differences.iteritems():
    if differences[s] < differences[smallest]:
      smallest = s
  return smallest

def vigenere_shift(text, shift_values):
  '''Rotates text by the shift_values given. Returns shifted value.'''
  key_length = len(shift_values)
  text = text.upper()
  shifted_letters = []
  for i in range(len(text)):
    rotated_letter = rotate_letter(text[i], -shift_values[i%key_length])
    shifted_letters.append(rotated_letter)
  return ''.join(shifted_letters)

def main():
  '''Main method'''
  cipher_text = raw_input('Enter the cipher text to decrypt: \n')
  print('Calculating...\n')
  substrings = all_substrings(cipher_text, 3)
  distances = calculate_distances(substrings)
  counts = mod_counts(zip(*distances)[1], 20)
  counts = [x for x in counts.iteritems()]
  counts.sort(key=lambda x: -x[1])
  # counts[x][0] should now contain key sizes from most to least probable
  key_size = counts[0][0]
  # Split letters by the key length and find the most common occurrences
  shift_values = []
  for i in range(key_size):
    shift_column = [cipher_text[x] for x in range(len(cipher_text)) if x%key_size==i]
    shift_values.append(find_shift_value(shift_column))
  decrypted_text = vigenere_shift(cipher_text, shift_values)
  print('\nExpected key: ')
  print(''.join([rotate_letter('A', c) for c in shift_values]))
  print('\nAlternative key (different starting shift sometimes encountered): ')
  print(''.join([rotate_letter('Z', c) for c in shift_values]))
  print('\nDecrypted text: ')
  print(decrypted_text)

# Call main method
if __name__ == '__main__':
  main()


"""Utilities for working with DRS objects."""
import os
import requests

from functools import lru_cache
from collections import namedtuple
from typing import Dict, List, Tuple, Optional, Union, Iterable
from requests import Response

from terra_notebook_utils import WORKSPACE_BUCKET, WORKSPACE_NAME, DRS_RESOLVER_URL, WORKSPACE_NAMESPACE, \
    WORKSPACE_GOOGLE_PROJECT
from terra_notebook_utils import workspace, gs, tar_gz, TERRA_DEPLOYMENT_ENV, _GS_SCHEMA, ExecutionPlatform
from terra_notebook_utils.utils import is_notebook, get_execution_context
from terra_notebook_utils.http import http
from terra_notebook_utils.blobstore.gs import GSBlob
from terra_notebook_utils.blobstore.local import LocalBlob
from terra_notebook_utils.blobstore.url import URLBlob
from terra_notebook_utils.blobstore.progress import Indicator
from terra_notebook_utils.blobstore import Blob, copy_client, BlobNotFoundError
from terra_notebook_utils.logger import logger
from terra_notebook_utils.terra_auth import get_terra_access_token

DRSInfo = namedtuple("DRSInfo", "credentials access_url bucket_name key name size updated checksums")

class DRSResolutionError(Exception):
    pass

class RequesterPaysNotSupported(Exception):
    pass

def _parse_gs_url(gs_url: str) -> Tuple[str, str]:
    if gs_url.startswith(_GS_SCHEMA):
        bucket_name, object_key = gs_url[len(_GS_SCHEMA):].split("/", 1)
        return bucket_name, object_key
    else:
        raise RuntimeError(f'Invalid gs url schema.  {gs_url} does not start with {_GS_SCHEMA}')

def is_requester_pays(drs_urls: Iterable[str]) -> bool:
    """
    Identify if any of the given DRS URIs require Google requester pays access

    :raises: RequesterPaysNotSupported
    """
    for drs_url in drs_urls:
        # Currently (1/2023), Gen3-hosted AnVIL data in GCS is the only
        # DRS data requiring requester pays access.
        # Even this will end in when the AnVIL data is hosted in TDR.
        # Note: If the Gen3 AnVIL DRS URI format is retained by TDR, this function must be updated.

        # The DRS specification (v1.2) states compact identifiers must be lowercase alphanumerical values:
        # https://ga4gh.github.io/data-repository-service-schemas/preview/release/drs-1.2.0/docs/#section/DRS-URIs
        # Yet for historical reasons the DRS URIs minted by Gen3 use uppercase.
        # Perform a case-insensitive comparison.
        anvil_drs_uri_prefix = "drs://dg.ANV0".lower()
        if drs_url.strip().lower().startswith(anvil_drs_uri_prefix):
            if get_execution_context().execution_platform == ExecutionPlatform.AZURE:
                raise RequesterPaysNotSupported(
                    f"Requester pays data access is not supported on the Azure platform. Cannot access: {drs_url}"
                )
            else:
                return True
    return False

@lru_cache()
def enable_requester_pays(workspace_name: Optional[str]=WORKSPACE_NAME,
                          workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE):

    assert get_execution_context().execution_platform != ExecutionPlatform.AZURE, \
        "Requester pays data access is not supported on the Terra Azure platform."

    if not workspace_name:
        raise RuntimeError('Workspace name is not set.  Please set the environment variable '
                           'WORKSPACE_NAME with the name of a valid Terra Workspace.')
    if not workspace_namespace:
        raise RuntimeError("Workspace namespace is not set.  Please set the environment variable "
                           "WORKSPACE_NAMESPACE with the name of your valid Terra Workspace Google Billing Project.")
    import urllib.parse
    encoded_workspace = urllib.parse.quote(workspace_name)
    rawls_url = (f"https://rawls.dsde-{TERRA_DEPLOYMENT_ENV}.broadinstitute.org/api/workspaces/"
                 f"{workspace_namespace}/{encoded_workspace}/enableRequesterPaysForLinkedServiceAccounts")
    logger.info("Enabling requester pays for your workspace. This will only take a few seconds...")
    access_token = get_terra_access_token()

    headers = {
        'authorization': f"Bearer {access_token}",
        'content-type': "application/json"
    }
    resp = http.put(rawls_url, headers=headers)

    if resp.status_code != 204:
        logger.warning(f"Failed to init requester pays for workspace {workspace_namespace}/{workspace_name}: "
                       f"Expected '204', got '{resp.status_code}' for '{rawls_url}'. "
                       "You will not be able to access DRS URIs that interact with requester pays buckets.")

def get_drs(drs_url: str, fields: List[str]) -> Response:
    """Request DRS information from DRS Resolver."""
    access_token = get_terra_access_token()

    headers = {
        'authorization': f"Bearer {access_token}",
        'content-type': "application/json"
    }

    logger.debug(f"Resolving DRS uri '{drs_url}' through '{DRS_RESOLVER_URL}'.")

    json_body = dict(url=drs_url, fields=fields)
    resp = http.post(DRS_RESOLVER_URL, headers=headers, json=json_body)

    if 200 != resp.status_code:
        logger.warning(resp.content)
        error_details = resp.json().get('response', {}).get('text', '')
        raise DRSResolutionError(f"Unexpected response while resolving DRS URI. Expected status 200, got "
                                 f"{resp.status_code}. {error_details}")

    return resp

def info(drs_url: str) -> dict:
    """Return a curated subset of data from `get_drs`."""
    info = get_drs_info(drs_url)
    return dict(name=info.name, size=info.size, updated=info.updated, checksums=info.checksums)

def access(drs_url: str,
           workspace_name: Optional[str]=WORKSPACE_NAME,
           workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE,
           billing_project: Optional[str]=WORKSPACE_GOOGLE_PROJECT) -> str:
    """Return a signed url for a drs:// URI, if available."""

    if is_requester_pays([drs_url]):
        # We enable requester pays by specifying the workspace/namespace combo, not
        # with the billing project. Rawls then enables requester pays for the attached
        # project, but this won't work if a user specifies a project unattached to
        # the Terra workspace.
        enable_requester_pays(workspace_name, workspace_namespace)

    info = get_drs_info(drs_url, access_url=True)

    if info.access_url:
        drs_resolver_url = info.access_url.strip()
        response = requests.get(drs_resolver_url, headers={'Range': 'bytes=0-1'})
        if response.status_code < 300:
            return drs_resolver_url
        logger.warning('Got an invalid/inaccessible signed URL... attempting to generate an alternate signed URL...')

    url = gs.get_signed_url(bucket=info.bucket_name,
                            key=info.key,
                            sa_credentials=info.credentials)
    # attempt to get the first byte; we'll get an HTTPError error if we need requester pays
    # TODO: hopefully the DRS Resolver will return this information eventually and
    #  we can avoid this check, but even in the meantime, there's probably a better way of doing this
    response = requests.get(url, headers={'Range': 'bytes=0-1'})
    if b'Bucket is a requester pays bucket' in response.content and response.status_code >= 400:
        url = gs.get_signed_url(bucket=info.bucket_name,
                                key=info.key,
                                sa_credentials=info.credentials,
                                requester_pays_user_project=billing_project)
        # one last sanity check
        response = requests.get(url, headers={'Range': 'bytes=0-1'})

    if response.status_code >= 400:
        raise DRSResolutionError(f'Could not generate a valid signed URL for {drs_url} .  Make sure you have access '
                                 f'to this file.  If you believe you have access, relinking your account projects in '
                                 f'your Terra profile may solve this issue (even if it shows your linking is currently '
                                 f'valid).  Otherwise, if you believe you have access and have relinked, please '
                                 f'contact Terra support.')

    return url.strip()

def _get_drs_gs_creds(response: dict) -> Optional[dict]:
    service_account_info = response.get('googleServiceAccount')
    if service_account_info is not None:
        return service_account_info['data']
    else:
        return None

def _drs_info_from_martha_v2(drs_url: str, drs_data: dict) -> DRSInfo:
    """Convert response from martha_v2 to DRSInfo."""
    if 'data_object' in drs_data['dos']:
        data_object = drs_data['dos']['data_object']

        if 'urls' not in data_object:
            raise DRSResolutionError(f"No GS url found for DRS uri '{drs_url}'")
        else:
            data_url = None
            for url_info in data_object['urls']:
                if 'url' in url_info and url_info['url'].startswith(_GS_SCHEMA):
                    data_url = url_info['url']
                    break
            if data_url is None:
                raise DRSResolutionError(f"No GS url found for DRS uri '{drs_url}'")

        bucket_name, key = _parse_gs_url(data_url)
        return DRSInfo(credentials=_get_drs_gs_creds(drs_data),
                       access_url=None,
                       checksums=None,
                       bucket_name=bucket_name,
                       key=key,
                       name=data_object.get('name'),
                       size=data_object.get('size'),
                       updated=data_object.get('updated'))
    else:
        raise DRSResolutionError(f"No metadata was returned for DRS uri '{drs_url}'")

def _drs_info_from_martha_v3_or_drshub(drs_url: str, drs_data: dict) -> DRSInfo:
    """Convert DRS response to DRSInfo."""
    access_url: Optional[str] = None
    if drs_data.get('accessUrl') is not None:
        access_url = drs_data['accessUrl'].get('url')

    if 'gsUri' not in drs_data and not access_url:
        raise DRSResolutionError(f"Neither GS URL or access URL found in DRS response for '{drs_url}'")

    return DRSInfo(credentials=_get_drs_gs_creds(drs_data),
                   access_url=access_url,
                   checksums=drs_data.get('hashes'),
                   bucket_name=drs_data.get('bucket'),
                   key=drs_data.get('name'),
                   name=drs_data.get('fileName'),
                   size=drs_data.get('size'),
                   updated=drs_data.get('timeUpdated'))

def get_drs_info(drs_url: str, access_url: bool = False) -> DRSInfo:
    """Attempt to resolve gs:// url and credentials for a DRS object."""
    assert drs_url.startswith("drs://"), f"Expected DRS URI of the form 'drs://...', got '{drs_url}'"
    fields = ["fileName",
              "hashes",
              "size",
              "gsUri",
              "bucket",
              "name",
              "timeUpdated",
              "googleServiceAccount"]
    # only include this field if necessary because it significantly increases the time for the DRS Resolver to respond
    if access_url:
        fields.append("accessUrl")
    drs_data = get_drs(drs_url, fields).json()
    if 'dos' in drs_data:
        return _drs_info_from_martha_v2(drs_url, drs_data)
    else:
        return _drs_info_from_martha_v3_or_drshub(drs_url, drs_data)

def get_drs_blob(drs_url_or_info: Union[str, DRSInfo],
                 billing_project: Optional[str]=WORKSPACE_GOOGLE_PROJECT) -> Union[GSBlob, URLBlob]:
    if isinstance(drs_url_or_info, str):
        info = get_drs_info(drs_url_or_info, access_url=True)
    elif isinstance(drs_url_or_info, DRSInfo):
        info = drs_url_or_info
    else:
        raise TypeError(f'Unexpected DRS input type ({type(drs_url_or_info)}): {drs_url_or_info}')
    blob: Union[URLBlob, GSBlob]
    if info.access_url is not None:
        blob = URLBlob(info.access_url, info.checksums.get('md5'))
    else:
        if not (info.credentials or info.bucket_name or info.key):
            raise ValueError(f'DRS information is missing.  Check:\n{info}')
        blob = GSBlob(info.bucket_name, info.key, info.credentials, billing_project)
    return blob

def blob_for_url(url: str, billing_project: Optional[str]=WORKSPACE_GOOGLE_PROJECT) -> Blob:
    if url.startswith("drs://"):
        return get_drs_blob(url, billing_project)
    else:
        return copy_client.blob_for_url(url)

def head(drs_url: str,
         num_bytes: int = 1,
         workspace_name: Optional[str]=WORKSPACE_NAME,
         workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE,
         billing_project: Optional[str]=WORKSPACE_GOOGLE_PROJECT):
    """Head a DRS object by byte."""

    if is_requester_pays([drs_url]):
        enable_requester_pays(workspace_name, workspace_namespace)
    try:
        blob = get_drs_blob(drs_url, billing_project)
        with blob.open(chunk_size=num_bytes) as fh:
            the_bytes = fh.read(num_bytes)
    except (DRSResolutionError, BlobNotFoundError) as e:
        raise BlobNotFoundError(f"The DRS URI: '{drs_url}' could not be accessed.") from e
    return the_bytes

def _resolve_bucket_target(url: str, info: DRSInfo) -> Tuple[str, str]:
    bucket_name, pfx = _bucket_name_and_key(url)
    if not pfx or pfx.endswith("/"):
        if pfx.endswith("/"):
            pfx = pfx[:-1]
        basename = info.name or info.key.rsplit("/", 1)[-1]
        key = f"{pfx}/{basename}" if pfx else basename
    else:
        key = pfx
    return bucket_name, key

def _resolve_local_target(filepath: str, info: DRSInfo) -> str:
    # Checking for a file also prevents a batch_copy from overwriting
    # the same file over and over again.
    if os.path.isfile(filepath):
        raise FileExistsError(f'Cannot copy {info.name} to {filepath} because file already exists')
    if filepath.endswith(os.path.sep) or os.path.isdir(filepath):
        filename = info.name or info.key.rsplit("/", 1)[-1]
        filepath = os.path.join(os.path.abspath(filepath), filename)
    return filepath

def _do_copy_drs(drs_uri: str,
                 dst: str,
                 multipart_threshold: int,
                 indicator_type: Indicator):
    dst_blob: Union[GSBlob, URLBlob, LocalBlob]
    src_info = get_drs_info(drs_uri, access_url=True)
    src_blob = get_drs_blob(src_info)
    if dst.startswith("gs://"):
        bucket_name, key = _resolve_bucket_target(dst, src_info)
        dst_blob = GSBlob(bucket_name, key)
    else:
        dst_blob = copy_client.blob_for_url(_resolve_local_target(dst, src_info))
    copy_client._do_copy(src_blob, dst_blob, multipart_threshold, indicator_type)

class DRSCopyClient(copy_client.CopyClient):
    workspace: Optional[str] = None
    workspace_namespace: Optional[str] = None

    def copy(self, drs_uri: str, dst: str):  # type: ignore
        self._queue.put(_do_copy_drs,
                        drs_uri,
                        dst,
                        self.multipart_threshold,
                        self.indicator_type)

def copy(drs_uri: str,
         dst: str,
         indicator_type: Indicator=Indicator.notebook_bar if is_notebook() else Indicator.bar,
         workspace_name: Optional[str]=WORKSPACE_NAME,
         workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE):
    """Copy a DRS object to either the local filesystem, or to a Google Storage location if `dst` starts with
    "gs://".
    """
    if is_requester_pays([drs_uri]):
        enable_requester_pays(workspace_name, workspace_namespace)
    with DRSCopyClient(raise_on_error=True, indicator_type=indicator_type) as cc:
        cc.workspace = workspace_name
        cc.workspace_namespace = workspace_namespace
        cc.copy(drs_uri, dst or ".")

def copy_to_bucket(drs_uri: str,
                   dst_key: str="",
                   dst_bucket_name: Optional[str]=None,
                   indicator_type: Indicator=Indicator.notebook_bar if is_notebook() else Indicator.bar,
                   workspace_name: Optional[str]=WORKSPACE_NAME,
                   workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE):
    """Resolve `drs_url` and copy into user-specified bucket `dst_bucket`.  If `dst_bucket` is None, copy into
    workspace bucket.
    """
    dst_bucket_name = dst_bucket_name or WORKSPACE_BUCKET
    dst_url = f"gs://{dst_bucket_name}"
    if dst_key:
        dst_url += f"/{dst_key}"
    copy(drs_uri, dst_url, indicator_type, workspace_name, workspace_namespace)

def copy_batch(drs_urls: Optional[Iterable[str]] = None,
               dst_pfx: Optional[str] = None,
               workspace_name: Optional[str] = WORKSPACE_NAME,
               workspace_namespace: Optional[str] = WORKSPACE_NAMESPACE,
               indicator_type: Indicator = Indicator.notebook_bar if is_notebook() else Indicator.log,
               manifest: Optional[List[Dict[str, str]]] = None):
    if (manifest is None) == (drs_urls is None):
        raise ValueError("Specify either 'manifest' or 'drs_urls' and 'dst_pfx'")
    elif manifest is not None:
        if dst_pfx is not None:
            raise ValueError('dst_pfx not compatible with manifest')
        copy_batch_manifest(manifest, indicator_type, workspace_name, workspace_namespace)
    elif drs_urls is not None:
        if dst_pfx is None:
            raise ValueError('dst_pfx required with drs_urls')
        copy_batch_urls(drs_urls, dst_pfx, indicator_type, workspace_name, workspace_namespace)
    else:
        assert False

def copy_batch_urls(drs_urls: Iterable[str],
                    dst_pfx: str,
                    indicator_type: Indicator = Indicator.notebook_bar if is_notebook() else Indicator.log,
                    workspace_name: Optional[str] = WORKSPACE_NAME,
                    workspace_namespace: Optional[str] = WORKSPACE_NAMESPACE):
    if is_requester_pays(drs_urls):
        enable_requester_pays(workspace_name, workspace_namespace)
    with DRSCopyClient(indicator_type=indicator_type) as cc:
        cc.workspace = workspace_name
        cc.workspace_namespace = workspace_namespace
        for drs_url in drs_urls:
            cc.copy(drs_url, dst_pfx)

manifest_schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "drs_uri": {"type": "string"},
            "dst": {"type": "string"},
        },
        "required": ["drs_uri", "dst"],
    },
}

def copy_batch_manifest(manifest: List[Dict[str, str]],
                        indicator_type: Indicator=Indicator.notebook_bar if is_notebook() else Indicator.log,
                        workspace_name: Optional[str]=WORKSPACE_NAME,
                        workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE):
    from jsonschema import validate
    validate(instance=manifest, schema=manifest_schema)

    drs_uri_list = [item['drs_uri'] for item in manifest]
    if is_requester_pays(drs_uri_list):
        enable_requester_pays(workspace_name, workspace_namespace)

    with DRSCopyClient(indicator_type=indicator_type) as cc:
        cc.workspace = workspace_name
        cc.workspace_namespace = workspace_namespace
        for item in manifest:
            cc.copy(item['drs_uri'], item['dst'])

def extract_tar_gz(drs_url: str,
                   dst: Optional[str]=None,
                   workspace_name: Optional[str]=WORKSPACE_NAME,
                   workspace_namespace: Optional[str]=WORKSPACE_NAMESPACE,
                   billing_project: Optional[str]=WORKSPACE_GOOGLE_PROJECT):
    """Extract a `.tar.gz` archive resolved by a DRS url. 'dst' may be either a local filepath or a 'gs://' url.
    Default extraction is to the bucket for 'workspace'.
    """
    dst = dst or f"gs://{workspace.get_workspace_bucket(workspace_name)}"

    if is_requester_pays([drs_url]):
        enable_requester_pays(workspace_name, workspace_namespace)
    blob = get_drs_blob(drs_url, billing_project)
    with blob.open() as fh:
        tar_gz.extract(fh, dst)

def _bucket_name_and_key(gs_url: str) -> Tuple[str, str]:
    assert gs_url.startswith("gs://")
    parts = gs_url[5:].split("/", 1)
    if 1 >= len(parts) or not parts[1]:
        bucket_name = parts[0]
        key = ""
    else:
        bucket_name, key = parts
    return bucket_name, key


"""
    sort + 2 pointers

    Time    O(NlogN + MlogM + max(N, M))
    Space   O(1)
    2144 ms, faster than 14.29%
"""

class Solution:
    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
        players.sort()
        trainers.sort()
        j = 0
        res = 0
        for i in range(len(players)):
            p = players[i]
            while j < len(trainers) and p > trainers[j]:
                j += 1
            if j < len(trainers) and p <= trainers[j]:
                res += 1
                j += 1
        return res


from test_framework.test_particl import ParticlTestFramework
from test_framework.util import connect_nodes, assert_equal

class SpentIndexTest(ParticlTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 4
        self.extra_args = [
            # Nodes 0/1 are "wallet" nodes
            ['-debug',],
            ['-debug','-spentindex'],
            # Nodes 2/3 are used for testing
            ['-debug','-spentindex'],
            ['-debug','-spentindex', '-txindex'],]

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()

    def setup_network(self):
        self.add_nodes(self.num_nodes, extra_args=self.extra_args)
        self.start_nodes()

        connect_nodes(self.nodes[0], 1)
        connect_nodes(self.nodes[0], 2)
        connect_nodes(self.nodes[0], 3)

        self.sync_all()

    def run_test(self):

        nodes = self.nodes

        # Stop staking
        for i in range(len(nodes)):
            nodes[i].reservebalance(True, 10000000)

        nodes[0].extkeyimportmaster('abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab absorb')
        assert(nodes[0].getwalletinfo()['total_balance'] == 100000)

        nodes[1].extkeyimportmaster('graine article givre hublot encadrer admirer stipuler capsule acajou paisible soutirer organe')
        nodes[2].extkeyimportmaster('sección grito médula hecho pauta posada nueve ebrio bruto buceo baúl mitad')
        nodes[3].extkeyimportmaster('けっこん　ゆそう　へいねつ　しあわせ　ちまた　きつね　たんたい　むかし　たかい　のいず　こわもて　けんこう')

        addrs = []
        addrs.append(nodes[1].getnewaddress())
        addrs.append(nodes[1].getnewaddress())

        # Check that
        print('Testing spent index...')

        unspent = nodes[0].listunspent()

        #{\"txid\":\"id\",\"vout\":n}
        inputs = [{'txid':unspent[0]['txid'],'vout':unspent[0]['vout']},]
        outputs = {addrs[0]:1}
        tx = nodes[0].createrawtransaction(inputs,outputs)

        # Add change output
        txfunded = nodes[0].fundrawtransaction(tx)

        txsigned = nodes[0].signrawtransactionwithwallet(txfunded['hex'])

        sent_txid = nodes[0].sendrawtransaction(txsigned['hex'], 0)

        self.stakeBlocks(1)

        print("Testing getspentinfo method...")

        # Check that the spentinfo works standalone
        info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]})
        assert_equal(info["txid"], sent_txid)
        assert_equal(info["index"], 0)
        assert_equal(info["height"], 1)

        print("Testing getrawtransaction method...")

        # Check that verbose raw transaction includes spent info
        txVerbose = self.nodes[3].getrawtransaction(unspent[0]["txid"], 1)
        assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentTxId"], sent_txid)
        assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentIndex"], 0)
        assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentHeight"], 1)

        # Check that verbose raw transaction includes address values and input values
        txVerbose2 = self.nodes[3].getrawtransaction(sent_txid, 1)
        assert_equal(txVerbose2["vin"][0]["address"], 'pcwP4hTtaMb7n4urszBTsgxWLdNLU4yNGz')
        assert(float(txVerbose2["vin"][0]["value"]) > 0)
        assert(txVerbose2["vin"][0]["valueSat"] > 0)

        # Check the mempool index
        txid2 = nodes[0].sendtoaddress(addrs[1], 5)
        self.sync_all()
        txVerbose3 = self.nodes[1].getrawtransaction(txid2, 1)
        assert(len(txVerbose3["vin"][0]["address"]) > 0)
        assert(float(txVerbose3["vin"][0]["value"]) > 0)
        assert(txVerbose3["vin"][0]["valueSat"] > 0)

        # Check the database index
        self.stakeBlocks(1)

        block1_hash = nodes[1].getblockhash(nodes[1].getblockcount())
        ro = nodes[1].getblock(block1_hash)
        assert(txid2 in ro['tx'])

        txVerbose4 = self.nodes[3].getrawtransaction(txid2, 1)
        assert(len(txVerbose4["vin"][0]["address"]) > 0)
        assert(float(txVerbose4["vin"][0]["value"]) > 0)
        assert(txVerbose4["vin"][0]["valueSat"] > 0)

        # Check block deltas
        print("Testing getblockdeltas...")

        block = nodes[3].getblockdeltas(block1_hash)
        assert_equal(len(block["deltas"]), 2)

        assert_equal(block["deltas"][0]["index"], 0)
        assert_equal(len(block["deltas"][0]["inputs"]), 1)
        assert_equal(len(block["deltas"][0]["outputs"]), 2)

        assert_equal(block["deltas"][1]["index"], 1)
        assert_equal(block["deltas"][1]["txid"], txid2)
        assert_equal(len(block["deltas"][1]["inputs"]), 1)
        assert_equal(len(block["deltas"][1]["outputs"]), 2)

        fFound = False
        for out in block["deltas"][1]["outputs"]:
            if out["satoshis"] == 500000000 and out["address"] == addrs[1]:
                fFound = True
                break
        assert(fFound)

        print("Passed\n")

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


#  Faça um programa que leia o peso de cinco pessoas. No final, mostre qual foi o maior e o menor peso lidos.

maior = 0
menor = 0
for i in range(1, 6):
    pess = float(input(f'Peso da {i}ª pessoa: '))
    if i == 1:
        maior = pess
        menor = pess
    else:
        if pess > maior:
            maior = pess
        if pess < menor:
            menor = pess
print(f'O maior peso lido foi {maior}Kg')
print(f'O menor peso lido foi {menor}Kg')


import sys
input = sys.stdin.readline

def solution():

    ans = 2147483647
    weight = 0
    price = 0
    result = 0

    for i in range(N):  #전부 다더해서
        result += li[i][0]

    if result < M:  #원하는 무게가 안되면 -1 리턴
        return -1

    for i in range(N):
        weight += li[i][0] #무게를 더하면서

        if i>=1 and li[i][1] == li[i-1][1]: #무게가 달라지지 않았으면
            price += li[i][1] #가격을 더한다
        else:
            price = 0 #무게가 달라졌으면 더하지 않는다

        if weight >= M:
            ans = min(ans, li[i][1]+price) #하나만 샀을때와 , 차근차근 더했을때의 가격중 싼 것을 출력

    return ans

N, M = map(int, input().split())

li = [list(map(int, input().split())) for _ in range(N)]

li = sorted(li, key=lambda x:(x[1], -x[0]))  # 가격은 싼순, 무게는 무거운순

result = solution()

print(result)


class Tv:

    def __init__(self):
        self.setChannal = 0
        self.volume = 0

    def setcanal(self, new_channal):
        self.setChannal = new_channal
        if 100 >= new_channal > 0:
            return self.setChannal

    def turn_up(self, up_volume):
        self.volume = up_volume
        if up_volume < 100:
            up_volume += 1
        else:
            print('\033[31mMaximum volume - 100\033[m')

    def turn_down(self, down_volume):
        if down_volume > 0:
            self.volume -= down_volume
        else:
            print('\033[31mMinimum volume - 0\033[m')

    def show_mood(self):
        print(f'Channel: {self.setChannal} \nVolume: {self.volume}')

tv = Tv()
tv.show_mood()
print()

tv.setcanal(50)
tv.show_mood()
print()

tv.turn_up(60)
tv.show_mood()
print()

tv.turn_down(10)
tv.show_mood()


import base64

from gcloud._helpers import _datetime_to_rfc3339
from gcloud._helpers import _NOW
from gcloud.exceptions import NotFound
from gcloud.pubsub._helpers import subscription_name_from_path
from gcloud.pubsub._helpers import topic_name_from_path
from gcloud.pubsub.iam import Policy
from gcloud.pubsub.subscription import Subscription

class Topic(object):
    """Topics are targets to which messages can be published.

    Subscribers then receive those messages.

    See:
    https://cloud.google.com/pubsub/reference/rest/v1/projects.topics

    :type name: string
    :param name: the name of the topic

    :type client: :class:`gcloud.pubsub.client.Client`
    :param client: A client which holds credentials and project configuration
                   for the topic (which requires a project).

    :type timestamp_messages: boolean
    :param timestamp_messages: If true, the topic will add a ``timestamp`` key
                               to the attributes of each published message:
                               the value will be an RFC 3339 timestamp.
    """
    def __init__(self, name, client, timestamp_messages=False):
        self.name = name
        self._client = client
        self.timestamp_messages = timestamp_messages

    def subscription(self, name, ack_deadline=None, push_endpoint=None):
        """Creates a subscription bound to the current topic.

        Example:  pull-mode subcription, default paramter values

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_defaults]
           :end-before: [END topic_subscription_defaults]

        Example:  pull-mode subcription, override ``ack_deadline`` default

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_ack90]
           :end-before: [END topic_subscription_ack90]

        Example:  push-mode subcription

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_subscription_push]
           :end-before: [END topic_subscription_push]

        :type name: string
        :param name: the name of the subscription

        :type ack_deadline: int
        :param ack_deadline: the deadline (in seconds) by which messages pulled
                             from the back-end must be acknowledged.

        :type push_endpoint: string
        :param push_endpoint: URL to which messages will be pushed by the
                              back-end. If not set, the application must pull
                              messages.
        """
        return Subscription(name, self, ack_deadline=ack_deadline,
                            push_endpoint=push_endpoint)

    @classmethod
    def from_api_repr(cls, resource, client):
        """Factory:  construct a topic given its API representation

        :type resource: dict
        :param resource: topic resource representation returned from the API

        :type client: :class:`gcloud.pubsub.client.Client`
        :param client: Client which holds credentials and project
                       configuration for the topic.

        :rtype: :class:`gcloud.pubsub.topic.Topic`
        :returns: Topic parsed from ``resource``.
        :raises: :class:`ValueError` if ``client`` is not ``None`` and the
                 project from the resource does not agree with the project
                 from the client.
        """
        topic_name = topic_name_from_path(resource['name'], client.project)
        return cls(topic_name, client=client)

    @property
    def project(self):
        """Project bound to the topic."""
        return self._client.project

    @property
    def full_name(self):
        """Fully-qualified name used in topic / subscription APIs"""
        return 'projects/%s/topics/%s' % (self.project, self.name)

    def _require_client(self, client):
        """Check client or verify over-ride.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: :class:`gcloud.pubsub.client.Client`
        :returns: The client passed in or the currently bound client.
        """
        if client is None:
            client = self._client
        return client

    def create(self, client=None):
        """API call:  create the topic via a PUT request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/create

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_create]
           :end-before: [END topic_create]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.
        """
        client = self._require_client(client)
        api = client.publisher_api
        api.topic_create(topic_path=self.full_name)

    def exists(self, client=None):
        """API call:  test for the existence of the topic via a GET request

        See
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/get

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_exists]
           :end-before: [END topic_exists]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.
        """
        client = self._require_client(client)
        api = client.publisher_api

        try:
            api.topic_get(topic_path=self.full_name)
        except NotFound:
            return False
        else:
            return True

    def delete(self, client=None):
        """API call:  delete the topic via a DELETE request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/delete

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_delete]
           :end-before: [END topic_delete]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.
        """
        client = self._require_client(client)
        api = client.publisher_api
        api.topic_delete(topic_path=self.full_name)

    def _timestamp_message(self, attrs):
        """Add a timestamp to ``attrs``, if the topic is so configured.

        If ``attrs`` already has the key, do nothing.

        Helper method for ``publish``/``Batch.publish``.
        """
        if self.timestamp_messages and 'timestamp' not in attrs:
            attrs['timestamp'] = _datetime_to_rfc3339(_NOW())

    def publish(self, message, client=None, **attrs):
        """API call:  publish a message to a topic via a POST request

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/publish

        Example without message attributes:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_publish_simple_message]
           :end-before: [END topic_publish_simple_message]

        With message attributes:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_publish_message_with_attrs]
           :end-before: [END topic_publish_message_with_attrs]

        :type message: bytes
        :param message: the message payload

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :type attrs: dict (string -> string)
        :param attrs: key-value pairs to send as message attributes

        :rtype: str
        :returns: message ID assigned by the server to the published message
        """
        client = self._require_client(client)
        api = client.publisher_api

        self._timestamp_message(attrs)
        message_b = base64.b64encode(message).decode('ascii')
        message_data = {'data': message_b, 'attributes': attrs}
        message_ids = api.topic_publish(self.full_name, [message_data])
        return message_ids[0]

    def batch(self, client=None):
        """Return a batch to use as a context manager.

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_batch]
           :end-before: [END topic_batch]

        .. note::

           The only API request happens during the ``__exit__()`` of the topic
           used as a context manager, and only if the block exits without
           raising an exception.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: :class:`Batch`
        :returns: A batch to use as a context manager.
        """
        client = self._require_client(client)
        return Batch(self, client)

    def list_subscriptions(self, page_size=None, page_token=None, client=None):
        """List subscriptions for the project associated with this client.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics.subscriptions/list

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_list_subscriptions]
           :end-before: [END topic_list_subscriptions]

        :type page_size: int
        :param page_size: maximum number of topics to return, If not passed,
                          defaults to a value set by the API.

        :type page_token: string
        :param page_token: opaque marker for the next "page" of topics. If not
                           passed, the API will return the first page of
                           topics.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current topic.

        :rtype: tuple, (list, str)
        :returns: list of :class:`gcloud.pubsub.subscription.Subscription`,
                  plus a "next page token" string:  if not None, indicates that
                  more topics can be retrieved with another call (pass that
                  value as ``page_token``).
        """
        client = self._require_client(client)
        api = client.publisher_api
        sub_paths, next_token = api.topic_list_subscriptions(
            self.full_name, page_size, page_token)
        subscriptions = []
        for sub_path in sub_paths:
            sub_name = subscription_name_from_path(sub_path, self.project)
            subscriptions.append(Subscription(sub_name, self))
        return subscriptions, next_token

    def get_iam_policy(self, client=None):
        """Fetch the IAM policy for the topic.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/getIamPolicy

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_get_iam_policy]
           :end-before: [END topic_get_iam_policy]

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: :class:`gcloud.pubsub.iam.Policy`
        :returns: policy created from the resource returned by the
                  ``getIamPolicy`` API request.
        """
        client = self._require_client(client)
        api = client.iam_policy_api
        resp = api.get_iam_policy(self.full_name)
        return Policy.from_api_repr(resp)

    def set_iam_policy(self, policy, client=None):
        """Update the IAM policy for the topic.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/setIamPolicy

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_set_iam_policy]
           :end-before: [END topic_set_iam_policy]

        :type policy: :class:`gcloud.pubsub.iam.Policy`
        :param policy: the new policy, typically fetched via
                       :meth:`get_iam_policy` and updated in place.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: :class:`gcloud.pubsub.iam.Policy`
        :returns: updated policy created from the resource returned by the
                  ``setIamPolicy`` API request.
        """
        client = self._require_client(client)
        api = client.iam_policy_api
        resource = policy.to_api_repr()
        resp = api.set_iam_policy(self.full_name, resource)
        return Policy.from_api_repr(resp)

    def check_iam_permissions(self, permissions, client=None):
        """Verify permissions allowed for the current user.

        See:
        https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/testIamPermissions

        Example:

        .. literalinclude:: pubsub_snippets.py
           :start-after: [START topic_check_iam_permissions]
           :end-before: [END topic_check_iam_permissions]

        :type permissions: list of string
        :param permissions: list of permissions to be tested

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.

        :rtype: sequence of string
        :returns: subset of ``permissions`` allowed by current IAM policy.
        """
        client = self._require_client(client)
        api = client.iam_policy_api
        return api.test_iam_permissions(
            self.full_name, list(permissions))

class Batch(object):
    """Context manager:  collect messages to publish via a single API call.

    Helper returned by :meth:Topic.batch

    :type topic: :class:`gcloud.pubsub.topic.Topic`
    :param topic: the topic being published

    :type client: :class:`gcloud.pubsub.client.Client`
    :param client: The client to use.
    """
    def __init__(self, topic, client):
        self.topic = topic
        self.messages = []
        self.message_ids = []
        self.client = client

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is None:
            self.commit()

    def __iter__(self):
        return iter(self.message_ids)

    def publish(self, message, **attrs):
        """Emulate publishing a message, but save it.

        :type message: bytes
        :param message: the message payload

        :type attrs: dict (string -> string)
        :param attrs: key-value pairs to send as message attributes
        """
        self.topic._timestamp_message(attrs)
        self.messages.append(
            {'data': base64.b64encode(message).decode('ascii'),
             'attributes': attrs})

    def commit(self, client=None):
        """Send saved messages as a single API call.

        :type client: :class:`gcloud.pubsub.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current batch.
        """
        if client is None:
            client = self.client
        api = client.publisher_api
        message_ids = api.topic_publish(self.topic.full_name, self.messages[:])
        self.message_ids.extend(message_ids)
        del self.messages[:]


import sys

if __name__ == '__main__':
  flag =     1
  write = ""
  while    flag ==  1:
    print(       "Write Something:")
    write = input("") if sys.version_info >= (3,0) else raw_input("")
    print(       "You typed:" + write)
    if        write ==     "q":
      flag=0

  print(       "Quit Program")


##################################################
#
# Python Solar Panel Characterization
# -- analyzing Arduino .csv data
#
# by Joshua Hrisko | Maker Portal LLC (c) 2021
#
##################################################
#
#
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import csv

##################################################
# Data parsing from .csv
##################################################
#
filename = "SOLARLOG.CSV" # csv filename

data_array = [] # for saving variables
with open(filename,"r") as csvfile:
    csvreader = csv.reader(csvfile,delimiter=",")
    header = next(csvreader) # print header
    for row in csvreader:
        data_array.append(row) # save data to variable array

start_pt = 0 # start point
end_pt   = int(0.8*(np.shape(data_array)[0])) # end point
data_array = data_array[start_pt:end_pt] # select the portion of data with valid data
time_vector = np.array([float(ii[0]) for ii in data_array]) # time in [ms] since start of program
voltage_V   = np.array([float(ii[1]) for ii in data_array]) # voltage in [V]
current_mA  = np.array([float(ii[2]) for ii in data_array]) # current in [mA]

##################################################
# Data Plotting of Voltage [V] vs Current [mA]
##################################################
#
plt.style.use('ggplot') # style plot
fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(211)
ax.tick_params(labelsize=14)

ax.plot(voltage_V,current_mA,linewidth=3.5,color=plt.cm.tab20c(0),
        label='54mm x 54mm Solar Panel') # plot V vs I
ax.set_xlabel(r'$V$, Voltage [V]',fontsize=16)
ax.set_ylabel(r'$I$, Current [mA]',fontsize=16)

ax2 = fig.add_subplot(212)
ax2.tick_params(labelsize=14)

power_W = voltage_V*(current_mA/1000.0) # power output [W]
max_P_volt = voltage_V[np.argmax(power_W)] # max power Voltage [V]
maX_P_curr = current_mA[np.argmax(power_W)] # max power current [mA]
max_power_W = np.max(power_W) # max power [W]

ax.plot(np.repeat(np.max(voltage_V),len(current_mA)),current_mA,
        linestyle='dotted',color=plt.cm.tab20c(4)) # open-circuit voltage
ax.plot(voltage_V,np.repeat(np.max(current_mA),len(voltage_V)),
        linestyle='--',color=plt.cm.tab20c(1)) # short-circuit current

ax2.plot(voltage_V,power_W,linewidth=3.5,color=plt.cm.tab20c(8),
         label='54mm x 54mm Solar Panel') # plot V vs I

ax2.plot(np.repeat(max_P_volt,len(power_W)),power_W,
         linestyle='dotted',color=plt.cm.tab20c(12)) # plot max voltage
ax2.plot(voltage_V,np.repeat(max_power_W,len(voltage_V)),
         color=plt.cm.tab20c(9),linestyle='--') # plot max current

ax2.set_xlabel(r'$V$, Voltage [V]',fontsize=16)
ax2.set_ylabel(r'$P$, Power [W]',fontsize=16)

ax.legend(fontsize=14)
ax2.legend(fontsize=14)

print("Max Power: {0:2.2f} W\nMax Power Voltage: {1:2.2f} V\nMax Power Current: {2:2.2f} mA".format(
    max_power_W,max_P_volt,maX_P_curr)+"\nOpen-Circuit Voltage: {0:2.2f} V".format(np.max(voltage_V))+
      "\nShort-Circuit Current: {0:2.2f} mA".format(np.max(current_mA)))

plt.savefig('solar_panel_char_output.png',dpi=300,bbox_inches='tight',facecolor='#FFFFFF')
plt.show() # show plot


from ..questions import question, answersCount
import random
from .. import utils

@question('movies', ['movies/movies', 'movies/directors'])
def directorByMovie(mvs, dcs):
    movie, director, year = mvs.sample(1).iloc[0][['movie', 'director', 'year']]
    wrongDirectors = dcs[dcs.director != director].sample(answersCount() - 1).director
    question = utils.string('directorByMovie').f(movie=movie, year=year)
    return question, (director, *wrongDirectors)

@question('movies', ['movies/movies'])
def movieByDirector(mvs):
    movie, director, year = mvs.sample(1).iloc[0][['movie', 'director', 'year']]
    wrongMovies = mvs[mvs.director != director].sample(answersCount() - 1).movie
    question = utils.string('movieByDirector').f(director=director, year=year)
    return question, (movie, *wrongMovies)

@question('movies', ['movies/movies'])
def movieByYear(mvs):
    import pandas
    right = mvs.sample(1)
    year = right.year.iloc[0]
    weights = mvs.year.apply(lambda y: min(abs(y - year), 20) ** 8)
    wrong = mvs.sample(answersCount() - 1, weights=weights)
    question = utils.string('movieByYear').f(year=year)
    return question, utils.format(right, wrong, utils.string('movieByDirectorAnswer').s)

@question('movies', ['movies/movies'])
def yearByMovie(mvs):
    movie, director, year = mvs.sample(1).iloc[0][['movie', 'director', 'year']]
    question = utils.string('yearByMovie').f(movie=movie, director=director)
    return question, tuple(str(y) for y in utils.years(year))

@question('movies', ['movies/movies'])
def movieByActor(mvs):
    right = mvs.sample(1)
    actors, year = right.iloc[0][['actors', 'year']]
    actor = random.choice(actors)
    wrong = mvs[mvs.actors.apply(lambda a: actor not in a)].sample(answersCount() - 1)
    question = utils.string('movieByActor').f(actor=actor, year=year)
    return question, utils.format(right, wrong, utils.string('movieByDirectorAnswer').s)

@question('movies', ['movies/movies'])
def actorByMovie(mvs):
    movie, year, director, actors = mvs.sample(1).iloc[0][['movie', 'year', 'director', 'actors']]
    collector = utils.Collector(actors)
    collector.addIterable(mvs.actors)
    question = utils.string('actorByMovie').f(movie=movie, year=year, director=director)
    return question, collector.answers


# -*- coding=utf-8 -*-
# author: zhihuaye@gmail.com

"""
1. try to get flags
https://www.countries-ofthe-world.com/flags-of-the-world.html
2. find the sample url
https://www.countries-ofthe-world.com/flags-normal/flag-of-China.png
3. add UA to get file
"""

import sys
import os
from pyquery import PyQuery as pq
import requests
import time
import functools
import errno
from timeit import default_timer as timer
import shutil
from concurrent import futures

FLAG_BASEURL='https://www.countries-ofthe-world.com/'
INDEX_URI='flags-of-asia.html'
FLAGPAGE='./flags.html'
FLAGDIR='./flags'
Headers = {
    'user-agent': "Mozilla"
}
PY3k = sys.version_info >= (3,)

def benchmark(func):
    """
    decorator to print the time a function executes.
    :param func:
    :return:
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = timer()
        res = func(*args, **kwargs)
        print("{0} takes {1:.7f} seconds".format(func.__name__, timer() - start))
        return res
    return wrapper

@benchmark
def hello():
    print("start to run")

@benchmark
def mkdirp(dirname):
    try:
        os.makedirs(dirname)
    except OSError as error:
        if error.errno == errno.EEXIST and os.path.isdir(dirname):
            pass
        else:
            raise

@benchmark
def getflagpage():
    rsp = requests.get(FLAG_BASEURL + INDEX_URI, headers=Headers)
    rsp.raise_for_status()
    if PY3k:
        with open(FLAGPAGE, 'w+', encoding='utf-8') as handle:
            handle.write(rsp.text)   #works for py 3.x
            return rsp.text
    else:
        with open(FLAGPAGE, 'w+') as handle:
            htmlstr = rsp.text.encode('utf-8')
            handle.write(htmlstr) # works for py 2.7
            return htmlstr

@benchmark
def getoneflag(imgelement):
    imgurl = FLAG_BASEURL + imgelement.get('src')
    #flags-normal/flag-of-Afghanistan.png
    imgname = FLAGDIR + '/' + imgelement.get('src').split('/')[1]
    rsp = requests.get(imgurl, headers=Headers)
    rsp.raise_for_status()
    # open as binary and get content
    with open(imgname, 'wb') as handle:
        handle.write(rsp.content)

@benchmark
def getcountryflags(htmlstr):
    #avoid file open in pq, unicode is not well supported.
    page = pq(htmlstr)
    #listcomp, lxml element
    #imglist = [ i.get('src') for i in page('table').find('img')]
    #print(imglist)
    #use items() is better, so imgele will be PyQuery obj instead of lxml obj.
    for imgelement in page('table').find('img'):
        getoneflag(imgelement)

@benchmark
def futuresgetflags(htmlstr):
    page = pq(htmlstr)
    workers = 20
    imgelementlist = [element for element in page('table').find('img')]
    with futures.ThreadPoolExecutor(workers) as executor:
        executor.map(getoneflag, imgelementlist)

if __name__ == '__main__':
    if os.path.exists(FLAGDIR):
        shutil.rmtree(FLAGDIR)
    mkdirp(FLAGDIR)
    htmlstr=getflagpage()
    getcountryflags(htmlstr)
    #futuresgetflags(htmlstr)


################################Deprecated (changed data source)########################
import numpy as np
import pandas as pd
from datetime import datetime

###############################################################################
#-------------------------stop data--------------------------------------------
# tring to identify DUI for stop data and collision data.
DUI_lst = ['CONTRABAND VISIBLE', 'ODOR OF CONTRABAND', 'DUI']

rename_dict = {
    'perceived_age':'subject_age',\
    'perceived_gender':'subject_sex',\
    'result':'arrested',\
    'reason_for_stop_detail':'stop_cause',\
    'basis_for_search':'searched',\
    'race':'subject_race',\
    'type_of_property_seized':'property_seized',\
    'contraband':'contraband_found',\
    # below is for filtering out DUI records
    # I rename the old versions DUI col to the new versions's name
    'search_details_description':'reason_for_stop_explanation'}

# return subset of the new stop data with only DUI related record
def filter_to_dui(stop):
    copy = stop.copy(deep=True)
    copy = copy[~copy.reason_for_stop_explanation.isna()]
    copy.reason_for_stop_explanation = copy.reason_for_stop_explanation.str.upper()
    return copy[copy.reason_for_stop_explanation.str.contains("DUI")]

# standardize df according to the full columns
def standardize(df,cols):
    result = df.rename(rename_dict,axis=1)
    # find cols that are not in df
    cols_series = pd.Series(cols)
    not_in = cols_series[~cols_series.isin(result.columns)]
    for col in not_in:
        result[col] = np.NaN
    result = result[cols]
    return result

# clean race by Asian,pacific, black, hispanic, white, other/nan  A,P,B,H,W,O
def clean_old_race(x):
    if not isinstance(x,str):
        return np.NaN
    elif x.upper() in ['B','O','W','H']:
        return x.upper()
    elif x.upper() in 'A C D F I J K L V Z'.split():
        return 'A'
    elif x.upper() in  'G P S U'.split():
        return 'P'
    else:
        return 'O'

def clean_new_race(x):
    if x.upper() == "White".upper():
        return 'W'
    elif x.upper() == "Hispanic/Latino/a".upper():
        return 'H'
    elif x.upper() == "Black/African American".upper():
        return 'B'
    elif x.upper() == "Native American".upper():
        return 'O'
    elif x.upper() == "Middle Eastern or South Asian".upper():
        return 'A'
    elif x.upper() == "Asian".upper():
        return 'A'
    elif x.upper() == "Pacific Islander".upper():
        return 'P'
    elif not isinstance(x,str):
        return np.NaN
    else:
        return 'O'

# derive the new version service area from beat
def new_to_service(x):
    if np.isnan(x):
        return np.NaN
    else:
        return int(x//10*10)

def clean_old_service(x):
    if isinstance(x,str) and x.upper() == 'Unknown'.upper():
        return np.NaN
    else:
        return int(x)

# clean subject_age may contain NaN, so type becomes float
def clean_age(x):
    try:
        return float(x)
    except:
        return np.NaN

# clean sex based on the old version 'M','F','X',nan
def clean_sex(x):
    if not isinstance(x,str):
        return np.NaN
    elif x.upper() in ['F','FEMALE']:
        return 'F'
    elif x.upper() in ['M','MALE']:
        return 'M'
    else:
        return 'X'

def clean_time(df):
    temp = df[~pd.isnull(df.time_stop)]
    temp.time_stop = temp.time_stop.str.extract('([0-9]+:[0-9]+)')
    temp.time_stop = pd.to_datetime(temp.time_stop,errors='coerce')
    return temp

# clean old verison searched, arrested, contraband_found, and property_seized
def clean_old_post(x):
    if not isinstance(x,str):
        return np.NaN
    else:
        return x.upper()

# clean new version searched, contraband_found,property_seized based on
# https://oag.ca.gov/sites/all/files/agweb/pdfs/ripa/stop-data-reg-final-text-110717.pdf?
def clean_new_post(x):
    if not isinstance(x,str):
        return 'N'
    else:
        if x.upper() == 'NONE':
            return 'N'
        else:
            return 'Y'

def clean_new_arrest(x):
    arrest_lst = ['Custodial Arrest without warrant'.upper(),'Custodial Arrest pursuant to outstanding warrant'.upper()]
    if not isinstance(x,str):
        return np.NaN
    elif x.upper() in arrest_lst:
        return 'Y'
    else:
        return 'N'

old_clean = {"service_area":clean_old_service,"subject_race":clean_old_race\
,"subject_age":clean_age,"subject_sex":clean_sex,"searched":clean_old_post,\
"arrested":clean_old_post,"contraband_found":clean_old_post,"property_seized":clean_old_post}

new_clean = {"subject_race":clean_new_race\
,"subject_age":clean_age,"subject_sex":clean_sex,"searched":clean_new_post,\
"arrested":clean_new_arrest,"contraband_found":clean_new_post,"property_seized":clean_new_post}

###############################################################################
#-------------------------collision data--------------------------------------------
# return subset of the collision data with only DUI related record
def collision_dui(collision_df):
    copy = stop.copy(deep=True)
    copy = copy[copy['PCF_VIOL_CATEGORY'] == '01']
    return copy

def clean_longitude(x):
    if x:
        return (-1)*float(x)
    else:
        return x

def clean_PCF(x):
    if x == '01':
        return 'DUI'
    else:
        return x

def clean_date(x):
    return datetime.strptime(str(x), '%Y%m%d')

def get_hour(x):
    #collision time is non zero padded
    x = str(int(x)).zfill(4)[0:2]+':00'
    x = datetime.strptime(x,'%H:%M')
    return x

def type_collision(x):
    dic = {'A': 'Head-On','B':'Sideswipe','C': 'Rear End',\
          'D':'Broadside','E':'Hit Object','F':'Overturned',\
          'G':'Vehicle/Pedstrian','H':'Other','-':'Not Stated'}
    return dic[x]

def type_injury(x):
    dic = {0:'property damage only',1:'fatal',2:'severe injury',3:'other visible injury',\
          4:'complaint of pain'}
    return dic[x]

collision_clean = {'PCF_VIOL_CATEGORY': clean_PCF, 'LONGITUDE': clean_longitude,\
'COLLISION_DATE': clean_date}


from gensim.test.utils import common_corpus, common_dictionary, datapath, get_tmpfile, common_texts
from gensim.models import HdpModel
from gensim.corpora import MalletCorpus
from gensim.corpora import Dictionary
from gensim import corpora
from six import iteritems # collect statistics about all tokens
import numpy as np
import pandas as pd
import sys

# import sets of words to remove
from gensim.parsing.preprocessing import remove_stopwords, STOPWORDS
from OSTK_STOPWORDS import DESCRIPTIVE_WORDS

def create_dict(corpus_file_path):
    dictionary = corpora.Dictionary(line.lower().split() for line in open(corpus_file_path))
    return dictionary

def get_filter_ids(filter_word_set, dictionary):
    filter_set_ids = [dictionary.token2id[filter_word] for filter_word in filter_word_set
                      if filter_word in dictionary.token2id]
    return filter_set_ids

class hHdpModel(object):
    def __init__(self, corpus_file_path):
        pass

class MyCorpus(object):
    def __init__(self, corpus_file_path):
        self.corpusFilepPath = corpus_file_path
        self.dict = create_dict(corpus_file_path)
        self.filteredTokens = set({}) # This can be used later when creating a sub-hpd
        self.hdpModels = [] # This is where each model will live for the current corpus

    def __iter__(self):
        for line in open(self.corpusFilepPath):
            yield self.dict.doc2bow(line.lower().split()) # assume there's one document per line, tokens separated by whitespace

    def __len__(self):
        return len(list(iter(self)))

    def remove_words(self, *filter_word_sets):
        for filter_word_set in filter_word_sets:
            if not isinstance(filter_word_set, (set, frozenset)): # type check
                sys.exit("Filter word set must by of type set or frozenset")
            filter_set_ids = get_filter_ids(filter_word_set, dictionary = self.dict)
            self.filteredTokens.add(filter_word_set) # Record which tokens were removed from the current corpus instance
            self.dict.filter_tokens(filter_set_ids) # remove the tokens by the id
            self.dict.compactify() # remove the gaps in the dictionary left from the filter step

    def get_new_doc_id(self, doc):
        doc_id = self.dict.doc2bow(doc.lower().split())
        return doc_id

    def run_hdp(self, modelId, **kwargs):
        print(kwargs)
        hdpModel = HdpModel(self, self.dict, kwargs)
        hdpData = {modelId:
                   {'model':hdpModel,
                    'args':kwargs}}

        self.hdpModels.append(hdpData)

    def get_new_doc_topics(self, doc, *modelIds):
        for modelId in modelIds:
            doc_ids = []
            doc_ids.append(self.hdpModels[modelid]['model'][self.get_new_doc_id(doc)])
            return doc_ids

def main():
    # Create the corpus
    hundoGrandoCorpus = MyCorpus("../data/corpus_clean100000.txt")
    # Remove words
    hundoGrandoCorpus.remove_words(STOPWORDS, DESCRIPTIVE_WORDS)
    hundoGrandoCorpus.dict.filter_extremes(no_below=1)
    hundoGrandoCorpus.dict.compactify()

    hundoGrandoCorpus.run_hdp(modelId='testMod',chunksize=1000, max_time=100, K = 4, T = 10000, alpha=10, gamma=1, scale=1, eta=0.5)

if __name__ == '__main__':
    main()


import torch

from ..optimizers.mirror_descent_optimizer import MirrorDescentOptimizer

###############################################################################
#
# This file implements logging of certain metrics associated to well-specified
# linear regression datasets. In particular, the ones generated using the
# rprml.data.synthetic_data.generate_synthetic_regression_dataset function.
# The model class is assumed to be of type rprml.models.LinearModel.
#
###############################################################################

def log_linear_model_complexity_parameters(simulation):
    """ Registers logging various complexity parameters, such as in-sample
    prediction-error, model parameters at iteration t,
    Bregman divergence to the true minimizer (if applied to a mirror descent
    simulation), etc. """

    X = simulation.train_dataset.X
    y = simulation.train_dataset.y
    w_star = simulation.train_dataset.w_star.view(-1, 1)
    loss_w_star = torch.sum((X.matmul(w_star) - y)**2) / X.shape[0]
    covariance_matrix = None

    if hasattr(simulation.train_dataset, 'covariance_matrix'):
        covariance_matrix = simulation.train_dataset.covariance_matrix
    if covariance_matrix is None:
        # Set covariance_matrix to identity
        covariance_matrix = torch.diag(
            torch.ones(X.shape[1], device=X.device, dtype=X.dtype))
    else:
        if not isinstance(covariance_matrix, torch.Tensor):
            covariance_matrix = torch.tensor(
                covariance_matrix, device=X.device, dtype=X.dtype)

    def _log_wt(engine, executor):
        return simulation.model.get_w().detach().cpu().numpy().flatten()

    def _log_bregman_divergence(engine, executor):
        with torch.no_grad():
            if not isinstance(simulation.optimizer, MirrorDescentOptimizer):
                return -1  # This is not a mirror descent simulation.
            w_t = simulation.model.get_w()
            mirror_map = simulation.optimizer.mirror_map
            return mirror_map.compute_bregman_divergence(w_star, w_t)

    def _log_true_excess_risk(engine, executor):
        with torch.no_grad():
            w_t = simulation.model.get_w()
            delta_t = w_t - w_star
            excess_risk_t = delta_t.t() @ covariance_matrix @ delta_t
            return excess_risk_t.item()

    def _log_rt(engine, executor):
        # Logs in sample prediction.
        with torch.no_grad():
            w_t = simulation.model.get_w()
            r_t = torch.sum(X.matmul(w_t - w_star)**2) / X.shape[0]
            return r_t.item()

    def _log_delta_t(engine, executor):
        with torch.no_grad():
            w_t = simulation.model.get_w()
            loss_w_t = torch.sum((X.matmul(w_t) - y)**2) / X.shape[0]
            return (loss_w_t - loss_w_star).item()

    simulation.executor.register_not_printable_metric(
        _log_wt, 'w_t')
    # The excess risk metric should be ignored for fixed-design simulations.
    # In such a case, covariance is assumed to be identity and excess risk
    # outputs the l2 squared parameter estimation error.
    simulation.executor.register_not_printable_metric(
        _log_true_excess_risk, 'excess_risk_t')
    simulation.executor.register_not_printable_metric(
        _log_rt, 'r_t')
    simulation.executor.register_not_printable_metric(
        _log_delta_t, 'delta_t')
    simulation.executor.register_not_printable_metric(
        _log_bregman_divergence, 'bregman_t')


import pandas as pd
# import requests

url = 'https://www.fdic.gov/bank/individual/failed/banklist.html'
dfs = pd.read_html(url)

print(type(dfs))
print(len(dfs))

# Esse código não funcionou. A função read_html não encontrou tabelas no endereço especificado.


import json
from flaskr.tests.fixtures.album_fixture import AlbumFixture
from flaskr.tests.fixtures.user_fixtures import UsuarioFixture
from flaskr.modelos import Acceso, Medio
from flaskr.tests import BaseCase, fixtures

class TestAlbum(BaseCase):
    def test_given_existing_user_that_creates_new_album_when_login_and_retrieve_album_list_then_success(self):
        """
        Asserts un usuario puede ver su lista de albumes
        """
        # Arrange
        user = UsuarioFixture().create()
        album_data = {
            'titulo': 'Test',
            'descripcion': 'Test',
            'medio': Medio.CASETE.name,
            'anio': 2020
        }

        # Act
        crear_album_response = self.run_authenticated(user, 'post', f'/usuario/{user.id}/albumes', data=json.dumps(album_data))
        response = self.run_authenticated(user, 'get', f'/usuario/{user.id}/albumes')

        new_album = crear_album_response.json
        albums = response.json

        # Assert
        self.assertEqual(200, crear_album_response.status_code)
        self.assertEqual(200, response.status_code)
        self.assertEqual(len(albums), 1)
        self.assertEqual(new_album['acceso']['llave'], Acceso.PRIVADO.name)
        self.assertEqual(new_album['titulo'], album_data['titulo'])
        self.assertTrue(any(album['id'] is new_album['id'] for album in albums))

    def test_given_existing_user_that_have_albums_when_login_and_retrieve_album_list_then_success(self):
        """
        Asserts un usuario puede ver su lista de albumes
        """
        # Arrange
        user = UsuarioFixture().create()
        album_fixture = AlbumFixture()
        album_fixture.default_usuario = fixtures.ObjectFixture(user)

        first_album = album_fixture.create()
        second_album = album_fixture.create()

        # Act
        response = self.run_authenticated(user, 'get', f'/usuario/{user.id}/albumes')
        albums = response.json
        # Assert
        self.assertEqual(200, response.status_code)
        self.assertEqual(len(albums), 2)
        self.assertTrue(any(album['id'] is first_album.id for album in albums))
        self.assertTrue(any(album['id'] is second_album.id for album in albums))

    def test_given_existing_user_that_have_albums_and_public_albums_exists_when_login_and_retrieve_album_list_then_success(self):
        """
        Asserts que en la lista de albumes de un usuario puede ver sus albumes y los albumes publicos
        """
        # Arrange
        user_fixture = UsuarioFixture()
        first_user = user_fixture.create()
        album_fixture = AlbumFixture()
        album_fixture.default_usuario = fixtures.ObjectFixture(first_user)

        private_first_album = album_fixture.create()
        private_second_album = album_fixture.create()

        # otro usuario que tendrá albumes publicos
        second_user = user_fixture.create()
        album_fixture.default_usuario = fixtures.ObjectFixture(second_user)

        private_album = album_fixture.create()

        album_fixture.default_acceso = fixtures.ObjectFixture(Acceso.PUBLICO)
        public_first_album = album_fixture.create()

        # Act
        response = self.run_authenticated(first_user, 'get', f'/usuario/{first_user.id}/albumes')
        albums = response.json
        # Assert
        self.assertEqual(200, response.status_code)
        self.assertEqual(len(albums), 3)
        self.assertTrue(any(album['id'] is private_first_album.id for album in albums))
        self.assertTrue(any(album['id'] is private_second_album.id for album in albums))
        self.assertTrue(any(album['id'] is public_first_album.id and album['pertenece'] == False for album in albums)) # album publico que pertenece a otro coleccionista

    def test_given_existing_user_that_makes_an_existing_album_to_public_then_success(self):
        """
        Asserts un usuario puede ver su lista de albumes, incluidos sus albumes publicos
        """
        # Arrange
        user = UsuarioFixture().create()
        album_fixture = AlbumFixture()
        album_fixture.default_usuario = fixtures.ObjectFixture(user)

        first_album = album_fixture.create()
        second_album = album_fixture.create()

        # Act
        update_response = self.run_authenticated(user, 'patch', f'/album/{first_album.id}', data=json.dumps({'acceso': Acceso.PUBLICO.name}))

        albums_response = self.run_authenticated(user, 'get', f'/usuario/{user.id}/albumes')
        albums = albums_response.json

        # Assert
        self.assertEqual(204, update_response.status_code)
        self.assertEqual(len(albums), 2)
        self.assertTrue(any(album['id'] is first_album.id and album['acceso']['llave'] == Acceso.PUBLICO.name for album in albums)) # el album se hizo publico
        self.assertTrue(any(album['id'] is second_album.id for album in albums))

    def test_given_existing_user_that_is_not_owner_of_public_album_when_mark_album_as_public_then_fail(self):
        """
        Asserts un usuario puede ver su lista de albumes, incluidos sus albumes publicos
        """
        # Arrange
        user = UsuarioFixture().create()

        album_fixture = AlbumFixture()
        first_album = album_fixture.create()

        # Act
        update_response = self.run_authenticated(user, 'patch', f'/album/{first_album.id}', data=json.dumps({'acceso': Acceso.PUBLICO.name}))

        # Assert
        error_response = update_response.json
        self.assertEqual(400, update_response.status_code)
        self.assertEqual('No puede cambiar el acceso del album.', error_response['usuario'])


# Write your code here
import random

# Enter name
name = input('Enter your name: ')
print("Hello, " + name, end="!")

# Enter list of options
options = input()

# If nothing is entered, original rock, paper scissors starts:
if options == "":
    options = "rock,paper,scissors"
valid_options = options.split(sep=',')  # Splitting string into a list

# Reading rating.txt, storing as a list and removing line breaks
f = open('rating.txt', 'r+')
rating = f.readlines()
f.close()

rating = [i.strip("\n") for i in rating]

# Creating key-value pairs dictionary for names and ratings to look like {'Tim': '350', 'Jane': '200', 'Alex': '400'}:
rating_dict = {}

for i in rating:
    rating_dict[i.split(' ')[0]] = i.split(' ')[1]

# Checking if name is in the keys:
name_exists = name in rating_dict.keys()

# If name exists in the rating.txt file, take starting rating from the file. Otherwise set at 0:
if name_exists:
    game_rating = int(rating_dict[name])
else:
    game_rating = 0
# Line 30-36 could be reduced but I'm too exhausted to spend a minute on it.

# Playing the game
print("Okay, let's start")
while True:
    pick = input()
    if pick == "!exit":
        print("Bye!")
        break
    elif pick == "!rating":
        print("Your rating: " + str(game_rating))
        continue
    elif pick not in valid_options:
        print("Invalid input")
        continue
    else:

    # This is the rotation of custom list. where we treat a linear list as a circle. This was the key concept required to solve this stage.

        comp = random.choice(valid_options)
        index_pick = valid_options.index(pick)
        before = valid_options[:index_pick]
        after = valid_options[index_pick + 1:]
        new_list = after + before
        half = int(len(new_list) / 2)
        comp_wins = new_list[:half]
        if pick == comp:
            print("There is a draw " + comp)
            game_rating += 50
        elif comp in comp_wins:
            print("Sorry, but the computer chose " + comp)
        else:
            print("Well done. The computer chose " + comp + " and failed")
            game_rating += 100


import pushmanager.core.util
import tornado.gen
import tornado.web
from pushmanager.core.requesthandler import RequestHandler

class PushByRequestServlet(RequestHandler):

    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        requestid = pushmanager.core.util.get_int_arg(self.request, 'id')
        response = yield tornado.gen.Task(
                        self.async_api_call,
                        "pushbyrequest",
                        {"id": requestid}
                    )

        push = self.get_api_results(response)
        if push:
            self.redirect('/push?id=%s' % push['id'])


from django.urls import path
from .views import FasesList, FasesUpdate, FasesDelete, FasesCreate

urlpatterns = [
    path('list', FasesList.as_view(), name='list_fases'),
    path('update/<int:pk>/', FasesUpdate.as_view(), name='update_fases'),
    path('delete/<int:pk>/', FasesDelete.as_view(), name='delete_fases'),
    path('create', FasesCreate.as_view(), name='create_fases'),
]


# if this doesn't work then probabaly fuzzywuzzy is not installed so for that do this:
# pip install fuzzywuzzy

from fuzzywuzzy import process
str2Match = "apple inc"
strOptions = ["Apple Inc.","apple park","apple incorporated","iphone"]
Ratios = process.extract(str2Match,strOptions)
print(Ratios)
# You can also select the string with the highest matching percentage
highest = process.extractOne(str2Match,strOptions)
print(highest)


from __future__ import absolute_import, division, print_function
from six.moves import range
def binTable(binners, labels):
  pass
  size = binners[0].size()
  print(size,"bins:")
  length = len(labels)

  print("             ", end=' ')
  for x in range(length):
   if len(labels)>=x+1 and labels[x] is not None:
     print("%12s"%labels[x], end=' ')
   else:
     print("%12s"%None, end=' ')
  print()

  binlimits=binners[0].getbinlimits()
  for x in range(size):
    if x == 0:
      print("       %6.2f"%binlimits[0], end=' ')
    else:
      print("%6.2f %6.2f"%(binlimits[x-1],binlimits[x]), end=' ')

    for list in binners:
      value = list[x]

      if value is not None:
        if -2.0<value<2.0:
          print("%12.5f"%value, end=' ')
        else:
          print("%12.2f"%value, end=' ')
      else:
        print("            ", end=' ')
    print()


import numpy as np

def scalar_function(x, y):
    """
    Returns the f(x,y) defined in the problem statement.
    """
    #Your code here

    if x <= y:
        func = np.dot(x,y)
        return func
    else:
        func = x/y
        return func

def vector_function(x, y):
    """
    Make sure vector_function can deal with vector input x,y
    """
    #Your code here
    vfunc = np.vectorize(scalar_function(x,y))
    return vfunc

if __name__ == '__main__':
    D = vector_function(2,5)
    print(D)


import socket
from receiver_server import GenericServer, Data
import logging

#  very simple server with 2 socket open: one for data stream and the other for commands

class SocketSpineServer(GenericServer):
    _server_socket = None
    _data_socket = None
    _cmd_socket = None

    def __init__(self):
        GenericServer.__init__(self)

    def start(self):

        # Bind the server socket to the server address
        self._server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._server_socket.bind((self._server_address, self._server_port))

    def listen_for_connection(self):
        print("listening for connections")

        # listen for 2 incoming connections
        self._server_socket.listen(2)

        # Accept the incoming connections
        in_socket_1, address_1 = self._server_socket.accept()
        self._handshake(in_socket_1, address_1)

        in_socket_2, address_2 = self._server_socket.accept()
        self._handshake(in_socket_2, address_2)

    def setTimeout(self, timeout=0):
        if self._data_socket is not None:
            self._data_socket.settimeout(timeout)
            self._cmd_socket.settimeout(timeout)

    def kill_client_sockets(self):
        logging.info("shutting down connection")

        try:
            self._data_socket.shutdown(socket.SHUT_RDWR)
            self._data_socket.close()
        except:
            print("data socket already disconnected")

        try:
            self._cmd_socket.shutdown(socket.SHUT_RDWR)
            self._cmd_socket.close()
        except:
            print("command socket already disconnected")
        print("Connection are shut down.. listening for new connections")

    def kill(self):
        logging.info("shutting down connection")

        self._data_socket.shutdown(socket.SHUT_RDWR)
        self._data_socket.close()

        self._cmd_socket.shutdown(socket.SHUT_RDWR)
        self._cmd_socket.close()

        self._server_socket.shutdown(socket.SHUT_RDWR)
        self._server_socket.close()

    def _handshake(self, s, address=None):
        return

    def _receive_data(self):
        pass

    def send_data(self, data):
        self._cmd_socket.sendall(data)

    @staticmethod
    def _recvall(s, n):
        # Helper function to recv n bytes or return None if EOF is hit
        raw_data = B''
        while len(raw_data) < n:
            packet = s.recv(n - len(raw_data))
            if not packet:
                return False, raw_data
            raw_data += packet
        return True, raw_data