from flaml import AutoML
from sklearn.datasets import load_boston
import os
import unittest
import logging
import tempfile
import io

class TestLogging(unittest.TestCase):

    def test_logging_level(self):

        from flaml import logger, logger_formatter

        with tempfile.TemporaryDirectory() as d:

            training_log = os.path.join(d, "training.log")

            # Configure logging for the FLAML logger
            # and add a handler that outputs to a buffer.
            logger.setLevel(logging.INFO)
            buf = io.StringIO()
            ch = logging.StreamHandler(buf)
            ch.setFormatter(logger_formatter)
            logger.addHandler(ch)

            # Run a simple job.
            automl = AutoML()
            automl_settings = {
                "time_budget": 1,
                "metric": 'mse',
                "task": 'regression',
                "log_file_name": training_log,
                "log_training_metric": True,
                "n_jobs": 1,
                "model_history": True,
            }
            X_train, y_train = load_boston(return_X_y=True)
            n = len(y_train) >> 1
            automl.fit(X_train=X_train[:n], y_train=y_train[:n],
                       X_val=X_train[n:], y_val=y_train[n:],
                       **automl_settings)

            # Check if the log buffer is populated.
            self.assertTrue(len(buf.getvalue()) > 0)

        import pickle
        with open('automl.pkl', 'wb') as f:
            pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL)
        print(automl.__version__)


#2a)
def summerOlympics(firstYear,lastYear):
    years = []
    for i in range(1948,lastYear+1,4):
        if (i>=firstYear):
            if(i<=lastYear):
                years.append(i)
    return years

print(summerOlympics(1999,2012))

#2b)
def findAge(bYear,bMonth,bDay):
    (yyyy,mm,dd) = current_date()
    age = yyyy-bYear
    if (bMonth == mm):
        if(bDay>dd):
            age-=1
    elif(bMonth > mm):
        age-=1
    return age
#IKKE DEL AV OPPGAVEN - MEN PÅKREVD Å BRUKE FUNKSJONEN I OPPGAVEN
def current_date():
    return 2012,12,11
#-------------------------------------------
print(findAge(2000,12,15))

#2c)
def printAgeDiff(table):
    for n in range(len(table)-1):
        age1 = findAge(table[n][2],table[n][3],table[n][4])
        age2 = findAge(table[n+1][2],table[n+1][3],table[n+1][4])
        print(table[n][0],table[n][1],end="")
        if (age1==age2):
            print("is the same age as",end="")
        if(age1>age2):
            print("is older than",end="")
        if(age1<age2):
            print("is younger than",end="")
        print(table[n+1][0],table[n+1][1])
table=[['Justin','Bieber',1994,3,1],
       ['Donald','Duck',1934,8,1],
       ['George','Clooney',1961,5,6],
       ['Eddie','Murphy',1961,4,3]]
printAgeDiff(table)

#3a)
def fu1(a):
    r=0
    while(a>0):
        s=a%10
        r=r+s
        a=(a-s)/10
    return r
print(fu1(1234))

#3b)
def fu2(input):
    r=0
    s=0
    t=0
    u=0
    n=len(input)
    for c in input:
        if(c.isalpha()):
            r=r+1
        elif(c.isdigit()):
            s=s+1
        elif(c==' '):
            t=t+1
        else:
            u=u+1
    r=100*r/n
    s=100*s/n
    t=100*t/n
    u=100*u/n
    return(r,s,t,u)
(a,b,c,d) = fu2('Ut pa tur, aldri sur')
print(a,b,c,d)

#3c)
def fu3(a):
    if(a<=2):
        r=1
    else:
        r=1+fu3(a/2)
    return r
print(fu3(100))

#4a)
def cold_days(templist):
    days=0
    for i in templist:
        if(i<0):
            days+=1
    return days
print(cold_days([1,-5,3,0,-6,-3,15,0]))

#4b)
def cap_data(array,min_value,max_value):
    result=array[:]
    for i in range(len(result)):
        if (result[i]<min_value):
            result[i]=min_value
        elif(result[i]>max_value):
            result[i]=max_value
    return result
A=[-70,30,0,90,23,-12,95,12]
print(cap_data(A,-50,50))

#4c)
import random
def generate_testdata(N,min_value,max_value):
    result=[]
    while len(result)<N:
        verdi=random.randint(min_value,max_value)
        if verdi not in result:
            result.append(verdi)
    return result
print(generate_testdata(10,-5,10))

#4d)
def create_db(temp,rain,humidity,wind):
    weather= {}
    for i in range(1,len(temp)+1):
        weather[i]=[temp[i-1],rain[i-1],humidity[i-1],wind[i-1]]
    return weather
temp=[1,5,3]
rain=[0,30,120]
humidity=[30,50,65]
wind=[3,5,7]
weather=create_db(temp,rain,humidity,wind)
print(weather)

#4e) - manglet str() rundt objektene for at de skulle kunne printes., plut mellomrom.
def print_db(weather):
    print("Day | Temp | rain | humidity | wind")
    print("====+======+======+==========+======")
    for i in weather.keys():
        print(str(i).rjust(4),str(weather[i][0]).rjust(6),end=" ")
        print(str(weather[i][1]).rjust(6),end=" ")
        print(str(weather[i][2]).rjust(10),end=" ")
        print(str(weather[i][3]).rjust(6))
print_db(weather)

#4f)
def strange_weather(temp,rain):
    start=-1
    stop=-1
    tempstart=0
    tempstop=0
    for i in range(len(temp)-1):
        if temp[i]<0:
            if temp[i] > temp[i+1]:
                if rain[i] < rain[i+1]:
                    tempstop = i+1
                    if (stop-start < tempstop-tempstart):
                        start = tempstart
                        stop =tempstop
                else:
                    tempstart = i+1
                    tempstop = i+1
            else:
                tempstart = i+1
                tempstop = i+1
        else:
            tempstart = i+1
            tempstop = i+1
    return start+1,stop+1
temp=[1,3,4,-5,-6,-7,-8,-9,3,0]
rain=[0,20,30,0,10,30,50,0,5,2]
(start,stop)=strange_weather(temp,rain)
print(start)
print(stop)


import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import csv

article_id = ['Article_id']
article_title = ['Title']
page_no = ['Pages']
volume_id = ['Volume']

with open(r"articles.csv") as csvfile:
    readCSV = csv.DictReader(csvfile, delimiter=',')
    for row in readCSV:
        article_id.append(row['Article_id'])
        article_title.append(row['Title'])
        page_no.append(row['Pages'])
        volume_id.append(row['Volume'])

X = [list(a) for a in zip(article_id,volume_id,article_title,page_no)]

with open (r"New_Article.csv", "wb") as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerows(X)


# -*- coding: utf-8 -*-
"""
    hexonet.apiconnector.apiclient
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module covers all necessary functionality for http communicatiton
    with our Backend System.
    :copyright: © 2018 by HEXONET GmbH.
    :license: MIT, see LICENSE for more details.
"""

from hexonet.apiconnector.response import Response
from hexonet.apiconnector.responsetemplatemanager import ResponseTemplateManager as RTM
from hexonet.apiconnector.socketconfig import SocketConfig
from six.moves.urllib.parse import quote, unquote
from six import string_types
import re
from six.moves.urllib.request import urlopen, Request
from six.moves.urllib.parse import urlparse, urlencode
import copy

rtm = RTM()

class APIClient(object):

    def __init__(self):
        # API connection url
        self.setURL("https://coreapi.1api.net/api/call.cgi")
        # Object covering API connection data
        self.__socketConfig = SocketConfig()
        # activity flag for debug mode
        self.__debugMode = False
        # API connection timeout setting
        self.__socketTimeout = 300000
        self.useLIVESystem()

    def enableDebugMode(self):
        """
        Enable Debug Output to STDOUT
        """
        self.__debugMode = True
        return self

    def disableDebugMode(self):
        """
        Disable Debug Output
        """
        self.__debugMode = False
        return self

    def getPOSTData(self, cmd):
        """
        Serialize given command for POST request including connection configuration data
        """
        data = self.__socketConfig.getPOSTData()
        tmp = ""
        if not isinstance(cmd, string_types):
            for key in cmd.keys():
                if (cmd[key] is not None):
                    tmp += ("{0}={1}\n").format(key, re.sub('[\r\n]', '', str(cmd[key])))
        return ("{0}{1}={2}").format(data, quote('s_command'), quote(re.sub('\n$', '', tmp)))

    def getSession(self):
        """
        Get the API Session that is currently set
        """
        return self.__socketConfig.getSession()

    def getURL(self):
        """
        Get the API connection url that is currently set
        """
        return self.__socketURL

    def getVersion(self):
        """
        Get the current module version
        """
        return "2.0.1"

    def saveSession(self, session):
        """
        Apply session data (session id and system entity) to given client request session
        """
        session["socketcfg"] = {
            "entity": self.__socketConfig.getSystemEntity(),
            "session": self.__socketConfig.getSession()
        }
        return self

    def reuseSession(self, session):
        """
        Use existing configuration out of session
        to rebuild and reuse connection settings
        """
        self.__socketConfig.setSystemEntity(session["socketcfg"]["entity"])
        self.setSession(session["socketcfg"]["session"])
        return self

    def setURL(self, value):
        """
        Set another connection url to be used for API communication
        """
        self.__socketURL = value
        return self

    def setOTP(self, value):
        """
        Set one time password to be used for API communication
        """
        self.__socketConfig.setOTP(value)
        return self

    def setSession(self, value):
        """
        Set an API session id to be used for API communication
        """
        self.__socketConfig.setSession(value)
        return self

    def setRemoteIPAddress(self, value):
        """
        Set an Remote IP Address to be used for API communication.
        To be used in case you have an active ip filter setting.
        """
        self.__socketConfig.setRemoteAddress(value)
        return self

    def setCredentials(self, uid, pw):
        """
        Set Credentials to be used for API communication
        """
        self.__socketConfig.setLogin(uid)
        self.__socketConfig.setPassword(pw)
        return self

    def setRoleCredentials(self, uid, role, pw):
        """
        Set Credentials to be used for API communication
        """
        if (role == ''):
            return self.setCredentials(uid, pw)
        return self.setCredentials(("{0}!{1}").format(uid, role), pw)

    def login(self, otp=""):
        """
        Perform API login to start session-based communication
        """
        self.setOTP(otp)
        rr = self.request({"COMMAND": "StartSession"})
        if (rr.isSuccess()):
            col = rr.getColumn("SESSION")
            self.setSession(col.getData()[0] if (col is not None) else None)
        return rr

    def loginExtended(self, params, otp=""):
        """
        Perform API login to start session-based communication.
        Use given specific command parameters.
        """
        self.setOTP(otp)
        cmd = {
            "COMMAND": "StartSession"
        }
        cmd.update(params)
        rr = self.request(cmd)
        if (rr.isSuccess()):
            col = rr.getColumn("SESSION")
            self.setSession(col.getData()[0] if (col is not None) else None)
        return rr

    def logout(self):
        """
        Perform API logout to close API session in use
        """
        rr = self.request({
            "COMMAND": "EndSession",
        })
        if (rr.isSuccess()):
            self.setSession(None)
        return rr

    def request(self, cmd):
        """
        Perform API request using the given command
        """
        data = self.getPOSTData(cmd).encode('UTF-8')
        # TODO: 300s (to be sure to get an API response)
        try:
            req = Request(self.__socketURL, data, {
                'User-Agent': 'python-sdk::' + self.getVersion()
            })
            body = urlopen(req, timeout=self.__socketTimeout).read()
            if (self.__debugMode):
                print(self.__socketURL, data, body, '\n', '\n')
        except Exception:
            body = rtm.getTemplate("httperror").getPlain()
            if (self.__debugMode):
                print(self.__socketURL, data, "HTTP communication failed", body, '\n', '\n')
        return Response(body, cmd)

    def requestNextResponsePage(self, rr):
        """
        Request the next page of list entries for the current list query
        Useful for tables
        """
        mycmd = self.__toUpperCaseKeys(rr.getCommand())
        if ("LAST" in mycmd):
            raise Exception("Parameter LAST in use. Please remove it to avoid issues in requestNextPage.")
        first = 0
        if ("FIRST" in mycmd):
            first = mycmd["FIRST"]
        total = rr.getRecordsTotalCount()
        limit = rr.getRecordsLimitation()
        first += limit
        if (first < total):
            mycmd["FIRST"] = first
            mycmd["LIMIT"] = limit
            return self.request(mycmd)
        else:
            return None

    def requestAllResponsePages(self, cmd):
        """
        Request all pages/entries for the given query command
        """
        responses = []
        mycmd = copy.deepcopy(cmd)
        mycmd["FIRST"] = 0
        rr = self.request(mycmd)
        tmp = rr
        while tmp is not None:
            responses.append(tmp)
            tmp = self.requestNextResponsePage(tmp)
            if tmp is None:
                break
        return responses

    def setUserView(self, uid):
        """
        Set a data view to a given subuser
        """
        self.__socketConfig.setUser(uid)
        return self

    def resetUserView(self):
        """
        Reset data view back from subuser to user
        """
        self.__socketConfig.setUser(None)
        return self

    def useOTESystem(self):
        """
        Set OT&E System for API communication
        """
        self.__socketConfig.setSystemEntity("1234")
        return self

    def useLIVESystem(self):
        """
        Set LIVE System for API communication (this is the default setting)
        """
        self.__socketConfig.setSystemEntity("54cd")
        return self

    def __toUpperCaseKeys(self, cmd):
        """
        Translate all command parameter names to uppercase
        """
        newcmd = {}
        for k in cmd.keys():
            newcmd[k.upper()] = cmd[k]
        return newcmd


#!/usr/bin/env python
# coding: utf-8

# In[73]:

import numpy as np
import pandas as pd
from scipy.spatial import distance
import os

# In[79]:

# Import data

filename_1 = "pre_outdoor_embedding.csv "
filename_2 = "pre_outdoor_mfcc.csv "
filename_3 = "pre_TV_embedding.csv"
filename_4 = "pre_TV_mfcc.csv"
data_1 = np.array(pd.read_csv(filename_1, header = None))
data_2 = np.array(pd.read_csv(filename_2, header = None))
data_3 = np.array(pd.read_csv(filename_3, header = None))
data_4 = np.array(pd.read_csv(filename_4, header = None))
X_1 = data_1[:, :-1]
Y_1 = data_1[:, -1]
X_2 = data_2[:, :-1]
Y_2 = data_2[:, -1]
X_3 = data_3[:, :-1]
Y_3 = data_3[:, -1]
X_4 = data_4[:, :-1]
Y_4 = data_4[:, -1]

# In[80]:

def getDistance(seg_1, seg_2):
#    return np.linalg.norm((seg_1 - seg_2), ord = 1)
    return scipy.spatial.distance.cosine(seg_1, seg_2)

def unsupervisedalgo(mfcc, Constants_mfcc_dist_diff_un):

    new_mfcc = np.zeros((mfcc.shape[0], mfcc.shape[1]))
    new_in_count = np.zeros((mfcc.shape[0], ))
    new_mfcc[0, :] = mfcc[0, :]
    speaker_count = 1
    new_in_count[0] = 1

    for i in range(1, mfcc.shape[0], 1):
        diff_count = 0
        for j in range(speaker_count):
            mfcc_dist = getDistance(mfcc[i], new_mfcc[j])
            if (mfcc_dist >= Constants_mfcc_dist_diff_un):
                diff_count = diff_count + 1;
            else:
                new_mfcc[j] = (new_mfcc[j]*new_in_count[j]+mfcc[i])/(new_in_count[j]+1)
                new_in_count[j] += 1
                break
        if (diff_count == speaker_count):
            new_mfcc[speaker_count, :] = mfcc[i, :]
            new_in_count[speaker_count] += 1
            speaker_count = speaker_count + 1
    return speaker_count, new_in_count

# In[81]:

print(unsupervisedalgo(X_1, 0.5))
print(unsupervisedalgo(X_2, 0.05))

print(unsupervisedalgo(X_3, 0.5))
print(unsupervisedalgo(X_4, 0.05))

# In[ ]:


from lib.H3Kernels import kernel
import utils.plot_utils as pltools
import numpy as np
import matplotlib.pyplot as plt

__author__ = 'morgan'

def obj_f(x):
    # 1d function
    return np.array([np.sin(x0)+x0*np.cos(x0) for x0 in x])

X = np.linspace(-5, 5, 100).reshape(100, 1)
y = obj_f(X)
given_idx = np.random.choice(y.size, 3)
x_cov = kernel(X, metric='rbf')
m = np.zeros(X.shape[0])
f = np.random.multivariate_normal(m, x_cov, size=3)
# posterior
k_tt = kernel(X, metric='rbf')
k_tr = kernel(X[given_idx], metric='rbf')
k_ttr = kernel(X, X[given_idx], metric='rbf')
m_posterior = k_ttr.dot(np.linalg.inv(k_tr)).dot(y[given_idx].reshape(given_idx.size, 1))
cov_posterior = k_tt-k_ttr.dot(np.linalg.inv(k_tr)).dot(k_ttr.T)
f_posterior = np.random.multivariate_normal(m_posterior.ravel(), cov_posterior, size=3)
plt.subplot(121)
plt.plot(X, f[0], 'r-')
plt.plot(X, f[1], 'b-')
plt.plot(X, f[2], 'g-')
plt.plot(X, y, 'k-.', label='groundtruth f')
plt.legend()
plt.subplot(122)
plt.plot(X, f_posterior[0], 'r-')
plt.plot(X, f_posterior[1], 'b-')
plt.plot(X, f_posterior[2], 'g-')
plt.plot(X, y, 'k-.', label='groundtruth f')
plt.plot(X[given_idx], y[given_idx], 'ko', ms=6)
plt.fill_between(X.ravel(), m_posterior.ravel()-4*cov_posterior.diagonal(),
                 m_posterior.ravel()+4*cov_posterior.diagonal(),
                 facecolor='gray', alpha=.25, edgecolor='none')
plt.gca().set(xlim=[-5, 5])
plt.legend()
plt.show()


import pygame
from tela import Tela

class Texto:
    def __init__(self,
                 texto,
                 font,
                 tamanho,
                 cor,
                 posicao):

        self.__texto = texto
        self.__font = font
        self.__tamanho = tamanho
        self.__cor = cor
        self.__tela = Tela()
        self.__posicao = posicao

        self.__fonteCompleta = pygame.font.Font(self.__font, self.__tamanho)

    @property
    def texto(self):
        return self.__texto

    @texto.setter
    def texto(self, texto):
        self.__texto = texto

    @property
    def posicao(self):
        return self.__posicao

    @posicao.setter
    def posicao(self, posicao):
        self.__posicao = posicao

    def draw(self):
        temp = self.__fonteCompleta.render(self.__texto, False, self.__cor).convert_alpha(self.__tela.display)
        self.__tela.display.blit(temp, self.__posicao)


import datetime
import uuid

from flask import Flask, request, session, g, render_template
from flaskext.sqlalchemy import SQLAlchemy
from flaskext.markdown import Markdown
from flaskext.mail import Mail, Message
from slugify import slugify

app = Flask(__name__)
app.config.from_object('np_settings')
db = SQLAlchemy(app)
Markdown(app)
mail = Mail(app)

class Memory(db.Model):
    __tablename__ = 'memories_memory'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    slug = db.Column(db.String(50))
    body = db.Column(db.Text)
    is_visible = db.Column(db.Boolean)
    created_date = db.Column(db.DateTime)
    created_by = db.Column(db.String(200))
    email = db.Column(db.String(200))
    confirmation_nbr = db.Column(db.String(50))

    def __init__(self, title, slug, body, created_by, email, confirmation_nbr):
        self.title = title
        self.slug = slug
        self.body = body
        self.created_by = created_by
        self.email = email
        self.confirmation_nbr = confirmation_nbr
        self.is_visible = False
        self.created_date = datetime.datetime.now()

    def __repr__(self):
        return '<Memory %r>' % self.title

    def get_url(self):
        return '/memory/%s' % self.slug

@app.route('/')
def index():
    memories = Memory.query.filter_by(is_visible=True).order_by(Memory.created_date.desc()).all()
    return render_template('index.html', memories=memories)

@app.route('/about')
def about():
    return render_template('about.html')

@app.route('/contact')
def contact():
    return render_template('contact.html')

@app.route('/memory/new', methods=['GET', 'POST'])
def new_memory():
    if request.method == 'GET':
        return render_template('memories/create_memory.html')
    else:
        title = request.form.get('title', '')
        created_by = request.form.get('created_by', 'Anonymous')
        body = request.form.get('body', '')
        email = request.form.get('email', '')

        slug = make_slug_for_title(title)
        confirmation_nbr = make_confirmation_nbr()

        memory = Memory(title, slug, body, created_by, email, confirmation_nbr)

        db.session.add(memory)
        db.session.commit()

        if app.config['PRODUCTION']:
            email_body = render_template('memories/memory_email.html', memory=memory)

            msg = Message(recipients=[app.config['DEFAULT_MAIL_SENDER']], html=email_body, subject="New memory")
            mail.send(msg)

        return "OK"

def make_slug_for_title(title):
    slug = slugify(title)

    for n in range(20):
        temp_slug = slug

        if n > 0:
            temp_slug = "%s-%s" % (slug, n)

        num = Memory.query.filter_by(slug=temp_slug).count()

        if num == 0:
            return temp_slug

    return slug

def make_confirmation_nbr():
    return str(uuid.uuid4())

@app.route("/memory/thanks")
def say_thanks():
    return render_template('memories/thanks.html')

@app.route('/memory/<slug>')
def get_memory(slug):
    memory = Memory.query.filter_by(slug=slug).first_or_404()
    return render_template('memories/memory.html', memory=memory)

@app.route('/memory/confirm/<slug>/<confirmation_nbr>')
def confirm(slug, confirmation_nbr):
    memory = Memory.query.filter_by(slug=slug, confirmation_nbr=confirmation_nbr, is_visible=False).first_or_404()

    memory.is_visible = True
    db.session.add(memory)
    db.session.commit()

    return 'Confirmed!'

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

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


import numpy as np

# Cross entropy 에러가 왜 더 좋은지에 대한 설명
# MSE로는 cost의 합이 cross entropy로 계산한 cost의 합보다 적게 나옴
# y1은 정답이 60%로 적당히 맞는 분류기. 아직 많이 학습이 필요함
# y2는 정답을 완전히 잘못 찍은 분류기. 학습이 매우 많이 필요
# MSE는 y1 : 0.089, y2 : 0.51 정도의 cost가 보이며
# Cross entropy는 y1 : 0.5, y2 : 2.3 정도의 cost가 나타난다.
# 그럼 gradient descent 예제에 cross entropy를 사용하면 더 향상되나??

def mean_squared_error(y, t):
    return 0.5 * np.sum((y - t)**2)

def cross_entropy_error(y, t):
    #delta는 너무 작은 값이 로그에 들어가는 것을 방지하기 위함
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

t = [0,0,1,0,0,0,0,0,0,0]
y1 = [0.1, 0.05, 0.6, 0.0, 0.05, 0.0, 0.1, 0.0, 0.1, 0.0]
y2 = [0.1, 0.05, 0.1, 0.0, 0.05, 0.0, 0.6, 0.0, 0.1, 0.0]

mse1 = mean_squared_error(np.array(y1), np.array(t))
print('mse1:', mse1)
mse2 = mean_squared_error(np.array(y2), np.array(t))
print('mse2:', mse2)

cee1 = cross_entropy_error(np.array(y1), np.array(t))
print('cee1:', cee1)
cee2 = cross_entropy_error(np.array(y2), np.array(t))
print('cee2:', cee2)


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.http import Http404

from core.models import MyProfile
from core.models_project import Project, ProjectMessage, ProjectPhase, RequiredSkills, Issue, TeamMember, CommitSkill
from .forms import ProposeProjectForm, RequiredSkillsForm, ChangePhaseForm
from .utils import project_prize, team_composition

@login_required
def project_details(request, pk):
    project = Project.objects.get(id=pk)
    issues = Issue.objects.filter(project=project)
    my_profile = MyProfile.objects.filter(owner=request.user).first()
    project_owner = MyProfile.objects.filter(owner=project.proposed_by).first()
    profiles = MyProfile.objects.all()
    requiredskills = RequiredSkills.objects.filter(project=project)
    project_team = TeamMember.objects.filter(projects=project)
    skill_coverage = CommitSkill.objects.filter(project=project)
    project_log = ProjectMessage.objects.filter(project=project).order_by('-message_date')
    gamification_dict = team_composition(project_team)
    data_dict = {'project': project, 'my_profile': my_profile, 'profiles': profiles, 'project_log': project_log,
                 'issues': issues, 'requiredskills': requiredskills, 'project_owner': project_owner,
                 'skill_coverage': skill_coverage}
    context = {**gamification_dict, **data_dict}

    return render(request, "project_details.html", context)

@login_required
def propose_project(request):
    my_profile = get_object_or_404(MyProfile, owner=request.user)

    # does not allow to start project without budget needed
    if my_profile.my_wallet < 450:
        messages.error(request, "Insufficient Funds to start a New Project")
        return redirect(reverse('dashboard:dashboard'))

    else:

        if request.method == 'POST':

            form = ProposeProjectForm(request.POST, request.FILES)

            if form.is_valid():
                name = form.cleaned_data['name']
                description = form.cleaned_data['description']
                budget = 450
                image = form.cleaned_data['image']
                proposed_by = request.user

                new_project = Project.objects.create(
                    name=name,
                    description=description,
                    budget=budget,
                    image=image,
                    proposed_by=proposed_by
                )
                new_project.save()

                # calculate remaining leancoins
                my_profile.my_wallet = my_profile.my_wallet - 450  # calculate remaining leancoins
                my_profile.save()

                RequiredSkills.objects.create(
                    project=new_project
                ).save()

                ProjectMessage.objects.create(
                    project=new_project,
                    message='Project "{0}" proposed by user {1}'.format(new_project.name, new_project.proposed_by)
                ).save()

                return redirect(reverse('dashboard:dashboard'))

        else:
            form = ProposeProjectForm()

    return render(request, 'propose_project.html', {'form': form})

@login_required
def project_skillset(request, pk):
    """DEFINE PROJECT SKILLSET"""
    project = Project.objects.get(id=pk)
    requiredskills = get_object_or_404(RequiredSkills, project=project)

    form = RequiredSkillsForm(request.POST, instance=requiredskills)  # define team skillset needed for project

    if form.is_valid():
        form.save()
        return redirect(reverse('projects:project_details', kwargs={'pk': pk}))
    else:
        form = RequiredSkillsForm(request.POST, instance=requiredskills)

    return render(request, 'project_skillset.html', {'form': form})

@login_required
def advance_project(request, pk):
    """ADVANCE PROJECT TO THE NEW PHASE"""
    if not request.user.is_superuser:
        raise Http404

    project = Project.objects.get(pk=pk)
    project_phases = ProjectPhase.objects.all()

    my_profile = MyProfile.objects.get(owner=project.proposed_by)

    form = ChangePhaseForm(request.POST, instance=project)

    context = {'form': form,
               'project': project,
               'project_phases': project_phases,
               'my_profile': my_profile}

    if request.method == 'POST':

        if form.is_valid():
            form.save()

            ProjectMessage.objects.create(
                project=project,
                message='Project "{0}" advanced to stage "{1}"'.format(project.name, project.phase)
            ).save()

            return redirect(reverse('projects:project_details', kwargs={'pk': pk}))

    return render(request, 'advance_project.html', context)

@login_required
def complete_project(request, pk):
    """FINISH PROJECT"""
    if not request.user.is_superuser:
        raise Http404

    project = Project.objects.get(pk=pk)
    project_team = TeamMember.objects.filter(projects=project)
    project_manager = MyProfile.objects.get(owner=project.proposed_by)

    team_members, prize = project_prize(project, project_team)

    project_manager.my_wallet = project_manager.my_wallet + 540
    project_manager.save()

    for element in project_team:
        user = element.current_user
        winners = MyProfile.objects.filter(owner=user)
        for element in winners:
            element.my_wallet += prize
            element.save()

    project.delete()

    return redirect(reverse('dashboard:dashboard'))

@login_required
def delete_project(request, pk):
    """REMOVE PROJECT"""
    if not request.user.is_superuser:
        raise Http404
    project = get_object_or_404(Project, pk=pk)
    project.delete()

    return redirect(reverse('dashboard:dashboard'))

def find_project(request):

    projects = Project.objects.filter(name__icontains=request.GET['query'])
    profiles = MyProfile.objects.all()
    issue_count = len(Issue.objects.all())
    project_count = len(projects)

    context = {'projects': projects, 'profiles': profiles, 'project_count': project_count,
               'issue_count': issue_count}

    return render(request, "dashboard.html", context)


# coding: utf-8
import os

class fileUtils:

    def __init__(self, root):
        self._list = []
        self._root = root

    def search_dir(self, path):
        for root, dirs, files in os.walk(path):
            for file in files:
                self._list.append(os.path.join(root, file))
        return self._list

    def selection(self, condition):
        self.search_dir(self._root)
        select = []
        for sub in self._list:
            for c in condition:
                if sub.endswith(c):
                    select.append(sub)
        return select

    """
    将目标文件夹中的所有目标代码文件读入到某一个汇总文件.txt中
    """

    def join(self, selection):
        files = os.listdir(self._root)
        target_files = []
        for file in files:
            if os.path.isdir(os.path.join(self._root, file)):
                name = os.path.join(self._root, file)
                name = name + "/" + file + ".tdb"
                target_files.append(name)
                f = open(name, "w")
                self._reader_under_target(f, os.path.join(self._root, file), selection)
                f.close()
        return target_files

    """
    读取目标文件夹下所有符合规则的文本文件，拷贝进 target 文本中
    """

    @staticmethod
    def _reader_under_target(target, path, selection):
        print(selection)
        print("+++++++++++++++++++++++++++++++++++++++++++++++++")
        for root, dirs, files in os.walk(path):
            for file in files:
                p = os.path.join(root, file)
                if p in selection:
                    fp = open(p)
                    target.write(fp.read())
                    fp.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    With Spotify_HTTP_manager you can control your Spotify devices
    with HTTP requests.
    The configuration must be edit in the config.py file.

    I use the spotipy wrapper spotipy (https://github.com/plamere/spotipy) that
    uses the spotify web-api (https://developer.spotify.com/web-api/).

    You must install spotipy :
    pip install git+https://github.com/plamere/spotipy.git

    You must declare the application to Spotify
    https://developer.spotify.com/dashboard/
    When you have create your app, you must specify the "Redirect URIs"
    in "EDIT SETTINGS".
    Ex value :
    - http://localhost:8581/usertoken
    - http://127.0.0.1:8581/usertoken
    - http://192.168.1.x:8581/usertoken
    The value must be the same in the config file

    USAGE :
        (After edit config.py)

        manager = Spotify_HTTP_manager()
        manager.web_interface()

        Commands :
        List of all available equipment
        http://xxx.xxx.xxx.xxx:8581/

        Device status :
        http://xxx.xxx.xxx.xxx:8581/device_name

        Play device :
        http://xxx.xxx.xxx.xxx:8581/device_name/play
        You can choose a track with the spotify URI :
        http://xxx.xxx.xxx.xxx:8581/device_name/play?track=spotify:track:xxxxxxxxx
        Or a playlist
        http://xxx.xxx.xxx.xxx:8581/device_name/play?playlist=spotify:user:username:playlist:xxxxxxxxxx
        http://xxx.xxx.xxx.xxx:8581/device_name/play?playlist=spotify:user:username:playlist:xxxxxxxxxx&position=1

        Volume 70%:
        http://xxx.xxx.xxx.xxx:8581/device_name/volume_70
        or get parameter volume=70

        Other commands :
        pause, next, previous,

"""

#__author__ = "Samuel Poggioli"
#__version__ = "1.0.1"

# Install github version
#pip install git+https://github.com/plamere/spotipy.git
import spotipy
from spotipy import oauth2

# Bottle in local file
# pip install bottle
from bottle import route, run, template, static_file, request, redirect

from samlog import log
from config import Config

class Spotify_HTTP_manager():

    def __init__(self, username=None, client_id=None, client_secret=None,
        redirect_uri=None):
        """ Init Spotify_HTTP_manager.
        You can specify the API parameters here but it
        is easier to edit the config.py file
        """

        self.spotify = None
        self.sp_oauth = None
        self.token = None

        if username:
            self.username = username
        else:
            self.username = Config.USERNAME

        if client_id:
            self.client_id = client_id
        else:
            self.client_id = Config.CLIENT_ID

        if client_secret:
            self.client_secret = client_secret
        else:
            self.client_secret = Config.CLIENT_SECRET

        self.redirect_uri = Config.REDIRECT_URI
        self.scope = Config.SCOPE
        self.cache_path = ".cache-" + self.username

    def spotify_connect(self):
        """ Init Spotify object with token.
        """
        self.spotify = None

        if not self.token:
            self.token = self.get_spotify_api_token()

        if self.token:
            self.spotify = spotipy.Spotify(auth=self.token)
            if self.spotify:
                self.spotify.trace = False
                return True
        else:
            return False

    def get_spotify_api_token(self):
        """

        Replace spotipy.util.prompt_for_user_token to
        request the user token.
        """
        self.sp_oauth = oauth2.SpotifyOAuth(self.client_id,
                                       self.client_secret,
                                       self.redirect_uri,
                                       scope=self.scope,
                                       cache_path=self.cache_path)

        token_info = self.sp_oauth.get_cached_token()

        # Auth'ed API request
        if token_info and token_info.get('access_token'):
            self.token = token_info.get('access_token')
            self.spotify_connect()
            return token_info['access_token']
        else:
            return None

    def get_authorize_url(self):
        """ If necessary, generate URL for request the user token
        """
        if self.sp_oauth:
            return self.sp_oauth.get_authorize_url()
        else:
            return None

    def receives_user_token(self, responce):
        if self.sp_oauth:
            token_info = self.sp_oauth.get_access_token(responce)
        if token_info and token_info.get('access_token'):
            self.token = token_info.get('access_token')
            self.spotify_connect()
            return token_info['access_token']
        else:
            return None

    # don't work
    def connect_to_chromecast(self, name_device):
        """ Seach Chromecast with device name

        Search on the network if a chromecast corresponds to the equipment
        in parameter. If yes, connect Spotify to Chromecast with client token
        """
        # pip install pychromecast
        import pychromecast
        from pychromecast.controllers.spotify import SpotifyController

        chromecasts = pychromecast.get_chromecasts()
        for chromecast in chromecasts:
            if chromecast.name == name_device:
                # Action for Chromecast
                log("Try to connect Chromecast device")
                sp = SpotifyController(self.token)
                chromecast.register_handler(sp)
                sp.launch_app()

                return self.get_spotify_connect_device_id(
                    name_device, check_chromecast=False)

    def get_spotify_connect_device_id(self, name_device, check_chromecast=True):
        """ Return id of device
        """
        if self.spotify and name_device:

            list_dict_devices = self.spotify.devices()['devices']
            device_id = ""
            for d in list_dict_devices:
                if d.get('name') == name_device:
                    device_id = d.get('id')
                    return  device_id

            # If device is offline, seach Chromecast with this name
            if not device_id and check_chromecast == True:
                device_id = self.connect_to_chromecast(name_device)

        if not device_id:
            return None

    def web_interface(self):
        """ Web interface manage with Bottle
        """

        @route('/')
        def webui_list_devices():
            check_connexion_spotify_api()

            # List device available in Spotify Connect
            list_dict_devices = self.spotify.devices()['devices']

            # If not name device specified, return all device available
            # in Spotify Connect
            retour = "List of available equipment : </br>\n"
            for d in list_dict_devices:
                retour += "  - {} : {}\n".format(d.get('id'), d.get('name'))
            return template('{{retour}}', retour=retour)

        @route('/token')
        def user_token():
            """ Show user token
            """
            return template('{{retour}}', retour=self.token)

        @route('/<name_device>')
        @route('/<name_device>/<command>')
        def webui_device_control(name_device=None, command=None):
            check_connexion_spotify_api()
            retour = ""
            device_id = self.get_spotify_connect_device_id(name_device)

            # If device is online in Spotify Connect
            if device_id:
                #import pdb; pdb.set_trace()
                try:
                    # Volume en parametre
                    if request.params.volume:
                        valume_value = int(request.params.volume)
                        self.spotify.volume(valume_value, device_id=device_id)

                    if command == 'play':
                        if request.params.track:
                            uris = [request.params.track,]
                        else:
                            uris = None

                        if request.params.playlist:
                            context_uri = request.params.playlist
                        else:
                            context_uri = None

                        if request.params.playlist and request.params.position:
                            offset = {'position': int(request.params.position)}
                        else:
                            offset = None

                        self.spotify.start_playback(device_id=device_id,
                                                    context_uri=context_uri,
                                                    uris=uris,
                                                    offset=offset)
                        retour += "ok"

                    elif command == 'pause' or command == 'stop':
                        self.spotify.pause_playback(device_id)
                        retour += "ok"

                    elif command == 'next':
                        self.spotify.next_track(device_id)
                        retour += "ok"

                    elif command == 'previous':
                        self.spotify.previous_track(device_id)
                        retour += "ok"

                    elif command and 'volume' in command:
                        try:
                            valume_value = int(command.split('_')[1])
                            self.spotify.volume(valume_value, device_id=device_id)
                        except Exception:
                            retour = "Volume Error"

                    elif command is None:
                        item = self.spotify.currently_playing().get('item')
                        if item:
                            retour = item.get('name')

                except Exception as e:
                    retour = "Error"
                    print(e)

            else:
                retour = "Device not found"

            return template('{{retour}}', retour=retour)

        def check_connexion_spotify_api():
            # Connect to spotify API
            if not self.spotify or not self.spotify.me():
                if not self.get_spotify_api_token():
                    u = self.get_authorize_url()
                    if u:
                        redirect(u)
                    else:
                        retour = "User token request error."

        @route('/usertoken')
        def user_token_receive():
            """ Receive the Authorization Code Flow
            """
            if request.params.code:
                   self.receives_user_token(request.params.code)

            return template('{{retour}}', retour="Token receive OK")

        run(host=Config.IP_WEB_INTERFACE, port=Config.PORT_WEB_INTERFACE) #quiet=True


from tensorflow import keras
import random,pickle,os,datetime,time
import numpy as np
spsymbol="&^$"

def combineCapitalSingle(words):
	combinedWords=[]
	comword=""
	for word in words:
		if len(word)>1 or (len(word)==1 and not 65<=ord(word)<=90 and not 48<=ord(word)<=57 ) :
			if len(comword)>0:
				combinedWords.append(comword)
				comword=""
			combinedWords.append(word)
		else:
			comword+=word
	if len(comword) > 0:
		combinedWords.append(comword)
	return combinedWords

def dividehashTag(hasgtag):
	hasgtag=hasgtag.replace("_"," ")
	allwords=[]
	currentword=""
	for ch in hasgtag:
		if (65<=ord(ch)<=90 or 48<=ord(ch)<=57 or ch==" ") and len(currentword)>0:
			allwords.append(currentword)
			currentword=""
		if ch!=" ":
			currentword+=ch
	allwords.append(currentword)
	allwords=combineCapitalSingle(allwords)
	hasgtag=(" ".join(allwords))
	return hasgtag

def replcaeHashtag(sentence):
	if "#" in sentence:
		sentence1,sentence2=sentence.split("#",1)
		sentences=sentence2.strip().split(" ",1)
		if len(sentences)==1:
			hastag=dividehashTag(sentences[0])
			return sentence1.strip()+" "+hastag,[0]*len(sentence1.split())+[1]*len(hastag.split())
		else:
			hasgtag,sentence2=sentence2.split(" ",1)
			sentence1+dividehashTag(sentences[0])
			hashtag,(sentence2,mask)=dividehashTag(hasgtag),replcaeHashtag(sentence2)
			return sentence1.strip() +" "+ hashtag + " " + sentence2,[0]*len(sentence1.split())+[1]*len(hashtag.split())+mask

	return sentence.strip(),[0]*len(sentence.split())

def clearnSentence(sentence):
	for sep in ["\\n","।","॥"]:
		sentence = sentence.replace(sep, " ")
	while "  " in sentence:
		sentence=sentence.replace("  "," ")
	sentence=sentence.replace("!","").replace("\"","").replace("##","#")
	sentence="".join([s for s in sentence if ord(s)<5000])
	sentence,map=replcaeHashtag(sentence)
	while "  " in sentence:
		sentence=sentence.replace("  "," ")
	return sentence.strip(),map

def prepaireDict(all_sentences,minword=3):
	words=[d1 for data in all_sentences for d1 in data[0].split() if len(d1)>0]
	unique_word,un_count=np.unique(words, return_counts=True)
	unique_word=[w for w,c in zip(unique_word, un_count) if c>=minword]
	dict={w:i for w,i in zip(unique_word,range(1,1+len(unique_word)))}
	with open("models/Dict.pk","wb") as f:
		pickle.dump(dict,f)
	print("Total unique Word",len(dict))
	return dict

def convertdataToNumber(data,dict,label,padding=50):
	indata=[]
	for sent,masks in data:
		words=sent.split()
		assert len(words)==len(masks), "Issue in preprocessing"
		sentindex=[]
		maskindex=[]
		for word,wordmask in zip(words,masks):
			if word in dict:
				sentindex.append(dict[word])
				maskindex.append(wordmask)
		if len(sentindex)>padding:
			sentindex=sentindex[:padding]
			maskindex=maskindex[:padding]
		elif len(sentindex)<padding:
			maskindex=[0]*(padding - len(sentindex)) + maskindex
			sentindex=[0]*(padding-len(sentindex))+sentindex
		indata.append((np.array(sentindex),np.array([[1,0],[0,1]][label]),np.array(maskindex),sent))
	return indata

def prepaireData(padding=50,train=True):
	with open("Data/PROTweetProccessed.txt") as f:
		pro=[d[:-1] for d in f.readlines()]
	with open("Data/AntiTweetProcessed.txt") as f:
		anti=[d[:-1] for d in f.readlines()]
	if train:
		pro,anti=pro[:int(len(pro)*.8)],anti[:int(len(anti)*.8)]
	else:
		pro, anti = pro[int(len(pro)*.8):], anti[int(len(anti)*.8):]
	pro=list(filter(len,map(clearnSentence,pro)))
	anti=list(filter(len,map(clearnSentence,anti)))
	all_sentences=pro+anti
	if train:
		dict=prepaireDict(all_sentences)
	else:
		with open("models/Dict.pk", "rb") as f:
			dict=pickle.load(f)
	prodata=convertdataToNumber(pro,dict,0,padding=padding)
	antidata=convertdataToNumber(anti,dict,1,padding=padding)
	data=prodata+antidata
	random.shuffle(data)
	xdata=[d[0] for d in data]
	ydata=[d[1] for d in data]
	masks=[d[2] for d in data]
	senteces=[d[3] for d in data]
	return np.array(xdata),np.array(ydata),len(dict),np.array(masks),senteces

def ClassifyTweet(inputFile,models,padding=50,classified="Intelegent/ToRetweet/",thress=.95):
	if not os.path.exists(inputFile):
		return
	postiveoutputfile=classified+"Postive_IDS_"+"_".join(inputFile.split("/")[-1].split("_")[2:])
	negtiveoutputfile=classified+"Negtive_IDS_"+"_".join(inputFile.split("/")[-1].split("_")[2:])
	allids=[]
	if os.path.exists(postiveoutputfile):
		with open(postiveoutputfile,"r") as f:
			allids.extend([d[:-1] for d in f.readlines()])
	if os.path.exists(negtiveoutputfile):
		with open(negtiveoutputfile,"r") as f:
			allids.extend([d[:-1] for d in f.readlines()])
	model,dict=models
	os.makedirs(classified,exist_ok=True)
	with open(inputFile, "r") as f:
		odata = [fl.split(spsymbol)[:2] for fl in f.readlines()[1:]]
	odata=[d for d in odata if d[0] not in allids and len(d[1])>70]
	if len(odata)==0:
		print("No New Rocord Found for classification")
		return
	ids=[d[0] for d in odata]
	tweets=[d[1] for d in odata]
	tweets= list(filter(len, map(clearnSentence, tweets)))
	data=convertdataToNumber(tweets,dict,0,padding=padding)
	xdata=[d[0] for d in data]
	mask=[d[2] for d in data]
	# senteces=[d[3] for d in data]
	xdata,mask=np.array(xdata),np.array(mask)
	mask+=1
	pred=model.predict((xdata,mask))[:,0]
	postiveids=[i+"\n" for i,p in zip(ids,pred) if p>thress]
	negtiveids=[i+"\n" for i,p in zip(ids,pred) if p<=thress]
	with open(postiveoutputfile,"a") as f:
		f.writelines(postiveids)
	with open(negtiveoutputfile,"a") as f:
		f.writelines(negtiveids)

def TweetClassification(base_model_path="../models/"):
	model = keras.models.load_model(base_model_path+"MODEL.h5")
	with open(base_model_path+"Dict.pk", "rb") as f:
		dict = pickle.load(f)
	while True:
		ClassifyTweet("Intelegent/DataFiles/Base_FILE_"+datetime.datetime.now().strftime("%Y_%m_%d.csv") ,(model,dict))
		time.sleep(1*60)

if __name__=="__main__":
	TweetClassification()

# sentencelen=50
# xdata,ydata,vocab_size,mask,sentences=prepaireData(sentencelen,train=False)
# mask+=1
# YDATA=np.argmax(ydata,axis=1)
# model=keras.models.load_model("models/MODEL.h5")
# pred=model.predict((xdata,mask))
# print(confusion_matrix(YDATA, np.argmax(pred,axis=1)))
#
# print("actual,preddicted,text")
# for i in range(YDATA.shape[0]):
# 	# if YDATA[i]==1 and YDATA[i]!=np.argmax(pred[i]):
# 	if YDATA[i]!=np.argmax(pred[i]):
# 		print(YDATA[i],pred[i],sentences[i])
#
#


import sys
sys.stdin=open('input.txt')

T=int(input())

for tc in range(1,T+1):
    count = [0] * 10
    n=int(input())
    card_number=list(map(int, input()))

    for i in card_number:
        count[i]+=1
    arr=[]
    max=0
    for n in count:
        if max<n:
            max=n
    for n in range(len(count)):
        if max==count[n]:
            arr.append(n)
    arr_max=0
    for n in arr:
        if arr_max<n:
            arr_max=n

    print('#{} {} {}'.format(tc, arr_max, count[arr_max]))


## Author: Xinwu Qian
## Last Update: 2019-2-5

#utility functions that will be used in the taxi_env
from collections import deque
#update the waiting time of passengers in Q

def waiting_time_update(waiting_time,expect_waiting_time):
    try:
        (new_wait, new_expect_wait) = map(deque, zip(*[(i+1,j) for i,j in zip(waiting_time,expect_waiting_time) if i<j]))
    except ValueError:
        new_wait=deque([])
        new_expect_wait=deque([])

    return new_wait,new_expect_wait


from django.core.management.base import BaseCommand, CommandError
from uwsgi_log_parser.utils import load_line

class Command(BaseCommand):
    args = "<log_path log_path ...>"
    help = "Load uwsgi log from raw log file"

    def handle(self, *args, **options):
        for log_path in args:
            self.stdout.write('Processing path: %s ...' % log_path)
            self.load_single_log_file(log_path)

    def load_single_log_file(self, log_path):
        try:
            with open(log_path, 'r') as log_file:
                for line in log_file:
                    load_line(line)
        except IOError:
            raise CommandError('%s does not exist.' % log_path)


from odoo import api, fields, models, http
from datetime import datetime, timedelta

class WizardKgPayrollPayslipEmployee(models.TransientModel):
    _inherit = ['wizard.kg.report.base']
    _name = 'wizard.kg.report.payslip_employee'
    _title = "KG Report - Payslip Employee"

    date_from = fields.Date(string='Start Period Date', default=fields.Date.today)
    location_ids = fields.Many2many('hr.location', string='Location')
    structure_ids = fields.Many2many('hr.payroll.structure', string='Payslip Structure')
    department_ids = fields.Many2many('hr.department', string='Department')
    employee_ids = fields.Many2many('hr.employee', string='Employee')
    company_id = fields.Many2one('res.company',
                                 'Company',
                                 help="Company Id",
                                 default=lambda self: self.env.user.company_id.id)
    template = fields.Selection(
        [(1, 'Payslip Type 1'),
         (2, 'Payslip Type 2')
         ],
        string='Select Templates',
        default=1)

    @api.multi
    def _define_report_name(self):
        if self.template == 2:
            return "/kg_payroll/static/rpt/PayslipEmployee2.mrt"
        else:
            return "/kg_payroll/static/rpt/PayslipEmployee.mrt"

    @api.multi
    def _get_data(self):
        """ get data from database or any other source

        :return: dict or list of dict
        """
        # extract financial report data
        now = datetime.utcnow()+timedelta(hours=7)

        get_payslip_employee = self.get_payslip_employee()
        blank_name = "..........................."
        self.report_has_logo = False  # sample report has logo
        data = {
            "config": {
                "date_from": self.date_from,
                "company_name": self.env.user.company_id.name,
                "compiler": self.env.user.name,
                "template": self.template,
                "print_date": now.strftime("%Y-%m-%d"),

            },
            "data": get_payslip_employee
        }

        # return self._sample_data_reg_card()
        return data

    @api.multi
    def get_payslip_employee(self):
        where_department_clause, where_employee_clause, where_structure_clause, where_location_clause = self.query_get_clause()
        # get pos transaction data for Daily Card Report
        month = datetime.strptime(self.date_from, '%Y-%m-%d').strftime('%m')
        year = datetime.strptime(self.date_from, '%Y-%m-%d').strftime('%Y')
        depart_ids = tuple(self.department_ids.ids)
        if len(self.department_ids.ids) == 1:
            depart_ids = (self.department_ids.ids[0])
        emp_ids = tuple(self.employee_ids.ids)

        if len(self.employee_ids.ids) == 1:
            emp_ids = (self.employee_ids.ids[0])

        struct_ids = tuple(self.structure_ids.ids)
        if len(self.structure_ids.ids) == 1:
            struct_ids = (self.structure_ids.ids[0])

        loc_ids = tuple(self.location_ids.ids)
        if len(self.location_ids.ids) == 1:
            loc_ids = (self.location_ids.ids[0])

        query = """select case when sr.type_id = 'VAR' then 2 else 1 end as group_1,
                case when sr.type_id= 'VAR' then 'Penerimaan Tidak Tetap' else 'Penerimaan Tetap' end as group_1_name,
                case when src.code in ('DED','PPH21_REG','PPH21_IREG') then 1 else 2 end as group_2,
                case when src.code in ('DED','PPH21_REG','PPH21_IREG') then 'Potongan' else 'Penerimaan' end as group_2_name,
                e.employee_id, e."name" as emp_name, cf.code as fam_code, tp.tax_state,
                l.code, l.total, l.name, d.name as dept_name, l.slip_id, p.date_from, p.date_to, l.sequence
                from hr_payslip_line l
                left join hr_salary_rule sr on sr.id = l.salary_rule_id
                left join hr_salary_rule_category src on sr.category_id = src.id
                left join hr_payslip p on l.slip_id = p.id
                left join hr_employee e on p.employee_id = e.id
                left join hr_department d on e.department_id = d.id
                left join hr_kg_payroll_configuration_family cf on cf.id = e.fam_status_id
                left join hr_kg_payroll_tax_ptkp tp on tp.id = e.tax_status_id
                where p.state = 'done' and sr.appears_on_payslip is true and p.company_id = {company_id}
                """+where_department_clause+""" """+where_employee_clause+""" """+where_structure_clause+"""
                """+where_location_clause+""" and l.total != 0
                and extract(month from p.date_to) = {month} and extract(year from p.date_to) = {year}
                order by slip_id, group_1_name, group_2_name desc, l.sequence"""

        final_query = query.format(month=month, year=year, company_id=self.company_id.id, depart_ids=depart_ids, emp_ids=emp_ids,
                    struct_ids=struct_ids, loc_ids=loc_ids)

        self.env.cr.execute(final_query)
        payslip_employee_summary = self.env.cr.dictfetchall()

        return payslip_employee_summary

    def query_get_clause(self):
        where_department_clause = ""
        if self.department_ids:
            where_department_clause = "and e.department_id in {depart_ids}"
            if len(self.department_ids.ids) == 1:
                where_department_clause = "and e.department_id = {depart_ids}"

        where_employee_clause = ""
        if self.employee_ids:
            if self.employee_ids:
                where_employee_clause = "and p.employee_id in {emp_ids}"
                if len(self.employee_ids.ids) == 1:
                    where_employee_clause = "and p.employee_id = {emp_ids}"

        where_structure_clause = ""
        if self.structure_ids:
            if self.structure_ids:
                where_structure_clause = "and p.struct_id in {struct_ids}"
                if len(self.structure_ids.ids) == 1:
                    where_structure_clause = "and p.struct_id = {struct_ids}"

        where_location_clause = ""
        if self.location_ids:
            if self.location_ids:
                where_location_clause = "and p.struct_id in {loc_ids}"
                if len(self.location_ids.ids) == 1:
                    where_location_clause = "and p.struct_id = {loc_ids}"

        return where_department_clause, where_employee_clause, where_structure_clause, where_location_clause


# This is a python finite difference code to simulate waves in 2D linear isotropic medium
# SBP finite difference operators of interior accuracy 2, 4, 6 are used to approximate spatial derivatives
# Boundary conditions are imposed weakly using penalties (SATs)
# Time discretization is performed using the classical Runge-Kutta Method
# matplotlib notebook
# Created by: Kenneth Duru
#             LMU Munich, June, 2016.
#
#
import numpy as np
import matplotlib.pyplot as plt
import RK4_2D

plt.switch_backend("TkAgg")
plt.ion()

# ---------------------------------------------------------
# Summation-by-parts finite difference solver
#         for
# Elastic wave equation
#         in
# 2D Cartesian grid
# ---------------------------------------------------------

Lx = 50.0     # length of the domain (x-axis)
Ly = 50.0     # width of the domain (y-axis)
nx = 75       # grid points in x
ny = 75       # grid points in y
nt = 150      # number of time steps
dx = Lx/nx    # spatial step in x
dy = Ly/ny    # spatial step in y
isx = nx / 2  # source index x
isy = ny / 2  # source index y
ist = 100     # shifting of source time function
f0 = 100.0    # dominant frequency of source (Hz)
isnap = 20    # snapshot frequency
T = 1.0 / f0  # dominant period
nop = 5       # length of operator
nf = 5        # number of fields
cs = 3.464    # shear wave speed
cp = 6.0      # compresional wave speed
rho = 2.6702  # density

# extract Lame parameters
mu = rho*cs**2
Lambda = rho*cp**2-2.0*mu

dt = 0.5/np.sqrt(cp**2 + cs**2)*dx    # Time step

order = 6   # spatial order of accuracy

# Model type, available are "homogeneous", "fault_zone",
# "surface_low_velocity_zone", "random", "topography",
# "slab"
model_type = "homogeneous"

# Receiver locations
irx = np.array([30, 40, 50, 60, 70])
irz = np.array([5, 5, 5, 5, 5])
seis = np.zeros((len(irx), nt))

# Boundary reflection coefficients
r = np.array([1,0,0,0])

# Initialize pressure at different time steps and the second
# derivatives in each direction
F = np.zeros((nx, ny, nf))
Fnew = np.zeros((nx, ny, nf))

X = np.zeros((nx, ny))
Y = np.zeros((nx, ny))

p = np.zeros((nx, ny))
# Initialize velocity model
Mat = np.zeros((nx, ny, 3))

if model_type == "homogeneous":
    Mat[:,:,0] += rho
    Mat[:,:,1] += Lambda
    Mat[:,:,2] += mu

elif model_type == "random":
    pert = 0.4
    r_rho = 2.0 * (np.random.rand(nx, ny) - 0.5) * pert
    r_mu = 2.0 * (np.random.rand(nx, ny) - 0.5) * pert
    r_lambda = 2.0 * (np.random.rand(nx, ny) - 0.5) * pert

    Mat[:,:,0] += rho*(1.0 + r_rho)
    Mat[:,:,1] += Lambda*(1.0 + r_lambda)
    Mat[:,:,2] += mu*(1.0 + r_mu)

sigma = 3.0                      # standard deviation of the initial Gaussian perturbation

for i in range(0, nx):
    for j in range(0, ny):
        F[i, j,0] = np.exp(-np.log(2)*(((i-isx)*dx)**2/(sigma) + ((j-isy)*dy)**2/(sigma)))
        F[i, j,1] = np.exp(-np.log(2)*(((i-isx)*dx)**2/(sigma) + ((j-isy)*dy)**2/(sigma)))
        X[i, j] = i*dx
        Y[i,j] = j*dy

v = 0.5
p = np.sqrt(F[:,:,1]**2 + F[:,:,2]**2)
image = plt.imshow(p, interpolation='nearest', animated=True,
                   vmin=-v, vmax=+v, cmap=plt.cm.RdBu)

# Plot the receivers
for x, z in zip(irx, irz):
    plt.text(x, z, '+')

plt.text(isx, isy, 'o')
plt.colorbar()
plt.xlabel('ix')
plt.ylabel('iy')

plt.show()

# required for seismograms
ir = np.arange(len(irx))

#################################################

# Time-stepping
for it in range(nt):

    #4th order Runge-Kutta
    RK4_2D.elastic_RK4_2D(Fnew, F, Mat, nf, nx, ny, dx, dy, dt, order, r)

    # update fields
    F = Fnew

    #extract particle velocity for visualization
    p = F[:,:,0]

    # update time
    t = it*dt

    # Plot every isnap-th iteration
    if it % isnap == 0:                    # you can change the speed of the plot by increasing the plotting interval
        plt.title("time: %.2f" % t)
        image.set_data(p)
        plt.gcf().canvas.draw()
        plt.show()
        print(it)

    # Save seismograms
    seis[ir, it] = p[irz[ir], irx[ir]]

plt.ioff()
plt.figure(figsize=(12, 12))

plt.subplot(211)
ymax = seis.ravel().max()
time = np.arange(nt) * dt
for ir in range(len(seis)):
    plt.plot(time, seis[ir, :] + ymax * ir)
    plt.xlabel('Time (s)')
    plt.ylabel('Amplitude')

plt.subplot(212)
ymax = seis.ravel().max()
for ir in range(len(seis)):
    plt.plot(time, seis[ir, :] + ymax * ir)
    plt.xlabel('Time (s)')
    plt.ylabel('Amplitude')

plt.show()


# Standard Imports
import collections
import unittest
from unittest import mock

# ThirdParty Imports
import asyncio
from aiohttp import web, WSCloseCode
import json

# Internal Imports
import slack
import slack.errors as e
from tests.helpers import fake_send_req_args, mock_rtm_response

@mock.patch("slack.WebClient._send", new_callable=mock_rtm_response)
class TestRTMClientFunctional(unittest.TestCase):
    async def echo(self, ws, path):
        async for message in ws:
            await ws.send(
                json.dumps({"type": "message", "message_sent": json.loads(message)})
            )

    async def mock_server(self):
        app = web.Application()
        app["websockets"] = []
        app.router.add_get("/", self.websocket_handler)
        app.on_shutdown.append(self.on_shutdown)
        runner = web.AppRunner(app)
        await runner.setup()
        self.site = web.TCPSite(runner, "localhost", 8765)
        await self.site.start()

    async def websocket_handler(self, request):
        ws = web.WebSocketResponse()
        await ws.prepare(request)

        request.app["websockets"].append(ws)
        try:
            async for msg in ws:
                await ws.send_json({"type": "message", "message_sent": msg.json()})
        finally:
            request.app["websockets"].remove(ws)
        return ws

    async def on_shutdown(self, app):
        for ws in set(app["websockets"]):
            await ws.close(code=WSCloseCode.GOING_AWAY, message="Server shutdown")

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        task = asyncio.ensure_future(self.mock_server(), loop=self.loop)
        self.loop.run_until_complete(asyncio.wait_for(task, 0.1))
        self.client = slack.RTMClient(
            token="xoxa-1234", loop=self.loop, auto_reconnect=False
        )

    def tearDown(self):
        self.loop.run_until_complete(self.site.stop())
        slack.RTMClient._callbacks = collections.defaultdict(list)

    def test_client_auto_reconnects_if_connection_randomly_closes(
        self, mock_rtm_response
    ):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            rtm_client = payload["rtm_client"]

            if rtm_client._connection_attempts == 1:
                rtm_client._close_websocket()
            else:
                self.assertEqual(rtm_client._connection_attempts, 2)
                rtm_client.stop()

        client = slack.RTMClient(token="xoxa-1234", auto_reconnect=True, loop=self.loop)
        client.start()

    def test_client_auto_reconnects_if_an_error_is_thrown(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            rtm_client = payload["rtm_client"]

            if rtm_client._connection_attempts == 1:
                raise e.SlackApiError("Test Error", {"headers": {"Retry-After": 0.001}})
            else:
                self.assertEqual(rtm_client._connection_attempts, 2)
                rtm_client.stop()

        client = slack.RTMClient(token="xoxa-1234", auto_reconnect=True, loop=self.loop)
        client.start()

    def test_open_event_receives_expected_arguments(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            self.assertIsInstance(payload["data"], dict)
            self.assertIsInstance(payload["web_client"], slack.WebClient)
            rtm_client = payload["rtm_client"]
            self.assertIsInstance(rtm_client, slack.RTMClient)
            rtm_client.stop()

        self.client.start()

    def test_stop_closes_websocket(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            self.assertFalse(self.client._websocket.closed)

            rtm_client = payload["rtm_client"]
            rtm_client.stop()

        self.client.start()
        self.assertIsNone(self.client._websocket)

    def test_start_calls_rtm_connect_by_default(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            self.assertFalse(self.client._websocket.closed)
            rtm_client = payload["rtm_client"]
            rtm_client.stop()

        self.client.start()
        mock_rtm_response.assert_called_once_with(
            http_verb="GET",
            api_url="https://www.slack.com/api/rtm.connect",
            req_args=fake_send_req_args(),
        )

    def test_start_calls_rtm_start_when_specified(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            self.assertFalse(self.client._websocket.closed)
            rtm_client = payload["rtm_client"]
            rtm_client.stop()

        self.client.connect_method = "rtm.start"
        self.client.start()
        mock_rtm_response.assert_called_once_with(
            http_verb="GET",
            api_url="https://www.slack.com/api/rtm.start",
            req_args=fake_send_req_args(),
        )

    def test_send_over_websocket_sends_expected_message(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def echo_message(**payload):
            rtm_client = payload["rtm_client"]
            message = {
                "id": 1,
                "type": "message",
                "channel": "C024BE91L",
                "text": "Hello world",
            }
            rtm_client.send_over_websocket(payload=message)

        @slack.RTMClient.run_on(event="message")
        def check_message(**payload):
            message = {
                "id": 1,
                "type": "message",
                "channel": "C024BE91L",
                "text": "Hello world",
            }
            rtm_client = payload["rtm_client"]
            self.assertDictEqual(payload["data"]["message_sent"], message)
            rtm_client.stop()

        self.client.start()

    def test_ping_sends_expected_message(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        async def ping_message(**payload):
            rtm_client = payload["rtm_client"]
            await rtm_client.ping()

        @slack.RTMClient.run_on(event="message")
        def check_message(**payload):
            message = {"id": 1, "type": "ping"}
            rtm_client = payload["rtm_client"]
            self.assertDictEqual(payload["data"]["message_sent"], message)
            rtm_client.stop()

        self.client.start()

    def test_typing_sends_expected_message(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        async def typing_message(**payload):
            rtm_client = payload["rtm_client"]
            await rtm_client.typing(channel="C01234567")

        @slack.RTMClient.run_on(event="message")
        def check_message(**payload):
            message = {"id": 1, "type": "typing", "channel": "C01234567"}
            rtm_client = payload["rtm_client"]
            self.assertDictEqual(payload["data"]["message_sent"], message)
            rtm_client.stop()

        self.client.start()

    def test_on_error_callbacks(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def raise_an_error(**payload):
            raise e.SlackClientNotConnectedError("Testing error handling.")

        @slack.RTMClient.run_on(event="error")
        def error_callback(**payload):
            self.error_hanlding_mock(str(payload["data"]))

        self.error_hanlding_mock = mock.Mock()
        with self.assertRaises(e.SlackClientNotConnectedError):
            self.client.start()
        self.error_hanlding_mock.assert_called_once()

    def test_callback_errors_are_raised(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def raise_an_error(**payload):
            raise Exception("Testing error handling.")

        with self.assertRaises(Exception) as context:
            self.client.start()

        expected_error = "Testing error handling."
        self.assertIn(expected_error, str(context.exception))

    def test_on_close_callbacks(self, mock_rtm_response):
        @slack.RTMClient.run_on(event="open")
        def stop_on_open(**payload):
            payload["rtm_client"].stop()

        @slack.RTMClient.run_on(event="close")
        def assert_on_close(**payload):
            self.close_mock(str(payload["data"]))

        self.close_mock = mock.Mock()
        self.client.start()
        self.close_mock.assert_called_once()


n = int(input("Input the time : "))
count = 0
for i in range(n+1):  # 시
    for j in range(60):  # 분
        for k in range(60):  # 초
            if '3' in str(i) + str(j) + str(k) :
                count += 1
print(count)

# Page 113 - 114


from django.contrib.auth import get_user_model
from rest_framework.fields import SerializerMethodField, CharField
from rest_framework.serializers import ModelSerializer

from post.models import Post, Media, Comment

User = get_user_model()

class UserSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username']

class LikeSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['like_post']

class MediaSerializer(ModelSerializer):
    class Meta:
        model = Media
        fields = ['id', 'file', 'order']

class CommentListSerializer(ModelSerializer):
    username = CharField(source='user.username', read_only=True)

    class Meta:
        model = Comment
        fields = ['id', 'user', 'username', 'post', 'content']

class PostListSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = ['id', 'media_set']

class PostRetrieveSerializer(ModelSerializer):
    username = CharField(source='user.username', read_only=True)
    media_set = MediaSerializer(many=True)
    comment_set = CommentListSerializer(many=True)
    liked = SerializerMethodField(default=False)

    class Meta:
        model = Post
        fields = [
            'id',
            'username',
            'content',
            'created',
            'media_set',
            'liked',
            'like_count',
            'comment_set',
            'comment_count'
        ]

    def get_liked(self, obj):
        user_id = self.context.get('request')
        if user_id:
            if user_id.user.id in obj.like_user.values_list('id', flat=True):
                return True
            else:
                return False
        else:
            return False


import random
import math

from ivBag import IvBag
from hunger import Hunger

p_edge           = 51     #edge of square surrounding robot (in pixels)
p_transparency   = 0      #0 is totally transp., 255 totally opaque

class Patient(object):
    """description of class"""
    def __init__(self, id, x, y, init_iv, init_hunger):
        self.id = id
        self.x = x
        self.y = y
        self.n_tasks = 3
        self.rewards = [0]*4
        self.ivLevel = init_iv
        self.hunger = init_hunger
        self.vomit_time = -float("inf")
        self.dirty_time = -float("inf")
        self.vomit = False
        self.dirty = False

        self.ivPrior = -1
        self.hungerPrior = -1
        self.vomitPrior = -1
        self.dirtyPrior = -1

    def iterate(self, arg):
        time_step = arg
        self.ivLevel -= 1
        self.hunger -= 1
        if self.hunger > 50:
           if random.random() < 0.01 and self.vomit == False:
               print("Patient " ,self.id, " vomitted")
               self.vomit = True
               self.vomit_time = time_step
             #  self.dirty = True
               self.hunger -= 30
           else:
               self.vomit = False

    def updateRewards( self, arg):
        dt = arg
        self.rewards[0] = self.getIVRewardAtTime( dt )
        self.rewards[1] = self.getHungerRewardAtTime( dt )
        self.rewards[2] = self.getVomitReward()
        self.rewards[3] = self.getCleanReward()

    def completeTask( self, arg ):
        t_index = arg
        if t_index == 0:
            self.ivLevel = 100
        elif t_index == 1:
            self.hunger = 100
        elif t_index == 2:
            self.vomit = False
        elif t_index  == 3:
            self.dirty = False

    def getIVRewardAtTime( self, arg ):
        dt = arg
        if self.ivLevel-dt > 20:
            return 0.0
        else:
            return 100# - (self.ivLevel - dt)

    def getHungerRewardAtTime( self, arg ):
        dt = arg
        if self.hunger - dt > 20:
            return 0.0
        else:
            return 50# - (self.hunger - dt)

    def getVomitReward( self ):
        if self.vomit == False:
            return 0.0
        else:
            return 50

    def getCleanReward( self ):
        if self.vomit == False:
            return 0.0
        else:
            return 50

    def checkStatus(self):
        if self.ivLevel > 0:
            return 0.0
        else:
            return -1.0


# 二叉树的最近公共祖先
# https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        # 寻找从 root 分别到节点 p、q 的路径
        def findPath(root, path, node):
            if not root:
                return False
            path.append(root)
            if root == node:
                return True
            if (root.left and findPath(root.left, path, node)) or (root.right and findPath(root.right, path, node)):
                return True
            path.pop()
            return False

        path_p, path_q = [], []
        if not findPath(root, path_p, p) or not findPath(root, path_q, q):
            return -1
        i = 0
        while i < len(path_p) and i < len(path_q):  # 遍历两个路径，返回元素值不相等的前一个公共元素
            if path_p[i] != path_q[i]:
                break
            i += 1
        return path_p[i - 1]

        '''
        if not root or root == p or root == q:
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)
        if left and right:
            if (p.val == left.val and q.val == right.val) or (p.val == right.val and q.val == left.val):
                return root
        return left or right
        '''

LeetCode/0236. Lowest Common Ancestor of a Binary Tree.py


import requests
from bs4 import BeautifulSoup
import lxml

with open('C:/django_word.txt', 'r') as word_file:
    base_word = word_file.readlines()

url = 'http://dic.daum.net/search.do'

original_words = []
overlap = []
index = 0
for word in base_word:
    word = word.replace('\n','').lower()
    html_doc = requests.get(url, params={'q':word})
    html = BeautifulSoup(html_doc.text, 'lxml')
    try:
        original_word = html.find('span', {'class':'txt_emph1'}).get_text()
        mean = html.find('ul', {'class':'list_search'}).get_text().replace('\n', ' ')
        read = html.find('span', {'class':'txt_pronounce'}).get_text()
    except:
        print('%s is No search' %(word))
        continue
    if original_word not in overlap:
        original_words.append('{}')
        original_words[index] =  {'word':original_word, 'mean':mean, 'read':read}
        overlap.append(original_word)
        print(original_word)
        index += 1
    else:
        print('%s is Checking' %(original_word))


import getopt
import json
import re
import sys

def main(args):
    opts, args = getopt.getopt(args, "as:c:")

    all_flag = False
    connector = None
    storage_gateway = None

    for o in opts:
        if o[0] == "-a":
            all_flag = True
        elif o[0] == "-c":
            connector = o[1]
        elif o[0] == "-s":
            storage_gateway = o[1]

    input_data = json.load(sys.stdin)

    if input_data.get("DATA_TYPE") != "identity_mapping_input#1.0.0":
        sys.exit("Invalid input DATA_TYPE")

    output_doc = {"DATA_TYPE": "identity_mapping_output#1.0.0", "result": []}

    mappings = read_mapfile(connector, storage_gateway)

    for i in input_data.get("identities"):
        res = map_identity(i, mappings, all_flag)

        if res:
            output_doc["result"].extend(res)
            if not all_flag:
                break

    json.dump(output_doc, fp=sys.stdout)
    sys.exit(0)

def read_mapfile(connector, storage_gateway):
    # Parses a simple mapping file containing entries
    # "identity_username" local_username[,local_username...]
    #mapfile = "/etc/globus/gridmap-{}".format(storage_gateway)
    if storage_gateway:
        mapfile = "/etc/grid-security/xsede-oauth-mapfile-{}".format(storage_gateway)
    else:
        mapfile = "/etc/grid-security/xsede-oauth-mapfile"
    mappings = []

    mapre = re.compile(r'(\S*)\s+(\S*)$')
    with open(mapfile, "r") as f:
        for line in f:
            m = mapre.match(line.strip())
            if m:
                identity_username = m.group(1)
                local_username = m.group(2)
                mapping = (identity_username, local_username)
                mappings.append(mapping)
    return mappings

def map_identity(identity, mappings, all_flag):
    res = []
    for m in mappings:
        if m[0] == identity["username"]:
            res.append({"id": identity["id"], "output": m[1]})
            if not all_flag:
                break
    return res

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))


#!/usr/bin/env python
import numpy as np
from mpmath import fp,mp
from scipy.integrate import quad,fixed_quad, simps, dblquad
from tools.config import conf
import obslib.idis.aux
#from obslib.lh.theory import LH

class STFUNCS(obslib.idis.aux.AUX):
  '''
  DIS structure functions for pion. It is a simplified
  copy from the nucleon DIS stfuncs. The main change is
  the call to pion pdfs.
  '''

  def __init__(self):

    self.mellin=conf['mellin-pion']
    #self.mellin=conf['mellin']
    if conf['order']=='LO': self.order=0
    if conf['order']=='NLO': self.order=1
    self.setup()

  # twist level unpolarized structure functions in Mellin space

  def get_T2CFX(self,stf,nucleon,Q2,evolve=True):
    """
    CF(2,L,3) = F2/x,FL/x,F3
    """
    if evolve: conf['pdf-pion'].evolve(Q2)
    #if evolve: conf['pdf'].evolve(Q2)

    g =np.copy(conf['pdf-pion'].storage[Q2]['g'])     # gluon[N] were N mellin contour
    #g =conf['pdf'].storage[Q2]['g']     # gluon[N] were N mellin contour
    Nf=conf['alphaS'].get_Nf(Q2) # num of active flav
    a=conf['alphaS'].get_a(Q2)   # a=alphaS/4pi

    if stf=='F2':
      CQ = self.C2Q[0] + a*self.order*self.C2Q[1]  # hard kernels
      CG = self.C2G[0] + a*self.order*self.C2G[1]
      q=np.copy(conf['pdf-pion'].storage[Q2]['qp'])    # (q + qb)[N]
      #q=np.copy(conf['pdf'].storage[Q2]['qp'])    # (q + qb)[N]
      aX='ap'

    elif stf=='FL':
      CQ = a*self.order*self.CLQ[1]
      CG = a*self.order*self.CLG[1]
      q=np.copy(conf['pdf-pion'].storage[Q2]['qp'])    # (q + qb)[N]
      aX='ap'

    elif stf=='F3':
      CQ = self.C3Q[0] + a*self.order*self.C3Q[1]
      CG = 0
      q=np.copy(conf['pdf-pion'].storage[Q2]['qm'])    # (q - qb)[N]
      aX='am'

    if nucleon=="neutron":
      qup=np.copy(q[1])
      qdn=np.copy(q[2])
      q[1]=qdn
      q[2]=qup

    FX  = np.zeros(self.mellin.N.size,dtype=complex) # initialize FX[N] array

    for i in range(1,Nf+1):
      aXval = self.get_aX(aX,i,Q2)
      FX+=aXval*(CQ*q[i] + 2*CG*g)

    return FX

  # unpolarized functions

  def get_FXN(self,x,Q2,stf='F2',twist=2,nucleon='proton',tmc=True,precalc=False,evolve=True):

    if   stf=='F2': FX= x*self.get_T2CFX('F2',nucleon,Q2,evolve)
    elif stf=='FL': FX= x*self.get_T2CFX('FL',nucleon,Q2,evolve)
    elif stf=='F3': FX=   self.get_T2CFX('F3',nucleon,Q2,evolve)
    FX*=x**(-self.mellin.N)

    return self.mellin.invert(1,FX) # here x=1 since the factor x**(-N) is included in the moments

# we define global parameters here
# for easy connection with parman.py

#L_p2pin=0.68  # UV regulator

#initial=True  # use this flag to cp the params to conf for the initial eval
              # see parman.set_p_to_pi_n_params to see how it works

#F={}          # dictionary to store f(y) values

class LN:

  def __init__(self):
    aux=conf['aux']

    mN     = aux.M
    mN2    = aux.M2
    mPi2   = aux.Mpi2
    mDel   = aux.Mdelta
    mDel2  = aux.Mdelta2
    Mbar2  = (mDel+mN)**2
    Del2   = (mDel-mN)**2
    theta  = lambda x: 0.5*(np.sign(x)+1)
    self.mN2=mN2
    self.mDel2=mDel2

    #g_PiNN = np.sqrt(14.5*4*np.pi)
    g_PiNN = np.sqrt(13.7*4*np.pi)
    gg     = g_PiNN**2/(16*np.pi**2)
    sPiN   = lambda kT2,y:(kT2+mPi2)/y+(kT2+mN2)/(1-y)
    pointN = lambda y,t,kT2: 2*gg*y/(1-y)*(-t)/(t-mPi2)**2

    g_PiND = 11.8  # in units of 1/GeV
    C      = g_PiND**2/(16*np.pi**2 * 18*mDel2)
    sPiD   = lambda kT2,y: (kT2+mPi2)/y + (kT2+mDel2)/(1-y)
    pointD = lambda y,t,kT2: -2*C*y/(1-y)*(Mbar2-mPi2)\
                             * ( (Mbar2-mPi2)*(Del2-mPi2)-(3*(Del2-mPi2)+4*mN*mDel)*(t-mPi2) )\
                             / (t-mPi2)**2

    self.L_p2pin=0.68

    self.model=conf['SPLFUNC model']
    if   self.model=='kT2 cutoff':   self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) #* theta(L_p2pin**2-kT2)
    elif self.model=='IMF exp':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * np.exp(2*(mN2-sPiN(kT2,y))/self.L_p2pin**2)
    elif self.model=='cov exp':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='cov mon':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**2
    elif self.model=='cov dip':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**4
    elif self.model=='Pauli-Villars':self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * (1 - (t-mPi2)**2/(t-self.L_p2pin**2)**2)
    elif self.model=='Regge cov exp':self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * y**(-2*t) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='Bishari':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * y**(-2*t)

    # factor -2 multiplying "C" from isospin:  f_{pi+ Del0} - f_{pi- Del++} = -2 f_{pi+ Del0} => -2 f_Del^(on)
    if   self.model=='kT2 cutoff':   self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) #* theta(L_p2pin**2-kT2)
    elif self.model=='IMF exp':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * np.exp(2*(mN2-sPiD(kT2,y))/self.L_p2pin**2)
    elif self.model=='cov exp':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='cov mon':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**2
    elif self.model=='Pauli-Villars':self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * (1 - (t-mPi2)**2/(t-self.L_p2pin**2)**2)
    elif self.model=='Regge cov exp':self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * y**(-2*t)*np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='Bishari':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * y**(-2*t)

    self.N=1
    self.a0=1
    self.a1=1
    LQCD2=0.4**2
    Q20=1.0
    self.eta=lambda Q2: np.log(np.log(Q2/LQCD2)/np.log(Q20/LQCD2))

    # storage for F2p
    self.storage={}

  def integrand(self,y,kT2):
    tN = -kT2/(1-y)-y**2*self.mN2/(1-y)
    tD = -kT2/(1-y)+y*self.mN2-y*self.mDel2/(1-y)
    return self.tgrandN(y,tN,kT2)+self.tgrandD(y,tD,kT2)

  def integrandN(self,y,kT2):
    tN = -kT2/(1-y)-y**2*self.mN2/(1-y)
    return self.tgrandN(y,tN,kT2)

  def integrandD(self,y,kT2):
    tD = -kT2/(1-y)+y*self.mN2-y*self.mDel2/(1-y)
    return self.tgrandD(y,tD,kT2)

  def get_f(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrand(y,kT2),0,kT2max)[0]

  def get_fN(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrandN(y,kT2),0,kT2max)[0]

  def get_fD(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrandD(y,kT2),0,kT2max)[0]

  def get_kinfact(self,Q2,x,ye):
      eweak=conf['eweak']
      aEM=eweak.get_alpha(Q2)
      return 4.0*np.pi*aEM**2/x/Q2**2*(1.0-ye+ye**2/2.0)

  def get_dsigdxdQ2dxL(self,x,xpi,y,Q2,kT2max,ye):
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      f=self.get_fN(y,kT2max)
      K=self.get_kinfact(Q2,x,ye)
      return F2pi*f*K

  def get_dsigdxdQ2dxLdt(self,x,Q2,xL,t,s):
      xpi=x/(1-xL)
      if xpi>1: return 0
      ye=Q2/x/s
      if ye>1: return 0
      K=self.get_kinfact(Q2,x,ye)
      kT2=-(t*xL+(1.0-xL)**2*self.mN2)
      kT2max=0.005*xL*135
      if kT2>kT2max: return 0
      f=self.tgrandN(1.0-xL,t,kT2)
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      Jac=xL
      return F2pi*f*K*Jac

  def get_dsigdxdQ2dxLdkT2(self,x,Q2,xL,kT2,s):
      xpi=x/(1.-xL)
      if xpi>1: return 0
      ye=Q2/x/s
      K=self.get_kinfact(Q2,x,ye)
      f=self.integrandN(1.0-xL,kT2)
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      return F2pi*f*K

  def get_dsigdxdQ2dxLKaon(self,x,xpi,y,Q2,kT2max,ye):
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      #f=self.get_fN(y,kT2max)
      fpikT=lambda kT2: self.integrandN(y,kT2)
      #lh=LH()
      lh=conf['lh']
      xL=1.0-np.array(y)
      fKkT=lambda kT2: lh.get_fLkT2(xL,kT2)
      frat=quad(lambda kT2: fKkT(kT2)/fpikT(kT2),0,kT2max)[0]
      K=self.get_kinfact(Q2,x,ye)
      return F2pi*frat*K

  def get_F2LN(self,xpi,y,Q2,kT2max):
    F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
    #F2pi=self.N*xpi**(self.a0+self.a1*self.eta(Q2))*(1-xpi)
    if conf['ln mode']=='model':
      f=self.get_fN(y,kT2max)
    elif conf['ln mode']=='piecewise':
      if y not in F: F[y]=self.get_fN(y,kT2max)
      f=F[y]
    return F2pi * f

  def get_R(self,x,xpi,deltaxL,y,Q2,kT2max):
    F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
    key='%f,%f'%(x,Q2)
    if key not in self.storage:
      #self.storage[key]=conf['stfuncs'].get_FXN(x,Q2,stf='F2',tmc=False)
      self.storage[key]=conf['idis stfuncs'].get_FXN(x,Q2,stf='F2',tmc=False)
    F2p= self.storage[key]
    if conf['ln mode']=='model':
      f=self.get_fN(y,kT2max)
    elif conf['ln mode']=='piecewise':
      if y not in F: F[y]=self.get_f(y,kT2max)
      f=F[y]
    return deltaxL * F2pi / F2p * f

  # moments of spliting functions

  def get_fmom(self):
    return quad(lambda y: self.get_f(y,np.inf),0,1)[0]

  def get_fNmom(self):
    return quad(lambda y: self.get_fN(y,np.inf),0,1)[0]

  def get_fDmom(self):
    return quad(lambda y: self.get_fD(y,np.inf),0,1)[0]

  #-> not sure what is the purpose of the next 2 functions

  def get_multiplicities(L_p2pin):
    L_p2pin = L
    return (3.0/2.0)*quad(lambda y: self.get_f(y,np.inf),0,1)[0]

  def get_f_tdis(self,y,kT2min,kT2max):
    return quad(lambda kT2: self.integrand(y,kT2),kT2min,kT2max)[0]

  def get_tmom(self,model,L,ymax,kT2max):
    return quad(lambda y: self.get_f(y,model,kT2max(y)),0,ymax)[0]

  # db-ub

  def get_qv(self,x,Q2):
    xqv = conf['pdf-pion'].get_xF(x,Q2,'ub')-conf['pdf-pion'].get_xF(x,Q2,'u')
    return xqv/x

  def get_dbub(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_f(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  def get_dbubN(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_fN(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  def get_dbubD(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_fD(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  # get, set state

  def get_state(self):
      return (self.L_p2pin)

  def set_state(self,state):
      self.L_p2pin=state

if __name__=='__main__':

  conf['Q20'] = 1.0
  conf['alphaSmode']='backward'
  conf['dglap mode']='truncated'
  conf['order']='NLO'
  conf['scheme']='ZMVFS'
  conf['ln mode']='model'
  conf['SPLFUNC model']='cov exp'

  conf['aux']=qcdlib.aux.AUX()
  conf['eweak']=qcdlib.eweak.EWEAK()
  conf['alphaS']=qcdlib.alphaS.ALPHAS()
  conf['mellin-ext']=qcdlib.mellin.MELLIN(extended=True)
  conf['mellin']=qcdlib.mellin.MELLIN()
  conf['pdf-pion']=qcdlib.pdfpion0.PDF()
  conf['pion-stfuncs']=STFUNCS()
  conf['pdf']=qcdlib.pdf0.PDF()
  conf['stfuncs']=obslib.dis.stfuncs.STFUNCS()
  ln=LN()

  x=0.000224
  Q2=7.3
  y=0.635
  xpi=x/y
  kT2max=0.4
  deltaxL=0.2
  print(ln.get_F2LN(xpi,y,Q2,kT2max))
  print(ln.get_R(x,xpi,deltaxL,y,Q2,kT2max))
  print(ln.get_dbub(x,Q2))


""" This program takes links of 2016,2017 and 2018 GSoC organization pages and
ouputs the frequency of each organization to a text file """

from bs4 import BeautifulSoup
import requests

years=['2018','2017','2016']
page_links =['https://summerofcode.withgoogle.com/archive/'+years[0]+'/organizations/',
			 'https://summerofcode.withgoogle.com/archive/'+years[1]+'/organizations/',
			 'https://summerofcode.withgoogle.com/archive/'+years[2]+'/organizations/']

def get_organizations(page_link):

	# fetch the content from url
	page_response = requests.get(page_link, timeout=5)
	# parse html
	page_content = BeautifulSoup(page_response.content, "html.parser")

	organizations = page_content.find_all(class_='organization-card__name font-black-54')

	for i in range(len(organizations)):
	   organizations[i]=organizations[i].text

	#print(organizations)
	return(organizations)

temp_organizations=[]
for i in range(len(page_links)):

	temp_organizations.append(get_organizations(page_links[i]))

all_organizations=[]
for i in temp_organizations:
	for j in i:
		all_organizations.append(j)

print(len(all_organizations))

unique_organizations=list(set(all_organizations))
print(len(unique_organizations))

organizations_count={}

for i in unique_organizations:
	organizations_count[i]=0

	for j in all_organizations:

		if i==j:
			organizations_count[i]+=1

organizations_count = dict(sorted(organizations_count.items(), key=lambda x: (-x[1], x[0])))
print(organizations_count)

with open('gsoc_orgs_freq.txt','w+') as f:

	for org in organizations_count.keys():
		f.write(org+'--'+str(organizations_count[org]))
		f.write('\n')

f.close()


from sys import stdin
import heapq
input = stdin.readline
MIS = lambda: map(int, input().rstrip().split())
MIRS = lambda: map(int, stdin.readlines())

arr = []

data = [-x for x in MIRS()]
for d in data[1:]:
    if not d:
        print(-heapq.heappop(arr) if arr else 0)
    else:
        heapq.heappush(arr, d)


#!/usr/bin/env python
# encoding: utf-8

"""
@Author: yangwenhao
@Contact: 874681044@qq.com
@Software: PyCharm
@File: compute_plda.py
@Time: 2020/12/22 21:28
@Overview: modified from "kaldi/src/ivector/ivector-compute-plda.cc"
"""

import argparse
import os

import kaldi_io
import numpy as np
from kaldi_io.kaldi_io import _read_vec_flt_binary, UnknownVectorHeader
from tqdm import tqdm

from Score.Plda.plda import PldaEstimationConfig, PldaStats, PldaEstimator, PLDA, write_vec_binary

# Training settings
parser = argparse.ArgumentParser(description='Kalid PLDA compute')
# Data options
parser.add_argument('--spk2utt', type=str, required=True, help='path to spk2utt')
parser.add_argument('--ivector-scp', type=str, required=True, help='path to ivector.scp')
parser.add_argument('--num-em-iters', type=int, default=10, help='path to ivector.scp')
parser.add_argument('--plda-file', type=str, required=True, help='path to plda directory')
parser.add_argument('--subtract-global-mean', action='store_false', default=True, help='path to plda directory')
parser.add_argument('--mean-vec', type=str, default='', help='path to plda directory')
parser.add_argument('--transform-vec', type=str, default='', help='path to plda directory')
parser.add_argument('--normalize-length', action='store_false', default=True, help='path to plda directory')
parser.add_argument('--scaleup', action='store_false', default=True, help='path to plda directory')
parser.add_argument('--vector-format', type=str, default='kaldi', help='path to plda directory')

args = parser.parse_args()

# kaldi command line:
# ivector-compute-plda [options] <spk2utt-rspecifier> <ivector-rspecifier> <plda-out>

if __name__ == '__main__':
    # spk2utt_path = args.spk2utt
    # ivector_scp = args.ivector_scp
    # plda_dir = args.plda_dir
    if args.vector_format == 'kaldi':
        vec_loader = kaldi_io.read_vec_flt
    elif args.vector_format == 'npy':
        vec_loader = np.load
    else:
        raise ValueError(args.vector_format)

    plda_config = PldaEstimationConfig(num_em_iters=args.num_em_iters)

    num_spk_done = 0
    num_spk_err = 0
    num_utt_done = 0
    num_utt_err = 0
    vec_dim = -1

    utt2vec = {}
    with open(args.ivector_scp, 'r') as f:
        for l in f.readlines():
            uid, vec_path = l.split()
            utt2vec[uid] = vec_path
            # spks[spk_utts[0]] = spk_utts[1:]
            if vec_dim == -1:
                vec_dim = vec_loader(os.path.join('Score/data', vec_path)).shape[-1] #Todo: change the dir
                # vec_dim = vec_loader(vec_path).shape[-1]  # Todo: change the dir

    spks = {}
    if args.subtract_global_mean:
        if args.mean_vec != "" and os.path.exists(args.mean_vec):
            with open(args.mean_vec, 'rb') as f:
                try:
                    global_mean = _read_vec_flt_binary(f)
                except UnknownVectorHeader as u:
                    mean_vec = []
                    vec_str = f.readline()
                    for v in vec_str.split():
                        try:
                            mean_vec.append(float(v))
                        except:
                            pass
                    global_mean = np.array(mean_vec)
        else:
            global_mean = []
            with open(args.spk2utt, 'r') as f:
                pbar = tqdm(f.readlines())
                for l in pbar:
                    spk_utts = l.split()
                    for uid in spk_utts[1:]:
                        try:
                            vec_path = os.path.join('Score/data', utt2vec[uid]) #Todo: change the dir
                            # vec_path = utt2vec[uid]  # Todo: change the dir
                            this_vec = vec_loader(vec_path)
                            global_mean.append(this_vec)
                        except Exception as e:
                            pass
            global_mean = np.array(global_mean).mean(axis=0)

            assert args.mean_vec != "", print("mean vector path should be assigned!")
            if not os.path.exists(os.path.dirname(args.mean_vec)):
                os.makedirs(os.path.dirname(args.mean_vec))
            with open(args.mean_vec, 'wb') as f:
                write_vec_binary(global_mean)
                print("Saving mean vector to: " % args.mean_vec)

    transform_vec = None
    if os.path.exists(args.transform_vec):
        try:
            with open(args.transform_vec, 'rb') as f:
                transform_vec = kaldi_io.read_mat(f)
                if transform_vec.shape[-1] != vec_dim:
                    transform_vec = transform_vec[:, :vec_dim]
                    vec_dim = transform_vec.shape[0]
                print("Transformed dim will be %d" % vec_dim)
        except Exception as e:
            print("Skippinng transform ... Transform vector loading error: \n%s" % str(e))

    tot_ratio = 0.0
    tot_ratio2 = 0.0

    plda_stats = PldaStats(dim=vec_dim)  # 记录说话人数目及其egs数目、维度类中心等
    with open(args.spk2utt, 'r') as f:
        spk_err = []
        for l in f.readlines():
            spk_utts = l.split()
            this_vecs = []
            for uid in spk_utts[1:]:
                try:
                    vec_path = os.path.join('Score/data', utt2vec[uid]) #Todo: change the dir
                    # vec_path = utt2vec[uid]  # Todo: change the dir
                    this_vec = vec_loader(vec_path)
                    this_vecs.append(this_vec)
                    num_utt_done += 1
                except Exception as e:
                    num_utt_err += 1

            ivector_mat = np.array(this_vecs)
            if args.subtract_global_mean:
                ivector_mat -= global_mean

            if transform_vec.all() != None:
                ivector_mat = np.matmul(ivector_mat, transform_vec.transpose())

            if args.normalize_length:
                norm = np.linalg.norm(ivector_mat, axis=1).reshape(-1, 1)
                ratio = norm / np.sqrt(ivector_mat.shape[-1]) if args.scaleup else norm

                assert ratio.all()>0.0

                ivector_mat /= ratio
                tot_ratio += ratio.sum()
                tot_ratio2 += (ratio * ratio).sum()

            weight = 1.0
            if len(ivector_mat) == 0:
                spk_err.append(spk_utts[0])
                num_spk_err += 1
            else:
                plda_stats.AddSamples(weight, ivector_mat)
                # spks[spk_utts[0]] = spk_utts[1:]
                num_spk_done += 1
        if len(spk_err) > 0:
            print("Not producing output for speaker: \n %s \nsince no utterances had iVectors" % spk_err)

    # if (num_done != 0) {
    #       BaseFloat avg_ratio = tot_ratio / num_done,
    #           ratio_stddev = sqrt(tot_ratio2 / num_done - avg_ratio * avg_ratio);
    #       KALDI_LOG << "Average ratio of iVector to expected length was "
    #                 << avg_ratio << ", standard deviation was " << ratio_stddev;
    #     }
    if args.normalize_length:
        avg_ratio = tot_ratio / num_utt_done
        ratio_stddev = np.sqrt(tot_ratio2 / num_utt_done - avg_ratio * avg_ratio)
        print("Average ratio of iVector to expected length was ", avg_ratio, ", standard deviation was ", ratio_stddev)

    plda_stats.sort()  # spk信息类按照egs排序
    plda_estimator = PldaEstimator(plda_stats)  # 使用统计量类new一个训练估计参数的类
    plda = PLDA()
    plda_estimator.Estimate(plda_config, plda)
    plda.Write(args.plda_file)
    print('Writing plda model files in: %s' % args.plda_file)

    # WriteKaldiObject(plda, args.plda_dir, binary)
    # return num_spk_done!=0 &, 1

"""
ivector-compute-plda ark:/home/yangwenhao/local/project/lstm_speaker_verification/data/vox1/pyfb_de/dev_fb24_mel/spk2utt \
      "ark:ivector-subtract-global-mean scp:exp/ivectors_train_fb24_mel/ivector.scp ark:- | transform-vec exp/ivectors_train_fb24_mel/transform.mat ark:- ark:- | ivector-normalize-length ark:-  ark:- |" \
      exp/ivectors_train_fb24_mel/plda

"""