import pytest
import cobra
import bayflux

''' This file tests repeated metabolite EMU computation
Example repeated metabolite reaction:
ACLS pyr + pyr --> alac-S + co2 cde + fgh : fgdhe + c

Here we create this as a one reaction model and confirm it's correct labeling in both directions
'''

def testForwardRepeatM():
    # create a blank model
    model = cobra.Model('repeatedM')

    # define metabolites
    pyr = cobra.Metabolite(
        'pyr',
        name='pyr',
        compartment='c')
    alacS = cobra.Metabolite(
        'alac-S',
        name='alac-S',
        compartment='c')
    co2 = cobra.Metabolite(
        'co2',
        name='co2',
        compartment='c')

    # define reactions and add them to the model
    ACLS = cobra.Reaction('ACLS')
    ACLS.lower_bound = 0
    ACLS.upper_bound = 500
    ACLS.add_metabolites({
        pyr: -2.0,
        alacS: 1.0,
        co2: 1.0,
    })
    model.add_reactions([ACLS])

    # Now we convert this model to a bayflux ReactionNetwork, which inherets 'EnhancedReaction' objects from each cobra.Reaction, allowing us to add in atom transitions
    model = bayflux.ReactionNetwork(model)

    # Here we set the atom transitions for each reaction, and show an example of viewing an EnhancedReaction which reports atom transitions
    m = {m.id:m for m in model.metabolites}

    model.reactions.ACLS.transitions = [bayflux.AtomTransition(
            ((m['pyr'], [1,2,3]),(m['pyr'], [4,5,6]),), # reactant labels
            ((m['alac-S'], [4,5,2,6,3]),(m['co2'], [1]),) # product labels
        )]
    # Here we represent fluxes as a FluxVector object, with fluxes ordered to match the model variables
    #
    # **Note**: non-reversible reactions should always carry zero reverse flux!
    fluxDict = {'ACLS': [1, 0]}
    fluxes = bayflux.FluxVector(model, fluxDict)

    # ### Now we define the substrate labeling vector, and test the ability to extract a mass distribution vector from this labeling
    # demo creation of a mass distribution vector for a binary labeling vector

    substrateLabelingDict = {
        m['pyr']:  ((1.0, [1, 0, 0]),),
    }

    # ### Run fully automated EMU simulation

    # #### First let's decompose the EMU network, and compile its structure into a list of matrix addition operations
    # EMUs to simulate in list form
    emusToSimulate = [bayflux.EMU(m['alac-S'],[0,1,2,3,4]), bayflux.EMU(m['co2'],[0])]

    compiledData = bayflux.emuCompile(emusToSimulate, model, substrateLabelingDict)
    results = bayflux.simulateLabeling(compiledData, fluxes, substrateLabelingDict)

    # confirm that CO2 is 100% labeled (from the first carbon of Pyr)
    assert results[1].tolist()[0] == [0.0, 1.0], 'incorrect computed labeling for repeated metabolite reaction'

    # confirm that alac-S is 100% 1 carbon labeled (from the first carbon of Pyr, coming from one of the two Pyrs)
    assert results[5].tolist()[0] == [0.0, 1.0, 0.0, 0.0, 0.0, 0.0], 'incorrect computed labeling for repeated metabolite reaction'

def testBackwardsRepeatM():
    # ## Now let's try reversing this and checking the labeling running backwards

    # create a blank model
    model = cobra.Model('repeatedM')

    # define metabolites
    pyr = cobra.Metabolite(
        'pyr',
        name='pyr',
        compartment='c')
    alacS = cobra.Metabolite(
        'alac-S',
        name='alac-S',
        compartment='c')
    co2 = cobra.Metabolite(
        'co2',
        name='co2',
        compartment='c')

    # define reactions and add them to the model
    ACLS = cobra.Reaction('ACLS')
    ACLS.lower_bound = 0
    ACLS.upper_bound = 500
    ACLS.add_metabolites({
        pyr: 2.0,
        alacS: -1.0,
        co2: -1.0,
    })

    model.add_reactions([ACLS])

    model = bayflux.ReactionNetwork(model)

    fluxDict = {'ACLS': [1, 0]}
    fluxes = bayflux.FluxVector(model, fluxDict)

    # create dict of metabolites by name
    # we use this instead of directly using the metabolite IDs above, because the conversion to
    # a bayflux.ReactionNetwork created new metabolite objects
    m = {m.id:m for m in model.metabolites}

    model.reactions.ACLS.transitions = [bayflux.AtomTransition(
            ((m['alac-S'], [4,5,2,6,3]),(m['co2'], [1]),), # reactant labels
            ((m['pyr'], [1,2,3]),(m['pyr'], [4,5,6]),) # product labels
    )]

    pyr = model.metabolites.get_by_id('pyr')
    outputEMU = bayflux.EMU(pyr,[0,1,2])
    result = bayflux.findProducingEMUTransitions(outputEMU)

    # check that we get two transitions for Pyruvate from this one reaction
    assert len(result) == 2, 'wrong number of producting transitions for repeated metabolite'

    substrateLabelingDict = {
        m['alac-S']: ((1.0, [0, 0, 0, 0, 0]),),
        m['co2']: ((1.0, [1]),)
    }

    # EMUs to simulate in list form
    emusToSimulate = [bayflux.EMU(m['pyr'],[0,1,2])]

    compiledData = bayflux.emuCompile(emusToSimulate, model, substrateLabelingDict)

    results = bayflux.simulateLabeling(compiledData, fluxes, substrateLabelingDict)

    # confirm that pyruvate is 50% unlabeled (fraction from alac-S) and 50% 1 carbon (fraction from CO2)
    assert results[3].tolist()[0] == [0.5, 0.5, 0.0, 0.0], 'incorrect computed labeling for repeated metabolite reaction'


# imports
def preprocess(url):
    import pandas as pd
    import numpy as np
    import re
    import html
    import string
    import emoji_processing as emo

    from bs4 import BeautifulSoup

    # import output file

    df_tweets = pd.read_csv(url)
    tweets_arr = []
    for item in df_tweets['Tweet']:
        tweets_arr.append(item)

    # pre-process
    tweets_clean = []

    for item in tweets_arr:
        soup = BeautifulSoup(item, 'html.parser')
        item = soup.get_text()
        item = item.lower()
        item = item.replace('rt', '')
        item = re.sub(r'http\S+', '', item)
        item = re.sub('@[^\s]+', '', item)
        item = "".join([char for char in item if char not in string.punctuation])
        item = re.sub('[0-9]+', '', item)
        item = re.sub('[^a-zA-Z]', ' ', item)
        tweets_clean.append(item)

    tweets_noemo = []
    for item in tweets_clean:
        x = emo.delete_emoji(item)
        tweets_noemo.append(x)

    return tweets_noemo

# for testing
def clean_tweets(arr):
    import re
    import string
    from bs4 import BeautifulSoup
    tweets_clean = []
    for item in arr:
        soup = BeautifulSoup(item, 'html.parser')
        item = soup.get_text()
        item = item.lower()
        item = item.replace('rt', '')
        item = re.sub(r'http\S+', '', item)
        item = re.sub('@[^\s]+', '', item)
        item = "".join([char for char in item if char not in string.punctuation])
        item = re.sub('[0-9]+', '', item)
        item = re.sub('[^a-zA-Z]', ' ', item)
        tweets_clean.append(item)
        return tweets_clean


import numpy as np

class SEIR(object):
    """ Implements the SEIR epidemiology model
        (Susceptibles, Exposed, Infectuos, Recovered)

    Learned params:
        alpha   = Incubation period (expressed as timesteps_incubation)
        beta    = Average contact rate
        gamma   = infectuos period (time before death or recovery or isolated)
                  (espressed as timesteps_infectuos)
    """

    def __init__(self):
        self.set_params()

    def type(self):
        return 'SEIR'

    def set_params(self,
                alpha_mu  = None,
                beta_mu   = None,
                gamma_mu  = None,
                alpha_rho = None,
                beta_rho  = None,
                gamma_rho = None):

        if alpha_mu is None:
            alpha_mu = np.random.uniform(0, 30)
        if beta_mu is None:
            beta_mu = np.random.uniform(1, 5)
        if gamma_mu is None:
            gamma_mu = np.random.uniform(0, 30)
        if alpha_rho is None:
            alpha_rho = np.random.uniform(0, 5)
        if beta_rho is None:
            beta_rho = np.random.uniform(0, 1)
        if gamma_rho is None:
            gamma_rho = np.random.uniform(0, 5)

        self._alpha_mu  = alpha_mu
        self._beta_mu   = beta_mu
        self._gamma_mu  = gamma_mu
        self._alpha_rho = alpha_rho
        self._beta_rho  = beta_rho
        self._gamma_rho = gamma_rho

    def get_params(self):
        return {
            'alpha_mu' : self._alpha_mu,
            'beta_mu' : self._beta_mu,
            'gamma_mu' : self._gamma_mu,
            'alpha_rho' : self._alpha_rho,
            'beta_rho' : self._beta_rho,
            'gamma_rho' : self._gamma_rho}

    def _step(self, S, E, I, R):
        """ Performs a step of the SEIR model
        Args:
            S,E,I,R (floats) Susceptibles, Exposed, Infectuos, Recovered
                             at timestep t
        Returns:
            S,E,I,R (floats) Susceptibles, Exposed, Infectuos, Recovered
                             at timestep t + 1

        """
        # sample weights from Normal distribution
        alpha = np.random.normal(self._alpha_mu, self._alpha_rho)
        beta = np.random.normal(self._beta_mu, self._beta_rho)
        gamma = np.random.normal(self._gamma_mu, self._gamma_rho)

        # we need the inverse of these params
        alpha = 1 / (alpha + np.finfo(np.float32).eps)
        gamma = 1 / (gamma + np.finfo(np.float32).eps)

        #Compute the SEIR results
        dS = -beta*S*I
        dE = beta*S*I - alpha*E
        dI = alpha*E - gamma*I
        dR = gamma*I
        S_next, E_next, I_next, R_next = S+dS, E+dE, I+dI, R+dR

        # Clip between 0 and 1 since data is supposed to be normalized
        S_next = np.clip(S_next, 0, 1)
        E_next = np.clip(E_next, 0, 1)
        I_next = np.clip(I_next, 0, 1)
        R_next = np.clip(R_next, 0, 1)
        return S_next, E_next, I_next, R_next

    def predict(self, S, E, I, R, timesteps):
        """ Predict with the SEIR model
        Args:
            S,E,I,R (floats) Susceptibles, Exposed, Infectuos, Recovered
                             at timestep t
            timesteps (int) How many timesteps to predict
        Returns:it,
                    params['alpha_mu'],
                    params['beta_mu'],
                    params['gamma_mu'],
                    params['alpha_rho'],
                    params['beta_rho'],
                    params['gamma_rho'], l)
            S,E,I,R (lists) of length  timesteps
        """
        S_pred = []
        E_pred = []
        I_pred = []
        R_pred = []

        for _ in range(timesteps):
            S, E, I, R = self._step(S, E, I, R)
            S_pred.append(S)
            E_pred.append(E)
            I_pred.append(I)
            R_pred.append(R)

        SEIR = np.stack([S_pred, E_pred, I_pred, R_pred]).T
        return SEIR


__author__ = 'Mahmoud Ahmed'
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
app = QApplication(sys.argv)
windo = QWidget()
windo.setWindowTitle('Hello PyQT')
windo.setWindowIcon(QIcon('pyqt.png'))
windo.setGeometry(50,50,600,400)

def show_colors():
    cd = QColorDialog.getColor()

bt = QPushButton(windo , text='Colors')
bt.move(200,180)
bt.resize(80,40)
bt.clicked.connect(show_colors)

windo.show()
app.exec_()


# This program is free software; you can redistribute and/or modify it
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

import os
from CvsntLoginfo import CvsntLoginfo
from ProjectConfig import ProjectConfig

# all 'intelligence' is implemented in the CvsntLoginfo class
config = ProjectConfig()
loginfo = CvsntLoginfo(config)

# create the databases
if not os.path.exists(config.call_db):
    loginfo.db_check_create_calls()
if not os.path.exists(config.changeset_db):
    loginfo.db_check_create_changeset()

# retrieve raw info from the loginfo hook that is calling us
loginfo.get_raw_info_from_hook()

# store raw info for replay/debug
loginfo.db_insert_call()

# parse raw info
loginfo.get_loginfo_from_argv()
loginfo.get_loginfo_from_stdin()

# insert changeset into the database (either insert new record or append to recent record with same commit log message)
loginfo.db_insert_append_changeset()

# now call trac (the cvsnt repository within trac will get the info from the record that we inserted above)
loginfo.trac_insert_changeset()


import logging

from fastapi import APIRouter, Depends, status, UploadFile
from fastapi.responses import JSONResponse
from typing_extensions import Annotated

from operationsgateway_api.src.auth.authorisation import authorise_route
from operationsgateway_api.src.channels.channel_manifest import ChannelManifest
from operationsgateway_api.src.error_handling import endpoint_error_handling
from operationsgateway_api.src.records.hdf_handler import HDFDataHandler
from operationsgateway_api.src.records.image import Image
from operationsgateway_api.src.records.ingestion_validator import IngestionValidator
from operationsgateway_api.src.records.record import Record
from operationsgateway_api.src.records.waveform import Waveform

log = logging.getLogger()
router = APIRouter()
AuthoriseRoute = Annotated[str, Depends(authorise_route)]

@router.post(
    "/submit/hdf",
    summary="Submit a HDF file for ingestion into MongoDB",
    response_description="ID of the record document that has been inserted/updated",
    tags=["Ingestion"],
)
@endpoint_error_handling
async def submit_hdf(
    file: UploadFile,
    access_token: AuthoriseRoute,
):
    """
    This endpoint accepts a HDF file, processes it and stores the data in MongoDB (with
    images being stored on disk). The HDF file should follow the format specified for
    the OperationsGateway project. Example files can be obtained via
    https://github.com/CentralLaserFacility/OG-HDF5, when you provide this tool with
    exported ecat data
    """

    log.info("Submitting CLF data in HDF file to be processed then stored in MongoDB")
    log.debug("Filename: %s, Content: %s", file.filename, file.content_type)

    hdf_handler = HDFDataHandler(file.file)
    record_data, waveforms, images = hdf_handler.extract_data()
    record = Record(record_data)

    stored_record = await record.find_existing_record()
    # TODO - when I implement the validation, it should only run if `stored_record`
    # actually contains something (i.e. isn't None)
    ingest_checker = IngestionValidator(record_data, stored_record)  # noqa: F841

    log.debug("Processing waveforms")
    for w in waveforms:
        waveform = Waveform(w)
        await waveform.insert_waveform()
        waveform.create_thumbnail()
        record.store_thumbnail(waveform)

    log.debug("Processing images")
    for i in images:
        image = Image(i)
        image.store()
        image.create_thumbnail()
        record.store_thumbnail(image)

    if stored_record:
        log.debug(
            "Record matching ID %s already exists in the database, updating existing"
            " document",
        )
        await record.update()
        return f"Updated {stored_record.id_}"
    else:
        log.debug("Inserting new record into MongoDB")
        await record.insert()
        return JSONResponse(
            record.record.id_,
            status_code=status.HTTP_201_CREATED,
            headers={"Location": f"/records/{record.record.id_}"},
        )

@router.post(
    "/submit/manifest",
    summary="Submit a channel manifest file for ingestion into MongoDB",
    response_description="ID of the channel metadata document inserted/updated into DB",
    tags=["Ingestion"],
)
@endpoint_error_handling
async def submit_manifest(
    file: UploadFile,
    access_token: AuthoriseRoute,
    bypass_channel_check: bool = False,
):
    log.info("Submitting channel manifest file into database")
    log.debug("Filename: %s, Content: %s", file.filename, file.content_type)

    channel_manifest = ChannelManifest(file.file)
    await channel_manifest.validate(bypass_channel_check)

    await channel_manifest.insert()
    return JSONResponse(
        channel_manifest.data.id_,
        status_code=status.HTTP_201_CREATED,
    )


import fileinput

def main():
    count = 0
    for line in fileinput.input():
        first, second = line.split(",")
        first = first.split("-")
        first = tuple(map(int, first))
        second = second.split("-")
        second = tuple(map(int, second))
        if overlaps(first, second):
            count += 1
    print(count)

def overlaps(one, other):
    """
    >>> overlaps((2,3),(4,5))
    False
    >>> overlaps((5,7),(7,9))
    True
    >>> overlaps((2,6),(4,8))
    True

       s1   e1
    s2   e2
           s2  e2
    s2         e2
    """
    s1, e1 = one
    s2, e2 = other
    return not (e2 < s1 or s2 > e1) and not (e1 < s2 or s1 > e2)

if __name__ == "__main__":
    main()


import time
import turtle
from turtle import Turtle, Screen
from cars import Cars, car_list
from racer import Racer
from finishline import RaceLine
from levels import Level

# a turtle crossing game. Get the turtle to the finish line without hitting a car. Move forward with spacebar

turtle.listen()
# generate screen
screen = Screen()
screen.setup(height=600, width=600)
screen.tracer(0)

# generate game objects
car = Cars()
racer_turtle = Racer()
finish_line = RaceLine(250)
start_line = RaceLine(-250)
level_start = Level()
# instructions = level_start.instructions()
# start game
game_on = True
while game_on:
    time.sleep(level_start.time_set)
    screen.update()
    for car in car_list:
        car.move()
        # generate one car after previous reaches a given coordinate
        if car_list[-1].xcor() in range(200, 250):
            car = Cars()
        # Detect if turtle collides with car, end game
        if car.distance(racer_turtle) <= 20:
            level_start.game_over()
            game_on = False
        # Reset turtle and increase speed of cars after crossing finish line
        if racer_turtle.ycor() > 250:
            racer_turtle.reset_pos()
            level_start.next_level()

    turtle.onkey(fun=racer_turtle.move_racer, key="space")

screen.exitonclick()


"""
This module contains the JSON Writer class.
"""

class JSONWriter(object):
    """
    A writer to write json formated strings.
    This class provides method to wite list, dicts and property values in json format.
    """
    def __init__(self, indent="\t"):
        """
        Initializes the JSONWriter.
        """
        self._default_prefix = ' ' * indent if type(indent) is int else indent

    @classmethod
    def dumps(cls, o, indent="\t", property_order=()):
        w = cls(indent=indent)
        _natives = int, long, float, bool, str, type(None)
        _iterables = list,

        props = dict((k, v) for k, v in o.items() if isinstance(v, _natives))
        tabs = dict((k, v) for k, v in o.items() if isinstance(v, _iterables))

        json_lines = w.get_dict_in_json_list(props, property_order)
        for range_lable, range_rows in tabs.items():
            datarange_json = w.write_datarange_from_rows(range_rows, 20) # todo get column width before
            prop_val = w.write_property_value(range_lable, datarange_json, value_in_quotes=False)
            json_lines.append(prop_val)
        ret = w.write_obj_from_lines(json_lines, new_line_separator=", \n")

        return ret

    def write_obj(self, properties, property_order, table_names, tables):
        """
        Writes an object, which consists of properties and tables in a json formated string.
        """
        lines = self.get_dict_in_json_list(properties, property_order)
        for i in xrange(len(table_names)):
            tab = self.write_table(tables[i])
            lines.append(self.write_property_value(table_names[i], tab, value_in_quotes=False))
        return self.write_obj_from_lines(lines)

    def get_dict_in_json_list(self, properties, property_order=[]):
        """
        Writes a dictionary into a list of json strings, where the given property
        order will be considered.
        Returns a list of strings.
        """
        json_props = []
        for prop in property_order:
            if prop in properties:
                json_props.append(self.write_property_value(prop, properties[prop]))
        for prop in properties.keys():
            if not prop in property_order:
                json_props.append(self.write_property_value(prop, properties[prop]))
        return json_props

    def write_property_value(self, prop, value, prefix_sign=None, value_in_quotes=True):
        """
        Writes the given property value pair in json formated string.
        It is checked, if the value is a float value, which is displayd without quote signs,
        otherwise is rounded with quotes,
        except the optional argument 'value_in_quotes' is set to false.
        """
        pre = prefix_sign if prefix_sign is not None else self._default_prefix
        val = self._in_quotes_if_necessary(value) if value_in_quotes else value
        return pre + self._in_quotes_if_necessary(prop) + ": " + val

    def write_table(self, table):
        """
        Writes a table (object which has col_keys, row_keys and
        a corresponding value(row_key, col_key) method)
        in a data range json, where each column is displayed in one line.
        Returns the json string.
        """
        rows = []
        header = [self._in_quotes_if_necessary(e) for e in table.col_keys]
        header.insert(0, '" "')
        rows.append("\t" + self.write_array_row(header))
        for row_key in sorted(table.row_keys):
            row = [self._in_quotes_if_necessary(table.value(row_key, c)) for c in table.col_keys]
            row.insert(0, self._in_quotes_if_necessary(row_key))
            rows.append(self.write_array_row(row))
        ret = self.write_obj_from_lines(rows, "[", "\t\t]", ",\n\t")
        return ret

    def write_list_as_line(self, the_list, entry_formatter=lambda s:s):
        """
        Writes the given list into a json line, where it is checked, if the list entries
        need to be rounded with quotes.
        Formats each list entry with the entry formatter.
        """
        line = [entry_formatter(self._in_quotes_if_necessary(v)) for v in the_list]
        return self.write_array_row(line)

    def write_objs(self, *objs):
        """
        Writes the params array of objs (in json format) as a json array.
        """
        return self.write_obj_from_lines(objs, left_envolop="[", right_envolop="]")

    def write_obj_from_lines(self, lines, left_envolop="{", right_envolop="}",
                             new_line_separator=",\n", left_new_line_envolop=True,
                             right_new_line_envolop=True):
        """
        Writes the given lines as separated string with the 'new_line_separator',
        with left and right envolop signs. Adds a new line at the end,
        if not 'right_new_line_envolop' is set to false.
        Using the default values, this method writes the lines (which should be in json format) as an
        json object.
        Using left_envolop = "[", right_envolop = "]" wites the lines as a json array.

        Returns a json string.
        """
        left = left_envolop + "\n" if left_new_line_envolop else left_envolop
        right = "\n" + right_envolop if right_new_line_envolop else right_envolop
        return left + new_line_separator.join(lines) + right

    def write_array_row(self, row_list):
        """
        Writes the row list of json strings as an array in one line.
        Returns a json string
        """
        return self.write_obj_from_lines(row_list, "\t[", "]", ", ", False, False)

    def write_json_from_rows(self, rows, col_width):
        """
        Writes an array of rows as json, where the col width is set to col_width signs.
        :param rows:
        :param col_width:
        :param json_writer:
        :return:
        """
        lines = []
        formatter = lambda s: s.ljust(col_width)
        for row in rows:
            lines.append("\t" + self.write_list_as_line(row, formatter))
        return self.write_obj_from_lines(lines, left_envolop="[", right_envolop="\t]")

    def write_datarange_from_rows(self, rows, entry_len):
        lines = []
        formatter = lambda s: s.ljust(entry_len)
        for row in rows:
            lines.append("\t" + self.write_list_as_line(row, formatter))
        return self.write_obj_from_lines(lines, left_envolop="[", right_envolop="\t]")

    def _in_quotes_if_necessary(self, value):
        if isinstance(value, bool):
            return self._in_quotes_if_necessary(str(value))
        if JSONWriter._is_a_number(value):
            return str(value)
        return '"' + str(value) + '"'

    def write_dict_to_json(self, dictionary, iteration_level=0):
        """
        writes the dictionary to json, recursively.
        :param dictionary:
        :param iteration_level:
        :return:
        """

        def _tabs():
            return iteration_level * "\t"

        lines = []
        for k, v in dictionary.items():
            if isinstance(v, dict):
                dict_json = self.write_dict_to_json(v, iteration_level + 1)
                line = _tabs() + self.write_property_value(k, dict_json, value_in_quotes=False)
                lines.append(line)
            elif isinstance(v, (list, tuple)):
                line = self.write_list_to_json(v, iteration_level + 1)
                lines.append(line)
            else:
                line = _tabs() + self.write_property_value(k, v)
                lines.append(line)
        return self.write_obj_from_lines(lines, right_envolop=_tabs() + "}")

    def write_list_to_json(self, value_list, iteration_level=0):
        """
        write the list to json, recursively.
        :param value_list:
        :param iteration_level:
        :return:
        """

        def _tabs():
            return iteration_level * "\t"

        lines = list()
        for v in value_list:
            if isinstance(v, dict):
                dict_json = self.write_dict_to_json(v, iteration_level + 1)
                lines.append(dict_json)
            elif isinstance(v, (list, tuple)):
                lines.append(self.write_list_to_json(v, iteration_level + 1))
            else:
                lines.append(self._in_quotes_if_necessary(v))
        return self.write_obj_from_lines(lines, _tabs() + "[", _tabs() + "]")

    @staticmethod
    def _is_a_number(value):
        if isinstance(value, (float, int)):
            return True
        '''
        if isinstance(value, str):
            val = value
            if val.startswith("-"):
                val = val[1:]
            val = val.replace(".", "")
            if val.isdigit():
                return True
        '''
        return False


# coding: utf-8

import requests
from bs4 import BeautifulSoup
import redis
import json

base_url = 'http://www.kuaidaili.com/free/inha/'

# 112.126.85.106:8118
# 113.209.31.29:8118
# 59.110.242.51:8118
# 120.26.199.103:8118
# 115.29.170.58:8118

# db = redis.Redis(host='localhost', port=6379, db='ip-pool')

def get_page_data(page_num):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
        'host': 'www.kuaidaili.com',
        'connection': 'close',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'accept-Encoding': 'gzip, deflate',
        'accept-Language': 'zh-CN,zh;q=0.8'
    }
    try:
        r = requests.get('%s/%s' % (base_url, page_num), headers=headers,
                         proxies={'http': 'http://120.26.199.103:8118'})
        if r.status_code != 200:
            r = get_page_data(page_num)
    except:
        r = get_page_data(page_num)
    return r

def get_total_page(page_data):
    soup = BeautifulSoup(page_data, 'html5lib')
    dom_list = soup.select('#listnav a')
    total_page_dom = dom_list[-2]
    return total_page_dom.text

def error_handler(current_page):
    state_code = 0
    while state_code != 200:
        r = get_page_data(current_page)
        state_code = r.status_code
    return r

def test_survive_ip(ip, port='80', protocol='http'):
    protocol = protocol.lower()
    test_list = [
        'http://47.52.0.122',
        'https://www.bilibili.com',
        'http://183.232.231.173',
        'http://www.alibaba.com',
        'http://www.gov.cn',
        'http://www.kuaidaili.com'
    ]
    for i in test_list:
        try:
            print(i)
            requests.get(i, proxies={protocol: '%s://%s:%s' % (protocol, ip, port)}, timeout=3)
        except:
            print('ip: %s:%s error' % (ip, port))
            return False
    print('ip: %s:%s success' % (ip, port))
    return True

def get_survive_ip_list(page_data):
    soup = BeautifulSoup(page_data, 'html5lib')
    dom_list = soup.select('#list tr')
    for dom in dom_list[1:]:
        ip = BeautifulSoup(str(dom.select('td[data-title="IP"]')[0]), 'html5lib').select('body')[0].text
        port = BeautifulSoup(str(dom.select('td[data-title="PORT"]')[0]), 'html5lib').select('body')[0].text
        protocol = BeautifulSoup(str(dom.select('td[data-title="类型"]')[0]), 'html5lib').select('body')[0].text
        if test_survive_ip(ip, port, protocol):
            with open('./../ip.txt', 'a') as f:
                f.write('%s:%s' % (ip, port))
                # if test_survive_ip(ip, port, protocol):
                # save(ip, port, protocol)

# def save(ip, port, protocol):
#     value = json.dumps(dict(port=port, protocol=protocol))
#     db.set(ip, value)

def deal_page(current_page):
    r = get_page_data(current_page)
    page_data = r.text
    get_survive_ip_list(page_data)
    return get_total_page(page_data)

def main():
    current_page = 1
    total_page = deal_page(current_page)
    while int(current_page) < int(total_page):
        current_page += 1
        deal_page(current_page)

if __name__ == '__main__':
    main()


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

def lazy_sum(l):
    def sum():
        ax = 0
        for n in l:
            ax = ax + n
        return ax
    return sum

l = [1, 3, 5, 7, 9]

print(l)

f1 = lazy_sum(l)
print(f1())
f2 = lazy_sum(l)
print(f2())

print(f1 == f2)

def count():
    fs = []
    for i in range(1, 4):
        def f():
             return i*i
        fs.append(f)
    return fs

f1, f2, f3 = count()
print(f1())
print(f2())
print(f3())

print('-------------')

l = [1]

def func(L):
    print('func')
    def fun():
        print('fun')
        return L[0]
    return fun

f1 = func(l)
print(f1())
l[0] = 3
print(f1())

print('-------------')

def count_1():
    def f(j):
        def g():
            return j*j
        return g
    fs = []
    for i in range(1, 4):
        fs.append(f(i)) # f(i)立刻被执行，因此i的当前值被传入f()
    return fs

f1, f2, f3 = count_1()
print(f1())
print(f2())
print(f3())


n = int(input("Введите целое число больше 0\n"))
if n < 0:
    print("Число должно быть больше нуля!")
    exit(10)
i = 0
n = str(n)
num = 0
maximum = 0
while True:
    if i == len(n):
        break

    if int(n[i]) > int(maximum):
        maximum = n[i]
    num = n[i]
    i += 1
print("Наибольшая цифра в введенном числе:", maximum)


import math

_2PI = 2. * math.pi

class Desc: pass
E = Desc()

E.RESET = 'R'
E.STOP = 's'
E.MOTOR = 'd'
E.RESET_TICKS = 'p,0,0'
E.READ_TICKS = 'q'
E.NULL = 'a'
E.CAMERA = 'I'

class Params:
    def __init__(self, wheel_radius, distance_between_wheels, devices,
                 k_forward, k_rotation, delta_angle=0):
        self.wheel_radius = wheel_radius
        self.distance_between_wheels = distance_between_wheels
        self.devices = devices
        self.k_forward = k_forward
        self.k_rotation = k_rotation
        self.delta_angle = delta_angle

E.BOT = {}
E.BOT[2107] = Params(0.02076, 0.05365, '/dev/rfcomm2', 0.00005, 0.001)
E.BOT[2150] = Params(0.02069, 0.05290, '/dev/rfcomm1', 0.0000652, 2 * 0.0000652/0.05290)
#E.BOT[2187] = Params(0.02069*0.9, 0.05249, '/dev/rfcomm0', 0.0000652, 2 * 0.0000652/0.05249, delta_angle=0.699842)
E.BOT[2187] = Params(0.02069, 0.05249, '/dev/rfcomm0', 0.0000652*1., 2 * 0.0000652/0.05249*1.02 , delta_angle=0.397829)

E.HUB = Desc()
E.HUB.HOST = 'localhost'
E.HUB.PORT = 50101

E.DATA = Desc()
E.DATA.INIT = 'init'
E.DATA.STATE = 'state'
E.DATA.STOP = 'stop'
E.DATA.U = 'u'

E.NUMBER_OF_BEACON = 4

def limit(value, limit):
    if value > limit:
        value = limit
    elif value < -limit:
        value = -limit
    return value

def fetch_angle_to_2pi(angle):
    while angle > _2PI:
        angle -= _2PI
    while angle < 0:
        angle += _2PI
    return angle

def get_delta_angle(angle_1, angle_2):
    delta_angle = abs(angle_1 - angle_2)
    while delta_angle > _2PI:
        delta_angle -= _2PI
    return delta_angle

def angle_to_pi(angle_rad):
    angle_rad = angle_rad % _2PI
    if angle_rad > math.pi:
        angle_rad -= _2PI
#    while (angle_rad > math.pi):
#        angle_rad -= _2PI
#    while (angle_rad < -math.pi):
#        angle_rad += _2PI
    return angle_rad

def angle_to_2pi(angle_rad):
    while (angle_rad > _2PI):
        angle_rad -= _2PI
    while (angle_rad < -_2PI):
        angle_rad += _2PI
    return angle_rad

def get_phi_ref(delta_x, delta_y):
    if delta_y != 0:
        phi_0 = math.atan(delta_x/delta_y)
    if delta_x < 0 and delta_y < 0:
        phi_ref = phi_0
    elif delta_x < 0 and delta_y > 0 or delta_x >= 0 and delta_y > 0:
        phi_ref = phi_0 + math.pi
    elif delta_x > 0 and delta_y < 0:
        phi_ref = phi_0 + 2. * math.pi
    elif delta_x > 0 and delta_y == 0:
        phi_ref = 3./2. * math.pi
    elif delta_x < 0 and delta_y == 0:
        phi_ref = math.pi / 2.
    elif delta_x == 0:
        phi_ref = 0.
    return phi_ref

def get_delta_phi(delta_phi):
    while delta_phi > math.pi:
        delta_phi -= _2PI
    while delta_phi < -math.pi:
        delta_phi += _2PI
    return delta_phi


import time
Name = input('Name: ')
print("Hello " + Name + "! We are going to find out how long you have been alive!")
Age = int(input("What is your age? "))
print("You are " + str(Age) + " years old.")
Months = Age*12
Days = Age*365
print(Name + " has been alive for about: " + str(Months) + " months or " + str(Days) + " days!")
time.sleep(.5)
Minutes = Age*525948
Seconds = Age*31556926
print("That would be " + str(Minutes) + " minutes, or " + str(Seconds) + " seconds!")


# --- Anh Tran --- #
# This file gives a basic GUI to help user to test out commands, plotting the position of the agents in real time.

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import pyqtgraph.opengl as gl
import numpy as np

import time
from threading import Timer

import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig
from cf_drone_interface import cf_control

class PlotWindow(QDialog):
	"""docstring for PlotWindow"""
	def __init__(self, *args, **kwargs):
		super(PlotWindow, self).__init__(*args, **kwargs)

		uic.loadUi('plotter.ui', self)

		self.gl_widget.setWindowTitle('Multi-agent Plotter')
		self.addAxis()
		self.gl_widget.addItem(gl.GLGridItem()) # Add the grid to the plot

		# Initialise mesh item to represent an agent
		md = gl.MeshData.sphere(rows=5, cols=5, radius=0.25)
		self.agent_mesh = gl.GLMeshItem(meshdata=md, smooth=False, drawFaces=False, drawEdges=True, edgeColor=(1,1,1,1))

		self.gl_widget.addItem(self.agent_mesh)

		self.startplotButton.clicked.connect(self.update_plot)

		self.prev_x = 0
		self.prev_y = 0
		self.prev_z = 0

	def show_plotter(self):
		self.show()
		self.gl_widget.show()

	def addAxis(self):
		x_axis = gl.GLLinePlotItem()
		y_axis = gl.GLLinePlotItem()
		z_axis = gl.GLLinePlotItem()

		x_line = np.array([[0,0,0],[10,0,0]])
		y_line = np.array([[0,0,0],[0,10,0]])
		z_line = np.array([[0,0,0],[0,0,10]])

		x_color = np.array([255,0,0,0.75])
		y_color = np.array([0,255,0,0.75])
		z_color = np.array([0,0,255,0.75])

		width = 2
		mode = 'lines'

		x_axis.setData(pos=x_line, color=x_color, width=width, antialias=True, mode=mode)
		y_axis.setData(pos=y_line, color=y_color, width=width, antialias=True, mode=mode)
		z_axis.setData(pos=z_line, color=z_color, width=width, antialias=True, mode=mode)

		self.gl_widget.addItem(x_axis)
		self.gl_widget.addItem(y_axis)
		self.gl_widget.addItem(z_axis)

	def add_agent(self, crazyflie):
		self.cf = crazyflie

	def update_plot(self):
		dx = (self.cf.x - self.prev_x)*3
		dy = (self.cf.y - self.prev_y)*3
		dz = (self.cf.z - self.prev_z)*3

		self.prev_x = self.cf.x
		self.prev_y = self.cf.y
		self.prev_z = self.cf.z

		self.agent_mesh.translate(dx,dy,dz)
		# self.agent_mesh.translate(1,1,1,local=True)
		print(f"\n[dx, dy, dz]: [{dx},{dy},{dz}]")

		Timer(0.1,self.update_plot).start()

class CommWindow(QMainWindow):
	"""docstring for MainWindow"""
	def __init__(self, *args, **kwargs):
		super(CommWindow, self).__init__(*args, **kwargs)

		self.setWindowTitle("Multiagent Communication")
		uic.loadUi('crazyflie_multiagent.ui', self)

		self.PlotWindow = PlotWindow()

		self.cfs = []
		self.selected_cf = None

		self.btn_exit.clicked.connect(self.btn_exit_clicked)
		self.btn_scan.clicked.connect(self.btn_scan_clicked)
		self.btn_connectall.clicked.connect(self.btn_connect_all_clicked)
		self.btn_snd_cmd.clicked.connect(self.btn_snd_cmd_clicked)
		self.btn_snd_pos_cmd.clicked.connect(self.btn_snd_pos_cmd_clicked)
		self.action3D_visualiser.triggered.connect(self.open_visualiser)

	def btn_exit_clicked(self):
		print("Program closed.")
		for i in range(len(self.cfs)):
			self.cfs[i]._cf.close_link()
		sys.exit()

	def btn_scan_clicked(self):
		# Scan for Crazyflies and use the first one found
		print('Scanning interfaces for Crazyflies...')
		available = cflib.crtp.scan_interfaces()

		if len(available) > 0:
			self.radio_items = [item[0] for item in available]
			self.combobox_drone.clear()
			self.combobox_drone.addItems(self.radio_items)
			print('Crazyflies found:')
			for i in available:
				print(i[0])

		else:
			print('No Crazyflies found :(')

	def btn_connect_all_clicked(self):
		for i in range(len(self.radio_items)):
			cf = cf_control(self.radio_items[i])
			self.cfs.append(cf)
			while(cf.is_connected == False):
				continue
			cf = None

		if (len(self.cfs) > 0):
			self.selected_cf = self.cfs[0]
			self.PlotWindow.add_agent(self.selected_cf)

	def comboDrones_index_changed(self):
		self.selected_cf = cfs[self.combobox_drone.currentIndex()]
		self.PlotWindow.add_agent(self.selected_cf)

	def btn_snd_pos_cmd_clicked(self):
		x = float(self.edit_x.text())
		y = float(self.edit_y.text())
		z = float(self.edit_z.text())
		yaw = float(self.edit_yaw.text())

		self.selected_cf.set_position_command_param(x, y, z, yaw)
		self.selected_cf.send_position_command()

	def btn_snd_cmd_clicked(self):
		roll = int(self.edit_roll.text())
		pitch = int(self.edit_pitch.text())
		yawrate = int(self.edit_yawrate.text())
		thrust = int(self.edit_thrust.text())

		self.selected_cf.new_command = 1
		self.selected_cf.set_control_command_param(roll, pitch, yawrate, thrust)
		self.selected_cf.send_control_command()

	def open_visualiser(self):
		# self.PlotWindow.show()
		self.PlotWindow.show_plotter()

if __name__ == "__main__":
	# Initialize the low-level drivers
	cflib.crtp.init_drivers(enable_debug_driver=False)

	app = QApplication(sys.argv)

	window = CommWindow()

	window.show()
	app.exec_()


class Graph:
    def __init__(self):
        self.graph = dict()
        self.path_all = []
        self.n = 0

    def add_edge(self, u, v):
        if u not in self.graph.keys():
            self.graph[u] = [v]
        else:
            self.graph[u].append(v)

    def bfs(self, start, end):
        queue = [ [start, [start]] ]  # current node, current path
        while queue:
            curr_node, curr_path = queue.pop(0)
            if curr_node == end:
                print("find path", curr_path)

            if curr_node not in self.graph.keys():
                continue

            children = self.graph[curr_node]
            for child in children:
                # must copy so that each child path is independent
                path = curr_path.copy()
                if child not in curr_path:
                    path.append(child)
                    queue.append([child, path])


#!/usr/bin/env python3
#author : MIGUEL ANGEL GÓMEZ GORDILLO
#email : mangel-95@hotmail.com
#url : https://github.com/mangel2095

import sys
from PyQt5 import QtWidgets , QtGui
from GUI import mainwindow
from math import ceil
#Hasta acá se escribe siempre
#Se crea la clase que va a heredar propiedades de la clase QMainWindow
class MainWindowClass(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindowClass, self).__init__()
        self.ui = mainwindow.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton_calcular.clicked.connect(self.calculo)

        #Para permitir unicamente digitos en las entradas de los QLineEdit:
        onlyNumber = QtGui.QDoubleValidator()
        self.ui.lineEdit_ancho.setValidator(onlyNumber)
        self.ui.lineEdit_a.setValidator(onlyNumber)
        self.ui.lineEdit_b.setValidator(onlyNumber)
        self.ui.lineEdit_costo.setValidator(onlyNumber)

    def calculo(self):
        # Get Data
        a = float(self.ui.lineEdit_a.text())/100
        b = float(self.ui.lineEdit_b.text())/100
        ancho = float(self.ui.lineEdit_ancho.text())/100
        try:
            costoTela = float(self.ui.lineEdit_costo.text())
        except:
            pass

        uni_ancho= int(ancho/b)
        metros = 1
        while True:
            n = metros/a
            n_int = int(n)

            if n==n_int:
                uni_largo = n_int
                total_uni = uni_largo*uni_ancho
                unidad_metro = int(total_uni/metros)
                try:
                    costo_unidad = (costoTela/unidad_metro)
                    costo_unidad = ceil(costo_unidad) #round up
                except:
                    costo_unidad = 'No Info'

                self.ui.label_totalPiezas.setText(str(total_uni))
                self.ui.label_totalMetros.setText(str(metros))
                self.ui.label_piezasXmetro.setText(str(unidad_metro))
                self.ui.label_costoPieza.setText(str(costo_unidad))
                break
            else:
                metros = metros +1

aplicacion = QtWidgets.QApplication(sys.argv) ##esto es estandar

## Se crea el objeto de la clase MainWindowClass y se muestra en la pantalla
ventana = MainWindowClass()
ventana.show()
aplicacion.exec_() ## esto es estandar


import pygame
import tile
from constants import *
# Used for extracting the surfaces and creating tiles from the sprite sheet

class SpriteSheet(pygame.sprite.Sprite):
    """
        Constructor method, creates a Sprite sheet object used for tile creation\n
        :param filepath: the path for the tiles sprite sheet that must have the tiles places in line and at equal
            distance set by the constant TILE_GAP
    """

    def __init__(self, filepath):
        super().__init__()
        self.filepath = filepath
        try:
            self.sprite_sheet = pygame.image.load(filepath).convert()
        except pygame.error as sprite_load_error:
            print(f"Unable to load sprite sheet image:{filepath}")
            raise SystemExit(sprite_load_error)

    def sprite_at(self, identifier,
                  sprite_sheet_x, sprite_sheet_y, tile_width, tile_height, on_screen_x, on_screen_y, layer, tile_type):
        """
        Function responsible for creating a singular Tile object with specific zoom factor, color correction and surface
        and rectangle grouping\n
        :param identifier: unique number for identifying tiles
        :param sprite_sheet_x: the x coordinate in the sprite sheet
        :param sprite_sheet_y: the y coordinate in the sprite sheet
        :param tile_width: the width of the tile
        :param tile_height: the height of the tile
        :param on_screen_x: the absolute x position of the tile on screen
        :param on_screen_y: the absolute y position of the tile on screen
        :param layer: the height attribute of the tile object, used for interactions
        :param tile_type: the type of the tile object, specifies the relative position of the tile in the sprite sheet
            and the the types that can match with it
        :return: tile.Tile()
        """

        self.image = pygame.Surface((tile_width, tile_height))
        # Takes image transparency into account
        # Links a surface with the current sprite
        self.image.blit(self.sprite_sheet, (0, 0), pygame.Rect(sprite_sheet_x, sprite_sheet_y, tile_width, tile_height))
        self.image = pygame.transform.rotozoom(self.image, 0, ZOOM_FACTOR)
        self.rect = self.image.get_rect()
        self.image.set_colorkey(TRANSPARENCY_COLOR)
        self.rect.center = [on_screen_x, on_screen_y]
        # self.image = pygame.transform.scale(self.image, (100, 100))
        new_tile = tile.Tile(identifier, self.image, self.rect, self.rect.center, layer, tile_type)
        return new_tile

    def all_sprites(self, mahjong_tiles_dictionary, middle_x, middle_y):
        """
        Function responsible for creating a list of Tile objects by reading a dictionary with tuples of information\n
        :param mahjong_tiles_dictionary: a dictionary with a unique identifier that has attached the relative
            coordinates, tile layer and tile type in a tuple\n
        :param middle_x: pygame.screen middle x
        :param middle_y: pygame.screen middle y
        :return: list of tile.Tile()
        """

        # returns a vector of
        return [
                [self.sprite_at(
                    identifier,
                    (TILE_WIDTH+TILE_GAP)*sprite_type,    # x position in the sprite sheet image
                    0,                                    # y position
                    TILE_WIDTH,
                    TILE_HEIGHT,
                    (middle_x+TILE_WIDTH/2*x)*ZOOM_FACTOR,     # surface rectangle center x
                    (middle_y+TILE_HEIGHT/2*y)*ZOOM_FACTOR,    # surface rectangle center y
                    layer,                                # the tile layer/height
                    sprite_type),                         # the sprite type (the order in the sprite sheet line)
                    (middle_x+TILE_WIDTH/2*x)*ZOOM_FACTOR, (middle_y+TILE_HEIGHT/2*y)*ZOOM_FACTOR]
                for identifier, (layer, x, y, sprite_type) in mahjong_tiles_dictionary.items()
        ]


#运行时间：3284ms   占用内存：24516k
#归并排序
# -*- coding:utf-8 -*-
count = 0
class Solution:
    def InversePairs(self, data):
        global count
        def MergeSort(lists):
            global count
            if len(lists) <= 1:
                return lists
            num = int( len(lists)/2 )
            left = MergeSort(lists[:num])
            right = MergeSort(lists[num:])
            r, l=0, 0
            result=[]
            while l<len(left) and r<len(right):
                if left[l] < right[r]:
                    result.append(left[l])
                    l += 1
                else:
                    result.append(right[r])
                    r += 1
                    count += len(left)-l
            result += right[r:]
            result += left[l:]
            return result
        MergeSort(data)
        return count%1000000007

剑指Offer/数组中的逆序对.py


import json
import requests
from bs4 import BeautifulSoup

print("Collected Players...\n Now collecting their details...")
print("We are trying our best...")

readLink = []
with open("player.json", "r") as read_file:
    readLink = json.load(read_file)

start_urls=[]
player_names=[]

for link in readLink:
    dia = link['link']
    dia = dia[12:]
    start_urls.append('http://stats.espncricinfo.com/ci/engine/{val}?class=2;template=results;type=batting'.format(val = dia))
    player_names.append(link['text'])

def check(runs):
    try:
        int(runs)
    except:
        return 'error'

def check(runs):
    try:
        int(runs)
    except:
        return 'error'

def detcall(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    results = soup.find_all('table', attrs={'class': 'engineTable'})
    result2= results[3].find_all('tbody')
    result3=result2[4].find_all('tr', attrs={'class': 'data1'})

    #check year
    yearres=str(result3[1].find('b').text)
    if('year' not in yearres):
        result3=result2[5].find_all('tr', attrs={'class': 'data1'})

    result3=result3[1:]

    #check run
    runwrapcheck=False
    siblingres=[]
    for sibling in result3[0].td.next_siblings:
        siblingres.append(repr(sibling))
#    print(siblingres)
    runyeartagind=9
#    print(runyeartagind)
    runyeartag=siblingres[runyeartagind]
    start=runyeartag.find('')
    end=runyeartag.find('	')
    runsres=runyeartag[start+4: end]
    if (check(runsres) == 'error'):
        if('nowrap' in runsres):
            runwrapcheck=True
        else:
            runyeartagind=7
#        print('changed',runyeartagind)

    dictres={}
    for res in result3:
        #find year
        yearres=str(res.find('b').text)
        # print(yearres)
        # if('year' not in yearres):
        #     return {}

        #find runs in that year
        siblingres=[]
        for sibling in res.td.next_siblings:
            siblingres.append(repr(sibling))
        runyeartag=siblingres[runyeartagind]
        start=runyeartag.find('')
        end=runyeartag.find('	')
        runsres=runyeartag[start+4: end]
        #add result found to the result dictionary
        if('nowrap' in runsres):
            runsres='0'
        dictres[yearres]=runsres

    return dictres

final_res={}

for i in range(len(player_names)):
    print(player_names[i], start_urls[i], i)
    detrun=detcall(start_urls[i])
    print(player_names[i], detrun)
    final_res[player_names[i]]=detrun

with open("finalresfile.json", "w") as write_file:
    json.dump(final_res, write_file)


import time
import sys
import json
import nmap

from core.languages.python.base.PythonJobWithBackend import PythonJobWithBackend

class MachineDiscovery(PythonJobWithBackend):
    def __init__(self, input_args, sleep_seconds=5.0):
        super().__init__(input_args)
        self.sleep_seconds = sleep_seconds

        self.ip_address = self.redis_client.get("ip_address")
        self.scan_string = ".".join(self.ip_address.split(".")[:-1]) + ".0/24"

    def run_job(self):
        while True:
            try:
                if self.ip_address != "127.0.0.1":
                    network_scanner = nmap.PortScanner()
                    network_scanner.scan(self.scan_string, arguments="-sP -n")
                    machines_on_network = network_scanner.all_hosts()

                    p = self.redis_client.strict_redis.pipeline()
                    p.delete("machines_on_network")

                    if machines_on_network is not None:
                        if len(machines_on_network) > 0:
                            p.rpush(
                                "machines_on_network",
                                *[json.dumps(ip) for ip in machines_on_network]
                            )

                    p.execute()

            except Exception as e:
                print(e)

            time.sleep(self.sleep_seconds)

if __name__ == "__main__":
    MachineDiscovery(sys.argv).run()


def improveLabels(val):

    for u in S:
        lu[u] -= val
    for v in V:
        if v in T:
            lv[v] += val
        else:
            minSlack[v][0] -= val

def improveMatching(v):

    u = T[v]
    if u in Mu:
        improveMatching(Mu[u])
    Mu[u] = v
    Mv[v] = u

def slack(u, v): return lu[u] + lv[v] - w[u][v]

def solve():

    while True:
        ((val, u), v) = min([(minSlack[v], v) for v in V if v not in T])

        if val > 0:
            improveLabels(val)

        T[v] = u  # add (u,v) to the tree
        if v in Mv:
            u1 = Mv[v]  # matched edge,

            S[u1] = True  # ... add endpoint to tree
            for v in V:  # maintain minSlack
                if not v in T and minSlack[v][0] > slack(u1, v):
                    minSlack[v] = [slack(u1, v), u1]
        else:
            improveMatching(v)
            return

def result(weights):

    global U, V, S, T, Mu, Mv, lu, lv, minSlack, w
    w = weights
    n = len(w)

    U = V = range(n)
    lu = [max([w[u][v] for v in V]) for u in U]
    lv = [0 for v in V]

    Mu = {}
    Mv = {}

    while len(Mu) < n:
        free = [u for u in V if u not in Mu]
        u0 = free[0]
        S = {u0: True}
        T = {}
        minSlack = [[slack(u0, v), u0] for v in V]
        solve()

    val = sum(lu) + sum(lv)
    return val

n = 3
w = [[0 for v in range(n)] for u in range(n)]
w=[[15.9, 4, 5],
   [5, 7, 6],
   [5, 8, 8]]

print(result(w))


# 谢尔排序：以插入排序为基础，对无序表进行“间隔”划分子列表，每个子列表进行插入排序，复杂度介于n和n*n之间
def shellSort(alist):
    # 间隔设定，间隔越来越小，则子列表数量越来越少，无序表的整体越来越有序，从而减小整体比对次数
    sublistcount=len(alist)//2
    while sublistcount>0:
        for startposition in range(sublistcount):
            # 子列表排序
            gapInsertionSort(alist,startposition,sublistcount)
        print("After increments of size",sublistcount,"The list is ",alist)
        # 间隔缩小
        sublistcount=sublistcount//2
    return alist

def gapInsertionSort(alist,start,gap):
    # 参考插入排序对比即可
    for i in range(start+gap,len(alist),gap):
        currentvalue=alist[i]
        position=i

        while position>=gap and alist[position-gap]>currentvalue:
            alist[position]=alist[position-gap]
            position=position-gap
        alist[position]=currentvalue

alist=[54,26,93,17,77,31,44,55,20]
print(shellSort(alist))


import sys

from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QDialog, QMessageBox
from module6.league_database import LeagueDatabase
from module6.team import Team
from module6.team_member import TeamMember
from module6.custom_exceptions import DuplicateEmail

Ui_MainWindow, QtBaseWindow = uic.loadUiType("team_edit_dialog.ui")

class TeamEditDialog(QtBaseWindow, Ui_MainWindow):
    """
    Create TeamEditDialog class.
    """
    def __init__(self, league_selected, team_selected, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.league_in = league_selected
        self.team_in = team_selected
        self.add_button.clicked.connect(self.add_button_clicked)
        self.delete_button.clicked.connect(self.delete_button_clicked)
        self.update_button.clicked.connect(self.update_button_clicked)
        for tm in self.team_in.members:
            self.team_listWidget.addItem(str(tm))

    def add_button_clicked(self):
        """
        Create add_button_clicked method to add functionality to the add button.
        """
        name_string_in = self.name_lineEdit.text()
        if name_string_in == '':
            self.warn("Add Team Member", "Please give the team member a name")
        email_string_in = self.email_lineEdit.text()
        if email_string_in == '':
            self.warn("Add Team Member", "Please give the team member an email")
        if name_string_in != '' and email_string_in != '':
            # LeagueDatabase.instance()
            for l in LeagueDatabase.instance().leagues:
                if self.league_in == l:
                    for tm in l.teams:
                        if tm == self.team_in:
                            try:
                                tm.add_member(TeamMember(LeagueDatabase.instance().next_oid(), name_string_in, email_string_in))
                            except DuplicateEmail:
                                return self.warn("Add Error", "Attempted to add a team member with a duplicate email")
        self.update_ui()

    def update_ui(self):
        """
        Create update_ui method to update the listWidget at the end of a method.
        """
        self.team_listWidget.clear()
        # LeagueDatabase.instance()
        for l in LeagueDatabase.instance().leagues:
            if self.league_in == l:
                for l2 in l.teams:
                    if self.team_in == l2:
                        for tm in l2.members:
                            self.team_listWidget.addItem(str(tm))

    def find_selected_row(self):
        """
        Create find_selected_row method to pick the correct team member from the database.  Return -1 if no one is selected.
        """
        selection = self.team_listWidget.selectedItems()
        if len(selection) == 0:
            return -1
        assert len(selection) == 1
        selected_item = selection[0]
        for l in LeagueDatabase.instance().leagues:
            if self.league_in == l:
                for team in l.teams:
                    if team == self.team_in:
                        for i, c in enumerate(team.members):
                            if str(c) == selected_item.text():
                                return i
        return -1

    def warn(self, title, message):
        """
        Create warn method to warn the user of missing data.
        :param title:
        :param message:
        :return: dialog screen
        """
        mb = QMessageBox(QMessageBox.Icon.NoIcon, title, message, QMessageBox.StandardButton.Ok)
        return mb.exec()

    def delete_button_clicked(self):
        """
        Create delete_button_clicked method to add functionality to the delete button.
        """
        row = self.find_selected_row()
        if row == -1:
            self.warn("Select Team Member", "Please select a team member to delete")
        if row != -1:
            for l in LeagueDatabase.instance().leagues:
                if self.league_in == l:
                    for team in l.teams:
                        if self.team_in == team:
                            for i, tm in enumerate(team.members):
                                if row == i:
                                    team.remove_member(tm)
                                    break
        self.update_ui()

    def update_button_clicked(self):
        """
        Create update_button_clicked method to add functionality to the update button.
        :return:
        """
        row = self.find_selected_row()
        if row == -1:
            self.warn("Select Team Member", "Please select a team member to update")
        name_string_in = self.name2_lineEdit.text()
        email_string_in = self.email2_lineEdit.text()
        if name_string_in != '' and email_string_in != '':
            # LeagueDatabase.instance()
            for l in LeagueDatabase.instance().leagues:
                if self.league_in == l:
                    for team in l.teams:
                        if self.team_in == team:
                            for i, tm in enumerate(team.members):
                                if i == row:
                                    email_lowercase_string_in = email_string_in.lower()
                                    existing_email_list = [t.email.lower() for t in team.members]
                                    if email_lowercase_string_in not in existing_email_list:
                                        tm.name = name_string_in
                                        tm.email = email_string_in
                                    elif tm.email.lower() == email_lowercase_string_in:
                                        tm.name = name_string_in
                                        tm.email = email_string_in
                                    else:
                                        self.warn("Duplicate Update", "Attempted to update with a duplicate email")
        self.update_ui()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = TeamEditDialog()
    window.show()
    sys.exit(app.exec_())


# Cameron M, Merrick

# jira-handler.py

# A script used internally for our SALT Vulnerability Management program
# Script used to ingest and parse vulnerbility reports from 3rd parties
# provided as csv files, and then create tickets and assign persons responsible
# for remediation of each vulnerability.

import os
from openpyxl import Workbook               # For working w/ MS Excel files
import datetime
from datetime import date as dt

class VulnObject:

    def __init__(self, title, severity, source, qid, vendor):

                                            # Initialize from the parser
        self.title = title                  # with the fields passed to it
        self.severity = severity
        self.source = source
        self.dateReported = dt.today()
        self.qid = qid
        self.vendor = vendor

    def printTitle(self):                   # Test by printing the title line
        print(str(self.title))
        return

def testModule():

                                            # create dummy data for test
    title = "Apache HTTP Server Prior to 2.4.30 Multiple Vulnerabilities"
    severity = 5
    source = "Qualys Daily Vulnerability Report"
    qid = 390114
    vendor = "Apache 2.4.30"
    vuln1 = VulnObject(title, severity, source, qid, vendor)
    vuln1.printTitle()                      # Print test line
    return vuln1                            # Return the whole object

# Will return the creation or modified date of the file "Latest_vulnerabilities.csv"
# to check if new tickets should be created or not for the given file
def creation_date(pathToFile):
    stat = os.stat(pathToFile)
    try:
        return stat.st_birthtime            # Check creation date
    except AttributeError:
        return stat.st_mtime                # If not found, go with modified dt
def main():

    today = dt.today()                      # # Create timestamp

    # Move to the dir where we want to save persisting files
    os.chdir('/Users/cmerrick/code/git/Environments/jira-handler/')
    distro_file_unixts = creation_date('/Users/cmerrick/code/git/Environments/jira-handler/Latest_vulnerabilities.csv')
    distro_file_date = dt.fromtimestamp(distro_file_unixts)
# *****************************************************************#
    if (os.path.isfile((str(today) + ".xls")) or distro_file_date != today):
        os.sys.exit(2)
    wb = Workbook()                         # Create workbook object
    ws = wb.active
    ws.title = "Remediation Assignments"    # Rename the worksheet
    wb.save(str(today) + ".xls")            # Rename the workbook

    vuln1 = testModule()                    # # Run initial tests
    return

if __name__ == '__main__':
    main()


print('欢迎来到名片系统'.center(30,"*"))
print('请选择功能'.center(20,' '))
print('1：新增名片'.center(20,' '))
print('2：查找名片'.center(20,' '))
print('3：更改名片'.center(20,' '))
print('4：删除名片'.center(20,' '))
print('5：退出系统'.center(20,' '))
cards = []
while True:
    fun_number = int(input('请选择功能'))
    if fun_number == 1:
        card = {}
        flag = False
        name = input('请输入名字')
        for temp in cards:   #判断重复 14行到20行
            if name == temp['名字']:
                flag = True
                break   #跳出for循环
        if flag == True:
            print('名字重复，请重新输入')
            continue    #继续while循环
        phone = int(input('请输入手机号'))
        job= input('请输入职位')
        company= input('请输入公司名字')
        address = input('请输入公司地址')
        card['名字']=name
        card['手机号']=phone
        card['职位']=job
        card['公司']=company
        card['地址']=address
        cards.append(card)
        print('添加成功')
    elif fun_number == 2:
        random = input('请输入要查找的neirong')
        for temp in cards:
            for i in temp:
                print('%s:%s'%(i,temp[i]))
    elif fun_number == 3:
        pass
    elif fun_number == 4:
        pass
    elif fun_number == 5:
        break


import os,sys,time,subprocess,argparse
from kazoo.client import KazooClient
import pandas as pd
sys.path.insert(0, './scripts/benchmarking/execute')
import guarded

def time_offset_check(nodes):
  open('/home/shweta/workspace/research/dag-placement/offsets.csv','w').close()
  subprocess.check_call(['ansible-playbook','playbooks/offset.yml',\
    '--limit',','.join(nodes)])
  data=pd.read_csv('/home/shweta/workspace/research/dag-placement/offsets.csv',names=['host','offset'],delimiter=',')
  return (abs(data['offset']) <5).all()

def test_colocation(k,count,start_id,log_dir,zk_connector,domain_id,vertex_bbb,collector_bbb):
  zk=KazooClient(zk_connector)
  zk.start()
  with open('%s/parameters/k%d'%(log_dir,k),'r') as inp:
    next(inp) #skip header
    for i in range(count):
      parameters=next(inp).strip()
      if i<(start_id-1):
        continue
      if(i%10==0):
        #test whether time drift has happened
        if not time_offset_check([vertex_bbb,collector_bbb]):
          print('Clock has drifted by more than 5ms. Aborting...')
          return
      print('Testing configuration:%d'%(i+1))

      if not zk.exists('/dom%d/runid'%(domain_id)):
        zk.ensure_path('/dom%d/runid'%(domain_id))
      zk.set('/dom%d/runid'%(domain_id),b'%d'%(i+1))
      #make sub-dirs
      if not os.path.exists('%s/data/k%d/%d'%(log_dir,k,i+1)):
        os.makedirs('%s/data/k%d/%d'%(log_dir,k,i+1))
      if not os.path.exists('%s/data/k%d/%d/graphs'%(log_dir,k,i+1)):
        os.makedirs('%s/data/k%d/%d/graphs'%(log_dir,k,i+1))

      dags=parameters.split(',')
      for idx,dag in enumerate(dags):
        with open('%s/data/k%d/%d/graphs/g%d.txt'%(log_dir,k,i+1,idx+1),'w') as f:
          processing_interval,publication_rate,selectivity=dag.split(':')
          f.write('node;vertex;subscription;publication;selectivity;input_rate;sinks;sources;vertices;publication_rate,processing_interval\n')
          f.write('localhost;g%d_v0;;g%d_e01;%f;%f;1;1;3;%s;-1\n'%(idx+1,idx+1,1,1,publication_rate))
          f.write('%s;g%d_v1;g%d_e01;g%d_e12;%s;%f;1;1;3;%s;%s\n'%(vertex_bbb,idx+1,idx+1,idx+1,selectivity,1,publication_rate,processing_interval))
          f.write('%s;g%d_v2;g%d_e12;;%f;%s;1;1;3;%s;-1\n'%(collector_bbb,idx+1,idx+1,1,selectivity,publication_rate))

      start_ts=time.time()
      #execute experiment
      guarded.Guarded(zk_connector=zk_connector,\
        config_dir='%s/data/k%d/%d'%(log_dir,k,i+1),\
        remote_log_dir='/home/riaps/workspace/dag-placement/log/%d'%(domain_id),\
        local_log_dir='%s/data/k%d/%d'%(log_dir,k,i+1),\
        execution_time=120,domain_id=domain_id).run()

      end_ts=time.time()
      print('Test configuration:%d took %.1f min'%(i+1,(end_ts-start_ts)/60.0))
  zk.stop()

if __name__=="__main__":
  parser=argparse.ArgumentParser(description='script to gather data for model learning')
  parser.add_argument('-k',type=int,required=True,help='degree of co-location')
  parser.add_argument('-count',type=int,required=True,help='number of experiments to perform')
  parser.add_argument('-start_id',type=int,required=True,help='start test id')
  parser.add_argument('-log_dir',required=True,help='parent log directory')
  parser.add_argument('-zk_connector',required=True,help='zookeeper connector')
  parser.add_argument('-domain_id',type=int,required=True,help='DDS domain id')
  parser.add_argument('-vertex_bbb',required=True,help='name of bbb hosting the vertex')
  parser.add_argument('-collector_bbb',required=True,help='name of bbb hosting the collector')
  args=parser.parse_args()

  test_colocation(args.k,args.count,args.start_id,\
    args.log_dir,args.zk_connector,args.domain_id,\
    args.vertex_bbb,args.collector_bbb)


from flask import Blueprint, request
from restpager import Pager

from grano.lib.serialisation import jsonify
from grano.model import Property, Relation, Entity
from grano.views.cache import validate_cache
from grano.lib.args import arg_bool
from grano import authz

blueprint = Blueprint('properties_api', __name__)

@blueprint.route('/api/1/relations/<obj_id>/properties', methods=['GET'])
def relations_index(obj_id):
    query = Property.all()
    query = query.filter(Property.relation_id != None)
    obj = Relation.by_id(obj_id)
    query = query.filter_by(relation_id=obj_id)
    return _index(query, obj)

@blueprint.route('/api/1/entities/<obj_id>/properties', methods=['GET'])
def entities_index(obj_id):
    query = Property.all()
    query = query.filter(Property.relation_id != None)
    obj = Entity.by_id(obj_id)
    query = query.filter_by(entity_id=obj_id)
    return _index(query, obj)

def _index(query, obj):
    authz.require(authz.project_read(obj.project))
    active_only = arg_bool('active', default=True)
    if active_only:
        query = query.filter_by(active=True)

    if 'name' in request.args:
        query = query.filter_by(name=request.args.get('name'))

    query = query.order_by(Property.created_at.desc())
    pager = Pager(query, obj_id=obj.id)
    validate_cache(keys=pager.cache_keys())
    return jsonify(pager, index=False)


# Print to three decimal places by rounding up the proportion of students above average for each case
c = int(input())  # number of test cases
avg_list = []

for i in range(c):
    scores = list(map(int, input().split()))  # scores list
    n = scores[0]  # number of students

    del scores[0]

    sum = 0
    count = 0

    for j in scores:
        sum += j
    avg_list.append(sum / n)  # avg = sum / n

    for j in scores:
        if j > avg_list[i]:
            count += 1

    avg_list[i] = count / n * 100

for i in range(len(avg_list)):
    print("%.3f%%" % round(avg_list[i], 3))


#!/usr/bin/env python
# -*- coding: utf8 -*-
import random
import re
from requests import get
from bs4 import BeautifulSoup
from flask import Flask, jsonify, request
from flask_restful import Api
from pathlib import Path

app = Flask(__name__)
api = Api(app)

# initializing of User-Agent list to avoid being blocked by google
user_agent_file_check = Path("config/user-agents.conf")
if user_agent_file_check.is_file():
    with open('config/user-agents.conf') as file:
        usr_agent_list = file.readlines()
        random_seed_size = len(usr_agent_list)
else:
    usr_agent_list = {
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'}

def google_search(term, num_results=10, lang="en"):
    usr_agent = {'User-Agent': usr_agent_list[random.randint(1, random_seed_size)].rstrip('\n')}

    def fetch_results(search_term, number_results, language_code):
        escaped_search_term = search_term.replace(' ', '+')

        google_url = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, number_results + 1,
                                                                              language_code)
        response = get(google_url, headers=usr_agent)
        response.raise_for_status()
        return response.text

    def parse_results(raw_html):
        soup = BeautifulSoup(raw_html, 'html.parser')
        result_block = soup.find_all('div', attrs={'class': 'g'})
        for result in result_block:
            link = result.find('a', href=True)
            title = result.find('h3')
            if link and title:
                yield link['href']

    html = fetch_results(term, num_results, lang)
    return list(parse_results(html))

@app.route('/defect/', methods=['GET'])
def get_defects():
    input_search_string = request.args.get('err_msg')
    input_sanitized = list(dict.fromkeys(get_google_results(input_search_string.replace('%20', '* '))))
    list_final = [
        '[{}]("https://quickview.cloudapps.cisco.com/quickview/bug/{}) : {}'.format(x, x, get_defect_title(x)) for
        x in input_sanitized]
    return jsonify(list_final)

@app.route('/defect/description', methods=['GET'])
def get_defect_description():
    defect_id = request.args.get('defect_id')
    page = get("https://quickview.cloudapps.cisco.com/quickview/bug/{}".format(defect_id),
               headers={'User-agent': 'Chrome/90.0.4430.72'})
    regexp_title = re.compile(r'<title>Cisco Bug: ([\S\s]*?)</title>', re.MULTILINE)
    regexp_symptom = re.compile(r'Symptom:([\S\s]*?)Conditions:', re.MULTILINE)
    regexp_condition = re.compile(r'Conditions:([\S\s]*?)
', re.MULTILINE)
    ##########################################################
    result_title = re.findall(regexp_title, page.text)
    result_symptom = re.findall(regexp_symptom, page.text)
    result_condition = re.findall(regexp_condition, page.text)
    result_full = []
    if not result_symptom:
        result_full.insert(0, "No information available")
    else:
        result_symptom.insert(0, "Title : {}".format(result_title[0]))
        result_symptom.insert(1, "Quickview URL : https://quickview.cloudapps.cisco.com/quickview/bug/{}".format(
            defect_id))
        result_symptom.insert(2,
                              "Symptoms of {} :".format(defect_id))
        result_condition.insert(0, "Condition :")
        result = result_symptom + result_condition
        result_full = list(filter(None, result))
    return jsonify(result_full)

def get_defect_title(defect_id):
    page_load_result = get("https://quickview.cloudapps.cisco.com/quickview/bug/{}".format(defect_id))

    regexp_title = re.compile(r'<title>Cisco Bug: .{13}([\S\s]*?)</title>', re.MULTILINE)
    result_title = re.findall(regexp_title, page_load_result.text)
    result_final = []
    if not result_title:
        result_final.insert(0, " - No information available externally")
    else:
        result_final = list(filter(None, result_title))
    return result_final[0]

def list_to_lines(list_source):
    return '\n'.join(str(p) for p in list_source)

def get_google_results(user_input):
    regexp_search = re.compile(r'(?=CSC[\S\s]).{10}', re.MULTILINE)
    result_search = google_search("""Cisco Defect """ + user_input, num_results=100)
    result_final = re.findall(regexp_search, list_to_lines(result_search))
    if not result_final:
        result_final.insert(0, "Nothing found, please be more exact")
    return result_final[:50]

app.run(host='0.0.0.0', debug=True)


import sys
input = sys.stdin.readline
from operator import itemgetter

def main():
    v = input().strip()
    for i in range(len(v)):
        if i % 2 == 0:
            if v[i] not in ('R', 'U', 'D'):
                print('No')
                return
        elif i % 2 == 1:
            if v[i] not in ('L', 'U', 'D'):
                print('No')
                return
    print('Yes')

if __name__ == '__main__':
    main()


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

rng = np.random.default_rng(1)

# 1. - Batch processing
def batch(X, T, B=32):
    """
    Create a batch from input data

    Args:
        X (np.array): the input data (N, D)
        T (np.array): targets (D,)
        B (int): batch_size
    """
    # Add bias
    X_bias = np.pad(
        X,
        ((0, 0), (1, 0)),
        constant_values=1
    )
    # Shuffle
    dataset = np.concatenate(
        (
            X_bias,
            T
        ),
        axis=1
    )
    rng.shuffle(dataset)

    N = X_bias.shape[0]
    assert N >= B
    n_batches = N // B
    for i in range(n_batches+1):
        yield dataset[i*B:(i+1)*B, :]

# 2. - Multi-target network
def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def forward(X, W1, W2):
    """
    Args:
        X: (N, D+1)
        W1: (K, D+1)
        W2: (O, K)
    """
    # (N, K)
    A = np.dot(X, W1.T)
    H_ = sigmoid(A)
    H = np.pad(H_, ((0, 0), (1, 0)), constant_values=1)

    Y = np.dot(H, W2.T)
    return Y, H

# 3. - Gradient Descent Step
def loss_frobenius(y, t):
    N = y.shape[0]
    #print(y.shape, t.shape)
    return np.linalg.norm(y - t, ord="fro") / N

def gradient_W1(y, t, h, w, x):
    N = y.shape[0]
    left = np.dot((y-t), w) * h * (1-h)
    return 2 * np.dot(left.T, x) / N

def gradient_W2(y, t, h):
    N = y.shape[0]
    return 2 * np.dot((y - t).T, h) / N

def descent(X, T, W1, W2, eta, mu=0, prev_W1=None, prev_W2=None):
    """
    Args:
        X: (N, D+1)
        T: (N, 1)
        W1: (K, D+1)
        W2: (O, K)
        eta: learning rate
        mu: momentum term (default=0)
    """
    Y, H = forward(X, W1, W2)
    assert Y.shape[0] == X.shape[0]

    loss = loss_frobenius(Y, T)
    grad_W1 = gradient_W1(Y, T, H, W2, X)
    grad_W1 = grad_W1[1:, :]
    grad_W2 = gradient_W2(Y, T, H)

    assert grad_W1.shape == W1.shape and grad_W2.shape == W2.shape

    # Momentum
    if prev_W1 is not None and prev_W2 is not None:
        W1 = W1 - eta * grad_W1 + mu * (W1 - prev_W1)
        W2 = W2 - eta * grad_W2 + mu * (W2 - prev_W2)
    else:
        W1 = W1 - eta * grad_W1
        W2 = W2 - eta * grad_W2

    return W1, W2, loss

# 4. - Data Set Loading
def get_data(filename):
    student = pd.read_csv(filename, delimiter=";")
    student_orig = student.copy()

    for col in range(student.shape[1]):
        unique_vals = student.iloc[:, col].unique().tolist()
        # If binary
        if len(unique_vals) == 2:
            # Replace with -1/1
            student.iloc[:, col].replace(unique_vals, [-1, 1], inplace=True)

    X_1 = student.iloc[:, 0:8].to_numpy(dtype=np.float32)
    X_2 = student.iloc[:, 13:31].to_numpy(dtype=np.float32)
    X = np.concatenate(
        (X_1, X_2),
        axis=1
    )
    X = np.hstack((X_1, X_2))
    T = student.iloc[:, -3:].to_numpy(dtype=np.float32)

    return X, T, student_orig

X, T, df_mat = get_data("data/student-mat.csv")
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)

# 5. - Learning
D = X.shape[1]          # Number of features
B = 32                  # Batch size
K = 150                 # Hidden layer size
O = T.shape[1]          # Number of outputs
eta = 0.01              # Learning rate
max_epochs = 10000      # Maximum number of epochs

W1 = rng.uniform(size=(K, D+1))
W2 = rng.uniform(size=(O, K+1))

def learn(X, T, B, W1, W2, eta, momentum=False):
    mu = 0.9 if momentum else 0
    losses = []
    prev_W1 = None
    prev_W2 = None
    for e in range(max_epochs):
        loss = 0
        for b in batch(X, T, B):
            x = b[:, :-O]
            t = b[:, -O:]

            W1_new, W2_new, err = descent(x, t, W1, W2, eta, mu=mu, prev_W1=prev_W1, prev_W2=prev_W2)
            loss += err

            prev_W1 = W1
            prev_W2 = W2

            W1 = W1_new
            W2 = W2_new

        losses.append(loss)
    return losses, W1, W2

sgd_losses, _, _ = learn(X, T, B, W1, W2, eta, momentum=False)
sgd_momentum_losses, mom_W1, mom_W2 = learn(X, T, B, W1, W2, eta, momentum=True)

plt.clf()
plt.yscale("log")
plt.xscale("log")
plt.plot(np.arange(len(sgd_losses)), sgd_losses, label="Stochastic Gradient Descent")
plt.plot(np.arange(len(sgd_momentum_losses)), sgd_momentum_losses, label="Stochastic Gradient Descent + Momentum")
plt.suptitle("Losses for training methods")
plt.legend()
plt.savefig("loss.png")
plt.clf()

# 7. - Evaluation
# Select students by parameter
female_students = X[(df_mat["sex"] == "F").to_numpy()]
male_students = X[(df_mat["sex"] == "M").to_numpy()]
paid_classes_yes = X[(df_mat["paid"] == "yes").to_numpy()]
paid_classes_no = X[(df_mat["paid"] == "no").to_numpy()]
romantic_yes = X[(df_mat["romantic"] == "yes").to_numpy()]
romantic_no = X[(df_mat["romantic"] == "no").to_numpy()]

def add_bias(x):
    return np.pad(x, ((0, 0), (1, 0)), constant_values=1)

# Compute estimated average grades from network output
female_y, _ = forward(add_bias(female_students), mom_W1, mom_W2)
male_y, _ = forward(add_bias(male_students), mom_W1, mom_W2)
paid_y, _ = forward(add_bias(paid_classes_yes), mom_W1, mom_W2)
paid_no_y, _ = forward(add_bias(paid_classes_no), mom_W1, mom_W2)
romantic_y, _ = forward(add_bias(romantic_yes), mom_W1, mom_W2)
romantic_no_y, _ = forward(add_bias(romantic_no), mom_W1, mom_W2)

# Print result
result = np.vstack(
    (
        np.mean(female_y, axis=0),
        np.mean(male_y, axis=0),
        np.mean(paid_y, axis=0),
        np.mean(paid_no_y, axis=0),
        np.mean(romantic_y, axis=0),
        np.mean(romantic_no_y, axis=0)
    )
)
print(result.T)


import time
import uuid

from nova.compute import task_states
from nova.compute import vm_states
from nova import db
from nova import utils

def stub_out_db_instance_api(stubs):
    """Stubs out the db API for creating Instances."""

    INSTANCE_TYPES = {
        'm1.tiny': dict(memory_mb=512, vcpus=1, root_gb=0, flavorid=1),
        'm1.small': dict(memory_mb=2048, vcpus=1, root_gb=20, flavorid=2),
        'm1.medium':
            dict(memory_mb=4096, vcpus=2, root_gb=40, flavorid=3),
        'm1.large': dict(memory_mb=8192, vcpus=4, root_gb=80, flavorid=4),
        'm1.xlarge':
            dict(memory_mb=16384, vcpus=8, root_gb=160, flavorid=5)}

    class FakeModel(object):
        """Stubs out for model."""

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

        def __getattr__(self, name):
            return self.values[name]

        def __getitem__(self, key):
            if key in self.values:
                return self.values[key]
            else:
                raise NotImplementedError()

    def fake_instance_create(context, values):
        """Stubs out the db.instance_create method."""

        type_data = INSTANCE_TYPES[values['instance_type']]

        base_options = {
            'name': values['name'],
            'id': values['id'],
            'uuid': uuid.uuid4(),
            'reservation_id': utils.generate_uid('r'),
            'image_ref': values['image_ref'],
            'kernel_id': values['kernel_id'],
            'ramdisk_id': values['ramdisk_id'],
            'vm_state': vm_states.BUILDING,
            'task_state': task_states.SCHEDULING,
            'user_id': values['user_id'],
            'project_id': values['project_id'],
            'launch_time': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
            'instance_type': values['instance_type'],
            'memory_mb': type_data['memory_mb'],
            'vcpus': type_data['vcpus'],
            'mac_addresses': [{'address': values['mac_address']}],
            'root_gb': type_data['root_gb'],
            }
        return FakeModel(base_options)

    def fake_instance_type_get_all(context, inactive=0, filters=None):
        return INSTANCE_TYPES.values()

    def fake_instance_type_get_by_name(context, name):
        return INSTANCE_TYPES[name]

    stubs.Set(db, 'instance_create', fake_instance_create)
    stubs.Set(db, 'instance_type_get_all', fake_instance_type_get_all)
    stubs.Set(db, 'instance_type_get_by_name', fake_instance_type_get_by_name)


import requests
import bs4 as bs

added_urls = []

for i in range(0, 462):
    with open("./buy_html/adondevivir-buy-page-{}.txt".format(i), "r") as page:
        content = page.read()

        soup = bs.BeautifulSoup(content, "html.parser")

        items = soup.find_all("div", {"class": "sc-1tt2vbg-3 kGTsiT"})

        for item in items:
            url = "https://adondevivir.com{}.html".format(str(item).split("data-to-posting")[1].split(".html")[0].split("=\"")[1])
            if url in added_urls:
                print("Duplicated url")
            else:
                added_urls.append(url)
                with open("./adondevivir-pe-buy.txt", "a") as rentUrls:
                    rentUrls.writelines("\"{}\", \n".format(url))
                    rentUrls.close()

        page.close()


from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone

from .logic.perform_mailing.model import is_time_of_client_in_valid_mailing_time_interval
from .validators import MailingValidator, FilterValidator, is_start_date_expired, validate_start_date_expired

class TestFilterValidator(TestCase):
    def setUp(self) -> None:
        self.filter_validator = FilterValidator(['key1', 'key2'])

    def test_valid_filters(self):
        datasets = [{'key1': 1, 'key2': 2}, {'key1': 'a'}, {'key2': 'v'}, {}]
        for dataset in datasets:
            self.assertIsNone(self.filter_validator(dataset))

    def test_invalid_filters(self):
        datasets = [['key1'], {'key3': 1}, {'key1': 1, 'key3': 2}, {'key1': {'key1': 'a'}}]
        for dataset in datasets:
            with self.assertRaises(ValidationError):
                self.filter_validator(dataset)

class TestMailingValidator(TestCase):
    def setUp(self) -> None:
        self.validator = MailingValidator()

    def test_valid_filters(self):
        datasets = [{'mobile_operator_code': 1, 'tag': 2}, {'mobile_operator_code': 'a'}, {'tag': 'v'}, {}]
        for dataset in datasets:
            self.assertIsNone(self.validator.validate_filters(dataset))

    def test_invalid_filters(self):
        datasets = [['tag'], {'key3': 1}, {'tag': 1, 'key3': 2}, {'tag': {'tag': 'a'}}]
        for dataset in datasets:
            with self.assertRaises(ValidationError):
                self.validator.validate_filters(dataset)

    def test_valid_kwargs_in_validate_method(self):
        datasets = [
            {'start_date': timezone.now(), 'end_date': timezone.now() + timezone.timedelta(days=1)},
            {'start_date': timezone.now()}
        ]
        for dataset in datasets:
            self.assertIsNone(self.validator.validate(**dataset))

    def test_invalid_kwargs_in_validate_method(self):
        datasets = [
            {'start_date': timezone.now(), 'end_date': timezone.now() - timezone.timedelta(days=1)},
        ]
        for dataset in datasets:
            with self.assertRaises(ValidationError):
                self.validator.validate(**dataset)

class TestStartDateExpired(TestCase):
    def setUp(self) -> None:
        self.gt_date = timezone.now() + timezone.timedelta(days=1)
        self.lt_date = timezone.now() - timezone.timedelta(days=1)

    def test_is_start_date_expired(self):
        self.assertFalse(is_start_date_expired(self.gt_date))
        self.assertTrue(is_start_date_expired(self.lt_date))

    def test_validate_start_date_expired(self):
        self.assertIsNone(validate_start_date_expired(self.gt_date))
        with self.assertRaises(ValidationError):
            validate_start_date_expired(self.lt_date)

class TestIsTimeOfClientInValidMailingTimeInterval(TestCase):
    def setUp(self) -> None:
        self.curr_time = timezone.now()

    def test_time_interval_start_lt_time_interval_end(self):
        start_time = (self.curr_time - timezone.timedelta(hours=3)).time()
        end_time = (self.curr_time + timezone.timedelta(hours=3)).time()
        for tz in ['Etc/GMT+6', 'Etc/GMT-6']:
            self.assertFalse(is_time_of_client_in_valid_mailing_time_interval(start_time, end_time, tz))
        for tz in ['Etc/GMT+2', 'Etc/GMT-2', 'Etc/GMT']:
            self.assertTrue(is_time_of_client_in_valid_mailing_time_interval(start_time, end_time, tz))

    def test_time_interval_start_gt_time_interval_end(self):
        start_time = (self.curr_time + timezone.timedelta(hours=3)).time()
        end_time = (self.curr_time - timezone.timedelta(hours=3)).time()
        for tz in ['Etc/GMT+6', 'Etc/GMT-6']:
            self.assertTrue(is_time_of_client_in_valid_mailing_time_interval(start_time, end_time, tz))
        for tz in ['Etc/GMT+2', 'Etc/GMT-2', 'Etc/GMT']:
            self.assertFalse(is_time_of_client_in_valid_mailing_time_interval(start_time, end_time, tz))


from pbgpp.Output.Filter import BGPFilter
from pbgpp.BGP.Statics import BGPStatics

class LargeCommunityFilter(BGPFilter):
    def __init__(self, values=[]):
        BGPFilter.__init__(self, values)

    def apply(self, message):
        try:
            # LARGE_COMMUNITIES is a path attribute of BGP UPDATE message
            # Therefore we first need to make that we are currently handling an UPDATE message
            if message.type is not BGPStatics.MESSAGE_TYPE_UPDATE:
                # Skip messages that are no UPDATE messages
                return None

            for attribute in message.path_attributes:
                # Skip attributes that are no LARGE_COMMUNITIES attributes
                if attribute.type is not BGPStatics.UPDATE_ATTRIBUTE_LARGE_COMMUNITIES:
                    continue

                for community in attribute.large_communities:

                    community_parts = str(community).split(":")

                    # Here we found the LARGE_COMMUNITIES attribute - loop through allowed values
                    for value in self.values:
                        negated = False
                        if value[0:1] == "~":
                            negated = True
                            value = value[1:]

                        match = True

                        value_parts = value.split(":")

                        for part_idx in range(0, 3):
                            if value_parts[part_idx] == "*":
                                continue
                            if community_parts[part_idx] != value_parts[part_idx]:
                                match = False
                                break

                        if not negated and match:
                            # Match on LARGE_COMMUNITY attribute - Return message
                            return message

                        if negated and not match:
                            return message

            # Searched value was not found
            return None
        except Exception as e:
            # On error the filtering was not successful (due to wrong fields, etc.)
            return None


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

import logging
from email.utils import formatdate
from time import time

from obra_hacks.backend.data import DISCIPLINE_MAP
from obra_hacks.backend.models import Person
from obra_hacks.backend.rankings import get_ranks
from peewee import Entity, Select, fn

from flask_restx import Resource, fields, marshal

try:
    import ujson as json
except ImportError:
    import json

logger = logging.getLogger(__name__)
cache_timeout = 900

def register(api, cache):
    ns = api.namespace('ranks', 'USAC-style Rankings')

    person = ns.model('Person',
                      {'id': fields.Integer,
                       'first_name': fields.String,
                       'last_name': fields.String,
                       'team_name': fields.String,
                       'name': fields.String(attribute=lambda p: '{} {}'.format(p.first_name, p.last_name).title() if p else None),
                       })

    person_rank = ns.clone('PersonRank', person,
                          {'rank': fields.Integer,
                           'place': fields.Integer,
                           })

    discipline_ranks = ns.model('DisciplineRanks',
                                {'name': fields.String,
                                 'display': fields.String,
                                 'ranks': fields.List(fields.Nested(person_rank)),
                                 })

    @ns.route('/')
    @ns.response(200, 'Success', [discipline_ranks])
    @ns.response(500, 'Server Error')
    class DisciplineRanks(Resource):
        """
        Get the top 500 ranks, grouped by discipline
        """
        @cache.cached(timeout=cache_timeout)
        def get(self):
            disciplines = []

            for upgrade_discipline in DISCIPLINE_MAP.keys():
                ranks = get_ranks(upgrade_discipline)
                ranked_people_ids = sorted([k for k in ranks.keys() if k], key=lambda k: ranks[k])[:501]
                ranked_people_json = Select(from_list=[fn.JSON_EACH(json.dumps(ranked_people_ids))], columns=[Entity('value')])
                query = (Person.select().where(Person.id << ranked_people_json))

                for person in query:
                    person.rank = int(ranks[person.id])
                    ranks[person.id] = person

                place = 0
                prev_rank = 0
                for person_id in ranked_people_ids:
                    person = ranks[person_id]
                    place += person.rank != prev_rank
                    person.place = place
                    prev_rank = person.rank

                disciplines.append({'name': upgrade_discipline,
                                    'display': upgrade_discipline.split('_')[0].title(),
                                    'ranks': [ranks[person_id] for person_id in ranked_people_ids],
                                    })

            return ([marshal(d, discipline_ranks) for d in disciplines], 200, {'Expires': formatdate(timeval=time() + cache_timeout, usegmt=True)})


import logging

from orthos2.data.models import Machine
from orthos2.taskmanager import tasks
from orthos2.taskmanager.models import Task, TaskManager
from orthos2.taskmanager.tasks.ansible import Ansible

logger = logging.getLogger('tasks')

class DailyMachineChecks(Task):
    """Trigger full machine check/scan for all qualified machines."""

    is_running = False

    def __init__(self):
        if DailyMachineChecks.is_running:
            raise Exception("Scan via already running")
        DailyMachineChecks.is_running = True

    @staticmethod
    def do_scan_all():
        ansible_scan = []
        for machine in Machine.objects.all():
            # only status check for administrative machines
            if machine.administrative or machine.system.administrative:
                action = 'status'
            else:
                action = 'all'
                ansible_scan.append(machine.fqdn)
            task = tasks.MachineCheck(machine.fqdn,
                                      tasks.MachineCheck.Scan.to_int(action))
            TaskManager.add(task)
        """
        for machine in Machine.objects.all():
            if machine.administrative or machine.system.administrative:
                continue
            ansible_scan.append(machine.fqdn)
        """
        task = Ansible(ansible_scan)
        TaskManager.add(task)
        DailyMachineChecks.is_running = False

    def execute(self):
        """Execute the task."""
        DailyMachineChecks.do_scan_all()

class DailyCheckReservationExpirations(Task):
    """Check for expiring reservations for reserved machines."""

    def __init__(self):
        pass

    def execute(self):
        """Execute the task."""
        from orthos2.taskmanager import tasks

        for machine in Machine.objects.all():
            if machine.administrative or machine.system.administrative:
                continue

            if machine.reserved_by and not machine.is_reserved_infinite():
                task = tasks.CheckReservationExpiration(machine.fqdn)
                TaskManager.add(task)

class DailyCheckForPrimaryNetwork(Task):
    """ Check for machines without primary network interfaces and complain via email."""

    def __init_(self):
        pass

    def execute(self):
        """Execute the task."""
        from orthos2.taskmanager import tasks

        for machine in Machine.objects.all():
            task = tasks.CheckForPrimaryNetwork(machine.fqdn)
            TaskManager.add(task)


import os
import inspect
from obspy import read
import pytomo3d.adjoint.utils as adj_utils
# import pyadjoint.adjoint_source

def _upper_level(path, nlevel=4):
    """
    Go the nlevel dir up
    """
    for i in range(nlevel):
        path = os.path.dirname(path)
    return path

# Most generic way to get the data folder path.
TESTBASE_DIR = _upper_level(os.path.abspath(
    inspect.getfile(inspect.currentframe())), 4)
DATA_DIR = os.path.join(TESTBASE_DIR, "tests", "data")

obsfile = os.path.join(DATA_DIR, "proc", "IU.KBL.obs.proc.mseed")
synfile = os.path.join(DATA_DIR, "proc", "IU.KBL.syn.proc.mseed")
winfile = os.path.join(DATA_DIR, "window", "IU.KBL..BHR.window.json")

class TestUtilOne:

    @staticmethod
    def get_fake_adjsrcs():
        obs = read(obsfile)
        adjsrcs = adj_utils.ensemble_fake_adj(obs)
        return adjsrcs

    def test_ensemble_fake_adj(self):
        adjsrcs = self.get_fake_adjsrcs()

        assert len(adjsrcs) == 3
        for adj in adjsrcs:
            assert adj.adj_src_type == "waveform_misfit"
            assert adj.misfit == 0.0
            assert adj.dt == 0.5
            assert adj.min_period == 50.0
            assert adj.max_period == 100.0
            assert adj.network == "IU"
            assert adj.station == "KBL"
            assert adj.component in ["BHR", "BHT", "BHZ"]
            assert adj.location == ""
            assert adj.measurement is None

    def test_change_adjsrc_channel_name(self):
        adjsrcs = self.get_fake_adjsrcs()
        adj_utils.change_adjsrc_channel_name(adjsrcs, "MX")
        for adj in adjsrcs:
            assert adj.component[:2] == "MX"