from kryptlib.ElGamalImpl import ElGamal
from charm.toolbox.pairinggroup import PairingGroup,G1,G2, ZR
import unittest
from kryptlib.AGHOBlind import AGHOBlind
class ZKPTest(unittest.TestCase):
    def testZKPG1(self):
        groupObj=PairingGroup('BN254')
        params=1
        el = ElGamal(params)
        (pk, sk)=el.keygen()
        (ch,r)=self.ZKP_exponentG1(pk['g'],sk['x'], pk['vk'])
        isTrue=self.ZKP_exponentG1_verify(ch,r, pk['vk'],pk['g'])
        print("Single exponent ZKP proof in G1", isTrue)

    def testZKPG2(self):
        groupObj=PairingGroup('BN254')
        params=1
        el = ElGamal(params)
        agho = AGHOBlind(el)
        h=groupObj.random(G2)
        (sk, pk)=agho.keygen(h)
        (ch,r)=self.ZKP_exponentG2(h,sk['v'], pk['V'])
        isTrue=self.ZKP_exponentG2_verify(ch,r, pk['V'],h)
        print("Single exponent ZKP proof in G2", isTrue)

    def testZKPGT(self):
        groupObj=PairingGroup('BN254')
        params=1
        el = ElGamal(params)
        agho = AGHOBlind(el)
        h=groupObj.random(G2)
        g=groupObj.random(G1)
        (sk, pk)=agho.keygen(h)
        (ch,r)=self.ZKP_exponentGT(h,sk['v'], pk['V'],g)
        isTrue=self.ZKP_exponentGT_verify(ch,r, pk['V'],h,g)
        print("Single exponent ZKP proof in GT", isTrue)

    def ZKP_exponentG1(self,g, sk, pk):
        group=PairingGroup('BN254')
        nu=group.random(ZR)
        N=g**nu
        ch=group.hash(str(N).encode('utf-8'),ZR)
        r=ch*sk+nu
        return (ch,r)

    def ZKP_exponentG1_verify(self,ch,r,pk,g):
        group=PairingGroup('BN254')
        N=(g**r)*(pk**(-ch))
        return ch==group.hash(str(N).encode('utf-8'),ZR)

    def ZKP_exponentG2(self,h, sk, pk):
        group=PairingGroup('BN254')
        nu=group.random(ZR)
        N=h**nu
        ch=group.hash(group.serialize(N),ZR)
        r=ch*sk+nu
        return (ch,r)

    def ZKP_exponentG2_verify(self,ch,r,pk,h):
        group=PairingGroup('BN254')
        N=(h**r)*(pk**(-ch))
        return ch==group.hash(group.serialize(N),ZR)

    def ZKP_exponentGT(self,h, sk, pk,g):
        group=PairingGroup('BN254')
        nu=group.random(ZR)
        N=h**nu
        N2=group.pair_prod(g,N)
        ch=group.hash(group.serialize(N2),ZR)
        r=ch*sk+nu
        return (ch,r)

    def ZKP_exponentGT_verify(self,ch,r,pk,h,g):
        group=PairingGroup('BN254')
        N=group.pair_prod(g,(h**r)*(pk**(-ch)))
        return ch==group.hash(group.serialize(N),ZR)

class PairingTest(unittest.TestCase):
    def testPairingBilinearity1(self):
        groupObj=PairingGroup('BN254')
        isLin1=True
        for i in range(0,10):
            g1=groupObj.random(G1)
            g2=groupObj.random(G1)
            h1=groupObj.random(G2)
            isLin1=isLin1 and (groupObj.pair_prod(g1*g2, h1)==(groupObj.pair_prod(g1, h1)*groupObj.pair_prod(g2, h1)))
        print("Linearity in 1st Argument:", isLin1)

    def testPairingBilinearity2(self):
        groupObj=PairingGroup('BN254')
        isLin2=True
        for i in range(0,10):
            g1=groupObj.random(G1)
            h1=groupObj.random(G2)
            h2=groupObj.random(G2)
            isLin2=isLin2 and(groupObj.pair_prod(g1, h1*h2)==(groupObj.pair_prod(g1, h1)*groupObj.pair_prod(g1, h2)))
        print("Linearity in 2nd Argument:", isLin2)

    def testScalarMultiplication1(self):
        groupObj=PairingGroup('BN254')
        isScalar1=True
        for i in range(0,10):
            k=groupObj.random(ZR)
            g=groupObj.random(G1)
            h=groupObj.random(G2)
            isScalar1=isScalar1 and (groupObj.pair_prod(g**k, h)==groupObj.pair_prod(g, h)**k)
        print("Scalar multiplication in 1st Argument:", isScalar1)

    def testScalarMultiplication2(self):
        groupObj=PairingGroup('BN254')
        isScalar2=True
        for i in range(0,10):
            k=groupObj.random(ZR)
            g=groupObj.random(G1)
            h=groupObj.random(G2)
            isScalar2=isScalar2 and (groupObj.pair_prod(g, h**k)==groupObj.pair_prod(g, h)**k)
        print("Scalar multiplication in 2nd Argument:", isScalar2)

    def testScalarMultiplication3(self):
        groupObj=PairingGroup('BN254')
        isScalarBoth=True
        for i in range(0,10):
            k=groupObj.random(ZR)
            g=groupObj.random(G1)
            h=groupObj.random(G2)
            isScalarBoth=isScalarBoth and (groupObj.pair_prod(g, h**k)==groupObj.pair_prod(g**k, h))
        print("Scalar multiplication in both Arguments:", isScalarBoth)

    def testExponentiation1(self):
        groupObj=PairingGroup('BN254')
        expoG1_1=True
        for i in range(0,10):
            k1=groupObj.random(ZR)
            k2=groupObj.random(ZR)
            g=groupObj.random(G1)
            expoG1_1=expoG1_1 and (g**(k1+k2)==(g**k1)*(g**k2))
        print("Exponentiation equation (g**(k1+k2)==(g**k1)*(g**k2)) in G1", expoG1_1)

    def testExponentiation2(self):
        groupObj=PairingGroup('BN254')
        expoG1_2=True
        for i in range(0,10):
            k1=groupObj.random(ZR)
            k2=groupObj.random(ZR)
            g=groupObj.random(G1)
            expoG1_2=expoG1_2 and (g**(k1*k2)==(g**k1)**k2)
        print("Exponentiation equation (g**(k1*k2)==(g**k1)**k2) in G1", expoG1_2)

    def testExponentiation3(self):
        groupObj=PairingGroup('BN254')
        expoG2_1=True
        for i in range(0,10):
            k1=groupObj.random(ZR)
            k2=groupObj.random(ZR)
            h=groupObj.random(G2)
            expoG2_1=expoG2_1 and (h**(k1+k2)==(h**k1)*(h**k2))
        print("Exponentiation equation (h**(k1+k2)==(h**k1)*(h**k2)) in G2", expoG2_1)

    def testExponentiation4(self):
        groupObj=PairingGroup('BN254')
        expoG2_2=True
        for i in range(0,10):
            k1=groupObj.random(ZR)
            k2=groupObj.random(ZR)
            h=groupObj.random(G2)
            expoG2_2=expoG2_2 and (h**(k1*k2)==(h**k1)**k2)
        print("Exponentiation equation (h**(k1*k2)==(h**k1)**k2)  in G2", expoG2_2)

    def testSerializationG1(self):
        groupObj=PairingGroup('BN254')
        for i in range(0,10):
            g=groupObj.random(G1)
            l=groupObj.serialize(g).decode('utf-8')
            m=groupObj.deserialize(l.encode('utf-8'))
        print("Serialization Test Result G1", m==g)

    def testSerializationG2(self):
        groupObj=PairingGroup('BN254')
        for i in range(0,10):
            g=groupObj.random(G2)
            l=groupObj.serialize(g).decode('utf-8')
            m=groupObj.deserialize(l.encode('utf-8'))
        print("Serialization Test Result G2", m==g)

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


from collections import deque

N = int(input())
adj = [[] for _ in range(N + 1)]
visit = [[False] * (N + 1) for _ in range(N + 1)]
dist = [[0] * (N + 1) for _ in range(N + 1)]
minScore = 1e9
answer = []

while True:
    a, b = map(int, input().split())
    if a == -1 and b == -1:
        break
    adj[a].append(b)
    adj[b].append(a)

for i in range(1, N + 1):
    queue = deque()
    queue.append(i)
    visit[i][i] = True
    cnt = 0

    while queue:
        size = len(queue)
        for _ in range(size):
            now = queue.popleft()
            dist[i][now] = cnt

            for next in adj[now]:
                if visit[i][next]:
                    continue
                visit[i][next] = True
                queue.append(next)
        cnt += 1

for i in range(1, N + 1):
    minScore = min(minScore, max(dist[i]))

for i in range(1, N + 1):
    if max(dist[i]) == minScore:
        answer.append(i)

print(minScore, len(answer))
print(*answer)


import os
import re

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

def list_issue_numbers(line, keyword):
    issue_numbers = []
    word_list = line.split()
    for i, word in enumerate(word_list):
        if word == keyword:
            if len(word_list) <= i + 1:
                break
            issue = Issue(word_list[i + 1])
            if issue.number:
                issue_numbers.append(issue.number)

    return issue_numbers

def owner(repo_slash_owner):
    return repo_slash_owner.split('/')[0]

def repo(repo_slash_owner):
    return repo_slash_owner.split('/')[1]

class Issue:
    pattern = r'^#([0-9]*)|https:\/\/github\.com\/.*\/.*\/issues\/([0-9]*)'

    def __init__(self, word):
        self.word = word

    @property
    def number(self):
        match = re.search(self.pattern, self.word)

        if match is None:
            return

        for group in match.groups():
            if group:
                return group


__author__ = 'roman'

from bottle import request, Bottle, abort
from bottle import route, run, template, static_file
import json
import jsonpickle
from client import Client
from serverToClientMessage import ServerToClientMessage
from taskmanager import *
from serverPageToServerMessage import *
from serverToServerPageMessage import *
from clientToServerMessage import *
from seleniumTest import *
from multiprocessing import Process

app = Bottle()
allClients = []
serverSocket = None
taskList = []

def sendAddClientMessageToServerPage(client):
    """
        Send new message to server page
        client: icludes all client info
    """
    newClientMessage = ServerToServerPageMessage(ServerToServerPageMessage.NEW_CLIENT_MSG) # add client to server page
    newClientMessage.addClient(client)
    if (serverSocket):
        serverSocket.send(jsonpickle.encode(newClientMessage))

def sendLeaveClientMessageToServerPage(client):
    """
        Sends leave message to the server page
    """
    deletedClientMessage = ServerToServerPageMessage(ServerToServerPageMessage.CLIENT_LEAVED_MSG) # delete client from server page
    deletedClientMessage.deletedClient(client)
    if (serverSocket):
        serverSocket.send(jsonpickle.encode(deletedClientMessage))

def sendWarningMessageToServerPage(message):
    """ Sends warning message to server page """
    warningMessage = ServerToServerPageMessage(ServerToServerPageMessage.WARNING_MSG) # add warning to server page
    warningMessage.warning(message)
    if (serverSocket):
        serverSocket.send(jsonpickle.encode(warningMessage))

def sendClientStatusMessageToServerPage(clientId, status, substringFound, time=None):
    """ Sends client status to server page """
    message = ServerToServerPageMessage(ServerToServerPageMessage.CLIENT_STATUS_MSG) # add client status to server page
    message.clientStatus(clientId, status)
    message.setSubstringFound(substringFound)
    if time != None:
        message.setTime(time)
    if (serverSocket):
        serverSocket.send(jsonpickle.encode(message))

def sendMsgToClient(msg, socket):
    if socket:
        socket.send(jsonpickle.encode(msg)) #send client info to server page

def giveOutTasks(allClients, taskList):
    """ sends tasks to free clients """
    for task in taskList: # set performers to every task
        if task.getClientPerformer() == None:
            for client in allClients:
                if client.isPerformer == False:
                    client.isPerformer = True
                    task.setClientPerformer(client)
                    print ("performer for task is ", client.getId())
                    break
    for task in taskList: # give out Tasks
        client = task.getClientPerformer()
        if (client != None) and (client.busy == False):
            #print("performer for task is client with id ", client.getId())
            msg = ServerToClientMessage(ServerToClientMessage.TASK_MSG, client.getId())
            msg.setTask(task)
            print("message for perfrmer: ", msg)
            if client.getSocket():
                client.getSocket().send(jsonpickle.encode(msg))
                client.busy = True

def removeTaskFromTaskList(performerId):
    """ Removes task with given performer id """
    for task in taskList:
        client = task.getClientPerformer()
        if client != None:
            if client.getId() == performerId:
                client.busy = False
                client.isPerformer = False
                taskList.remove(task)
                print ("Task done by client", performerId)
                print ("Task removed from taskList")

def removeTaskPerformer(cl):
    """ is called when working client suddenly closes """
    for task in taskList:
        client = task.getClientPerformer()
        if (client != None) and (cl != None):
            if client.getId() == cl.getId():
                task.setClientPerformer(None)

def generateClientId():
    """ Generates unique client id """
    currentClientId = 0
    if allClients.__len__() <= 0: #form new id
        currentClientId = 0
    else:
        for cl in allClients:
            currId =  cl.getId()
            if currentClientId <= currId:
                currentClientId = currId + 1
    return currentClientId

@app.route('/websocketClient')
def handle_websocket_client():
    """
        Is called when client sends socket, handles clients messages
    """
    wsock = request.environ.get('wsgi.websocket')

    _client = Client(generateClientId(), wsock, request.environ.get('HTTP_USER_AGENT'))
    print ("client socket received")

    allClients.append(_client) #append cluster in array to handle them
    serverMsg = ServerToClientMessage(ServerToClientMessage.CONECTION_MSG, _client.getId())
    sendMsgToClient(serverMsg, _client.getSocket()) # send message that client is connected

    print("all clients: ", allClients.__len__())
    print("server socket: ", serverSocket)

    sendAddClientMessageToServerPage(_client)

    if not wsock:
        abort(400, 'Expected WebSocket request.')

    while True:
        try:
            msg = wsock.receive()
            if (msg != None):
                message = ClientToServerMessage(msg)
            else: # client leaved
                for cl in allClients:
                    if cl.getSocket() == wsock:
                        allClients.remove(cl)
                        if cl.busy == True:
                            removeTaskPerformer(cl)
                            print("Working client disconnected")
                        sendLeaveClientMessageToServerPage(cl)
                        giveOutTasks(allClients, taskList)

                break
            if (message.type == ClientToServerMessage.STATUS): # send client status message to server page
                for cl in allClients:
                    if cl.getSocket() == wsock:
                        if message.status == 100: # if client completes task
                            # send status with time argument
                            print("time: ", message.time)
                            sendClientStatusMessageToServerPage(cl.getId(), message.status, message.substringFound, message.time)
                            if cl != None:
                                removeTaskFromTaskList(cl.getId())
                            if taskList.__len__() == 0:
                                print("WORK DONE!!!") # send WORK_DONE_MSG
                                if serverSocket:
                                    serverSocket.send(jsonpickle.encode(ServerToServerPageMessage(ServerToServerPageMessage.WORK_DONE_MSG)))
                            else:
                                giveOutTasks(allClients, taskList)
                        else:
                            sendClientStatusMessageToServerPage(cl.getId(), message.status, message.substringFound)

        except WebSocketError:
            for cl in allClients:
                if cl.getSocket() == wsock:
                    allClients.remove(cl)
                    if cl.busy == True:
                        removeTaskPerformer(cl)
                        print("Working client disconnected")
                    sendLeaveClientMessageToServerPage(cl)
                    giveOutTasks(allClients, taskList)
            break

@app.route('/websocketServer')
def handle_websocket_server():
    """
        Is called when server sends socket, handles server messages
    """
    global serverSocket #use global serverSocket because i want to send some data to server page from another function
    global taskList
    serverSocket = request.environ.get('wsgi.websocket')

    if serverSocket:
        print ("Server socket received")
        for cl in allClients:
            sendAddClientMessageToServerPage(cl)

    if not serverSocket:
        abort(400, 'Expected WebSocket request.')

    while True:
        try:
            msg = serverSocket.receive()
            if (msg != None):
                message = ServerPageToServerMessage(msg)

                if message.type == ServerPageToServerMessage.START_SHARING_TASKS_MSG: # if server page wants to start sharing tasks
                    taskManager = TaskManager(message.data) # create task manager, arg - substring to search
                    taskList = taskManager.getTasks(allClients.__len__()) # generate tasks
                    if taskList != None:
                        print("Task manager generated tasks: ")
                        for task in taskList:
                            print("task -- ", task.string[task.string.__len__()-200:task.string.__len__()])
                        print("clients amount: ", allClients.__len__())
                        print("substring to search: ", task.substringToSearch)

                        giveOutTasks(allClients, taskList)
                    else:
                        sendWarningMessageToServerPage("No clients connected")
        except WebSocketError:
            print ("except WebSocketError")
            break

@app.route('/')
def index():
    return template('client_template')

@app.route('/server')
def server():
    print ("all clients: ", allClients)
    return template('server_template')

# Static Routes
@app.route('/<filename:re:.*\.js>')
def javascripts(filename):
    return static_file(filename, root='static/js')

@app.route('/<filename:re:.*\.css>')
def stylesheets(filename):
    return static_file(filename, root='static/css')

@app.route('/<filename:re:.*\.(jpg|png|gif|ico)>')
def images(filename):
    return static_file(filename, root='static/img')

@app.route('/<filename:re:.*\.(eot|ttf|woff|svg)>')
def fonts(filename):
    return static_file(filename, root='static/fonts')

from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketError
from geventwebsocket.handler import WebSocketHandler

def runServer():
    server = WSGIServer(("127.0.0.1", 8080), app,
                        handler_class=WebSocketHandler)
    server.serve_forever()

def runTesting():
    server_thread = Process(target=runServer)
    server_thread.start()
    unittest.main()

#runServer()
runTesting()


import codecs
import datetime
import logging
import os
import re
import sys
import urllib2
import hashlib

from cghub_python_api import WSAPIRequest, SOLRRequest
from cghub_python_api.utils import urlopen

from django.conf import settings
from django.template.loader import render_to_string
from django.utils import timezone

from .attributes import ATTRIBUTES, SORT_BY_ATTRIBUTES
from .templatetags.search_tags import file_size as file_size_to_str
from .utils import makedirs_group_write

if settings.API_TYPE == 'WSAPI':
    REQUEST_CLASS = WSAPIRequest
elif settings.API_TYPE == 'SOLR':
    REQUEST_CLASS = SOLRRequest

api_logger = logging.getLogger('api.request')

def get_from_test_cache(url, format='xml'):
    """
    Used while testing.
    Trying to get response from cache, if it fails - get response from server and save it to cache.

    :param url: url that passed to urlopen
    :param format: 'xml' or 'json'

    :return: file object
    """
    FORMAT_CHOICES = {
        'xml': 'text/xml',
        'json': 'application/json'
    }
    CACHE_DIR = settings.TEST_CACHE_DIR
    if not os.path.exists(CACHE_DIR) or not os.path.isdir(CACHE_DIR):
        makedirs_group_write(CACHE_DIR)
    md5 = hashlib.md5(url)
    path = os.path.join(CACHE_DIR, '%s_%s.%s.cache' % (
            settings.API_TYPE.lower(), md5.hexdigest(), format))
    if os.path.exists(path):
        if format == 'json':
            return open(path, 'r')
        else:
            return codecs.open(path, 'r', encoding='utf-8')
    headers = {'Accept': FORMAT_CHOICES.get(format, FORMAT_CHOICES['xml'])}
    req = urllib2.Request(url, headers=headers)
    content = urllib2.urlopen(req).read()
    if format == 'json':
        # JSON is always ASCII and uses \uXXXX to represent non-ASCII symbols.
        with open(path, 'w') as f:
            f.write(content)
        return open(path, 'r')
    with codecs.open(path, 'w', encoding='utf-8') as f:
        f.write(content)
    return codecs.open(path, 'r', encoding='utf-8')

def build_wsapi_xml(result):
    """
    Makes solr xml response the same as wsapi response.
    <str name="analysis_id"> -> <analysis_id>, etc.
    """
    files_count = 0
    files_size = 0
    files = []
    for i in range(10):
        if result['filename.%s' % i].text == None:
            break
        files.append({
            'filename': result['filename.%s' % i].text,
            'filesize': result['filesize.%s' % i].text,
            'checksum': result['checksum.%s' % i].text,
            'checksum_method': result['checksum_method.%s' % i].text,
            })
        files_count += 1
        files_size += int(result['filesize.%s' % i].text)
    size_str = file_size_to_str(files_size)
    try:
        files_size, files_units = size_str.split(' ')
        files_size = files_size.replace(',', '.')
    except ValueError:
        files_size = '0'
        files_units = 'KB'
    xml = render_to_string('xml/wsapi_response.xml', {
            'timestamp': datetime.datetime.strftime(
                    timezone.now(), '%Y-%m-%d %H:%M:%S'),
            'result': result,
            'files': files,
            'files_count': files_count,
            'files_size': files_size,
            'files_units': files_units,
            'server_url': settings.CGHUB_DOWNLOAD_SERVER})
    return xml

class RequestBase(REQUEST_CLASS):

    def patch_input_data(self):
        server_url = getattr(settings, 'CGHUB_SERVER', None)
        if server_url:
            self.server_url = server_url
        if settings.API_TYPE == 'SOLR':
            # set SOLR uri if specified
            self.uri = getattr(settings, 'CGHUB_SERVER_SOLR_URI', self.uri)
        # filter sort_by
        if self.sort_by:
            if self.sort_by.find('-') == 0:
                if self.sort_by[1:] not in SORT_BY_ATTRIBUTES:
                    self.sort_by = None
            else:
                if self.sort_by not in SORT_BY_ATTRIBUTES:
                    self.sort_by = None
        # patch for preservation_method filter
        # ffpe: xml_text:("EXPERIMENT_ATTRIBUTE TAG SAMPLE_PRESERVATION TAG VALUE FFPE")
        # frozen: -xml_text:("EXPERIMENT_ATTRIBUTE TAG SAMPLE_PRESERVATION TAG VALUE FFPE")
        if 'preservation_method' in self.query:
            if 'ffpe' in self.query['preservation_method']:
                if 'xml_text' in self.query:
                    if isinstance(self.query['xml_text'], list):
                        pass
                    elif isinstance(self.query['xml_text'], tuple):
                        self.query['xml_text'] = list(self.query['xml_text'])
                    else:
                        self.query['xml_text'] = [str(self.query['xml_text'])]
                else:
                    self.query['xml_text'] = []
                self.query['xml_text'].append('EXPERIMENT_ATTRIBUTE TAG SAMPLE_PRESERVATION TAG VALUE FFPE')
            elif 'frozen' in self.query['preservation_method']:
                if '-xml_text' in self.query:
                    if isinstance(self.query['-xml_text'], list):
                        pass
                    elif isinstance(self.query['-xml_text'], tuple):
                        self.query['-xml_text'] = list(self.query['-xml_text'])
                    else:
                        self.query['-xml_text'] = [str(self.query['-xml_text'])]
                else:
                    self.query['-xml_text'] = []
                self.query['-xml_text'].append('EXPERIMENT_ATTRIBUTE TAG SAMPLE_PRESERVATION TAG VALUE FFPE')
            del self.query['preservation_method']

    def get_source_file(self, url):
        if 'test' in sys.argv:
            return get_from_test_cache(url=url, format=self.format)
        api_logger.debug(urllib2.unquote(url))
        return urlopen(
                url=url,
                format=self.format,
                max_attempts=getattr(settings, 'API_HTTP_ERROR_ATTEMPTS', 5),
                sleep_time=getattr(settings, 'API_HTTP_ERROR_SLEEP_AFTER', 1))

    def patch_result(self, result, result_xml):
        new_result = {}
        for attr in ATTRIBUTES:
            if result[attr].exist:
                new_result[attr] = result[attr].text
        # files
        new_result['files'] = []
        for i in range(10):
            if not result['filename.%d' % i].exist:
                break
            new_result['files'].append({
                'filename': result['filename.%d' % i].text,
                'filesize': int(result['filesize.%d' % i].text),
                'checksum': result['checksum.%d' % i].text,
            })
        try:
            new_result['filename'] = new_result['files'][0]['filename']
            new_result['checksum'] = new_result['files'][0]['checksum']
            new_result['files_size'] = new_result['files'][0]['filesize']
        except KeyError:
            new_result['filename'] = ''
            new_result['checksum'] = ''
            new_result['files_size'] = 0
        return new_result

class RequestID(RequestBase):
    """
    URI: analysisID uri.
    Fields: analysis_id.
    """

    def patch_input_data(self):
        super(RequestID, self).patch_input_data()
        self.uri = getattr(self, 'CGHUB_ANALYSIS_ID_URI', self.uri)
        self.fields = ['analysis_id']

    def patch_result(self, result, result_xml):
        return {'analysis_id': result['analysis_id'].text}

class RequestMinimal(RequestBase):
    """
    URI: analysisDetail uri.
    Fields: analysis_id, last_modified.
    """

    def patch_input_data(self):
        super(RequestMinimal, self).patch_input_data()
        self.uri = getattr(self, 'CGHUB_ANALYSIS_DETAIL_URI', self.uri)
        self.fields = ['analysis_id', 'last_modified']

    def patch_result(self, result, result_xml):
        return {
                'analysis_id': result['analysis_id'].text,
                'last_modified': result['last_modified'].text}

class RequestDetail(RequestBase):
    """
    URI: analysisDetail uri.
    Fields: analysis_id, state, reason, last_modified,
            upload_date, published_date, center_name, study,
            aliquot_id, filename, filesize, checksum,
            sample_accession, legacy_sample_id, disease_abbr,
            tss_id, participant_id, sample_id, analyte_code,
            sample_type, library_strategy, platform,
            refassem_short_name
    """

    def patch_input_data(self):
        super(RequestDetail, self).patch_input_data()
        self.uri = getattr(self, 'CGHUB_ANALYSIS_DETAIL_URI', self.uri)
        self.fields = [
            'analysis_id', 'state', 'reason', 'last_modified',
            'upload_date', 'published_date', 'center_name', 'study',
            'aliquot_id', 'filename', 'filesize', 'checksum',
            'sample_accession', 'legacy_sample_id', 'disease_abbr',
            'tss_id', 'participant_id', 'sample_id', 'analyte_code',
            'sample_type', 'library_strategy', 'platform',
            'refassem_short_name']

class RequestDetailJSON(RequestDetail):
    """
    URI: analysisDetail uri.
    Fields: analysis_id, refassem_short_name,
        legacy_sample_id, center_name, checksum, disease_abbr,
        analyte_code, filename, filesize, library_strategy,
        last_modified, platform, sample_accession, sample_type,
        state, study, upload_date

    Used json response from wsapi/solr.
    """

    def patch_input_data(self):
        super(RequestDetailJSON, self).patch_input_data()
        self.format = self.FORMAT_JSON

    def patch_result(self, result, result_xml):
        try:
            if result.get('files'):
                for f in result['files']:
                    f['checksum'] = f['checksum']['#text']
            elif isinstance(result['filename'], list):
                result['files'] = []
                for i in range(len(result['filename'])):
                    result['files'].append({
                        'filename': result['filename'][i],
                        'checksum': result['checksum'][i],
                        'filesize': result['filesize'][i],
                    })
            result['filename'] = result['files'][0]['filename']
            result['checksum'] = result['files'][0]['checksum']
            result['files_size'] = result['files'][0]['filesize']
        except KeyError:
            result['filename'] = ''
            result['checksum'] = ''
            result['files_size'] = 0
        return result

class RequestFull(RequestBase):
    """
    URI: analysisFull uri.
    Fields: all.
    Raw xml added to results.
    """

    def patch_input_data(self):
        super(RequestFull, self).patch_input_data()
        self.uri = getattr(self, 'CGHUB_ANALYSIS_FULL_URI', self.uri)
        self.fields = None

    def patch_result(self, result, result_xml):
        if settings.API_TYPE == 'WSAPI':
            xml = result_xml
        elif settings.API_TYPE == 'SOLR':
            # create the same xml as WSAPI returns
            xml = build_wsapi_xml(result)
        new_result = super(RequestFull, self).patch_result(result, result_xml)
        new_result['reason'] = result['reason'].text or ''
        new_result['xml'] = xml
        return new_result

class ResultFromSOLRFile(SOLRRequest):
    """
    Used by RequestsTestCase.test_build_wsapi_xml
    """

    def get_source_file(self, url):
        filename = self.query['filename']
        return codecs.open(filename, 'r')

def get_results_for_ids(ids, sort_by=None):
    """
    Obtain all necessary attributes for specified analysis_ids
    """
    if not ids:
        return []
    api_request = RequestDetailJSON(query={'analysis_id': ids}, sort_by=sort_by)
    results = []
    for result in api_request.call():
        results.append(result)
    return results

class SearchByIDs(object):
    """
    Allows to search by multiple ids.
    """

    ID_ATTRS = ('analysis_id', 'aliquot_id', 'participant_id', 'sample_id')

    def __init__(self, ids, request_cls=RequestID, filters=None):
        """
        :param ids: list of ids to search by
        :param request_cls: Request class will be used to obtain results (RequestID, RequestDetail, ...)
        :param filters: filters dict to use in query
        """
        self.request_cls = request_cls
        self.filters = filters or {}
        self.legacy_sample_ids = []
        self.other_ids = []
        id_regexp = re.compile(settings.ID_PATTERN)
        for i in ids:
            if id_regexp.match(i.lower()):
                # other ids: aliquot_id, analysis_id, participant_id, sample_id
                self.other_ids.append(i.lower())
            else:
                self.legacy_sample_ids.append(i.upper())
        self.search()

    def search(self):
        self.results = {}
        if self.other_ids:
            for attr in self.ID_ATTRS:
                self.results[attr] = []
                query = dict(self.filters)
                query[attr] = self.other_ids
                api_request = self.request_cls(query=query)
                found = False
                for result in api_request.call():
                    self.results[attr].append(result)
                    found = True
                # stop if single id was found
                if found and len(self.other_ids) == 1:
                    break

        if self.legacy_sample_ids:
            self.results['legacy_sample_id'] = []
            query = dict(self.filters)
            query['legacy_sample_id'] = self.legacy_sample_ids
            api_request = self.request_cls(query=query)
            for result in api_request.call():
                self.results['legacy_sample_id'].append(result)

    def get_ids(self):
        ids = []
        for attr in self.results:
            for result in self.results[attr]:
                if result['analysis_id'] not in ids:
                    ids.append(result['analysis_id'])
        return ids

    def get_results(self):
        results = []
        ids = []
        for attr in self.results:
            for result in self.results[attr]:
                if result['analysis_id'] not in ids:
                    ids.append(result['analysis_id'])
                    results.append(result)
        return results

    def is_empty(self):
        return not any([val for key, val in self.results.iteritems()])


from django import forms
from .models import ReporteDiario, Funda, Defecto, PorcentajeDefecto # Asegúrate de importar los modelos necesarios
from django.forms import modelformset_factory

# class ReporteDiarioForm(forms.ModelForm):
#     class Meta:
#         model = ReporteDiario
#         fields = '__all__'

#     marca_funda = forms.ModelChoiceField(queryset=Funda.objects.all(), empty_label=None)

class ReporteDiarioForm(forms.ModelForm):
    defectos = forms.ModelMultipleChoiceField(
        queryset=Defecto.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False
    )

    class Meta:
        model = ReporteDiario
        fields = ['nombre_analista','turno','presentacion','lote','hora', 'fecha','clasificacion', 'cuenta_lbs', 'total_camaron','peso_neto', 'camarones_grandes', 'camarones_pequenos', 'marca_funda_manual', 'peso_bruto', 'peso_sin_funda_manual','porcentaje_glaseo','uniformidad','observaciones','correctivos']

    def __init__(self, *args, **kwargs):
        super(ReporteDiarioForm, self).__init__(*args, **kwargs)
        # Configura los campos como no requeridos
        self.fields['total_camaron'].required = False
        self.fields['camarones_grandes'].required = False
        self.fields['camarones_pequenos'].required = False
        self.fields['porcentaje_glaseo'].required = False
        self.fields['uniformidad'].required = False

class FundaForm(forms.ModelForm):
    class Meta:
        model = Funda
        fields = ['marca', 'peso_funda']

class PorcentajeDefectoForm(forms.ModelForm):
    defectos = [
        ("Rosado leve", "Rosado leve"),
        ("Deshidratado", "Deshidratado"),
        ("Picado", "Picado"),
        ("Dañados o piezas", "Dañados o piezas"),
        ("Mudado", "Mudado"),
        ("Suave o quebrado", "Suave o quebrado"),
        ("Mal descabezado", "Mal descabezado"),
        ("Sin telson", "Sin telson"),
        ("Cascara", "Cascara"),
        ("Corte profundo", "Corte profundo"),
        ("Falta de corte", "Falta de corte"),
        ("Intestino / vena", "Intestino / vena"),
        ("Semi crudo", "Semi crudo"),
        ("Pequeño", "Pequeño"),
        ("Otros", "Otros"),
    ]

    defecto = forms.ChoiceField(
        choices=defectos,
        widget=forms.Select(attrs={'class': 'form-control'})
    )
    class Meta:
        model = PorcentajeDefecto
        fields = ['defecto', 'cantidad', 'porcentaje']


import logging
logger = logging.getLogger("live_control")

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from django.contrib.auth.decorators import login_required

from django.template import RequestContext
from django.core.urlresolvers import reverse

from controller_setup.models import Document
from controller_setup.forms import DocumentForm
from controller_setup.management.commands.templates import main as generate_templates

# Create your views here.
from .models import Template, LinkUserToPlant
from .tools_ssh import start_plant, restart_plant, stop_plant, status_plant, sync_plant

@login_required(login_url='/login/')
def index(request):
    template = loader.get_template('controller_setup/index.html')
    return HttpResponse(template.render(context=None, request=request))

def done(request, go_back=None, string_list=None):
    template = loader.get_template('controller_setup/done.html')
    context = {
        'go_back': go_back,
        'string_list': string_list,
    }
    return HttpResponse(template.render(context, request))

@login_required(login_url='/login/')
def control(request):
    links = LinkUserToPlant.objects.all()
    template = loader.get_template('controller_setup/control.html')
    context = {
        'links': links,
    }
    return HttpResponse(template.render(context, request))

@login_required(login_url='/login/')
def command(request, command, link_pk):
    link = LinkUserToPlant.objects.get(pk=link_pk)
    response = command(link)
    logger.info("for command %s, response %s" % (command, response))
    try:
        response = map(str, response)
    except:
        response = [str(response)]
    return done(request, go_back='control', string_list=response)

def start(request, link_pk):
    return command(request, start_plant, link_pk)
def restart(request, link_pk):
    return command(request, restart_plant, link_pk)
def stop(request, link_pk):
    return command(request, stop_plant, link_pk)
def status(request, link_pk):
    return command(request, status_plant, link_pk)

def sync(request, link_pk):
    return command(request, sync_plant, link_pk)

@login_required(login_url='/login/')
def templates(request):
    templates = Template.objects.all()
    template = loader.get_template('controller_setup/templates.html')
    context = {
        'templates': templates,
    }
    return HttpResponse(template.render(context, request))

@login_required(login_url='/login/')
def upload(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('controller_setup.views.upload'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'controller_setup/upload.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

@login_required(login_url='/login/')
def autotemplates(request):
    return done(request, go_back='templates', string_list=generate_templates())


'''
将原始数据划分为训练样本和测试样本
'''
import argparse
import os
import cv2
from tqdm import tqdm
import random
import math
import numpy as np
import shutil

def mkdir_dir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def mkdir_dir_(path):
    if not os.path.exists(path):
        os.makedirs(path)
    else:
        files = os.listdir(path)
        for file in files:
            file_path = os.path.join(path, file)
            os.remove(file_path)

def main(opt):

    for name, num in opt.img_names_num_dict.items():
        print(name)
        img_dir = os.path.join(opt.img_dir, name + '/images')
        txt_dir = os.path.join(opt.img_dir, name + '/labels')

        save_train_dir = os.path.join(opt.save_dir, name + '/train')
        mkdir_dir(save_train_dir)
        save_test_dir = os.path.join(opt.save_dir, name + '/test')
        mkdir_dir(save_test_dir)

        save_train_img = os.path.join(save_train_dir, 'images')
        mkdir_dir_(save_train_img)

        save_train_labels = os.path.join(save_train_dir, 'labels')
        mkdir_dir_(save_train_labels)

        save_test_img = os.path.join(save_test_dir, 'images')
        mkdir_dir_(save_test_img)

        save_test_labels = os.path.join(save_test_dir, 'labels')
        mkdir_dir_(save_test_labels)

        imgs = os.listdir(img_dir)

        # 随机挑选
        list2 = [i for i in range(len(imgs))]
        choice_num_list = random.sample(list2, num)  # 100是需要制作的图片数量
        # dic_num_path = {str(i): path for (i, path) in zip(range(len(imgs)), imgs)}

        for img_i in tqdm(range(len(imgs))):
            img = imgs[img_i]
            img_path = os.path.join(img_dir, img)
            txt_path = os.path.join(txt_dir, img[:-4] + '.txt')
            # 随机挑选
            if img_i in choice_num_list:
                save_img_path = os.path.join(save_test_img, img)
                shutil.copy(img_path, save_img_path)
                save_txt_path = os.path.join(save_test_labels, img[:-4] + '.txt')
                shutil.copy(txt_path, save_txt_path)
            else:
                save_img_path = os.path.join(save_train_img, img)
                shutil.copy(img_path, save_img_path)
                save_txt_path = os.path.join(save_train_labels, img[:-4] + '.txt')
                shutil.copy(txt_path, save_txt_path)

def parse_opt(known=False):
    parser = argparse.ArgumentParser()
    names_num_dict = {'935_936': 7}
    pathdir = '/home/wqg/data/container/'
    parser.add_argument('--img_dir', type=str,
                        default= pathdir + 'container_rec', help='')
    parser.add_argument('--img_names_num_dict', default=names_num_dict, help='')
    parser.add_argument('--save_dir', type=str,
                        default=pathdir + 'container_rec', help='')

    opt = parser.parse_known_args()[0] if known else parser.parse_args()
    return opt

if __name__ == '__main__':
    opt = parse_opt()
    main(opt)


class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        # @sliding window + 2 pointer Time:O(n) Space: O(1)
        l, r = 0, 0
        charSet = set()
        res = 0
        while r!=len(s):
            while s[r] in charSet:
                charSet.remove(s[l])
                l+=1
            charSet.add(s[r])
            res = max(res, r-l+1)
            r+=1
        return res


from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from config.db import db
from templates import webhook_template
from functions import auth
from models.models import User

app = APIRouter()

@app.get("/admin/users")
async def getAllUsers(admin: User = Depends(auth.get_current_user)):
    """
    Get all users from database
    """
    try:
        if not admin["admin"]:
            return {
                'error':"This route is only for admins"
            }

        users = db.user.find()
        usersList = []
        for user in users:
            user["_id"] = str(user["_id"])
            usersList.append(user)
        return usersList
    except:
        raise HTTPException(status_code=500, detail={
            "error":"Something went wrong"
        })

@app.get("/admin/user/{email}", description="Get specific user by email")
async def getUserByEmail(email, admin: User = Depends(auth.get_current_user)):
    if not admin["admin"]:
        return {
            "error":"This route is only for admins"
        }

    user = db.user.find_one({
        "email":email
    })
    if not user:
        raise HTTPException(status_code=404, detail={
            "error":"User not found"
        })
    user["_id"] = str(user["_id"])
    return user

@app.delete("/admin/user/{email}",description="Delete's an user by it's email")
async def deleteUserByEmail(email, admin: User = Depends(auth.get_current_user)):
    if not admin["admin"]:
        return {
            "error":"This route is only for admins"
        }

    user = db.user.find_one_and_delete({
        "email":email
    })
    if not user:
        raise HTTPException(status_code=404, detail={
            "error":"User not found"
        })
    user["_id"] = str(user["_id"])
    return user

@app.post("/webhook")
async def test_webhook(admin: User = Depends(auth.get_current_user)):
    if not admin["admin"]:
        return {
            "error":"This route is only for admins"
        }
    return webhook_template.send_test_webhook()


import os
import multiprocessing as mp

file_list = os.listdir("2-GeneFamiliesDivTimeAligned/")

def to_run(fasta_file):
	os.chdir("2-GeneFamiliesDivTimeAligned/")
	os.system("iqtree2 -s " + str(fasta_file) + " -nt 1")

pool = mp.Pool(15)
pool.map(to_run, file_list)


#!/usr/bin/env python
import argparse
import os
from datetime import datetime

from jinja2 import Template

parser = argparse.ArgumentParser(
    description='Python app project setup'
)

parser.add_argument(
    '--author-name', '-a',
    dest='author_name',
    type=str,
    required=True,
    help='Your full name (in quotes)'
)
parser.add_argument(
    '--author-email', '-e',
    dest='author_email',
    type=str,
    required=True,
    help='Your email',
)
parser.add_argument(
    '--author-github', '-g',
    dest='author_github',
    type=str,
    required=True,
    help='Your github username',
)
parser.add_argument(
    '--pypi-name', '-p',
    dest='pypi_name',
    type=str,
    required=True,
    help='Name for the package on pypi'
)
parser.add_argument(
    '--repo-name', '-r',
    dest='repo_name',
    type=str,
    required=True,
    help='Name of the repository on GitHub'
)
parser.add_argument(
    '--project-name', '-n',
    dest='project_name',
    type=str,
    required=True,
    help='python importable name of the project'
)
parser.add_argument(
    '--rtd-subdomain', '-d',
    dest='rtd_subdomain',
    type=str,
    required=True,
    help='ReadTheDocs subdomain'
)
parser.add_argument(
    '--extension', '-x',
    dest='extensions',
    action='append',
    default=['py', 'md', 'rst', 'yml', 'cfg', 'LICENSE'],
    required=False,
    help='The file extension(s) to render (default: "py,rst,md,yml,cfg,LICENSE").'
         'Use -e multiple times. for multiple extensions'
)

class ProjectSetup(object):
    def __init__(self, args):
        self.args = args

        self.extensions = tuple(args.extensions)
        self.excluded_files = ['new_project.py']

        self.template_context = {
            'author_name': self.args.author_name,
            'author_email': self.args.author_email,
            'author_github': self.args.author_github,
            'current_year': datetime.utcnow().year,
            'project_name': self.args.project_name,
            'pypi_name': self.args.pypi_name,
            'repo_name': self.args.repo_name,
            'rtd_subdomain': self.args.rtd_subdomain,
        }

    ignored_extensions = ('.pyo', '.pyc', '.py.class', '.swp')

    def print(self, output):
        print(output)

    def can_render_file(self, filename):
        if filename.endswith(self.ignored_extensions):
            return False
        if filename in self.excluded_files:
            return False
        elif filename.endswith(self.extensions):
            return True

    def render_new_file_content(self, file_path):
        t = Template(open(file_path, 'r').read())
        return t.render(self.template_context) + '\n'

    def rename_file(self, root, filename):
        base_filename = filename.split('.')[0]
        extension = filename.split('.')[-1]
        old_path = '{}/{}'.format(root, filename)
        new_path = '{}/{}.{}'.format(root, self.template_context[base_filename], extension)
        os.rename(old_path, new_path)

        self.print('Renamed file {0} to {1}'.format(old_path, new_path))

    def run(self):
        """
        Setup the project
        """
        root_dir = os.getcwd()

        self.print(
            'Rendering files with extensions {0}\n'.format(', '.join(self.extensions))
        )

        # Render all files with valid extensions
        for root, dirs, files in os.walk(root_dir):
            for dirname in dirs[:]:
                if dirname.startswith('.') or dirname == '__pycache__':
                    dirs.remove(dirname)
            if '/env' not in root and '.git' not in root:
                for filename in files:
                    if self.can_render_file(filename):
                        new_path = '{root}/{filename}'.format(
                            root=root,
                            filename=filename
                        )
                        rendered_content = self.render_new_file_content(new_path)

                        with open(new_path, 'wb') as new_file:
                            new_file.write(rendered_content.encode('utf-8'))

                        self.print('Rendered file {0}'.format(new_path))

        # Rename any files named 'project_name'
        for root, dirs, files in os.walk(root_dir):
            if '/env' not in root and '.git' not in root:
                for filename in files:
                    base_filename = filename.split('.')[0]
                    if base_filename == 'project_name':
                        self.rename_file(root, filename)

        # Rename any directories named 'project_name'
        for root, dirs, files in os.walk(root_dir):
            if '/env' not in root and '.git' not in root:
                dirname = root.split('/')[-1]

                if dirname == 'project_name':
                    path_parent = '/'.join(root.split('/')[:-1])

                    new_path_name = '{0}/{1}'.format(
                        path_parent,
                        self.template_context[dirname]
                    )
                    os.rename(root, new_path_name)
                    self.print('Renamed directory {0} to {1}'.format(
                        root,
                        new_path_name
                    ))

if __name__ == '__main__':  # pragma: no coverage
    args = parser.parse_args()
    status = ProjectSetup(args)
    status.run()


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import collections
import enum

from googlecloudsdk.api_lib.storage import cloud_api
from googlecloudsdk.command_lib.storage import errors
from googlecloudsdk.command_lib.storage import plurality_checkable_iterator
from googlecloudsdk.command_lib.storage import storage_url
from googlecloudsdk.command_lib.storage import wildcard_iterator
from googlecloudsdk.command_lib.storage.resources import resource_reference
from googlecloudsdk.core import log
from googlecloudsdk.core.util import debug_output

class BucketSetting(enum.Enum):
  """An enum saying whether or not to include matched buckets."""

  YES = '_yes'
  NO = '_no'
  NO_WITH_ERROR = '_no_with_error'

class RecursionSetting(enum.Enum):
  """An enum saying whether or not recursion is requested."""
  YES = '_yes'
  NO = '_no'
  NO_WITH_WARNING = '_no_with_warning'

class NameExpansionIterator:
  """Expand all urls passed as arguments, and yield NameExpansionResult.

  For each url, expands wildcards, object-less bucket names,
  subdir bucket names, and directory names, and generates a flat listing of
  all the matching objects/files.
  The resulting objects/files are wrapped within a NameExpansionResult instance.
  See NameExpansionResult docstring for more info.
  """

  def __init__(
      self,
      urls_iterable,
      all_versions=False,
      fields_scope=cloud_api.FieldsScope.NO_ACL,
      ignore_symlinks=False,
      include_buckets=BucketSetting.NO,
      preserve_symlinks=False,
      recursion_requested=RecursionSetting.NO_WITH_WARNING,
  ):
    """Instantiates NameExpansionIterator.

    Args:
      urls_iterable (Iterable[str]): The URLs to expand.
      all_versions (bool): True if all versions of objects should be fetched,
        else False.
      fields_scope (cloud_api.FieldsScope): Determines amount of metadata
        returned by API.
      ignore_symlinks (bool): Skip over symlinks instead of following them.
      include_buckets (BucketSetting): Whether to fetch matched buckets and
        whether to raise an error.
      preserve_symlinks (bool): Preserve symlinks instead of following them.
      recursion_requested (RecursionSetting): Says whether or not recursion is
        requested.
    """
    self.all_versions = all_versions

    self._urls_iterator = (
        plurality_checkable_iterator.PluralityCheckableIterator(urls_iterable))
    self._fields_scope = fields_scope
    self._ignore_symlinks = ignore_symlinks
    self._include_buckets = include_buckets
    self._preserve_symlinks = preserve_symlinks
    self._recursion_requested = recursion_requested

    self._top_level_iterator = (
        plurality_checkable_iterator.PluralityCheckableIterator(
            self._get_top_level_iterator()))
    self._has_multiple_top_level_resources = None
    self._url_found_match_tracker = collections.OrderedDict()

  def _get_wildcard_iterator(self, url):
    """Returns get_wildcard_iterator with instance variables as args."""
    return wildcard_iterator.get_wildcard_iterator(
        url,
        all_versions=self.all_versions,
        fetch_encrypted_object_hashes=True,
        fields_scope=self._fields_scope,
        ignore_symlinks=self._ignore_symlinks,
        preserve_symlinks=self._preserve_symlinks,
    )

  @property
  def has_multiple_top_level_resources(self):
    """Returns if the iterator yields plural items without recursing.

    Also returns True if the iterator was created with multiple URLs.
    This may not be true if one URL doesn't return anything, but it's
    consistent with gsutil and the user's probable intentions.

    Returns:
      Boolean indicating if iterator contains multiple top-level sources.
    """
    if self._has_multiple_top_level_resources is None:
      self._has_multiple_top_level_resources = (
          self._urls_iterator.is_plural() or
          self._top_level_iterator.is_plural())
    return self._has_multiple_top_level_resources

  def _get_top_level_iterator(self):
    """Iterates over user-entered URLs and does initial processing."""
    for url in self._urls_iterator:
      original_storage_url = storage_url.storage_url_from_string(url)
      if (
          isinstance(original_storage_url, storage_url.CloudUrl)
          and original_storage_url.is_bucket()
          and self._recursion_requested is not RecursionSetting.YES
          and self._include_buckets is BucketSetting.NO_WITH_ERROR
      ):
        raise errors.InvalidUrlError(
            'Expected object URL. Received: {}'.format(url)
        )
      # Set to True if associated Cloud resource found in __iter__.
      self._url_found_match_tracker[url] = False
      for resource in self._get_wildcard_iterator(url):
        yield url, self._get_name_expansion_result(resource,
                                                   resource.storage_url,
                                                   original_storage_url)

  def _get_nested_objects_iterator(self, parent_name_expansion_result):
    new_storage_url = parent_name_expansion_result.resource.storage_url.join(
        '**')
    child_resources = self._get_wildcard_iterator(new_storage_url.url_string)
    for child_resource in child_resources:
      yield self._get_name_expansion_result(
          child_resource, parent_name_expansion_result.resource.storage_url,
          parent_name_expansion_result.original_url)

  def _get_name_expansion_result(self, resource, expanded_url, original_url):
    """Returns a NameExpansionResult, removing generations when appropriate."""
    keep_generation_in_url = (
        self.all_versions or
        original_url.generation  # User requested a specific generation.
    )
    if not keep_generation_in_url:
      new_storage_url = storage_url.storage_url_from_string(
          resource.storage_url.versionless_url_string)
      resource.storage_url = new_storage_url
    return NameExpansionResult(resource, expanded_url, original_url)

  def _raise_no_url_match_error_if_necessary(self):
    non_matching_urls = [
        url for url, found_match in self._url_found_match_tracker.items()
        if not found_match
    ]
    if non_matching_urls:
      raise errors.InvalidUrlError(
          'The following URLs matched no objects or files:\n-{}'.format(
              '\n-'.join(non_matching_urls)))

  def _raise_error_if_multiple_sources_include_stdin(self, expanded_url):
    if (self._has_multiple_top_level_resources and isinstance(
        expanded_url, storage_url.FileUrl) and expanded_url.is_stdio):
      raise errors.Error(
          'Multiple URL strings are not supported when transferring from'
          ' stdin.')

  def __iter__(self):
    """Iterates over each URL in self._urls_iterator and yield the expanded result.

    Yields:
      NameExpansionResult instance.

    Raises:
      InvalidUrlError: No matching objects found.
    """
    self._has_multiple_top_level_resources = self._top_level_iterator.is_plural(
    )
    for input_url, name_expansion_result in self._top_level_iterator:

      self._raise_error_if_multiple_sources_include_stdin(
          name_expansion_result.expanded_url)

      should_return_bucket = self._include_buckets is BucketSetting.YES and (
          name_expansion_result.resource.storage_url.is_bucket()
      )
      if not resource_reference.is_container_or_has_container_url(
          name_expansion_result.resource) or should_return_bucket:
        self._url_found_match_tracker[input_url] = True
        yield name_expansion_result

      if resource_reference.is_container_or_has_container_url(
          name_expansion_result.resource):
        if self._recursion_requested is RecursionSetting.YES:
          for nested_name_expansion_result in self._get_nested_objects_iterator(
              name_expansion_result):
            self._url_found_match_tracker[input_url] = True
            yield nested_name_expansion_result

        elif not should_return_bucket:
          if self._recursion_requested is RecursionSetting.NO_WITH_WARNING:
            # Does not warn about buckets processed above because it's confusing
            # to warn about something that was successfully processed.
            log.warning('Omitting {} because it is a container, and recursion'
                        ' is not enabled.'.format(
                            name_expansion_result.resource))

    self._raise_no_url_match_error_if_necessary()

class NameExpansionResult:
  """Holds one fully expanded result from iterating over NameExpansionIterator.

  This class is required to pass the expanded_url information to the caller.
  This information is required for cp and rsync command, where the destination
  name is determined based on the expanded source url.
  For example, let's say we have the following objects:
  gs://bucket/dir1/a.txt
  gs://bucket/dir1/b/c.txt

  If we run the following command:
  cp -r gs://bucket/dir* foo

  We would need to know that gs://bucket/dir* was expanded to gs://bucket/dir1
  so that we can determine destination paths (foo/a.txt, foo/b/c.txt) assuming
  that foo does not exist.

  Attributes:
    resource (Resource): Yielded by the WildcardIterator.
    expanded_url (StorageUrl): The expanded wildcard url.
    original_url (StorageUrl): Pre-expanded URL.
  """

  def __init__(self, resource, expanded_url, original_url):
    """Initialize NameExpansionResult.

    Args:
      resource (resource_reference.Resource): Yielded by the WildcardIterator.
      expanded_url (StorageUrl): The expanded url string without any wildcard.
        This value should preserve generation even if not available in
        resource.storage_url. The versionless version of this should be same
        as resource.storage_url if recursion was not requested. This field is
        intended for only the cp and rsync commands.
      original_url (StorageUrl): Pre-expanded URL. Useful for knowing intention.
    """
    self.resource = resource
    self.expanded_url = expanded_url
    self.original_url = original_url

  def __repr__(self):
    return debug_output.generic_repr(self)

  def __str__(self):
    return self.resource.storage_url.url_string

  def __eq__(self, other):
    if not isinstance(other, type(self)):
      return NotImplemented
    # Leave out original_url because two different URLs can expand to the same
    # result. This is a "results" class.
    return (self.resource == other.resource
            and self.expanded_url == other.expanded_url)


class Solution:
    def pushDominoes(self, doms: str) -> str:

        prev = ""
        cur = doms
        while prev!=cur:
            new = cur.replace("R.L","xxx").replace("R.","RR").replace(".L","LL")
            prev = cur
            cur = new
        return cur.replace("xxx","R.L")


import spacy
nlp = spacy.load("en_core_web_lg")
class FileCleaning:
    def __init__(self,filename):
        self.filename = filename

    def filtering(self):
        with open(self.filename) as f:
            lines = f.readlines()
        for line in range(0,len(lines)):
            lines[line] = lines[line].strip().strip("-")
        cancel = []
        changes = []
        current = ""
        for line in lines:
            if line=="CHANGES":
                current = "changes"
            elif line=="CANCELLATIONS":
                current = "cancel"
            if current == "changes":
                changes.append(line)
            elif current == "cancel":
                cancel.append(line)
            else:
                continue
        cancel = '\n'.join(cancel).replace('HR', 'HOURS')
        cancel = cancel.replace('HRS', "HOURS")
        changes = '\n'.join(changes).replace('HR', 'HOURS')
        changes = changes.replace('HRS', "HOURS")
        return [cancel,changes]

    def cancellation_split(self):
        file_rules = self.filtering()[0]
        rules = file_rules.split("\n\n")
        return rules

    def changes_split(self):
        file_rules = self.filtering()[1]
        rules = file_rules.split("\n\n")
        return rules

    def decision_making_cancellation(self):
        decision_1 = nlp("REFUNDABLE")
        decision_2 = nlp("NON REFUNDABLE")
        decision_rules = self.cancellation_split()
        result = nlp(str(decision_rules[0]))
        if decision_1.similarity(result) > decision_2.similarity(result):
            return 1
        else:
            return 0

    def decision_making_changes(self):
        decision_1 = nlp("CHANGES PERMITTED")
        decision_2 = nlp("CHNAGES NOT PERMITTED")
        decision_rules = self.changes_split()
        for rule in decision_rules:
            doc = nlp(str(rule))
            if decision_1.similarity(doc) < decision_2.similarity(doc):
                print(doc, " => ", "CHNAGES PERMITTED\n\n")

            else:
                print(doc, " => ", "NO CHNAGES PERMITTED\n\n")

# filerules = FileCleaning("DELBOM-AI-INR.txt")
# rules = filerules.changes_split()[0]
# print(rules)


def sort(a,b):
        for i in range(len(a)):
                for j in range(i+1,len(a)):
                        if(b[i]<b[j]):
                                b[i],b[j]=b[j],b[i]
                                a[i],a[j]=a[j],a[i]
        return(a,b)

def output(a,b,c):
        big=0
        x=-1
        y=-1
        for i in range(len(a)):
                for j in range(len(a)):
                        if(i!=j):
                                if(big<=(b[i]+b[j]) and ((b[i]+b[j])%c==0)and ((a[x]+a[y])<(a[i]+a[j]))):
                                        big=b[i]+b[j]
                                        x=i
                                        y=j
        if(big==0):
                return(-1)
        else:
                #print(a[x],a[y],big)
                return(a[x]+a[y])

def check(a,b):
        while(b):
                a,b=b,a%b
        return(a)
def check1(a,b):
        out=1
        for i in range(b):
                out*=a
        return(out)

n,k,b,m=map(int, input().split())
a=[int(x) for x in input().split()]
power1=[]
for i in a:
        k=0
        for j in range(1,i):
                if(check(j,i)==1):
                        k+=1
        power1.append(check1(k,b))
new1,new2=sort(a,power1)
#print(new1,new2)
print(output(new1,new2,m))


import streamlit as st
import pandas as pd
import gig_scraper
def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

def remote_css(url):
    st.markdown(f'<link href="{url}" rel="stylesheet">', unsafe_allow_html=True)

def icon(icon_name):
    st.markdown(f'<i class="material-icons">{icon_name}</i>', unsafe_allow_html=True)
@st.cache
def get_df(v):
    try:
	    tor_process=gig_scraper.start_tor()
    except OSError:
	    pass
    return(gig_scraper.craigslist_gig_scrape(v))

def next(df):
    if st.checkbox("Remove Duplicate Titles"):
            df=df.drop_duplicates(subset=['titles'],keep="first")
            df=df.reset_index()
            #df=df.drop(['timestamp'], axis = 1)
            st.write(df)
            icon("search")
            selected = st.text_input("", "Search...")
            if st.button("OK"):
                df=df[df['titles'].str.contains(selected)]
                L=df.to_html(escape=False)
                st.write(L, unsafe_allow_html=True)
def main():
    #local_css("style.css")
    st.title('Craigslist Gig Scraper')

    remote_css('https://fonts.googleapis.com/icon?family=Material+Icons')
    values=("/d/computer-gigs/search/cpg","/d/creative-gigs/search/crg","/d/crew-gigs/search/cwg","/d/domestic-gigs/search/dmg","/d/event-gigs/search/evg","/d/labor-gigs/search/lbg","/d/talent-gigs/search/tlg","/d/writing-gigs/search/wrg")
    options=("Computer","Creative","Crew","Domestic","Event","Labor","Talent","Writing")
    value=st.sidebar.selectbox("Gig Type",options)
    if st.sidebar.checkbox("Start"):
	    v=values[options.index(value)]
	    df=get_df(v)
	    df
	    next(df)
if __name__ == '__main__':
	main()


from pprint import pprint

#Firma
data = {
    "telefon": 0,
    "e-mail": 1,
    "position": 2,
    "cabinet number": 3,
    "skype": 4
}
firm = {
    "Joe Dhons": [+380508374145, "dhons.joe@gmail.com", "CEO", 1, "chef.joe"],
    "William Conqueror": [+380675793175, "conqueror.williwam@gmail.com", "commercial Director", 2, "conqueror.williwam"],
    "Evelyn Miller": [+380964873964, "evelyn.miller@gmail.com", "chief Financial Officer", 3, "evelyn.miller"],
    "Harper Lee": [+380981587008, "harper.lee@gmail.com", "writer", 4, "harper.lee"],
    "Mason Mount": [+380930014800, "mason.mountg@gmail.com", "manager", 5, "mason.mount"],
    "Ella Purnell": [+380631001002, "ella.purnell@gmail.com", "actress", 6, "ella.purnell"],
    }
pprint(firm)
pprint(firm["William Conqueror"][2])

# remove
del firm["William Conqueror"]
pprint(firm)

#search
pprint(firm["Ella Purnell"])
pprint(firm.get("Ella Purnell"))

#adding
new_firm = {
    "Scarlett Moffatt": [+380986673212, "scarlett.moffatt@gmail.com", "cleaning lady", 10, "scarlett.moffatt"]
}
firm.update(new_firm)
pprint(firm)

#Replacing data
pprint(firm["Harper Lee"])
firm["Harper Lee"] = [+380934130093, "aleksandriv@ukr.net", "harper.lee@gmail.com", "writer", 4, "harper.lee"]
pprint(firm)


# https://www.acmicpc.net/problem/1660 문제 제목 : 캡틴 이다솜 , 언어 : Python, 날짜 : 2019-10-13, 결과 : 실패
# 아래에 성공 코드가 있다.
# 틀린 이유 : dp로 풀어야 했는데 그리디알고리즘을 사용했다.
#             고쳐야하는데 낼모레가 모의고사다..
import sys

N = int(sys.stdin.readline())
need_list = [0,1]
dp_list = [1]*(N+2)

n=2
count = 0
while need_list[n-1] < N:
    need_list.append(need_list[-1] + n*(n+1)//2)
    n+=1
print(need_list)
"""
while N:
    if need_list[n-1] < N:
        need_list.append(need_list[-1] + n*(n+1)//2)
        n+=1
    else:
        while need_list[n-1] > N:
            n-=1
        N-=need_list[n-1]
        count += 1
"""
print(count)

"""
N = int(sys.stdin.readline())
need_list = [0,1]# + [0]*(N-1)
n=2
count = 0
while N:
    if need_list[n-1] < N:
        need_list.append(need_list[-1] + n*(n+1)//2)
        n+=1
    else:
        while need_list[n-1] > N:
            n-=1
        N-=need_list[n-1]
        count += 1
print(count)
1 4 10 20

1 1+2 1+2+3 1+2+3+4
1 1+3 1+3+6 1+3+6+10
dp[n] = dp[n-1] + n(n+1)/2

"""

# https://www.acmicpc.net/problem/1660 문제 제목 : 캡틴 이다솜 , 언어 : Python, 날짜 : 2019-12-22, 결과 : 성공
# 최근 풀었던 dp문제의 유형이 모두 비슷한것같다. 별다른 검색없이 몇가지 테스트케이스를 직접 만들어보며 풀었다!
# 위의 예전에 짠 코드를 보니 성장한 느낌이 든다ㅋㅋ
# 아 도움을 받은 부분이 있는데 질문게시판에서 동전과 유사하게 생각하면 된다는 글이 접근법을 생각할때 크게 도움이 되었다.
import sys
N = int(sys.stdin.readline())
list_depo = [1]
list_dp = [0] * (N+1)
list_dp[1] = 1
for i in range(2, N+1):
    count_depo = i*(i+1)//2 + list_depo[-1]
    if count_depo <= N:
        list_depo.append(count_depo)
#print(list_depo)
for i in range(N+1):
    for j in list_depo:
        if i + j <= N:
            if list_dp[i+j] == 0:
                list_dp[i+j] = list_dp[i] + 1
            elif list_dp[i+j] > list_dp[i] + 1:
                list_dp[i+j] = list_dp[i] + 1
print(list_dp[-1])


from uuid import UUID

from django.urls import reverse

import pytest
from rest_framework import status
from rest_framework.test import APIClient

from tests.factories import RecipeFactory

pytestmark = [pytest.mark.django_db]

def get_recipe_detail_url(recipe_id: str) -> str:
    """Return a recipe detail url."""
    return reverse('api:recipes-detail', args=[recipe_id])

def test_update_recipe_successful(recipe_factory: RecipeFactory, api_user_client: APIClient) -> None:
    """Test retrieve recipe is successful.

    Args:
        recipe_factory: ingredient factory;
        api_user_client: django rest framework api client.
    """
    user, api_client = api_user_client
    recipe = recipe_factory.create(author=user)

    payload = {
        'id': recipe.id,
        'name': 'recipe',
        'text': 'good recipe',
        'tags': [tag.id for tag in recipe.tags.all()],
        'ingredients': [
            {
                'id': ingredient.id,
                'amount': 50,
            }
            for ingredient in recipe.ingredients.all()
        ],
        'cooking_time': 50,
    }

    response = api_client.patch(get_recipe_detail_url(recipe_id=recipe.id), payload, format='json')

    assert response.status_code == status.HTTP_200_OK
    assert response.data['name'] == 'recipe'


import pygame
from game import Game

SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 576

FPS = 60

def main():

    pygame.init()

    screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

    pygame.display.set_caption("Maze Escape-3D")

    done = False

    clock = pygame.time.Clock()

    game = Game()

    while not done:
        done = game.process_events()
        game.display_frame(screen)
        clock.tick(FPS)
    pygame.quit()

if __name__ == '__main__':
    main()


from decouple import config

if __name__ == "__main__":
    DEBUG = config('DEBUG', default=False, cast=bool)
    print('DEBUG', DEBUG)
    '''
    environment variables have precedence over config files.
    os.environ['PATH'] = '/my_path'
    '''
    PATH = config('PATH', cast=str)
    print('PATH', PATH)


# Problem 347
# In order to implement a max heap in python, simply multiply each val by -1.
# This will transform a minheap into a maxheap.

import heapq

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        if k == len(nums):
            return nums

        freq = dict()
        maxHeap = list()
        topK = list()

        for val in nums:
            if val in freq.keys():
                freq[val] += 1
            else:
                freq[val] = 1

        heapq.heapify(maxHeap)

        for key in freq.keys():
            heapq.heappush(maxHeap,(-freq[key], key))

        for i in range(0, k):
            topK.append(heapq.heappop(maxHeap)[1])

        return topK

# Approach 2: Quick Select

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        freq = dict()
        unique = list()
        topK = list()

        for val in nums:
            if val in freq.keys():
                freq[val] += 1
            else:
                freq[val] = 1

        unique = list(freq.keys())

        self.quickSelect(0, len(unique) - 1, len(unique) - k, unique, freq)

        return unique[len(unique) - k:]

    def partition(self, low, high, unique, freq) -> int:

        pivot = unique[high]
        i = low - 1

        for j in range(low, high):
            if freq.get(unique[j]) <= freq.get(pivot):
                i += 1
                unique[i], unique[j] = unique[j], unique[i]

        unique[i+1], unique[high] = unique[high], unique[i+1]

        return i + 1

    def quickSelect(self, left, right, k_smallest, unique, freq):

        if left == right:
            return

        pivot_index = self.partition(left, right, unique, freq)

        if k_smallest == pivot_index:
            return

        elif k_smallest < pivot_index:
            self.quickSelect(left, pivot_index - 1, k_smallest, unique, freq)

        else:
            self.quickSelect(pivot_index + 1, right, k_smallest, unique, freq)

# Approach 3: Bucket Sort

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        bucket = [[] for _ in range(len(nums) + 1)]
        freq = dict()
        topK = list()

        for val in nums:
            if val in freq.keys():
                freq[val] += 1
            else:
                freq[val] = 1

        for num, frequency in freq.items():
            bucket[frequency].append(num)

        for i in range(len(bucket) - 1, -1, -1):
            for j in range(len(bucket[i]) - 1, -1, -1):
                if (len(topK) < k):
                    topK.append(bucket[i][j])
                else:
                    break

        return topK


#!/usr/bin/env python
import glob
import os
import pandas as pd
import json
import nltk
from nltk.tokenize import MWETokenizer

from utils import int2str_label, get_rel_stance_label, get_pronouns, get_fulltext, read_quote_json, mv_files

PRONOUNS = get_pronouns()

# Create MWE Tokenizer for special words
tk = MWETokenizer()
tk.add_mwe(('peer', 'review'))
tk.add_mwe(('non', 'peer', 'review'))
tk.add_mwe(('climate', 'scientist'))
tk.add_mwe(('climate', 'scientist'))
tk.add_mwe(('medium', 'outlet'))
tk.add_mwe(('nobel', 'prize', 'win'))
tk.add_mwe(('nobel', 'win'))
tk.add_mwe(('nobel', 'peace', 'prize'))
tk.add_mwe(('nobel', 'laureate'))
tk.add_mwe(('nobel', 'laureates'))
tk.add_mwe(('prize', 'win'))
tk.add_mwe(('ocasio', 'cortez'))

def get_sorted_indices(q_dict):
    """Helper function to `get_context()`.
    Sorts indices of every Quote component ((main) subj., (main) verb, (main) neg.) within a Quote obj."""
    context_dict_ = {}
    for context_type in q_dict:
        context_dict_[context_type] = sorted([int(x) for x in q_dict[context_type]])
    return context_dict_

def get_inorder_text(context_indices,idx2text,idx2lemma,idx2coref):
    """Helper function to `get_context()`."""
    text_dict_ = {}
    for context_type in context_indices:
        text_dict_[context_type] = [idx2coref[str(x)] if idx2text[str(x)].lower() in PRONOUNS
                                    and idx2coref[str(x)] is not None
                                    else idx2text[str(x)]
                                    for x in context_indices[context_type]]
        text_dict_[context_type+'_lemmas'] = [idx2coref[str(x)] if idx2lemma[str(x)].lower() in PRONOUNS
                                    and idx2coref[str(x)] is not None
                                    else idx2lemma[str(x)]
                                    for x in context_indices[context_type]]
    return text_dict_

def get_context(src_guid,src_sent_no,src_q_no,q_dir):
    j = read_quote_json(src_guid,q_dir)
    q_dict = j['quote_tags'][src_sent_no]['quotes'][src_q_no]
    context_indices = get_sorted_indices(q_dict)
    idx2text = j['quote_tags'][src_sent_no]['idx2text']
    idx2lemma = j['quote_tags'][src_sent_no]['idx2lemma']
    idx2coref = j['coref_tags']
    text_dict = get_inorder_text(context_indices,idx2text,idx2lemma,idx2coref)
    return text_dict

def get_verb_tense(verb_text):
    return nltk.pos_tag([verb_text])[0][1]

def get_verb_mods(verb_list):
    """Given a list of verbs ('v_lemmas' in a Quote obj), groups into other verbs, adverbs, modals."""
    out = nltk.pos_tag(' '.join(verb_list).split())
    return {pos: [x[0] for x in out if x[1][:2] == pos] for pos in ['RB','MD','VB']}

with open('implicatives.txt','r') as f:
    IMPLICATIVES = set(f.read().splitlines())
print('Using implicatives set:',IMPLICATIVES)

def has_neg(context_dict):
    """Determines whether a comp. clause has been negated in its subject or verb, with a negative or implicative."""
    has_subj_neg = len(context_dict['main_neg_s']) > 0
    has_verb_neg = len(context_dict['main_neg_v']) > 0
    has_verb_impl = set(context_dict['v_lemmas']).intersection(IMPLICATIVES)
    has_verb_NEG = has_verb_neg or has_verb_impl
    if has_subj_neg and has_verb_NEG:
        return 'subj_verb_neg'
    elif has_subj_neg:
        return 'subj_neg'
    elif has_verb_neg:
        return 'verb_neg'
    elif has_verb_impl:
        return 'verb_impl'
    else:
        return 'no_neg'

if __name__=="__main__":

    articles_df = pd.read_pickle('../1_data_scraping/output/filtered_dedup_combined_df_2000_1_1_to_2020_4_12.pkl')
    src_guid2attrs = dict()
    for attr in ['stance','domain','date','is_AP']:
        src_guid2attrs[attr] = dict(zip(articles_df['guid'],articles_df[attr]))

    def get_src_attrs(src_guid):
        """Function for getting attributes of source article given article guid"""
        return {attr: src_guid2attrs[attr][src_guid] for attr in src_guid2attrs}

    orig = pd.read_csv('../2_data_processing/output/keyword_filtered_comp_clauses.tsv',
                      sep='\t',header=0,index_col=0)
    orig.reset_index(drop=True,inplace=True)

    print("Reading in batched BERT predictions...")
    PRED_DIR = "../3_cc_stance/2_Stance_model/model_preds"
    pred_files = glob.glob(os.path.join(PRED_DIR,'batch_*_pred.tsv'))
    print('\tFound {} prediction files.'.format(len(pred_files)))
    all_preds = [pd.read_csv(pred_file,sep='\t',header=0) for pred_file in pred_files]
    all_preds = pd.concat([x for x in all_preds],ignore_index=True)
    print('\tRead in {} predictions.'.format(len(all_preds)))

    print("Processing predictions...")
    all_preds['quote_text'] = orig['quote_text'].copy()
    all_preds['src_guid'] = orig['guid'].copy()
    all_preds['src_sent_no'] = orig['sent_no'].copy().apply(lambda x: str(int(x)))
    all_preds['src_quote_no'] = orig['quote_no'].copy().apply(lambda x: str(int(x)))
    all_preds['quote_guid'] = all_preds['src_guid'].apply(lambda x: x+"_") + \
                              all_preds['src_sent_no'].apply(lambda x: str(int(x))+"_") + \
                              all_preds['src_quote_no']
    all_preds['predicted_label'] = all_preds['predicted'].apply(int2str_label)
    all_preds['max_prob'] = all_preds[['0', '1','2']].max(axis=1)

    print("Creating main analysis dataframe...")
    src_guid_,src_sent_no_,src_q_no_,med_stance_,pub_date_,domain_,is_AP_,\
    quote_,cc_abs_stance_,cc_rel_stance_,neg_type_,subjs_,main_subj_lemma_,\
    verb_lemma_,verb_tense_,verb_advs_,verb_mds_,verb_other_ = [],[],[],[],[],[],[],[],[],\
                                                               [],[],[],[],[],[],[],[],[]
    for pred_item_ix,row in all_preds.iterrows():
        quote_.append(row['quote_text'])
        pred_label = row['predicted']
        src_guid = row['src_guid']
        src_sent_no = row['src_sent_no']
        src_q_no = int(row['src_quote_no'])

        src_media_attrs = get_src_attrs(src_guid)
        stance,domain,pub_date,is_AP = src_media_attrs['stance'],src_media_attrs['domain'],\
                                       src_media_attrs['date'],src_media_attrs['is_AP']
        rel_label = get_rel_stance_label(pred_label,stance)

        med_stance_.append(stance)
        domain_.append(domain)
        pub_date_.append(pub_date)
        is_AP_.append(is_AP)
        cc_abs_stance_.append(pred_label)
        cc_rel_stance_.append(rel_label)
        src_guid_.append(src_guid)
        src_sent_no_.append(src_sent_no)
        src_q_no_.append(src_q_no)

        context = get_context(src_guid,src_sent_no,src_q_no,
                              '../2_data_processing/url_quotes/')
        main_v_text = context['main_v'][0]
        verb_tense_.append(get_verb_tense(main_v_text))

        main_v_lemma = context['main_v_lemmas'][0]
        all_v = context['v_lemmas']
        other_v = all_v.copy()
        other_v.remove(main_v_lemma)
        verb_mods = get_verb_mods(other_v)
        verb_advs_.append(json.dumps(verb_mods['RB']))
        verb_mds_.append(json.dumps(verb_mods['MD']))
        verb_other_.append(json.dumps(verb_mods['VB']))

        main_s_lemma = context['main_s_lemmas']
        if len(main_s_lemma) > 0:
            main_subj_lemma_.append(main_s_lemma[0])
        else:
            main_subj_lemma_.append(None)
        subjs_.append(json.dumps(context['s_lemmas']))

        # Append particles
        if len(context['v_prt']) > 0:
            good_v_prts = list(set(context['v_prt']).intersection({'out'}))
            if len(good_v_prts) > 0:
                main_v_lemma += '_'+good_v_prts[0]

        verb_lemma_.append(main_v_lemma)

        neg_type = has_neg(context)
        neg_type_.append(neg_type)

        if pred_item_ix % 1000 == 0:
            print('Processed row:',pred_item_ix)

    main_df = pd.DataFrame({
        "guid":src_guid_,"sent_no":src_sent_no_,"quote_no":src_q_no_,
        "outlet_stance":med_stance_,"domain":domain_,"is_AP":is_AP_,"pub_date":pub_date_,
        "neg_type":neg_type_,"main_s_lemma":main_subj_lemma_,"s_lemmas":subjs_,
        "main_v_lemma":verb_lemma_,"tense":verb_tense_,"advs":verb_advs_,"modals":verb_mds_,"other_verbs":verb_other_,
        "quote":quote_,"rel_quote_stance":cc_rel_stance_,"abs_quote_stance":cc_abs_stance_
    })
    main_df['joined_s_lemmas'] = main_df['s_lemmas'].apply(lambda x: ' '.join(json.loads(x)).strip().replace(' - ',' ').lower())
    main_df['mwe_tok_s_lemmas'] = main_df['joined_s_lemmas'].apply(lambda x: json.dumps(tk.tokenize(x.split())))

    output_dir = 'test_output_dir'
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)

    main_df.to_pickle(os.path.join(output_dir,'quote_analysis_df.pkl'))
    print("Saving to {}...".format(os.path.join(output_dir,'quote_analysis_df.pkl')))
    print("Done!")


__author__ = 'brian'
import atom
import numpy as np

class MStructureObj:
    def __init__(self, data, num_species,num_beg_rules, num_cluster_rules, num_j_rules):
        data = data.split()
        self.num_beg_rules = num_beg_rules
        self.num_cluster_rules = num_cluster_rules
        self.num_j_rules = num_j_rules
        itter = 0
        self.composition = [0] * num_species
        self.num_Atoms = 0
        for i in range(num_species):
            self.num_Atoms += int(data[itter + 1])
            self.composition[i] = int(data[itter + 1])
            itter += 1
        self.phase_name = data[itter + 1]
        self.mag_phase = data[itter + 2]
        self.name = data[itter + 3]
        self.u = float(data[itter + 4])
        self.enrg = float(data[itter + 5])
        a = float(data[itter + 6])
        b = float(data[itter + 7])
        c = float(data[itter + 8])
        self.LCs = [a, b, c]
        find_c = [abs(b - c), abs(a - c), abs(a - b)]
        self.Cindex = find_c.index(min(find_c))
        self.C = self.LCs[self.Cindex]
        self.LCs[self.Cindex] = self.LCs[2]
        self.LCs[2] = self.C
        self.weight = 1.0
        self.BEG_sums = [0.0] * num_beg_rules
        self.Cluster_sums = [0.0] * num_cluster_rules
        self.J_sums = [0.0] * num_j_rules
        self.basis = []
        self.distances = np.ones([self.num_Atoms, self.num_Atoms * 27]) * 100
        self.mins = np.ones([self.num_Atoms, 10]) * 100

    def set_atom_properties(self, index, atom_data):
        atom_data = atom_data.split()
        mag = float(atom_data[1])
        pos = [round(float(atom_data[2]), 5), round(float(atom_data[3]), 5), round(float(atom_data[4]), 5)]
        self.basis.append(atom.AtomObj(index, self.composition, mag, pos, self.Cindex))

    def create_super_cell(self):
        for i in range(-1, 2):
            for j in range(-1, 2):
                for k in range(-1, 2):
                    if abs(i) + abs(j) + abs(k) != 0:
                        for l in range(self.num_Atoms):
                            atom_copy = self.basis[l]
                            mag = atom_copy.mag
                            pos = [atom_copy.a_pos + i, atom_copy.b_pos + j, atom_copy.c_pos + k]
                            self.basis.append(atom.AtomObj(l, self.composition, mag, pos))

    def calculate_distances(self):
        for i in range(self.num_Atoms):
            for j in range(len(self.basis)):
                dist_a = round(self.basis[i].a_pos - self.basis[j].a_pos, 5)
                dist_b = round(self.basis[i].b_pos - self.basis[j].b_pos, 5)
                dist_c = round(self.basis[i].c_pos - self.basis[j].c_pos, 5)
                self.distances[i, j] = round((dist_a ** 2 + dist_b ** 2 + dist_c ** 2) ** (0.5), 5)
                if self.distances[i, j] == 0:
                    self.distances[i, j] = 100

    def calculate_minimums(self):
        dists = [0] * len(self.basis)
        for i in range(self.num_Atoms):
            old_min = 0.0
            for j in range(len(self.basis)):
                dists[j] = self.distances[i, j]
            for j in range(10):
                self.mins[i, j] = self.next_min(dists, old_min)
                old_min = self.mins[i, j]

    def next_min(self, dists, old_min):
        for i in range(len(dists)):
            if round(dists[i], 5) <= old_min:
                dists[i] = 100
        new_min = np.min(dists)
        return new_min

    def check_plane(self, home_atom_index, neighbor_atom_index):
        if round(self.basis[home_atom_index].c_pos, 5) == round(self.basis[neighbor_atom_index].c_pos, 5):
            plane = 'IN'
        else:
            plane = 'OUT'
        if self.phase_name == 'aust':
            plane = 'ALL'
        return plane


# ZShura_Project2

def Startup():
    """The Start_up function begins the program and activates other functions as needed."""

    print("Hello! Welcome to The Hampshire College Trivia Game!\n")
    print("There are three sets of questions you can chose from: Anagrams, Quick Facts, and Presidents.")
    level = input("What set of questions would you like to play? ")
    level = level.lower()
    if level != "anagrams" or "quick facts" or "presidents":
        level = input("Set not properly chosen. Please input either Anagrams, Quick Facts, or Presidents: ")
        level = level.lower()
    if level == "anagrams" or "quick facts" or "presidents":
        print("Thank you for choosing", level, "!")
        return level, Level_choice(level)

def Level_choice(level):
    if level == "anagrams":
        from Project_Questions import Anagrams
        d = Anagrams()
        return d, Game(d)
    if level == "quick facts":
        from Project_Questions import Quick_facts
        d = Quick_facts()
        return d, Game(d)
    if level == "presidents":
        from Project_Questions import Presidents
        d = Presidents()
        print("Please be aware Presidents are listed by first name and last name, but not middle initial.")
        return d, Game(d)
    else:
        print("Incorrect input. Please try again.")
        return Startup()

def Game(d):
    """Runs the questions circuit depending on which questions were selected."""
    score = 0
    for key, value in d.items():
        answer = input(key)
        answer = answer.lower()
        while answer != value:
            answer = input("Incorrect, please try again: ")
            score -= 1
        else:
            print("Correct!")
            score += 1
    return score, End_Game(score)

def End_Game(score):
    """Will close out the program once it has been run through after providing the
player with their final score. It also offers a chance to play other sections of the game."""

    print("Good job! You have completed the trivia.\nYour final score is ", score)
    print()
    replay = input("Would you like to play a different level?: ")
    replay = replay.lower()
    if replay == "yes":
        return Startup()
    if replay == "no":
        print("Thanks for playing!")

d = Startup()


import sys
import random
from collections import deque
from config import *

class ContentStore():
    def __init__(self, topology):
        self.topology = topology
        self.library = {
            'a': {'base':[], 'enhancement':[] },
            'b': {'base':[], 'enhancement':[] },
            'c': {'base':[], 'enhancement':[] },
            'd': {'base':[], 'enhancement':[] },
            'e': {'base':[], 'enhancement':[] },
            'f': {'base':[], 'enhancement':[] },
            'g': {'base':[], 'enhancement':[] },
            'h': {'base':[], 'enhancement':[] },
            'i': {'base':[], 'enhancement':[] },
            'j': {'base':[], 'enhancement':[] },
            'k': {'base':[], 'enhancement':[] },
            'l': {'base':[], 'enhancement':[] }
        }
        self.caches = [switch for switch in self.topology.switches if random.random() < CACHE_PROBABILITY]
        # randomly allocate base and enhancement layers to the hosts initially
        for item, locations in self.library.iteritems():
            primaryHostCount = len(self.topology.hosts) - len(self.topology.switches)
            hostCount = random.randint(2, primaryHostCount * HOST_WITH_CONTENT_PROBABILITY)
            for index in range(0, hostCount):
                randomHost = [host for host in self.topology.hosts if not host.isCache][random.randint(0, primaryHostCount - 1)]
                if (index % 2 == 0):
                    if (randomHost not in locations['base']):
                        locations['base'].append(randomHost)
                else:
                    if (randomHost not in locations['enhancement']):
                        locations['enhancement'].append(randomHost)

    def find(self, content):
        if (content not in self.library):
            raise Exception(content + ' not found in the content store')
        cachedBaseLayers = [switch.cacheHost.name for switch in self.caches if (content in switch.cache['base'])]
        cachedEnhancementLayer = [switch.cacheHost.name for switch in self.caches if (content in switch.cache['enhancement'])]
        return {
            'content': content,
            'base': cachedBaseLayers + [host.name for host in self.library[content]['base']],
            'enhancement': cachedEnhancementLayer + [host.name for host in self.library[content]['enhancement']]
        }

    def updateCachesAlongPath(self, content, layer, path):
        for node in path:
            switchObject = self.topology.get(node)
            if (switchObject in self.caches and content not in switchObject.cache[layer]):
                # it's a LRU cache, so remove the first item if it's full
                if (len(switchObject.cache[layer]) == CACHE_SIZE):
                    switchObject.cache[layer].popleft()
                switchObject.cache[layer].append(content)

    def printLocations(self):
        sys.stderr.write('Current Cache Map:\n')
        for switch in self.caches:
            sys.stderr.write('  Switch ' + switch.name + ' (cache host: ' + switch.cacheHost.name + ')\n')
            sys.stderr.write('    Cached base layers: ' + ', '.join([str(item) for item in switch.cache['base']]) + '\n')
            sys.stderr.write('    Cached enhancement layers: ' + ', '.join([str(item) for item in switch.cache['enhancement']]) + '\n')
        sys.stderr.write('Content Locations:\n')
        for item, locations in self.library.iteritems():
            sys.stderr.write('  Content ' + item + ':\n')
            sys.stderr.write('    Base: ' + ', '.join(
                [l.name for l in locations['base']] +
                [switch.cacheHost.name for switch in self.caches if (item in switch.cache['base'])]
            ) + '\n')
            sys.stderr.write('    Enhancement: ' + ', '.join(
                [l.name for l in locations['enhancement']] +
                [switch.cacheHost.name for switch in self.caches if (item in switch.cache['enhancement'])]
            ) + '\n')
        sys.stderr.write('\n')


# -*- coding: utf-8 -*-
'''
Carta(card)
Cronometro(Stopwatch)
Puntajes(Score)
Modulo clases.py
'''
try:
    from tkinter import messagebox, Label, PhotoImage
    from archivos import buscar_registro
except ImportError as error:
    print(error)
PROCESO = 0

class Carta:
    '''
    has all attributes for the cards
    '''
    def __init__(self):
        self.valor = 0
        self.posicion = 0
        self.oculto = True
        self.foto = PhotoImage(file='imagenes/fondo.gif')

class Cronometro:
    '''
    Stopwatch Start in the fist click and end
    when the game is finished
    '''
    def __init__(self, ventana):
        self.ventana = ventana
        self.real_tiempo = ""
        self.time = Label()

    def iniciar(self, horas, minutos, segundos):
        '''
        Stopwatch start
        '''
        global PROCESO
        if segundos >= 60:
            segundos = 0
            minutos = minutos + 1
            if minutos >= 60:
                minutos = 0
                horas = horas + 1
                if horas >= 24:
                    horas = 0
        self.real_tiempo = (str(horas).zfill(2) + ":" +
                            str(minutos).zfill(2) + ":" +
                            str(segundos).zfill(2))
        self.repeticion()
        self.time['text'] = (str(horas) + ":" +
                             str(minutos) + ":" +
                             str(segundos))
        PROCESO = self.time.after(1000, self.iniciar, (horas),
                                  (minutos), (segundos + 1))

    def parar(self):
        '''Stopwatch stop
        '''
        global PROCESO
        self.time.after_cancel(PROCESO)

    def repeticion(self):
        '''Label for stopwatch
        '''
        Label(self.ventana, text=self.real_tiempo,
              bg="White",
              fg="red",
              font=("Times", 12)).place(x="380", y="120")

class Puntajes:
    '''save the data
    '''
    def __init__(self, nombre, tiempo, movimientos):
        self.nombre = "--" + nombre + "--"
        self.tiempo = "--" + tiempo + "--"
        self.movimientos = "--" + movimientos + "--"
        super().__init__()

    def guardar(self):
        '''name, time, moves will be save
        '''
        confirmacion = messagebox.askyesno(
            message=('Do you want to save your score?',
                     self.nombre, self.tiempo), title='Confirmación')
        if confirmacion:
            buscar_registro(self.nombre, self.tiempo, self.movimientos)
        else:
            print('Don´t worry you score is not save')


def parse_input(inputfile):
    heights = []
    with open(inputfile) as f:
        for l in f.readlines():
            row = [int(c) for c in l.strip()]
            heights.append(row)
    return heights

def get_adjacent_heights(x, y, heights):
    return [heights[ay][ax] for ax, ay in get_adjacent_points(x, y, heights)]

def get_adjacent_points(x, y, heights):
    deltas = [(0,1),(0,-1),(1,0),(-1,0)]
    candidates = [(x+dx,y+dy) for dx,dy in deltas] # orthogonally adjacent points
    max_x = len(heights[0])
    max_y = len(heights)
    # cull points outside of grid
    adj = [pt for pt in candidates if pt[0] >= 0 and pt[0] < max_x and pt[1] >= 0 and pt[1] < max_y]
    return adj

def p1(heights):
    low_points = []
    for y, row in enumerate(heights):
        for x, h in enumerate(row):
            adj_test = [h < v for v in get_adjacent_heights(x, y, heights)]
            if False not in adj_test:
                low_points.append((x,y))
    return low_points

# Recurse through points adjacent to (x,y), stopping at height 9
def get_basin(x, y, heights, visited):
    if (x,y) in visited:
        return
    visited.append((x,y))
    adj = get_adjacent_points(x, y, heights)
    for adj_x, adj_y in adj:
        if heights[adj_y][adj_x] != 9:
            get_basin(adj_x, adj_y, heights, visited)

def p2(low_points, heights):
    basins = []
    for low_x, low_y in low_points:
        visited = []
        basin_pts = get_basin(low_x, low_y, heights, visited)
        basins.append(visited)
        # print(f"Low point ({low_x}, {low_y}) has basin {visited} len {len(visited)}")
    big3 = sorted(basins, key=lambda b:len(b), reverse=True)[:3]
    print(f"Product of three largest basins = {len(big3[0]) * len(big3[1]) * len(big3[2])}")

if __name__ == "__main__":
    heights = parse_input("inputs/d9.txt")
    low_points = p1(heights)
    risk = sum([heights[y][x] + 1 for x,y in low_points])
    print(f"Total risk = {risk}")
    p2(low_points, heights)