Exemplo n.º 1
0
def find_highest_oeis_id():
    """Find the highest entry ID in the remote OEIS database by performing HTTP queries and doing a binary search."""

    SLEEP_AFTER_FAILURE = 5.0

    success_id =  263000 # We know a-priori that this entry exists.
    failure_id = 1000000 # We know a-priori that this entry does not exist.

    # Do a binary search, looking for the success/failure boundary.
    while success_id + 1 != failure_id:

        fetch_id = (success_id + failure_id) // 2

        logger.info("OEIS search range is ({}, {}), attempting to fetch entry {} ...".format(success_id, failure_id, fetch_id))

        try:
            fetch_remote_oeis_entry(fetch_id, fetch_bfile_flag = False)
        except BadOeisResponse:
            # This exception happens when trying to read beyond the last entry in the database.
            # We mark the failure and continue the binary search.
            logging.info("OEIS entry {} does not exist.".format(fetch_id))
            failure_id = fetch_id
        except BaseException as exception:
            # Some other error occurred. We have to retry.
            logger.error("Unexpected fetch result ({}), retrying in {} seconds.".format(exception, SLEEP_AFTER_FAILURE))
            time.sleep(SLEEP_AFTER_FAILURE)
        else:
            # We mark the success and continue the binary search.
            logging.info("OEIS entry {} exists.".format(fetch_id))
            success_id = fetch_id

    logger.info("Last valid OEIS entry is A{:06}.".format(success_id))

    return success_id
Exemplo n.º 2
0
def find_highest_oeis_id():
    """Find the highest entry ID in the remote OEIS database by performing HTTP queries and doing a binary search."""

    SLEEP_AFTER_FAILURE = 5.0

    success_id = 263000  # We know a-priori that this entry exists.
    failure_id = 1000000  # We know a-priori that this entry does not exist.

    # Do a binary search, looking for the success/failure boundary.
    while success_id + 1 != failure_id:

        fetch_id = (success_id + failure_id) // 2

        logger.info(
            "OEIS search range is ({}, {}), attempting to fetch entry {} ...".
            format(success_id, failure_id, fetch_id))

        try:
            fetch_remote_oeis_entry(fetch_id, fetch_bfile_flag=False)
        except BadOeisResponse:
            # This exception happens when trying to read beyond the last entry in the database.
            # We mark the failure and continue the binary search.
            logging.info("OEIS entry {} does not exist.".format(fetch_id))
            failure_id = fetch_id
        except BaseException as exception:
            # Some other error occurred. We have to retry.
            logger.error(
                "Unexpected fetch result ({}), retrying in {} seconds.".format(
                    exception, SLEEP_AFTER_FAILURE))
            time.sleep(SLEEP_AFTER_FAILURE)
        else:
            # We mark the success and continue the binary search.
            logging.info("OEIS entry {} exists.".format(fetch_id))
            success_id = fetch_id

    logger.info("Last valid OEIS entry is A{:06}.".format(success_id))

    return success_id
Exemplo n.º 3
0
def safe_fetch_remote_oeis_entry(entry):
    """Fetch a single OEIS entry from the remote OEIS database, and swallow any exceptions.

    If no issues are encountered, this function is identical to the 'fetch_remote_oeis_entry' function.
    In case of an exception, a log message is generated and 'None' is returned.

    The purpose of this function in to be used in a "map", where we want to inhibit exceptions.
    """

    # Intercepts and reports any exceptions.
    # In case of an exception, a log message is generated, and None is returned.
    try:
        result = fetch_remote_oeis_entry(entry, True)
    except BaseException as exception:
        logger.error("Unable to fetch entry {}: '{}'.".format(entry, exception))
        result = None
    return result
Exemplo n.º 4
0
def safe_fetch_remote_oeis_entry(entry):
    """Fetch a single OEIS entry from the remote OEIS database, and swallow any exceptions.

    If no issues are encountered, this function is identical to the 'fetch_remote_oeis_entry' function.
    In case of an exception, a log message is generated and 'None' is returned.

    The purpose of this function in to be used in a "map", where we want to inhibit exceptions.
    """

    # Intercepts and reports any exceptions.
    # In case of an exception, a log message is generated, and None is returned.
    try:
        result = fetch_remote_oeis_entry(entry, True)
    except BaseException as exception:
        logger.error("Unable to fetch entry {}: '{}'.".format(
            entry, exception))
        result = None
    return result