# Soal no. 1
def nb_year(p0, percent, aug, p):
    abs(p0)
    abs(p)
    int(aug)
    percent = abs(percent)/100
    year = 0
    while(True):
        if p0 >= p:
            break
        else:
            p0 = round((p0 + (p0*percent) + aug))
            year += 1
    print('Year(s) need to surpass the target population is {}'.format(year))

nb_year(1000, 2, 50, 1200)

#Soal 2
peopleInLine = [25, 25, 100, 50, 75]
def tickets(peopleInLine):
    list = []
    money = {}
    idx = [25,50,75,100]
    for i in idx:
        money.setdefault(i, 0)
    for j in peopleInLine:
        if j == 25:
            money[25] += 1
            list.append(j)
        elif j == 50 and money[25] > 0:
            money[25] -= 1
            money[50] += 1
            list.append(j)
        elif j == 50 and money[25] == 0:
            print('NO')
            break
        elif j == 75 and money[50] >= 1:
            money[75] += 1
            money[50] -= 1
            list.append(j)
        elif j == 75 and money[25] >= 2:
            money[75] += 1
            money[25] -= 2
            list.append(j)
        elif (j == 75 and money[25] <= 1) or (j == 75 and money[50] == 0):
            print('NO')
            break
        elif j == 100 and money[75] >= 1:
            money[100] += 1
            money[75] -= 1
            list.append(j)
        elif j == 100 and money[50] >= 1 and money[25] >= 1:
            money[100] += 1
            money[50] -= 1
            money[25] -= 1
            list.append(j)
        elif j == 100 and money[25] >= 3:
            money[100] += 1
            money[25] -= 3
            list.append(j)
        else:
            print('NO')
            break
    if len(list) == len(peopleInLine):
        print('YES')

tickets(peopleInLine)

# #Soal Nomor 3
def rowSumOddNumbers(n):
    z = ''
    odd = list(filter(lambda x: x%2 != 0, list(map(lambda x: x+1, range(n**3)))))
    odd_list = []
    var = 0
    if n == 1:
        odd_list = [1]
    else:
        for i in range(n):
            var += i+1
    for j in range(var):
        odd_list.append(odd[j])
    num = []
    for k in odd_list:
        num.append(str(k))
    idx = 0
    for i in range (0,n):
        for j in range (0, (len(odd_list))):
            if j >= len(odd_list)-i and j <= len(odd_list)+i:
                z += ' ' +str(odd_list[idx])+' '
                idx += 1
            else:
                z += '   '
        z += '\n'
    print(z)
    print(sum(odd_list))

rowSumOddNumbers(3)


'''
Exercício Python #082 - Dividindo valores em várias listas:
Crie um programa que vai ler vários números e colocar em uma lista.
Depois disso, crie duas listas extras que vão conter apenas os valores
pares e os valores ímpares digitados, respectivamente.
Ao final, mostre o conteúdo das três listas geradas.
'''
print("== Exercício Python #082 - Dividindo valores em várias listas: ==")
lista = []
pares = []
impares = []
while True:
    valor = int(input('Digite um valor: '))
    lista.append(valor)
    if valor % 2 == 0 and valor != 0:
        pares.append(valor)
    if valor % 2 != 0:
        impares.append(valor)
    escolha = input('Sair? [S/N]: ')
    if escolha in 'Ss':
        break
print('-'*40)
print(f'- Lista geral: {lista}')
if len(pares) > 0:
    print(f'- Lista pares: {pares}')
else:
    print('- Não foram digitados números pares.')
if len(impares) > 0:
    print(f'- Lista ímpares: {impares}')
else:
    print('- Não foram digitados números ímpares.')
print('-'*40)

CURSO DE PYTHON 3/Mundo 3 - Estruturas Compostas/2 - Listas em Python/Parte 01/Exerc082.py


# 20546번 기적의 매매법
# https://www.acmicpc.net/problem/20546

# 잔액, 갯수
junhyun = [int(input()), 0]
sungmin = [junhyun[0], 0]
stock = list(map(int, input().split()))

updown = [0 for _ in range(14)]
for idx in range(1, 14):
    if stock[idx] - stock[idx-1] > 0:
        updown[idx] = 1
    elif stock[idx] - stock[idx-1] < 0:
        updown[idx] = -1

last = 0
for idx in range(14):
    price = stock[idx]
    if idx == 13:
        last = price

    junhyun[1] += junhyun[0] // price
    junhyun[0] %= price

    # 성민이가 주식을 가지고 있을 때
    if sungmin[1]:
        for j in range(3):
            if idx - j < 0:
                break
            if updown[idx-j] != 1:
                break
        else:
            sungmin[0] += sungmin[1] * price
            sungmin[1] = 0

    # 성민이가 주식이 없을 때
    else:
        for j in range(3):
            if idx - j < 0:
                break
            if updown[idx-j] != -1:
                break
        else:
            sungmin[1] = sungmin[0] // price
            sungmin[0] += sungmin[1] % price

    # print(idx+1, price, junhyun, sungmin)

junhyun_balance = junhyun[0] + junhyun[1] * last
sungmin_balance = sungmin[0] + sungmin[1] * last

# print(junhyun_balance)
# print(sungmin_balance)

if junhyun_balance > sungmin_balance:
    print('BNP')
elif junhyun_balance == sungmin_balance:
    print('SAMESAME')
else:
    print('TIMING')


import re
from libs.python.helperJson import addKeyValuePair

def getTimingsForStatusRequest(btpUsecase, thisService):

    search_every_x_seconds = btpUsecase.repeatstatusrequest
    usecaseTimeout = btpUsecase.repeatstatustimeout

    # If the service has defined its own time to repeat a status request, take that time instead
    if "repeatstatusrequest" in thisService:
        search_every_x_seconds = int(thisService["repeatstatusrequest"])
    if "repeatstatustimeout" in thisService:
        usecaseTimeout = int(thisService["repeatstatustimeout"])

    return search_every_x_seconds, usecaseTimeout

def getServiceByServiceName(btpUsecase, serviceName):
    for service in btpUsecase.definedServices:
        if service["name"] == serviceName:
            return service
    return None

def getNamingPattern(btpUsecase, prefix, suffix):
    result = None
    if prefix is None:
        prefix = ""
    if suffix is None:
        suffix = ""

    if prefix is None:
        prefix = ""
    if suffix is None:
        suffix = ""
    result = prefix + suffix
    result = re.sub(r"[^\w\s]", '-', result)
    result = result.replace(" ", "-").lower()
    result = result.replace("_", "-").lower()

    return result

def getNamingPatternForServiceSuffix(btpUsecase):
    result = getNamingPattern(btpUsecase, " instance ", None)
    return result

def getNamingPatternForIdsNEW(btpUsecase):
    usecaseRegion = btpUsecase.region
    prefix = "BTP setup automator"
    suffix = usecaseRegion

    result = getNamingPattern(btpUsecase, prefix, suffix)
    return result

def createSubaccountName(btpUsecase):
    result = None
    if btpUsecase.subaccountname is not None and btpUsecase.subaccountname != "":
        result = btpUsecase.subaccountname.strip()
    else:
        result = "BTP setup automator (" + btpUsecase.region + ")"

    btpUsecase.accountMetadata = addKeyValuePair(
        btpUsecase.accountMetadata, "subaccount", result)

    return result

def createInstanceName(btpUsecase, service):
    result = "instance"
    if "instancename" in service:
        return service["instancename"]
    else:
        result = service["name"] + "_" + service["plan"] + "_" + btpUsecase.suffixinstancename
    result = re.sub(r"[^\w\s]", '_', result)
    result = result.replace("__", "_")
    if result[len(result) - 1] == "_":
        result = result[:-1]

    result = result[:40]

    return result

def createSubdomainID(btpUsecase):
    result = None
    if btpUsecase.subdomain is not None and btpUsecase.subdomain != "":
        result = btpUsecase.subdomain.strip()
    else:
        result = btpUsecase.accountMetadata["subaccount"] + \
            "-" + btpUsecase.accountMetadata["global_account_id"]

    result = re.sub(r"[^\w\s]", '-', result)
    result = result.replace(" ", "-")
    result = result.replace("_", "-")
    result = result.replace("--", "-")

    if result[len(result) - 1] == "-":
        result = result[:-1]
    result = result[:60].lower()

    btpUsecase.accountMetadata = addKeyValuePair(
        btpUsecase.accountMetadata, "subdomain", result)

    return result

def createOrgName(btpUsecase, envName):
    result = None
    result = envName + "-" + btpUsecase.accountMetadata["subdomain"]

    result = re.sub(r"[^\w\s]", '-', result)
    result = result.replace(" ", "-").lower()
    result = result.replace("_", "-").lower()
    result = result.replace("--", "-").lower()
    result = result[:60]

    return result


otroci = {
    "Adam": ["Matjaž", "Cilka", "Daniel"],
    "Aleksander": [],
    "Alenka": [],
    "Barbara": [],
    "Cilka": [],
    "Daniel": ["Elizabeta", "Hans"],
    "Erik": [],
    "Elizabeta": ["Ludvik", "Jurij", "Barbara"],
    "Franc": [],
    "Herman": ["Margareta"],
    "Hans": ["Herman", "Erik"],
    "Jožef": ["Alenka", "Aleksander", "Petra"],
    "Jurij": ["Franc", "Jožef"],
    "Ludvik": [],
    "Margareta": [],
    "Matjaž": ["Viljem"],
    "Petra": [],
    "Tadeja": [],
    "Viljem": ["Tadeja"],
}

def premozenje(oseba,denar):
    x = denar[oseba]
    for i in otroci[oseba]:
        x += premozenje(i,denar)
    return x

def najbogatejsi(oseba, denar):
    x = [(oseba, denar[oseba])]
    for i in otroci[oseba]:
        x.append(najbogatejsi(i, denar))
        x.sort(key=lambda a: a[1], reverse=True)
    return x[0]


import datetime
import sys
import time

from vwradio import avrclient
from vwradio import faceplates

class Demo(object):
    ''' Abstract class '''
    def __init__(self, client, faceplate):
        self.client = client
        self.faceplate = faceplate

    def run(self):
        ''' Abstract method '''
        raise NotImplementedError()

    def clear(self):
        self.client.faceplate_upd_clear_display()

    def read_keys(self):
        return self.client.read_keys()

    def write(self, text, pos=0):
        char_codes = [ self.faceplate.char_code(c) for c in text ]
        self.write_char_codes(char_codes, pos)

    def write_char_codes(self, char_codes, pos=0):
        addresses = self.faceplate.VISIBLE_DISPLAY_ADDRESSES

        if len(char_codes) > (len(addresses) - pos):
            raise ValueError("Data %r exceeds visible range from pos %d" %
                (char_codes, pos))

        if addresses[0] < addresses[1]:
            start_address = addresses[pos]
        else:
            start_address = addresses[pos + len(char_codes) - 1]
            char_codes = char_codes[::-1] # reverse it

        # send Data Setting command: write to display ram
        self.client.faceplate_upd_send_command([0x40])

        # send Address Setting command plus data to write to display ram
        data = [0x80 + start_address] + list(char_codes)
        self.client.faceplate_upd_send_command(data)

    def define_char(self, index, data):
        if index not in range(16):
            raise ValueError("Character number %r is not 0-15", index)
        if len(data) != 7:
            raise ValueError("Character data length %r is not 7" % len(data))
        # Data Setting command: write to chargen ram
        self.client.faceplate_upd_send_command([0x4a])
        # Address Setting command, data
        self.client.faceplate_upd_send_command([0x80 + index] + list(data))

class ShowCharsetDemo(Demo):
    '''Show the entire ROM character set on the faceplate'''

    def run(self):
        for i in range(0, 0x100, 7):
            self.write('0x%02X' % i, pos=0)
            data = [0x20] * 7 # 7 chars, default blank spaces
            for j in range(len(data)):
                code = i + j
                if code > 0xFF: # past end of charset
                    break
                data[j] = code
            self.write_char_codes(data, pos=4)
            time.sleep(1)
        self.clear()

class ComparseCharsetDemo(Demo):
    '''Compare the ROM-based character set to the dump included in
    this Python code, which should match it exactly.  A programmable
    character is set to the character data, then the display will
    alternate between it and the ROM character.  No changes should
    be observed during alternation.'''

    def run(self):
        def read_char_data(charset, index):
            start = i * 7
            end = start + 7
            return charset[start:end]

        for i in range(0x10, 0x100):
            # define character 0 with the rom pattern
            data = read_char_data(self.faceplate.ROM_CHARSET, i)
            self.define_char(0, data)

            # first four chars: display character code in hex
            self.write('0x%02X' % i, pos=0)

            # all other chars: alternate between rom and programmable char 0
            for blink in range(6):
                for code in (0, i):
                    self.write_char_codes([code]*7, pos=4)
                    time.sleep(0.2)
        self.controller.clear()

class ShowKeysDemo(Demo):
    '''Wait for a key to be pressed on the faceplate and the display it'''

    def run(self):
        self.write('    Hit Key')
        while True:
            keys = self.read_keys()
            names = [ self.faceplate.get_key_name(k) for k in keys ]
            if names:
                print("%r" % names)
                msg = names[0][:11].ljust(11)
                self.write(msg)

class ClockDemo(Demo):
    '''Display the time of day with a blinking hour-minute separator'''
    def run(self):
        self.clear()
        self.write("Time", pos=0)
        while True:
            now = datetime.datetime.now()
            sep = (':', ' ')[now.second & 1]
            clock = now.strftime("%I" + sep + "%M%p").lower()
            if clock[0] == '0':
                clock = ' ' + clock[1:]
            self.write(clock, pos=4)

_demos_by_name = {'show_charset':    ShowCharsetDemo,
                  'compare_charset': ComparseCharsetDemo,
                  'show_keys':       ShowKeysDemo,
                  'clock':           ClockDemo,
                 }

def usage():
    exe = sys.argv[0]
    names = _demos_by_name.keys()
    sys.stderr.write("Usage: %s <%s>\n" % (exe, '|'.join(names)))
    sys.exit(1)

def main():
    if len(sys.argv) != 2:
        usage()

    klass = _demos_by_name.get(sys.argv[1])
    if klass is None:
        usage()

    client = avrclient.make_client()
    faceplate = faceplates.Premium4()
    try:
        client.set_auto_display_passthru(False)
        client.set_auto_key_passthru(False)
        demo = klass(client, faceplate)
        demo.run()
    except KeyboardInterrupt:
        # if a command is in process, let it finish
        time.sleep(0.1)
    finally:
        client.set_auto_display_passthru(True)
        client.set_auto_key_passthru(True)

if __name__ == '__main__':
    main()


'''
Created on 22-04-2013

@author: Damian Rusinek <damian.rusinek@gmail.com>
'''

from aqopa.model.parser.lex_yacc import LexYaccParserExtension
from aqopa.model import BooleanExpression, TupleExpression,\
    TupleElementExpression, IdentifierExpression, ComparisonExpression,\
    CallFunctionExpression, COMPARISON_TYPE_EQUAL, COMPARISON_TYPE_NOT_EQUAL

class Builder():

    def build_expression_call_function(self, token):
        """
        expression_call_function : IDENTIFIER LPARAN RPARAN
                                | IDENTIFIER LPARAN expression_function_arguments RPARAN
                                | IDENTIFIER LPARAN RPARAN SQLPARAN expression_function_qop_arguments SQRPARAN
                                | IDENTIFIER LPARAN expression_function_arguments RPARAN SQLPARAN expression_function_qop_arguments SQRPARAN
        """
        arguments = []
        qop_arguments = []

        if len(token) == 5:
            arguments = token[3]
        elif len(token) == 7:
            qop_arguments = token[5]
        elif len(token) == 8:
            arguments = token[3]
            qop_arguments = token[6]

        return CallFunctionExpression(token[1], arguments, qop_arguments)

    def build_expression_call_id_function(self, token):
        """
        expression_call_function : ID LPARAN RPARAN
                                | ID LPARAN IDENTIFIER RPARAN
                                | ID LPARAN QUALIFIED_IDENTIFIER RPARAN
        """
        arguments = []
        if len(token) == 5:
            arguments.append(IdentifierExpression(token[3]))
        return CallFunctionExpression(token[1], arguments)

    def build_expression_call_routing_next(self, token):
        """
        expression_call_function : ROUTING_NEXT LPARAN IDENTIFIER COMMA expression_call_routing_next_argument RPARAN
                                            | ROUTING_NEXT LPARAN IDENTIFIER COMMA expression_call_routing_next_argument COMMA expression_call_routing_next_argument RPARAN
        """
        arguments = [IdentifierExpression(token[3]), token[5]]
        if len(token) == 9:
            arguments.append(token[7])
        return CallFunctionExpression(token[1], arguments)

    def build_comparison_expression(self, token):
        """
        expression_comparison : expression_simple EQUAL EQUAL expression_simple
                            | expression_simple EXCLAMATION EQUAL expression_simple
        """
        comparison_type = COMPARISON_TYPE_EQUAL if token[2] == '=' else COMPARISON_TYPE_NOT_EQUAL
        return ComparisonExpression(token[1], token[4], comparison_type)

class ModelParserExtension(LexYaccParserExtension):
    """
    Extension for parsing functions
    """

    def __init__(self):
        LexYaccParserExtension.__init__(self)

        self.builder = Builder()

    ##########################################
    #           RESERVED WORDS
    ##########################################

    ##########################################
    #                TOKENS
    ##########################################

    ##########################################
    #                RULES
    ##########################################

    def expression_conditional(self, t):
        """
        expression_conditional : expression_comparison
                        | BOOL
        """
        if isinstance(t[1], bool):
            t[0] = BooleanExpression(t[1])
        else:
            t[0] = t[1]

    def expression_tuple(self, t):
        """
        expression_tuple : LPARAN RPARAN
                        | LPARAN expression_function_arguments RPARAN
        """
        elements = []
        if len(t) > 3:
            elements = t[2]
        t[0] = TupleExpression(elements)

    def expression_tuple_element(self, t):
        """
        expression_tuple_element : IDENTIFIER SQLPARAN INTEGER SQRPARAN
        """
        t[0] = TupleElementExpression(t[1], t[3])

    def expression_function_arguments(self, t):
        """
        expression_function_arguments : expression_simple
                                    | expression_function_arguments COMMA expression_simple
        """
        if len(t) == 2:
            t[0] = []
            t[0].append(t[1])
        else:
            t[0] = t[1]
            t[0].append(t[3])

    def expression_call_function(self, t):
        """
        expression_call_function : IDENTIFIER LPARAN RPARAN
                                | IDENTIFIER LPARAN expression_function_arguments RPARAN
                                | IDENTIFIER LPARAN RPARAN SQLPARAN expression_function_qop_arguments SQRPARAN
                                | IDENTIFIER LPARAN expression_function_arguments RPARAN SQLPARAN expression_function_qop_arguments SQRPARAN
        """
        t[0] = self.builder.build_expression_call_function(t)

    def expression_call_id_function(self, t):
        """
        expression_call_function : ID LPARAN RPARAN
                                | ID LPARAN IDENTIFIER RPARAN
                                | ID LPARAN QUALIFIED_IDENTIFIER RPARAN
        """
        t[0] = self.builder.build_expression_call_id_function(t)

    def expression_call_routing_next_function(self, t):
        """
        expression_call_function : ROUTING_NEXT LPARAN IDENTIFIER COMMA expression_call_routing_next_argument RPARAN
                                            | ROUTING_NEXT LPARAN IDENTIFIER COMMA expression_call_routing_next_argument COMMA expression_call_routing_next_argument RPARAN
        """
        t[0] = self.builder.build_expression_call_routing_next(t)

    def expression_call_routing_next_argument(self, t):
        """
        expression_call_routing_next_argument : expression_call_function
                                            | expression_tuple_element
                                            | IDENTIFIER
        """
        if isinstance(t[1], str) or isinstance(t[1], unicode):
            t[0] = IdentifierExpression(t[1])
        else:
            t[0] = t[1]

    def expression_function_qop_arguments(self, t):
        """
        expression_function_qop_arguments : TEXT
                                        | expression_function_qop_arguments COMMA TEXT
        """
        if len(t) == 2:
            t[0] = []
            t[0].append(t[1].strip())
        else:
            t[0] = t[1]
            t[0].append(t[3].strip())

    def expression_simple(self, t):
        """
        expression_simple : expression_call_function
                        | expression_tuple_element
                        | expression_tuple
                        | IDENTIFIER
                        | BOOL
        """
        if isinstance(t[1], bool):
            t[0] = BooleanExpression(t[1])
        elif isinstance(t[1], str) or isinstance(t[1], unicode):
            t[0] = IdentifierExpression(t[1])
        else:
            t[0] = t[1]

    def expression_comparison(self, t):
        """
        expression_comparison : expression_simple EQUAL EQUAL expression_simple
                            | expression_simple EXCLAMATION EQUAL expression_simple
        """
        t[0] = self.builder.build_comparison_expression(t)

    def _extend(self):

        self.parser.add_reserved_word('routing_next', 'ROUTING_NEXT', case_sensitive=True)
        self.parser.add_reserved_word('id', 'ID', case_sensitive=True)

        self.parser.add_rule(self.expression_conditional)
        self.parser.add_rule(self.expression_tuple)
        self.parser.add_rule(self.expression_tuple_element)
        self.parser.add_rule(self.expression_function_arguments)
        self.parser.add_rule(self.expression_call_function)
        self.parser.add_rule(self.expression_call_id_function)
        self.parser.add_rule(self.expression_call_routing_next_function)
        self.parser.add_rule(self.expression_call_routing_next_argument)
        self.parser.add_rule(self.expression_function_qop_arguments)
        self.parser.add_rule(self.expression_simple)
        self.parser.add_rule(self.expression_comparison)


import turtle
t=turtle.Turtle()
s=turtle.Screen()
s.bgcolor("black")
t.pensize(5)
t.color("red")
t.left(90)
t.forward(100)
t.circle(-25,300)
t.home()
t.up()
t.goto(50,0)
t.down()
t.left(90)
t.forward(50)
t.circle(-12.5,300)
t.left(150)
t.goto(75,0)


from http import client as http_client
import json
import logging

from apiclient import discovery
from apiclient import errors
from application import utils
from application import buganizer_utils as b_utils

_DISCOVERY_URI = ('https://issuetracker.googleapis.com/$discovery/rest?version=v1&labels=GOOGLE_PUBLIC')

BUGANIZER_SCOPES = 'https://www.googleapis.com/auth/buganizer'
MAX_DISCOVERY_RETRIES = 3
MAX_REQUEST_RETRIES = 5

class NoComponentException(Exception):
  pass

class BuganizerClient:
  """Class for updating perf issues."""

  def __init__(self):
    """Initializes an object for communicate to the Buganizer.
    """
    http = utils.ServiceAccountHttp(BUGANIZER_SCOPES)
    http.timeout = 30

    # Retry connecting at least 3 times.
    attempt = 1
    while attempt != MAX_DISCOVERY_RETRIES:
      try:
        self._service = discovery.build(
            'issuetracker', 'v1', discoveryServiceUrl=_DISCOVERY_URI, http=http)
        break
      except http_client.HTTPException as e:
        logging.error('Attempt #%d: %s', attempt, e)
        if attempt == MAX_DISCOVERY_RETRIES:
          raise
      attempt += 1

  def GetIssuesList(self, limit, age, status, labels, project='chromium'):
    """Makes a request to the issue tracker to list issues.

    Args:
      limit: the limit of results to fetch
      age: the number of days after the issue was created
      status: the status of the issues
      labels: the labels (hotlists) of the issue.
              The term 'labels' is what we use in Monorail and it is no longer
              available in Buganizer. Instead, we will use hotlists.
      project: the project name in Monorail. It is not needed in Buganizer. We
              will use it to look for the corresponding 'component'.

    Returns:
      a list of issues.
      (The issues are now in Monorail format before consumers are updated.)
    """
    project = 'chromium' if project is None or not project.strip() else project

    components = b_utils.FindBuganizerComponents(monorail_project_name=project)
    if not components:
      logging.warning(
        '[Buganizer API] Failed to find components for the given project: %s',
        project)
      return []
    components_string = '|'.join(components)
    query_string = 'componentid:%s' % components_string

    # by default, buganizer return all.
    if status and status != 'all':
      query_string += ' AND status:%s' % status

    if age:
      query_string += ' AND created:%sd' % age

    if labels:
      label_list = labels.split(',')
      hotlists = b_utils.FindBuganizerHotlists(label_list)
      if hotlists:
        query_string += ' AND hotlistid:%s' % '|'.join(hotlists)

    logging.info('[PerfIssueService] GetIssueList Query: %s', query_string)
    request = self._service.issues().list(
      query=query_string,
      pageSize=min(500, int(limit)),
      view='FULL'
    )
    response = self._ExecuteRequest(request)

    buganizer_issues = response.get('issues', []) if response else []

    logging.debug('Buganizer Issues: %s', buganizer_issues)

    monorail_issues = [
      b_utils.ReconcileBuganizerIssue(issue) for issue in buganizer_issues]

    return monorail_issues

  def GetIssue(self, issue_id, project='chromium'):
    """Makes a request to the issue tracker to get an issue.

    Args:
      issue_id: the id of the issue

    Returns:
      an issue.
      (The issues are now in Monorail format before consumers are updated.)
    """
    del project
    request = self._service.issues().get(issueId=issue_id, view='FULL')
    buganizer_issue = self._ExecuteRequest(request)

    logging.debug('Buganizer Comments for %s: %s', issue_id, buganizer_issue)

    monorail_issue = b_utils.ReconcileBuganizerIssue(buganizer_issue)

    return monorail_issue

  def GetIssueComments(self, issue_id, project='chromium'):
    """Gets all the comments for the given issue.

    The GetIssueComments is used only in the alert group workflow to check
    whether the issue is closed by Pinpoint (chromeperf's service account).
    Unlike Monorail, status update and comments are two independent workflows
    in Buganizer. As the existing workflow only looks for the status update,
    we will get the issue updates instead of issue comments.
    To avoid changes on client side, I keep the field name in 'comments' in
    the return value.

    Args:
      issue_id: the id of the issue.

    Returns:
      a list of updates of the issue.
    (The updates are now in Monorail format before consumers are updated.)
    """
    del project

    request = self._service.issues().issueUpdates().list(issueId=issue_id)
    response = self._ExecuteRequest(request)

    logging.debug('Buganizer Issue for %s: %s', issue_id, response)

    schema = self._service._schema.get('IssueState')
    status_enum = schema['properties']['status']['enum']

    if not response:
      return []

    return [{
        'id': update.get('version'),
        'author': update.get('author', {}).get('emailAddress', ''),
        'content': update.get('issueComment', {}).get('comment', ''),
        'published': update.get('timestamp'),
        'updates': b_utils.GetBuganizerStatusUpdate(update, status_enum) or {}
    } for update in response.get('issueUpdates')]

  def NewIssue(self,
             title,
             description,
             project='chromium',
             labels=None,
             components=None,
             owner=None,
             cc=None,
             status=None):
    ''' Create an issue on Buganizer

    While the API looks the same as in monorail_client, similar to what we do
    in reconciling buganizer data, we need to reconstruct the data in the
    reversed way: from the monorail fashion to the buganizer fashion.
    The issueState property should always exist for an Issue, and these
    properties are required for an issueState:
      title, componentId, status, type, severity, priority.

    Args:
      title: a string as the issue title.
      description: a string as the initial description of the issue.
      project: this is no longer needed in Buganizer when creating an issue.
          It will be used look for project specific mappings between Monorail
          and Buganizer (which are not available yet).
      labels: a list of Monorail labels, each of which will be mapped to a
          Buganizer hotlist id.
      components: a list of component names in Monorail. The size of the list
          should always be 1 as required by Buganizer.
      owner: the email address of the issue owner/assignee.
      cc: a list of email address to which the issue update is cc'ed.
      status: the initial status of the issue

    Returns:
      {'issue_id': id, 'project_id': project_name} if succeeded; otherwise
      {'error': error_msg}
    '''
    if not components:
      raise NoComponentException(
        'Componenet ID is required when creating a new issue on Buganizer.')
    if len(components)>1:
      logging.warning(
        '[PerfIssueService] More than 1 components on issue create. Using the first one.')
    buganizer_component_id = b_utils.FindBuganizerComponentId(components[0])

    if owner:
      monorail_status = 'Assigned'
    elif not status:
      monorail_status = 'Unconfirmed'
    else:
      monorail_status = status
    buganizer_status = b_utils.FindBuganizerStatus(monorail_status)

    priority  = 'P%s' % b_utils.LoadPriorityFromMonorailLabels(labels)
    labels = [label for label in labels if not label.startswith('Pri-')]

    new_issue_state = {
      'title': title,
      'componentId': buganizer_component_id,
      'status': buganizer_status,
      'type': 'BUG',
      'severity': 'S2',
      'priority': priority,
    }

    new_description = {
      'comment': description
    }

    if owner:
      new_issue_state['assignee'] = {
        'emailAddress': owner
      }
    if cc:
      emails = set(email.strip() for email in cc if email.strip())
      new_issue_state['ccs'] = [
        {'emailAddress': email} for email in emails if email
      ]
    if labels:
      hotlist_list = b_utils.FindBuganizerHotlists(labels)
      new_issue_state['hotlistIds'] = [hotlist for hotlist in hotlist_list]

    new_issue = {
      'issueState': new_issue_state,
      'issueComment': new_description
    }

    logging.warning('[PerfIssueService] PostIssue request: %s', new_issue)
    request = self._service.issues().create(body=new_issue)

    try:
      response = self._ExecuteRequest(request)
      logging.debug('[PerfIssueService] PostIssue response: %s', response)
      if response and 'issueId' in response:
        return {'issue_id': response['issueId'], 'project_id': project}
      logging.error('Failed to create new issue; response %s', response)
    except errors.HttpError as e:
      reason = self._GetErrorReason(e)
      return {'error': reason}
    except http_client.HTTPException as e:
      return {'error': str(e)}
    return {'error': 'Unknown failure creating issue.'}

  def NewComment(self,
                  issue_id,
                  project='chromium',
                  comment='',
                  title=None,
                  status=None,
                  merge_issue=None,
                  owner=None,
                  cc=None,
                  components=None,
                  labels=None,
                  send_email='True'):
    ''' Add a new comment for an existing issue

    Adding a new comment using the same set of arguments as in Monorail.
    Those arguments will be restructured before sending to Buganizer. In
    Buganizer, we need to have an issueState with the values to add, and
    another issueState with the values to remove.

    Notice that there are rules to follow, otherwise we will see 'invalid
    argument' error. We don't have a complete list of the rules yet.

    Args:
      issue_id: the id of the issue.
      project: this is no longer needed in Buganizer when creating an issue.
          It will be used look for project specific mappings between Monorail
          and Buganizer (which are not available yet).
      comment: the new comment to add
      title: the new title of the issue
      status: the new Monorail status of the issue.
          Monorail status will be mapped to Buganizer status.
      merge_issue: the target issue which the current issue is being merged to.
      owner: the new assignee of the issue.
      cc: a list of users to CC the updates.
      components: a list of Monorail components.
          Monorail components will be mapped to Buganizer components.
      labels: a list of Monorail labels.
          Monorail labels will be mapped to Buganizer hotlists. Notice that
          a format like -Some-Label is valid in Monorail. We need to add those
          to the to-be-removed state.
      send_email: whether to send email for a specific update.
          In Buganizer we cannot force sending email to users. Instead, each
          user chooses to receive emails based on the uers's role (assignee,
          reporter, cc, etc.). Each update in Buganizer has a significance
          level. By default, user receive only Major updates unless the user is
          the assignee. Here, we send the update's significance level to be
          Minor in those cases when send_email is set False, otherwise MAJOR
          to enforce emails.

    Returns:
      The updates Issue, or error message.
    '''
    if not issue_id or issue_id < 0:
      return {
        'error': '[PerfIssueService] Missing issue id on PostIssueComment'
        }

    add_issue_state, remove_issue_state = {}, {}

    significance_override = 'MAJOR'
    if str(send_email).lower() == 'false':
      significance_override = 'MINOR'

    if title:
      add_issue_state['title'] = title

    if status:
      add_issue_state['status'] = b_utils.FindBuganizerStatus(status)

    if owner:
      add_issue_state['assignee'] = {'emailAddress': owner}

    if components:
      if len(components)>1:
        logging.warning(
          '[PerfIssueService] More than 1 components on issue create. Using the first one.')
      add_issue_state['componentId'] = b_utils.FindBuganizerComponentId(components[0])

    if cc:
      ccs_to_remove = [
        email[1:] for email in cc if email.startswith('-') and len(email)>1
      ]
      ccs_to_add = [
        email for email in cc if email and not email.startswith('-')
      ]
      if ccs_to_add:
        add_issue_state['ccs'] = [
          {'emailAddress': email} for email in ccs_to_add
        ]
      if ccs_to_remove:
        remove_issue_state['ccs'] = [
          {'emailAddress': email} for email in ccs_to_remove
        ]

    if labels and any(label.startswith('Pri-') for label in labels):
      priority = 'P%s' % b_utils.LoadPriorityFromMonorailLabels(labels)
      add_issue_state['priority'] = priority
      labels = [label for label in labels if not label.startswith('Pri-')]

    if labels:
      labels_to_remove = [
        label[1:] for label in labels if label.startswith('-') and len(label)>1
      ]
      hotlists_to_remove = b_utils.FindBuganizerHotlists(labels_to_remove)
      labels_to_add = [
        label for label in labels if label and not label.startswith('-')
      ]
      hotlists_to_add = b_utils.FindBuganizerHotlists(labels_to_add)

      for hotlist_id in hotlists_to_add:
        hotlist_entry_request = {
          'hotlistEntry': {'issueId': issue_id},
          'significanceOverride': significance_override
        }
        request = self._service.hotlists().createEntries(
          hotlistId=hotlist_id, body=hotlist_entry_request)
        response = self._ExecuteRequest(request)
        logging.debug('[PerfIssueService] Add hotlist response: %s', response)
      for hotlist_id in hotlists_to_remove:
        request = self._service.hotlists().entries().delete(
          hotlistId=str(hotlist_id), issueId=str(issue_id),
          significanceOverride=significance_override)
        response = self._ExecuteRequest(request)
        logging.debug('[PerfIssueService] Delete hotlist response: %s', response)

    if merge_issue and int(merge_issue) != issue_id:
      merge_request = {
        'targetId': merge_issue,
        'significanceOverride': significance_override
      }
      request = self._service.issues().duplicate(
        issueId=str(issue_id), body=merge_request)
      response = self._ExecuteRequest(request)
      logging.info(
        '[PerfIssueService] Issue %s marked as duplicate of %s',
        issue_id, merge_issue)
      logging.debug('[PerfIssueSeervice] Merge response: %s', response)

    modify_request = {}
    if comment:
      modify_request['issueComment'] = {'comment': comment}
    if add_issue_state:
      modify_request['addMask'] = ','.join(add_issue_state.keys())
      modify_request['add'] = add_issue_state
    if remove_issue_state:
      modify_request['removeMask'] = ','.join(remove_issue_state.keys())
      modify_request['remove'] = remove_issue_state

    if not modify_request:
      return {}

    modify_request['significanceOverride'] = significance_override

    response = self._MakeCommentRequest(issue_id, modify_request)

    return response

  def _MakeCommentRequest(self, issue_id, modify_request, retry=True):
    try:
      logging.debug('[PerfIssueService] Post comment request body %s', modify_request)
      request = self._service.issues().modify(issueId=str(issue_id), body=modify_request)
      response = self._ExecuteRequest(request)
      logging.debug('[PerfIssueService] Post comment response %s', response)
      if response:
        return response
    except errors.HttpError as e:
      logging.warning(
        '[PerfIssueService] Buganizer error on post comments: %s', str(e))
      reason = self._GetErrorReason(e)
      if reason is None:
        reason = ''
      if retry and 'The user does not exist' in reason:
        logging.debug(
          'Removing assignee and ccs from issue modify request: %s',
          modify_request)
        if 'assignee' in modify_request.get('addMask', ''):
          current_add_mask_list = modify_request.get('addMask').split(',')
          current_add_mask_list.remove('assignee')
          new_add_mask = ','.join(current_add_mask_list)
          modify_request['addMask'] = new_add_mask
          del modify_request['add']['assignee']
        if 'ccs' in modify_request.get('addMask', ''):
          current_add_mask_list = modify_request.get('addMask').split(',')
          current_add_mask_list.remove('ccs')
          new_add_mask = ','.join(current_add_mask_list)
          modify_request['ccs'] = new_add_mask
          del modify_request['add']['ccs']
        return self._MakeCommentRequest(issue_id, modify_request, retry=False)

    err_msg = 'Error updating issue %s with body %s' % (
      issue_id, modify_request)
    logging.error(err_msg)
    return {'error': err_msg}

  def _ExecuteRequest(self, request):
    """Makes a request to the issue tracker.

    Args:
      request: The request object, which has a execute method.

    Returns:
      The response if there was one, or else None.
    """
    response = request.execute(
        num_retries=MAX_REQUEST_RETRIES,
        http=utils.ServiceAccountHttp(BUGANIZER_SCOPES))
    return response

  def _GetErrorReason(self, request_error):
    if request_error.resp.get('content-type', '').startswith('application/json'):
      error_json = json.loads(request_error.content).get('error')
      if error_json:
        return error_json.get('message')
    return None


import tkinter as tk
import tkinter.ttk as ttk
from functools import partial
from tkinter import filedialog

def add_tab(file_path, notebook, blank_text_box):
    # 打开文件并读取内容
    with open(file_path, "r") as file:
        content = file.read()

    notebook.pack()

    #sidebar = ttk.Frame(notebook, width=200)
    #sidebar.pack(side=tk.LEFT, fill=tk.Y)

    frame = ttk.Frame(notebook)

    blank_text_box.pack_forget()
    notebook.pack_forget()

    # 创建文本框对象
    text_box = tk.Text(frame)
    text_box.insert(tk.END, content)
    text_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

    # 创建滚动条对象
    scroll_bar = ttk.Scrollbar(frame, command=text_box.yview)
    text_box.config(yscrollcommand=scroll_bar.set)
    scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)

    # 添加子页到 Notebook 中
    notebook.add(frame, text=file_path)
    notebook.pack(expand=True, fill=tk.BOTH)

def open_file(notebook, blank_text_box):
    file_path = filedialog.askopenfilename()
    if file_path:
        add_tab(file_path, notebook, blank_text_box)

def close_file(notebook, blank_text_box):
    current_tab_index = notebook.index(notebook.select())
    notebook.forget(current_tab_index)
    blank_text_box.pack(expand=True, fill=tk.BOTH)

def file_menu_add(menu, root):
    blank_text_box = tk.Text(root)
    blank_text_box.pack(expand=True, fill=tk.BOTH)
    notebook = ttk.Notebook(root)
    menu.add_command(label="Open", command=partial(open_file, notebook, blank_text_box))
    menu.add_command(label="Close", command=partial(close_file, notebook, blank_text_box))
    menu.add_separator()
    menu.add_command(label="Exit", command=root.quit)
    return "File"


import pandas
# import statsmodels.api as sm
# from sklearn.preprocessing import LabelEncoder
# from statsmodels.regression.linear_model import WLS
from sklearn import linear_model
import matplotlib.pyplot as plt
# from  matplotlib.axes import Axes
import numpy as np

def strip_title(name):
    return name.split("(")[0].strip()

def between_sd(group_codes, ys):
    ys = ys[:, None]
    ns = np.sum(group_codes, axis=0)
    sums = np.sum(group_codes*ys, axis=0)
    means = sums/ns
    grand_mean = np.sum(ys)/ys.size
    var = np.sum(ns*(means-grand_mean)**2)
    return np.sqrt(var/(ys.size-1))

class Analysis:
    def __init__(self, score_table, track_info):
        self._score_table = score_table
        self._track_info = track_info

    def plot(self):
        intercepts = np.sum(self.model.coef_[:-1]*self.xs[:, :-1], 1)
        t_normalized = self.ys #  - intercepts
        normalized = self._inverse_y(t_normalized)
        plt.scatter(self.xs[:, -1], normalized)
        plt.xlabel(self.xlabel)
        plt.ylabel("Score")
        plt.title(self.title)
        np.savetxt(self.title+"_scatter.tsv", np.vstack((self.xs[:, -1], normalized)),
                   delimiter="\t")
        plt.savefig(self.title+"_scoresvsize.png")
        plt.close()

        plt.scatter(self.xs[:, -1], t_normalized)
        plt.xlabel(self.xlabel)
        plt.ylabel(self.ylabel)
        plt.title(self.title)
        plt.savefig(self.title + "_t_scoresvsize.png")
        plt.close()
        np.savetxt(self.title + "_t_scatter.tsv", np.vstack((self.xs[:, -1], t_normalized)),
                   delimiter="\t")
        # end_points = np.linspace(np.min(self.xs[:, -1]),
        #                         np.max(self.xs[:, -1]), 50)
        #
        # line = self.model.intercept_ + self.model.coef_[-1]*end_points
        # line_p = line+self.sd
        # line_n = line-self.sd
        # line = self._inverse_y(line)
        # line_p = self._inverse_y(line_p)
        # line_n = self._inverse_y(line_n)
        # plt.plot(end_points, line, color="g")
        # np.savetxt("green_line.tsv", np.vstack((end_points, line)), delimiter="\t")
        # plt.plot(end_points, line_n, color="r")
        # np.savetxt("red_line.tsv", np.vstack((end_points, line_p)), delimiter="\t")
        # plt.plot(end_points, line_p, color="r")
        # np.savetxt("red_line2.tsv", np.vstack((end_points, line_p)), delimiter="\t")
        # plt.savefig("scoresvsize.png")
        # plt.close()

    def generate_data(self):
        self.pair_ids = []
        self.scores = []
        self.size_measures = []
        self.internal_idxs = []
        i = 0
        for title, name in zip(self._score_table._table.columns,
                               self._score_table.tf_types):
            score_column = self._score_table._table[title]
            for title2, name2 in zip(
                    score_column.index, self._score_table.tf_types):
                if name < name2 or strip_title(title) == strip_title(title2):
                    continue
                self.pair_ids.append(name+"_"+name2)
                self.size_measures.append(self.size_measure(
                    self._track_info.get_bp_covered(title),
                    self._track_info.get_bp_covered(title2)))
                self.scores.append(score_column[title2])
                if name == name2:
                    self.internal_idxs.append(i)
                i += 1
        self.encode()

    def encode(self):
        encoded_pairs = self.encode_data(self.pair_ids)
        self.xs = np.zeros((len(self.scores), encoded_pairs.shape[1]+1))
        self.xs[:, :-1] = encoded_pairs
        self.xs[:, -1] = self.size_measures
        # self.xs[self.internal_idxs, -1] = 1
        self.ys = self._transform_y(np.array(self.scores))
        self.sd = between_sd(self.xs[:, :-1], self.ys)

    def encode_data(self, data):
        values = list(set(data))
        lookup = {v: j for j, v in enumerate(values[1:])}
        enc_data = np.zeros((len(data), len(values)-1))
        for i, val in enumerate(data):
            if val not in lookup:
                continue
            enc_data[i, lookup[val]] = 1
        self.values = values
        self.lookup = lookup

        return enc_data

    def analyze(self):
        model = linear_model.LinearRegression()
        model.fit(self.xs, self.ys)
        self.model = model

    def size_measure(self, size1, size2):
        self.xlabel = "log(size1) + log(size2)"
        return np.log(size1*size2+1)

    def _size_measure(self, size1, size2):
        self.xlabel = "size1+size2"
        assert size1 >= 0 and size2 >= 0
        return size1+size2

class JaccardAnalysis(Analysis):
    title = "JaccardRawE"
    xlabel = "log(size1) + log(size2)"
    ylabel = "logit(score)"

    def _size_measure(self, size1, size2):
        self.xlabel = "log(size1) + log(size2)"
        return np.log(size1*size2+1)

    def _transform_y(self, ys):
        assert np.all(ys >= 0) and np.all(ys <= 1)
        return ys  # np.log(ys+0.000001)  # (ys+0.0001)/(1.0001-ys))

    def _inverse_y(self, ys):
        return (1.0001*np.exp(ys)-1.0001)/(1+np.exp(ys))

class ForbesAnalysis(Analysis):
    ylabel = "log(score)"
    title = "ForbesLog"

    def analyze(self):
        model = WLS(self.ys, sm.add_constant(self.xs),
                    weights=self.xs[:, -1])
        results = model.fit()
        results.coef_ = results.params[1:]
        results.intercept_ = results.params[0]
        self.model = results

    def _transform_y(self, ys):
        return np.log(ys+1)

    def _inverse_y(self, ys):
        return np.exp(ys)-1

    def size_measure(self, size1, size2):
        self.xlabel = "size1+size2"
        assert size1 >= 0 and size2 >= 0
        return size1+size2

class TetraAnalysis(Analysis):
    # xlabel = "size1 + size2"
    ylabel = "atanh(score)"
    title = "Tetrachoric correlation L"

    def _transform_y(self, ys):
        return np.arctanh(ys)

    def _inverse_y(self, ys):
        return np.tanh(ys)

    def _size_measure(self, size1, size2):
        self.xlabel = "size1+size2"
        assert size1 >= 0 and size2 >= 0
        return size1+size2

class TrackInfo:
    def __init__(self, filename):
        self._info = pandas.read_csv(filename, sep="\t", header=4, index_col=1)
        self._info.index = [name.split("(")[0].strip() for name in self._info.index]
        self._sizes = self._info["bpcovered"]

    def get_bp_covered(self, title):
        title = title.split("(")[0].strip()
        if title not in self._sizes.index:
            print("%s not in %s" % (title, self._sizes.index))
            raise
        return self._sizes[title]

class ScoreTable:
    def __init__(self, filename):
        self._table = pandas.read_csv(
            filename, sep="\t", header=2, index_col=0)

        self.tf_types = self._parse_names()

    def _tf_from_id(self, name):
        parts = name.split()[0].split("_")
        return parts[-1]

    def _parse_names(self):
        return [self._tf_from_id(name) for name in self._table.columns]

    @classmethod
    def from_file(cls, file_name):
        with open(file_name) as f:
            first_line = f.readline()
            header = None
            while (not first_line.strip()) or first_line.startswith("#"):
                header = first_line
                first_line = f.readline()
            lines = [[float(v) for v in first_line.split()[1:]]]
            lines.extend([[float(v) for v in line.split()[1:]] for line in f])
        return cls(header, lines)

class TetraSinglePairAnalysis(Analysis):

    def _transform_y(self, ys):
        return np.arctanh(ys)

    def size_measure(self, size1, size2):
        assert size1 >= 0 and size2 >= 0
        return size1+size2

    def endcode(self):
        pass

    def analyze(self):
        pair_ids = np.array(self.pair_ids)
        pair_keys = np.unique(pair_ids)
        sizes = np.array(self.size_measures)
        scores = np.array(self.scores)
        self.coefs = []
        for pair_key in pair_keys:
            model = linear_model.LinearRegression()
            model.fit(sizes[pair_ids == pair_key][:, None],
                      scores[pair_ids == pair_key][:, None])
            print(model.coef_)
            self.coefs.append(model.coef_[0])

    def plot(self):
        ys = np.array(self.coefs)
        plt.hist(ys/self.sd)
        np.savetxt("hist.png", ys/self.sd, delimiter="\t")
        # plt.xlim(-10000, 10000)
        # plt.ylim(np.min(ys), np.max(ys))

        # plt.xlim(-102000, 10000)
        # plt.ylim(0, 1)
        plt.savefig("slopes.png")

class JaccardSinglePairAnalysis(TetraSinglePairAnalysis):
    def _size_measure(self, size1, size2):
        return np.log(size1)+np.log(size2)

    def size_measure(self, size1, size2):
        assert size1 >= 0 and size2 >= 0
        return np.log(size1*size2+1)

class ForbesSinglePairAnalysis(TetraSinglePairAnalysis):

    def _transform_y(self, ys):
        return np.log(ys+1)

    def size_measure(self, size1, size2):
        assert size1 >= 0 and size2 >= 0
        return size1+size2

    def analyze(self):
        pair_ids = np.array(self.pair_ids)
        pair_keys = np.unique(pair_ids)
        sizes = np.array(self.size_measures)
        scores = np.array(self.scores)
        self.coefs = []
        for pair_key in pair_keys:
            mask = pair_ids == pair_key
            model = WLS(scores[mask], sm.add_constant(sizes[mask]),
                        weights=sizes[mask])
            res = model.fit()
            self.coefs.append(res.params[1])

class_dict = {'tetra': [TetraAnalysis, TetraSinglePairAnalysis],
              'jaccard': [JaccardAnalysis, JaccardSinglePairAnalysis],
              'forbes': [ForbesAnalysis, ForbesSinglePairAnalysis]}

if __name__ == "__main__":
    name = 'forbes'
    score_table = ScoreTable("scores.txt")
    track_info = TrackInfo(
        "../Galaxy52-[GSuite_(29_-_Filter_categorical_gSuite)_-_ready_for_analysis].gsuite")
    for cls in class_dict[name]:
        analysis = cls(score_table, track_info)
        analysis.generate_data()
        analysis.analyze()
        analysis.plot()


import ply.lex as lex
import ply.yacc as yacc

# List of token names.   This is always required
tokens = [
 'INCLUDE',
 'DEFINE',
 'STDIOLIB',
 'STRINGLIB',
 'STDLIB',
 'MATHLIB',
 'LSQPAREN',
 'RSQPAREN',
 'LEFTBRACE',
 'RIGHTBRACE',
 'LPAREN',
 'RPAREN',
 'DOT',
 'ARROW',
 'POSTFIXPLUS',
 'POSTFIXMINUS',
 'ADDRESS',
 'PLUS',
 'MINUS',
 'MULT',
 'DIVIDE',
 'MODULO',
 'NOT',
 'LEFTSHIFT',
 'RIGHTSHIFT',
 'GREATERTHAN',
 'LESSTHAN',
 'GREATERTHANEQUAL',
 'LESSTHANEQUAL',
 'EQUAL',
 'BINARYOR',
 'BINARYXOR',
 'COMPLEMENT',
 'AND',
 'OR',
 'ADDASSIGN',
 'SUBRACTASSIGN',
 'DIVIDEASSIGN',
 'MODULOASSIGN',
 'MULTASSIGN',
 'LEFTSHIFTASSIGN',
 'RIGHTSHIFTASSIGN',
 'BITWISEANDASSIGN',
 'BITWISEXORASSIGN',
 'BITWISEORASSIGN',
 'COMMA',
 'SEMICOLON',
 'ASSIGN',
 'CONDITIONALOP',
 'SIGNIFY',
 'ID',
 'DIGIT',
 'STRING'
]

reserved={
   'auto': 'AUTO',
   'register': 'RESTRICT',
   'int' : 'INT',
   'float' : 'FLOAT',
   'void' : 'VOID',
   'short' : 'SHORT',
   'long' : 'LONG',
   'double' : 'DOUBLE',
   'signed' : 'SIGNED',
   'unsigned' : 'UNSIGNED',
   'double' : 'DOUBLE',
   'continue' : 'CONTINUE',
   'char' : 'CHAR',
   'goto' : 'GOTO',
   'do' : 'DO',
   'extern' : 'EXTERN',
   'static' : 'STATIC',
   'auto' : 'AUTO',
   'register' : 'REGISTER',
   'break': 'BREAK',
   'return' : 'RETURN',
   'case':'CASE',
   'short':'SHORT',
   'volatile': 'VOLATILE',
   'restrict':'RESTRICT',
   'for' : 'FOR',
   'while' : 'WHILE',
   'const' : 'CONST',
   'sizeof' : 'SIZEOF',
   'default': 'DEFAULT',
   'inline':'INLINE',
   'while' : 'WHILE',
   'else' : 'ELSE',
   'if' : 'IF',
   'for' : 'FOR',
   'switch':'SWITCH',
   'case':'CASE',
   'do' : 'DO',
   'break': 'BREAK',
   'return' : 'RETURN',
   'printf':'PRINTF',
   'scanf' : 'SCANF',
   'goto' : 'GOTO',
   'do' : 'DO',
   'main' : 'MAIN'
}
tokens += reserved.values()
# Regular expression rules for simple tokens

t_INCLUDE = r'\#include'
t_DEFINE = r'\#define'
t_STDIOLIB = r'<stdlib.h>'
t_STRINGLIB = r'<string.h>'
t_STDLIB = r'<stdio.h>'
t_MATHLIB = r'\<math.h>'
t_LSQPAREN = r'\['
t_RSQPAREN = r'\]'
t_LEFTBRACE = r'\('
t_RIGHTBRACE = r'\)'
t_LPAREN = r'{'
t_RPAREN = r'}'
t_DOT = r'\.'
t_ARROW = r'->'
t_POSTFIXPLUS = r'\++'
t_POSTFIXMINUS = r'\--'
t_ADDRESS = r'&'
t_PLUS = r'\+'
t_MINUS = r'\-'
t_MULT = r'\*'
t_DIVIDE = r'/'
t_MODULO = r'%'
t_NOT = r'!'
t_LEFTSHIFT = r'<<'
t_RIGHTSHIFT = r'>>'
t_GREATERTHAN = r'>'
t_LESSTHAN = r'<'
t_GREATERTHANEQUAL = r'>='
t_LESSTHANEQUAL = r'<='
t_EQUAL = r'=='
t_BINARYOR = r'\|'
t_BINARYXOR = r'\^'
t_COMPLEMENT = r'~'
t_AND = r'&&'
t_OR = r'\|\|'
t_ADDASSIGN = r'\+='
t_SUBRACTASSIGN = r'\-='
t_DIVIDEASSIGN = r'/='
t_MODULOASSIGN = r'%='
t_MULTASSIGN = r'\*='
t_LEFTSHIFTASSIGN = r'<<='
t_RIGHTSHIFTASSIGN = r'>>='
t_BITWISEANDASSIGN = r'&='
t_BITWISEXORASSIGN = r'\|='
t_BITWISEORASSIGN = r'^='
t_COMMA = r','
t_SEMICOLON = r';'
t_ASSIGN = r'='
t_CONDITIONALOP = r'\?'
t_STRING = r'\".*\n*\"'
t_SIGNIFY = r'\:'

#reserved words
t_AUTO = r'auto'
t_REGISTER = r'register'
t_INT = r'int'
t_FLOAT = r'float'
t_VOID = r'void'
t_SHORT = r'short'
t_LONG = r'long'
t_DOUBLE = r'double'
t_SIGNED = r'signed'
t_UNSIGNED = r'unsigned'
t_CONTINUE = r'continue'
t_CHAR = r'char'
t_GOTO = r'goto'
t_DO = r'do'
t_EXTERN = r'extern'
t_STATIC = r'static'
t_BREAK = r'break'
t_RETURN = r'return'
t_CASE = r'case'
t_VOLATILE = r'volatile'
t_FOR = r'for'
t_WHILE = r'while'
t_CONST = r'const'
t_SIZEOF = r'sizeof'
t_MAIN = r'main'
t_DEFAULT = r'default'
t_INLINE = r'inline'
t_ELSE = r'else'
t_IF = r'if'
t_SWITCH = r'switch'
t_PRINTF = r'printf'
t_SCANF = r'scanf'
t_RESTRICT = r'restrict'
# A regular expression rule with some action code
digit = r'([0-9])'
nondigit = r'([_A-Za-z])'
identifier = r'(' + nondigit + r'(' + digit + r'|' + nondigit + r')*)'
def t_ID(t):
   r'[a-zA-Z_][a-zA-Z_0-9]*'
   t.type = reserved.get(t.value,'ID')    # Check for reserved words
   return t

def t_DIGIT(t):
 r'[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?'
 try:
   if(type(t.value)==float):
     t.value = float(t.value)
   else:
     t.value = int(t.value)
 except ValueError:
   print("Line %d: Number %s is too large!" ,(t.lineno,t.value))
   t.value = 0
 return t

#Define a rule so we can track line numbers

def t_newline(t):
 r'\n+'
 t.lexer.lineno += len(t.value)

# A string containing ignored characters (spaces and tabs)
t_ignore  = ' \t'

# Error handling rule
def t_error(t):
 print ("Illegal character '%s'" , t.value[0])
 t.lexer.skip(1)

t_ignore_COMMENT = r'//.*'

# No return value. Token discarded
# Build the lexer
lexer = lex.lex(optimize=1)


import librosa
import soundfile
import numpy as np
import glob
import os
import pickle
import telebot

from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from pydub import AudioSegment

def extract_feature(file_name, **datos):
    mfcc = datos.get("mfcc")
    chroma = datos.get("chroma")
    mel = datos.get("mel")
    contrast = datos.get("contrast")
    tonnetz = datos.get("tonnetz")
    with soundfile.SoundFile(file_name) as archivo_sonido:
        X = archivo_sonido.read(dtype="float32")
        sample_rate = archivo_sonido.samplerate
        if chroma or contrast:
            stft = np.abs(librosa.stft(X))
        result = np.array([])
        if mfcc:
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            result = np.hstack((result, mfccs))
        if chroma:
            chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
            result = np.hstack((result, chroma))
        if mel:
            mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T, axis=0)
            result = np.hstack((result, mel))
        if contrast:
            contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T, axis=0)
            result = np.hstack((result, contrast))
        if tonnetz:
            tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T, axis=0)
            result = np.hstack((result, tonnetz))
    return result

# todas las emocines cargadas en el datasetRAVDESS
int2emotion = {
    "01": "neutral",
    "02": "calmado",
    "03": "positivo",
    "04": "triste",
    "05": "negativo",
    "06": "timido",
    "07": "disgustado",
    "08": "sorprendido"
}

# usare las emociones solicitada en el requerimiento
AVAILABLE_EMOTIONS = {
    "negativo",
    "positivo"}

def load_data(test_size=0.2):
    X, y = [], []
    for file in glob.glob("D:\Dadafile\Actor_*\*.wav"):
        # obtengo el nombre base del archivo
        basename = os.path.basename(file)
        # Obtengo la etiqueta de emocion
        emotion = int2emotion[basename.split("-")[2]]
        # permitimos que escoja las etiqueta que necesitamos
        if emotion not in AVAILABLE_EMOTIONS:
            continue
        # extraccion de caracteristicas
        features = extract_feature(file, mfcc=True, chroma=True, mel=True)
        # se agregan los datos
        X.append(features)
        y.append(emotion)
    # aqui se dividen los datos para el entreamiento
    return train_test_split(np.array(X), y, test_size=test_size, random_state=7)
# se carga el test con el 25% de la data
X_train, X_test, y_train, y_test = load_data(test_size=0.25)

# mejor modelo para busqueda
model_params = {
    'alpha': 0.01,
 'batch_size': 256,
    'epsilon': 1e-08,
    'hidden_layer_sizes': (300,),
    'learning_rate': 'adaptive',
    'max_iter': 700,
}

model = MLPClassifier(**model_params)

# entreanmiento del modelo
print("[*] Entrenando Modelo...")
model.fit(X_train, y_train)

# prediccion con el 25 % de la data
y_pred = model.predict(X_test)
print(y_pred)

# calculate the accuracy
accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
print()
print("Efectividad al: {:.2f}%".format(accuracy*100))
print()
print("Tabla de reporte")
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
print()

from sklearn.metrics import confusion_matrix
matrix = confusion_matrix(y_test, y_pred)
print("Matriz de confusión")
print (matrix)
print()

Pkl_Filename = "Detector_de_emocion_por_voz_Modelo.pkl"

with open(Pkl_Filename, 'wb') as file:
    pickle.dump(model, file)

with open(Pkl_Filename, 'rb') as file:
    loaded_model = pickle.load(file)

loaded_model

def aextract_feature(file_name, **kwargs):
    mfcc = kwargs.get("mfcc")
    chroma = kwargs.get("chroma")
    mel = kwargs.get("mel")
    contrast = kwargs.get("contrast")
    tonnetz = kwargs.get("tonnetz")
    with soundfile.SoundFile(file_name) as sound_file:
        X = sound_file.read(dtype="float32")
        sample_rate = sound_file.samplerate
        if chroma or contrast:
            stft = np.abs(librosa.stft(X))
        result = np.array([])
        if mfcc:
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            result = np.hstack((result, mfccs))
        if chroma:
            chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
            result = np.hstack((result, chroma))
        if mel:
            mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T, axis=0)
            result = np.hstack((result, mel))
        if contrast:
            contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T, axis=0)
            result = np.hstack((result, contrast))
        if tonnetz:
            tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T, axis=0)
            result = np.hstack((result, tonnetz))
    return result

filename = "03-01-02-01-01-02-08.wav"

new_features = aextract_feature(filename, mfcc=True, chroma=True, mel=True).reshape(1, -1)

result = loaded_model.predict(new_features)

print("Ejemplo de prueba")
print("Prediccion es : ", result)

bot = telebot.TeleBot("1943460407:AAFtrAwI7_Ji-TlUHVPzsDaCK2y8yvxCHFI")

@bot.message_handler(commands=['seguir'])
def bienvenidaa(message):
    bot.reply_to(message, "Envia un audio" )

@bot.message_handler(commands=['predic'])
def bienvenidaa(message):
    bot.reply_to(message, "El estado es positivo" )

@bot.message_handler(content_types=['voice'])
def voice_processing(update):
    file_info = bot.get_file(update.voice.file_id)
    downloaded_file = bot.download_file(file_info.file_path)
    with open('new_file.wav', 'wb') as new_file:
        new_file.write(downloaded_file)
bot.polling()


import re
import json
from logger import info

fn = 'IEEE VIS papers 1990-2013 - AllPapers+Abstracts+Authors.tsv'
fndd = 'author-dupes-to-fix.txt'
fnddcol = 'authors-deduped-column.txt'

dd = dict()

with open(fndd, 'r') as f:
    for l in f:
        name0, name1 = l.split('<-')
        name0 = name0.rstrip().lstrip()
        name1 = name1.rstrip().lstrip()
        dd[name1] = name0

column = ['deduped author names']

with open(fn, 'r') as f:
    f.readline()
    for l in f:
        authors = l.split('\t')[11].split(';')
        for i, author in enumerate(authors):
            author = author.lstrip().rstrip()
            if author in dd:
                authors[i] = dd[author]
        column.append(';'.join(authors))

with open(fnddcol, 'w') as f:
    for r in column:
        f.write(r + '\n')


# -*- coding:UTF-8 -*-
import unittest
import requests
import sys,time
import json,gc
import xlrd
import global_list
sys.path.append(global_list.path+"/public_1")
from get_url import Url
from get_token import Token
from read_data import Read_ExcelData
from write_data import Write_ExcelData
from get_version import Version
from func_requests import FuncRequests

#---------------与用户相关的QA信息列表----------------------
class Message(unittest.TestCase):

    def setUp(self):
        self.r = FuncRequests()

    #-----------------QA消息列表第一页数据----------------------------------
    def testcase_001(self):
        sheet_index = 5
        row =3
        print("testcase_001QA消息列表第一页数据：")
        member_id1 = "748"
        result=self.r.interface_requests(member_id1,sheet_index,row)
        message_list = result['data']['list']
        global question_id,comment_id,member_id,category
        question_id = message_list[9]['question_id']
        comment_id = message_list[9]['comment_id']
        member_id = message_list[9]['member_id']
        category = message_list[9]['category']
        print(question_id, comment_id, member_id, category)

        self.assertEqual(10000, result["code"])
        print("code返回值：10000")

    #-----------------QA消息列表第二页数据----------------------------------
    def testcase_002(self):
        sheet_index = 5
        row =4
        print("testcase_002 QA消息列表第二页数据：")
        member_id1 = "748"
        payload = {"page":2,"question_id":question_id,"comment_id":comment_id,"member_id":member_id,"category":category}
        result = self.r.interface_requests_payload(member_id1, sheet_index, row, payload)

        self.assertEqual(10000, result["code"])
        print("code返回值：10000")

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


# N으로 표현
def solution(N, number):
    answer = 0
    val = N

    dp = [[]  for i in range(9)]
    if (number == N):
        return 1

    dp[1].append(N)
    for leng in range(2, 9):
        val = val * 10 + N
        dp[leng].append(val)
        for i in range(1, leng // 2 + 1):
            for l in dp[i]:
                for r in dp[leng - i]:
                    dp[leng] = check(l, r, dp[leng])
                    if number in dp[leng]:
                        return leng
    answer = -1
    return answer

def check(a,b,r):
    t = [a+b,a*b,a-b,b-a,a//b,b//a]
    for y in t:
        if y not in r and y <= 32000 and y >= 1:
            r.append(y)
    return r


import os
import numpy as np
import cv2
import h5py

#SSL4MIS里面对ACDC数据处理成H5文件的代码
# for slice_ind in range(image.shape[0]):
#     f = h5py.File('/home/xdluo/data/ACDC/data/{}_slice_{}.h5'.format(item, slice_ind), 'w')
#     f.create_dataset('image', data=image[slice_ind], compression="gzip")
#     f.create_dataset('label', data=mask[slice_ind], compression="gzip")
#     f.close()
#     slice_num += 1
# x = label_tmp.reshape(label_tmp.shape[0], -1)
# np.savetxt("{}.csv".format(name), x, delimiter=",")

#先把单个图片写成H5文件，同时获得np.array的图片的尺寸
def save_image_to_h5py(imgs_path,GT_path):
    img_list = []
    label_list = []
    dir_counter = 0

    for child_dir in os.listdir(imgs_path):
        dir_image = os.path.join(imgs_path, child_dir)
        print('dir_image中图像的名称是:\n', dir_image)

        name, suffix = child_dir.split('.')
        print('name = ',name)
        print('suffix =',suffix)
        names = name + '.png'
        print('names = ', names)
        dir_label = os.path.join(GT_path , names )
        print('dir_label 中图像的名称是:\n', dir_label)

        # img = cv2.imread(dir_image)
        # print('img type:', type(img))
        # print('img shape:', img.shape)
        # # img =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#单通道，分辨率会下降
        # # 从GT文件里面读取图像获取label标签
        # img_0 = img[:, :, 0]
        # print('img_0 shape:', img_0.shape)
        # img_1 = img[:, :, 1]
        # print('img_1 shape:', img_1.shape)
        # img_2 = img[:, :, 2]
        # print('img_2 shape:', img_2.shape)
        # img_2_1 = img_2 - img_1
        # img_1_0 = img_1 - img_0
        # print('Is img_2_1 all zeros?: ', not (np.any(img_2_1)))
        # print('Is img_1_0 all zeros?: ', not (np.any(img_1_0)))

        label_tmp = cv2.imread(dir_label)
        print('label_tmp shape:', label_tmp.shape)
        label_tmp_0 = label_tmp[:,:,0]
        # print('label_tmp_0 shape:', label_tmp_0.shape)
        # label_tmp_1 = label_tmp[:, :, 1]
        # # print('label_tmp_1 shape:', label_tmp_1.shape)
        # label_tmp_2 = label_tmp[:, :, 2]
        # # print('label_tmp_2 shape:', label_tmp_2.shape)
        # label_tmp_2_1 = label_tmp_2 - label_tmp_1
        # label_tmp_1_0 = label_tmp_1 - label_tmp_0
        # print('Is label_tmp_2_1 all zeros?: ', not (np.any(label_tmp_2_1)))
        # print('Is label_tmp_1_0 all zeros?: ', not (np.any(label_tmp_1_0)))

        label_tmp_0_0 = np.int64(label_tmp_0 > 1)
        np.savetxt('label_tmp_0.txt', label_tmp_0)
        np.savetxt('label_tmp_0_0.txt',label_tmp_0_0)

    #     result_file = h5py.File('Doctor-label/H5/COVID_2.h5', "w")
    #     result_file.create_dataset(f"train/{dir_image}/data", data=img, compression='gzip')
    #     result_file.create_dataset(f"train/{dir_image}/label", data=label_tmp)
    #     print(f"***Finish create one database:{dir_image},***")
    #     result_file.close()
    # # 返回的img_list转成了 np.array的格式
    # dir_counter += 1

if __name__ == '__main__':
    imgs_path = r'../Doctor-label/Imgs'
    GT_path = r'../Doctor-label/GT'
    save_image_to_h5py(imgs_path,GT_path)


import cv2

img = cv2.imread("/home/nitolapio/Escritorio/Programación/Machine Learning/OpenCV/Resources/lambo.jpeg")
print(img.shape)

# In this way we resize the size of the image. Also we can increase it
imgResize = cv2.resize(img, (100,200))

# We can crop the image this way, as an array of numbers
imgCropped = img[0:100, 20: 150]

cv2.imshow("Lambo", img)
cv2.imshow("Lambo resized", imgResize)
cv2.imshow("Cropped Image", imgCropped)
cv2.waitKey(0)


#!/usr/bin/env python

from lib import *

import swampyer

logging.basicConfig(stream=sys.stdout, level=30)
# We want to see the protocol information
# being exchanged
#logging.basicConfig(stream=sys.stdout, level=1)

def hello(event,data):
    return data

def test_connection():
    client = connect_service()
    client2 = connect_service()

    # Check if we can register
    reg_result = client.register('com.izaber.wamp.hello', hello, details={"force_reregister": True})
    assert swampyer.WAMP_REGISTERED == reg_result
    assert reg_result == swampyer.WAMP_REGISTERED

    # Can we call data?
    call_result = client2.call('com.izaber.wamp.hello','something')
    assert call_result == 'something'

    # Send some binary data
    binary_data = b'\xEF\xBB\xBF\x00'
    call_result = client2.call('com.izaber.wamp.hello',binary_data)
    assert call_result
    assert call_result.encode('utf8') == binary_data

    # Let's send a huge amount of data
    binary_data = b'\xEF\xBB\xBF\x00'*1000000
    call_result = client2.call('com.izaber.wamp.hello',binary_data)
    assert call_result
    assert call_result.encode('utf8') == binary_data

    # Let's unregister then
    unreg_result = client.unregister(reg_result.registration_id)
    assert unreg_result == swampyer.WAMP_UNREGISTERED

    # Can we subscribe?
    sub_data = {'data':None}
    def sub_capture(event,data):
        sub_data['data'] = data

    sub_result = client.subscribe('com.izaber.wamp.pub.hello', sub_capture)
    assert sub_result == swampyer.WAMP_SUBSCRIBED

    # And then publish
    pub_result = client2.publish(
            'com.izaber.wamp.pub.hello',
            options={
                'acknowledge': True,
                'exclude_me': False,
            },
            args=['Hej!']
        )
    assert pub_result == swampyer.WAMP_PUBLISHED

    # And check that we received the data
    time.sleep(0.1)
    assert sub_data['data'] == 'Hej!'

    # Unsubscribe
    unsub_result = client.unsubscribe(sub_result.subscription_id)
    assert unsub_result == swampyer.WAMP_UNSUBSCRIBED

    # We had an issue where multiple publishes in a row took
    # longer and longer. In this case we'll run the query
    # 100 times quickly than see how much time has passed
    # This should take less than 1 second
    start_time = time.time()
    for _ in range(30):
        pub_result = client2.publish(
            'com.izaber.wamp.pub.hello',
            options={
                'acknowledge': True,
            },
            args=['Hej!']
        )
    end_time = time.time()
    duration = end_time - start_time
    assert duration < 1.0

    # Then shutdown
    client.shutdown()
    client2.shutdown()

if __name__ == '__main__':
    test_connection()


# coding: utf-8

"""
    OpenAPI Petstore

    This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\  # noqa: E501

    The version of the OpenAPI document: 1.0.0
    Generated by: https://github.com/openapi-json-schema-tools/openapi-json-schema-generator
"""

import unittest

import petstore_api
from petstore_api.components.schema import abstract_step_message

class TestAbstractStepMessage(unittest.TestCase):
    """AbstractStepMessage unit test stubs"""

    def test_model_instantiation(self):
        arg: abstract_step_message.AbstractStepMessageDictInput = {
            'discriminator': 'AbstractStepMessage',
            'sequenceNumber': 1,
            'description': 'some description'
        }
        inst = abstract_step_message.AbstractStepMessage.validate(
            arg
        )
        assert inst == {
            'discriminator': 'AbstractStepMessage',
            'sequenceNumber': 1,
            'description': 'some description'
        }

        # without the above required properties an exception is thrown
        invalid_kwargs = [
            {
                'sequenceNumber': 1,
                'description': 'some description'
            },
            {
                'discriminator': 'AbstractStepMessage',
                'description': 'some description'
            },
            {
                'discriminator': 'AbstractStepMessage',
                'sequenceNumber': 1,
            },
        ]
        for invalid_kwarg in invalid_kwargs:
            with self.assertRaises(TypeError):
                abstract_step_message.AbstractStepMessage.validate(invalid_kwarg)

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


from datetime import datetime, timezone

UNIX_START = datetime(1970, 1, 1, tzinfo=timezone.utc)
MIMETYPES = (('application/feed+json', 1),
             ('application/atom+xml', 1),
             ('application/rss+xml', 1),
             ('application/rdf+xml', 0.9),
             ('application/xml', 0.8),
             ('application/json', 0.8),
             ('text/xml', 0.6),
             ('*/*', 0.2))

FEED_ACCEPT_HEADERS = ','.join(mtype + (';q=%0.1f' % qual if qual < 1 else '')
                               for mtype, qual in MIMETYPES)
FEED_MIMETYPES = [mtype for mtype, quality in MIMETYPES if quality >= 0.5]
REQUIRED_JSON_FEED = {'version', 'title'}
GOOGLE_BOT_UA = ("Mozilla/5.0 (compatible; Googlebot/2.1; "
                 "+http://www.google.com/bot.html)")


from fasterRCNN import fasterRCNN
from dataSequence import dataSequence, category_name
from config import config
from keras.optimizers import Adam, SGD
from keras.callbacks import ModelCheckpoint, Callback, LearningRateScheduler
from keras.utils import multi_gpu_model
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpu_cnt = len(os.environ['CUDA_VISIBLE_DEVICES'].split(','))

class ParallelModelCheckpoint(ModelCheckpoint):
    def __init__(self,model,filepath, monitor='val_loss', verbose=0,
                 save_best_only=False, save_weights_only=False,
                 mode='auto', period=1):
        self.single_model = model
        super(ParallelModelCheckpoint,self).__init__(filepath, monitor, verbose,save_best_only, save_weights_only,mode, period)

    def set_model(self, model):
        super(ParallelModelCheckpoint,self).set_model(self.single_model)

if __name__ == '__main__':

    img_dir = "data/"
    label_dir = "data/"
    weight_dir = "weights"

    input_shape = (512,512,3)
    n_classes = 2
    output_stride = 16
    n_anchors = config.n_anchors

    # model
    rpn_model, detection_model = fasterRCNN(input_shape, n_classes, n_anchors, mode='train')

    # train
    step = 2
    if step==1:
        # step1: pretrain & std init, train rpn_model
        batch_size = 32
        train_generator = dataSequence(img_dir, label_dir, n_classes, output_stride=output_stride,
                                       input_shape=input_shape, batch_size=batch_size)
        rpn_model.compile(Adam(1e-3), loss=lambda y_true,y_pred: y_pred)
        filepath = weight_dir + "/rpn_epoch_{epoch:02d}_loss_{loss:.3f}.h5"
        if gpu_cnt>1:
            checkpoint = ParallelModelCheckpoint(rpn_model, filepath, monitor='loss', verbose=1, mode='auto')
            rpn_model = multi_gpu_model(rpn_model, gpu_cnt)
            rpn_model.compile(Adam(1e-3), loss=lambda y_true,y_pred: y_pred)
        else:
            checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, mode='auto')
        rpn_model.fit_generator(train_generator,
                                steps_per_epoch=1,
                                epochs=1,
                                initial_epoch=0,
                                workers=16,
                                use_multiprocessing=False,
                                callbacks=[checkpoint],
                                verbose=1)

    elif step==2:
        # step2: pretrain & std init, use step1 proposals, train detection_model
        batch_size = 8
        train_generator = dataSequence(img_dir, label_dir, n_classes, output_stride=output_stride,
                                       input_shape=input_shape, batch_size=batch_size)
        detection_model.load_weights("rpn.h5", by_name=True, skip_mismatch=True)
        # freeze rpn heads
        for layer in detection_model.layers:
            if layer.name in ['rpn', 'rpn_back']:
                layer.trainable = False
        detection_model.compile(Adam(3e-4), loss=lambda y_true,y_pred: y_pred)
        filepath = weight_dir + "/detector_epoch_{epoch:02d}_loss_{loss:.3f}.h5"
        checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, mode='auto')
        detection_model.fit_generator(train_generator,
                                      steps_per_epoch=1,
                                      epochs=1,
                                      initial_epoch=0,
                                      workers=16,
                                      use_multiprocessing=False,
                                      callbacks=[checkpoint],
                                      verbose=1)
    elif step==3:
        # step3: load step2 weights, freeze backbone, train rpn head
        batch_size = 32
        rpn_model.load_weights("detector.h5", by_name=True, skip_mismatch=True)
        for layer in rpn_model.layers:
            if layer.name in ['rpn_back']:
                layer.trainable = False
        rpn_model.compile(Adam(1e-4), loss=lambda y_true,y_pred: y_pred)
        filepath = weight_dir + "/rpn_ft_epoch_{epoch:02d}_loss_{loss:.3f}.h5"
        checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, mode='auto')
        rpn_model.fit_generator(train_generator,
                                steps_per_epoch=1,
                                epochs=1,
                                initial_epoch=0,
                                workers=16,
                                use_multiprocessing=False,
                                callbacks=[checkpoint],
                                verbose=1)
    elif step==4:
        # step4: load step3 weights, freeze backbone, use step3 proposals, ttrain detection head
        batch_size = 8
        detection_model.load_weights("rpn_ft.h5", by_name=True, skip_mismatch=True)
        for layer in detection_model.layers:
            if layer.name in ['rpn', 'rpn_back', 'det_back']:
                layer.trainable = False
        detection_model.compile(Adam(1e-4), loss=lambda y_true,y_pred: y_pred)
        filepath = weight_dir + "/detector_ft_epoch_{epoch:02d}_loss_{loss:.3f}.h5"
        checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, mode='auto')
        detection_model.fit_generator(train_generator,
                                      steps_per_epoch=1,
                                      epochs=1,
                                      initial_epoch=0,
                                      workers=16,
                                      use_multiprocessing=False,
                                      callbacks=[checkpoint],
                                      verbose=1)


# format
# def <NameOfFunction> (<essential parameters>,<optional parameters>):
#   <block>
# return <>

# variable parameters
def fact(n,*b):
    s=1
    for i in range(1,n+1):
        s*=i
    for item in b:
        s*=item
    return s

print(fact(5,1,2,3))
# When a function is called, parameters can be passed by location or name
def fun(n,m=1):
    for i in range(1,n+1):
        n*=i
    return n+m
print(fun(10,5))
print(fun(m=5,n=10))

# Function can return 0 or more results
def func(a,b,c):
    return a+b,a+c,b+c
# The result returned is a tuple type
# Or
a,b,c=func(1,2,3)
print(a,b,c)

# Rule 1:
# When a global variable is used inside a function, add global before the variable

# Rule 2:
# A local variable is a composite data type and is not created inside a function so it is equivalent to a global variable

# lambda ->anonymous function
# Format: <Name of Function> = lambda <parameters>: <expression>
f=lambda x,y:x+y
print(f(1,2))
f=lambda :print("Hello World")
f()


# How to create your own exception
# The exceptions hierarchy is neither closed nor finished,
# and you can always extend it if you want or need to create your own world populated with your own exceptions.

# It may be useful when you create a complex module which detects errors and raises exceptions,
# and you want the exceptions to be easily distinguishable from any others brought by Python.

# This is done by defining your own, new exceptions as subclasses derived from predefined ones.

# Note: if you want to create an exception which will be utilized as a specialized case of any built-in exception,
# derive it from just this one. If you want to build your own hierarchy, and don't want it to be closely connected
# to Python's exception tree, derive it from any of the top exception classes, like Exception.

# Imagine that you've created a brand new arithmetic, ruled by your own laws and theorems.
# It's clear that division has been redefined, too, and has to behave in a different way than routine dividing.
# It's also clear that this new division should raise its own exception, different from the built-in ZeroDivisionError,
# but it's reasonable to assume that in some circumstances, you (or your arithmetic's user) may want to treat
# all zero divisions in the same way.

# Demands like these may be fulfilled in the way presented in the editor.
# Look at the code, and let's analyze it:

class MyZeroDivisionError(ZeroDivisionError):
    pass

def do_the_division(mine):
    if mine:
        raise MyZeroDivisionError("some worse news")
    else:
        raise ZeroDivisionError("some bad news")

for mode in [False, True]:
    try:
        do_the_division(mode)
    except ZeroDivisionError:
        print('Division by zero')

for mode in [False, True]:
    try:
        do_the_division(mode)
    except MyZeroDivisionError:
        print('My division by zero')
    except ZeroDivisionError:
        print('Original division by zero')

print()

# We've defined our own exception, named MyZeroDivisionError, derived from the built-in ZeroDivisionError.
# As you can see, we've decided not to add any new components to the class.

# In effect, an exception of this class can be - depending on the desired point of view -
# treated like a plain ZeroDivisionError, or considered separately.

# The do_the_division() function raises either a MyZeroDivisionError or ZeroDivisionError exception,
# depending on the argument's value.

# The function is invoked four times in total, while the first two invocations are handled using only
# one except branch (the more general one) and the last two ones with two different branches,
# able to distinguish the exceptions (don't forget: the order of the branches makes a fundamental difference!)

# How to create your own exception: continued
# When you're going to build a completely new universe filled with completely new creatures that
# have nothing in common with all the familiar things, you may want to build your own exception structure.
# For example, if you work on a large simulation system which is intended to model the activities
# of a pizza restaurant, it can be desirable to form a separate hierarchy of exceptions.
# You can start building it by defining a general exception as a new base class for any other specialized exception.
# We've done in in the following way:

class PizzaError(Exception):
    def __init__(self, pizza='unknown', message=''):
        Exception.__init__(self, message)
        self.pizza = pizza
        print(self.pizza, 'error occured --> pizza error: ', end='')

# Note: we're going to collect more specific information here than a regular Exception does,
# so our constructor will take two arguments:
# one specifying a pizza as a subject of the process,
# and one containing a more or less precise description of the problem.

# As you can see, we pass the second parameter to the superclass constructor, and save the first inside our own property.
# A more specific problem (like an excess of cheese) can require a more specific exception.
# It's possible to derive the new class from the already defined PizzaError class, like we've done here:

class TooMuchCheeseError(PizzaError):
    def __init__(self, pizza='unknown', cheese='>100', message=''):
        PizzaError.__init__(self, pizza, message)
        self.cheese = cheese
        print('cheese error: ', end='')

# The TooMuchCheeseError exception needs more information than the regular PizzaError exception,
# so we add it to the constructor - the name cheese is then stored for further processing.

def make_pizza(pizza, cheese):
    if pizza not in ['margherita', 'capricciosa', 'calzone']:
        raise PizzaError(pizza, "no such pizza on the menu")
    if cheese > 100:
        raise TooMuchCheeseError(pizza, cheese, "too much cheese")
    print("Pizza ready!")

for (pz, ch) in [('calzone', 0), ('margherita', 110), ('mafia', 20), ('frankie', 160)]:
    try:
        make_pizza(pz, ch)
    except TooMuchCheeseError as tmce:
        print(tmce, ':', tmce.cheese)
    except PizzaError as pe:
        print(pe, ':', pe.pizza)
    finally:
        print('Bye!')


"""Movie recommender

Takes a user's favourite movies from a web interface and returns recommendations"""

# from sklearn.decomposition import NMF
import pickle
import numpy as np
import pandas as pd
from fuzzywuzzy import process
from train import join_df, pivot, RATINGS, MOVIES
# import server

# lookup tables between movieId and title
LOOKUPTABLE = dict(MOVIES[['movieId', 'title']].groupby('movieId')['title'].first())
LOOKUPTABLE_2 = dict(MOVIES[['title', 'movieId']].groupby('title')['movieId'].first())

def get_movie_ids(entries):
    """Takes in user's movie choices and returns their ID's"""
    titles = list(MOVIES['title'])
    movie = LOOKUPTABLE_2[process.extractOne(entries, titles)[0]]
    return movie

def recommend(ratings_df, movies_df, query):
    """Main function - takes in user's entered movies and produces recommendations"""
    # load the trained model
    binary = open('nmf_model.bin', 'rb').read()
    nmf = pickle.loads(binary)

    # We join the movies dataframe onto the ratings one in order to get the
    # movie information for each rating:
    main_df = join_df(ratings_df, movies_df, 'movieId')
    # We start by running the 'pivot' function to take a dataframe and put it
    # into the desired shape - in this case users as rows, movies as columns,
    # ratings as values. We also impute the empty values with 0:
    sparse = pivot(main_df, 'userId', 'movieId', 'rating', 0.0)

    # We create a profile for a new user:
    # Firstly we generate a list of all movie ID's:
    movie_ids = list(sparse['rating'].columns)

    # Then we generate an "empty" set of selections for the new user:
    profile = {movie: np.nan for movie in movie_ids}

    # Then we assign some ratings:
    profile[get_movie_ids(query[0])] = 5.0
    profile[get_movie_ids(query[1])] = 5.0
    profile[get_movie_ids(query[2])] = 5.0

    new_user_input = list(profile.values())
    new_user_input = pd.DataFrame(new_user_input, index=movie_ids).T.fillna(0.0)

    # We produce the "hidden" profile based on our new user input:
    hidden_profile = nmf.transform(new_user_input)

    # And reconstruct to get our new user's predicted ratings for all movies:
    ypred = np.dot(hidden_profile, nmf.components_)

    # Now we wish to output the best-ranking movies that the user has not
    # rated yet - our recommendations!
    df_ratings = pd.DataFrame(ypred.T, columns=['rating'], index=movie_ids)
    df_ratings['profile'] = profile.values()

    # We need to get movieId into the dataframe:
    df_ratings_id = df_ratings[df_ratings['profile'].isna()].sort_values(by='rating', ascending=False)
    df_ratings_id['movieId'] = df_ratings_id.index
    df_ratings_id['title'] = df_ratings_id['movieId'].map(lambda x: LOOKUPTABLE[x])

    recos = list(df_ratings_id['title'].head(10).values)
    return recos

if __name__ == "__main__":
    MOVIE1 = input("Enter movie 1:")
    MOVIE2 = input("Enter movie 2:")
    MOVIE3 = input("Enter movie 3:")
    RESULT = recommend(RATINGS, MOVIES)
    print(RESULT)


from __future__ import absolute_import
from .base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG

MANAGERS = (
    ("Tijs Teulings", "tijs@automatique.nl"),
    )

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'your_database',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Dummy backends are your friends (locally that is)
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        }
}
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


from nltk.corpus import wordnet
import numpy as np
import nltk
import random
import embedding_helper as emb

#fastText_emb_char = emb.get_word_embeddings('Word Embeddings\\fasttext_wiki.en.txt')

def wordnet_vec(word, pos, emb, useHypernyms=False):
    wordnet_vec=[]
    alt_wordnet_vec=[]
    synset_pos=''
    if 'N' in pos:
        synset_pos=wordnet.NOUN
    elif 'V' in pos:
        synset_pos=wordnet.VERB
    elif 'J' in pos:
        synset_pos=wordnet.ADJ

    synset_list = wordnet.synsets(word)
    if useHypernyms == False:
        for syn_list in synset_list:
            if syn_list.pos() == synset_pos:
                [wordnet_vec.append(emb[syn]) for syn in syn_list.lemma_names() if syn in emb]
            else:
                [alt_wordnet_vec.append(emb[syn]) for syn in syn_list.lemma_names() if syn in emb]
    else:
        for syn_list in synset_list:
            hyper_list = syn_list.hypernyms()
            if syn_list.pos() == synset_pos:
                [wordnet_vec.append(emb[hyper.lemmas()[0].name()]) for hyper in hyper_list if hyper.lemmas()[0].name() in emb]
            else:
                [alt_wordnet_vec.append(emb[hyper.lemmas()[0].name()]) for hyper in hyper_list if hyper.lemmas()[0].name() in emb]
    if len(wordnet_vec) > 1:
         return np.mean(wordnet_vec, axis=0)
    elif len(alt_wordnet_vec) > 1:
        return np.mean(alt_wordnet_vec, axis=0)
    else:
        return np.zeros(300, dtype="float32")

def close_distance_word(word, distance_dic, embeddings, pos=None):
    if word in distance_dic:
        new_word, _ = distance_dic[word]
        if new_word in embeddings:
            return embeddings[new_word]
    else:
        print(word)
        return np.zeros(300, dtype="float32")

def random_vector():
    return np.random.random(300)

def select_missing_word_strat(word,emb, strat='0-vector', edit_distance_dic=None, pos=None, jaccard_distance_dic=None):
    if strat == 'Nullvektor':
        return np.zeros(300, dtype="float32")
    elif strat == 'Synonym':
        return wordnet_vec(word, pos, emb)
    elif strat == 'Levenshtein-Distanz':
        return close_distance_word(word, edit_distance_dic, emb)
    elif strat =='Zufallsvektor':
        return random_vector()
    elif strat =='Hyperonym':
        return wordnet_vec(word, pos, emb, True)
    elif strat =='Jaccard-Distanz':
        return close_distance_word(word, jaccard_distance_dic, emb)


#! /usr/bin/enc python
# -*- coding: utf-8 -*-
# author: Irving He
# email: 1910646@tongji.edu.cn

# MyThread --- 继承+重写
# 实例化两个子线程

import threading
import time

def long_time_task(i):
    time.sleep(2)
    return 8**20

class MyThread(threading.Thread):
    def __init__(self,func,args,name='',):
        threading.Thread.__init__(self)
        self.func = func
        self.args = args
        self.name = name

    def run(self):
        print('开始子进程{}'.format(self.name))
        self.result = self.func(self.args[0],)
        print("结果:{}".format(self.result))
        print('结束子进程{}'.format(self.name))

if __name__ == "__main__":
    start = time.time()
    threads = []
    for i in range(1,3):
        t = MyThread(long_time_task,(i,),str(i))
        threads.append(t)

    for  t in threads:
        t.start()
    for t in threads:
        t.join()

    end = time.time()
    print("Total time consump {}s".format((end - start)))


import os

def replace_home_dir(cwd):
    home = os.getenv('HOME')
    if cwd.startswith(home):
        return '~' + cwd[len(home):]
    return cwd

def split_path_into_names(cwd):
    names = cwd.split(os.sep)

    if names[0] == '':
        names = names[1:]

    if not names[0]:
        return ['/']

    return names

def add_cwd_segment():
    cwd = (powerline.cwd or os.getenv('PWD')).decode('utf-8')
    cwd = replace_home_dir(cwd)
    names = split_path_into_names(cwd)

    max_depth = powerline.args.cwd_max_depth
    if len(names) > max_depth:
        names = names[:2] + [u'\u2026'] + names[2 - max_depth:]

    if powerline.args.cwd_mode == 'plain':
        powerline.append(' %s ' % (cwd,), Color.CWD_FG, Color.PATH_BG)
    else:
        if not (powerline.args.cwd_mode == 'dironly' or powerline.args.cwd_only):
            for n in names[:-1]:
                if n == '~' and Color.HOME_SPECIAL_DISPLAY:
                    powerline.append(' %s ' % n, Color.HOME_FG, Color.HOME_BG)
                else:
                    powerline.append(' %s ' % n, Color.PATH_FG, Color.PATH_BG,
                        powerline.separator_thin, Color.SEPARATOR_FG)

        if names[-1] == '~' and Color.HOME_SPECIAL_DISPLAY:
            powerline.append(' %s ' % names[-1], Color.HOME_FG, Color.HOME_BG)
        else:
            powerline.append(' %s ' % names[-1], Color.CWD_FG, Color.PATH_BG)

add_cwd_segment()