Exemplo n.º 1
0
def main():
    try:
        # Create Book.
        book = Book("The Hobbit", "J.R.R. Tolkien", 366, datetime.date(1937, 9, 15))

        # Log book object.
        Logging.line_separator("log_object(book)", 60)
        log_object(book)

        # Log invalid object.
        Logging.line_separator("log_invalid_object(book)", 60)
        log_invalid_object(book)

        # Disassemble both log_ functions.
        Logging.line_separator("DISASSEMBLY OF log_object()", 60)
        disassemble_object(log_object)

        Logging.line_separator("DISASSEMBLY OF log_invalid_object()", 60)
        disassemble_object(log_invalid_object)
    except NameError as error:
        # Output expected NameErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 2
0
def main():
    try:
        host = 'airbrake.io'

        Logging.line_separator('AIRBRAKE.IO HTTP REQUEST', 60)
        s = get_socket(host, False)
        get_socket_response(s,
                            f'GET / HTTP/1.1\r\nHost: {host}\r\n\r\n'.encode())

        Logging.line_separator('AIRBRAKE.IO HTTPS REQUEST', 60)
        s = get_socket(host, True)
        get_socket_response(s,
                            f'GET / HTTP/1.1\r\nHost: {host}\r\n\r\n'.encode())

        Logging.line_separator('AIRBRAKE.IO HTTP REQUEST w/o BLOCKING', 60)
        s = get_socket(host, False, False)
        get_socket_response(s,
                            f'GET / HTTP/1.1\r\nHost: {host}\r\n\r\n'.encode())

        Logging.line_separator('AIRBRAKE.IO HTTPS REQUEST w/o BLOCKING', 60)
        s = get_socket(host, True, False)
        get_socket_response(s,
                            f'GET / HTTP/1.1\r\nHost: {host}\r\n\r\n'.encode())
    except BlockingIOError as error:
        # Output expected BlockingIOErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 3
0
def increment_local_count():
    """Increment count by one and output new value.

    :return: None
    """
    try:
        Logging.line_separator("Incrementing LOCAL count.", 60)
        count += 1
        Logging.log("Count incremented to: {}".format(count))
    except UnboundLocalError as error:
        # Output expected UnboundLocalErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 4
0
def set_local_book_title(title):
    """Set title property of local book to passed value and output.

    :param title: Title to be set.
    :return: None
    """
    try:
        Logging.line_separator(
            "Setting LOCAL book title to '{}'.".format(title), 60)
        book.title = title
        Logging.log(book)
    except UnboundLocalError as error:
        # Output expected UnboundLocalErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 5
0
def main():
    Logging.line_separator("BOTH INCLUDE PUBLICATION DATES", 50, '+')
    # Create two Books with identical arguments.
    the_stand = Book("The Stand", "Stephen King", 1153,
                     datetime.date(1978, 1, 1))
    the_stand_2 = Book("The Stand", "Stephen King", 1153,
                       datetime.date(1978, 1, 1))

    # Check equivalency of Books.
    check_equality(the_stand, the_stand_2)

    Logging.line_separator("ONE MISSING PUBLICATION DATE", 50, '+')
    # Create two Books, one without publication_date argument specified.
    the_hobbit = Book("The Hobbit", "J.R.R. Tolkien", 366,
                      datetime.date(1937, 9, 15))
    the_hobbit_2 = Book("The Hobbit", "J.R.R. Tolkien", 366)

    # Check equivalency of Books.
    check_equality(the_hobbit, the_hobbit_2)
Exemplo n.º 6
0
def test():
    try:
        Logging.line_separator("CREATE BOOK", 50, '+')
        # Create and output book.
        book = Book("The Hobbit", "J.R.R. Tolkien", 366, datetime.date(1937, 9, 15))
        Logging.log(book)

        # Output valid attributes.
        Logging.log(book.title)
        Logging.log(book.author)

        # Output invalid attribute (publisher).
        Logging.log(book.publisher)
    except AttributeError as error:
        # Output expected AttributeErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 7
0
def main():
    try:
        # Increment local count.
        increment_local_count()

        # Set local book title.
        set_local_book_title("The Silmarillion")

        # Set global book title.
        set_global_book_title("The Silmarillion")

        # Disassemble functions.
        Logging.line_separator("DISASSEMBLY OF increment_count.", 60)
        disassemble_object(increment_local_count)

        Logging.line_separator("DISASSEMBLY OF set_local_book_title.", 60)
        disassemble_object(set_local_book_title)

        Logging.line_separator("DISASSEMBLY OF set_global_book_title.", 60)
        disassemble_object(set_global_book_title)
    except NameError as error:
        # Output expected NameErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 8
0
def main():
    try:
        # Create list and populate with Books.
        books = list()
        books.append(
            Book("Shadow of a Dark Queen", "Raymond E. Feist", 497,
                 datetime.date(1994, 1, 1)))
        books.append(
            Book("Rise of a Merchant Prince", "Raymond E. Feist", 479,
                 datetime.date(1995, 5, 1)))
        books.append(
            Book("Rage of a Demon King", "Raymond E. Feist", 436,
                 datetime.date(1997, 4, 1)))

        # Output Books in list, with and without index.
        Logging.line_separator('Books')
        log_list(books)
        Logging.line_separator('Books w/ index')
        log_list(books, True)

        # Output list element outside bounds.
        Logging.line_separator('books[len(books)]')
        Logging.log(f'books[{len(books)}]: {books[len(books)]}')
    except IndexError as error:
        # Output expected IndexErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 9
0
def check_equality(a, b):
    """Asserts the equivalent of the two passed objects.

    :param a: First object.
    :param b: Second object.
    :return: Indicates if assertion was successful.
    """
    try:
        Logging.line_separator("ASSERTING EQUIVALENCE OF...")
        # Output objects using __str__ method.
        Logging.log(a)
        Logging.log(b)
        # Assert equivalence of objects, indicating inequality if failed.
        assert a == b, "The objects ARE NOT equal."
        # Indicate that assertion succeeded.
        Logging.log("The objects are equal.")
        return True
    except AssertionError as error:
        # Output expected AssertionErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 10
0
def main():
    Logging.line_separator("FLOATING POINT")
    test_floating_point()

    Logging.line_separator("DIVISION BY ZERO")
    test_division_by_zero()

    Logging.line_separator("FLOATING POINT DIVISION BY ZERO", 60)
    test_floating_point_division_by_zero()
Exemplo n.º 11
0
def divide_test(denominator, numerator):
    """Perform division tests using all different numeric types and mathematic libraries.

    :param denominator: Denominator.
    :param numerator: Numerator.
    """
    Logging.line_separator('as int')
    Logging.log(divide(denominator, numerator))

    Logging.line_separator('as float')
    Logging.log(divide(denominator, numerator, NumberType.FLOAT))

    Logging.line_separator('as decimal.Decimal')
    Logging.log(divide(denominator, numerator, NumberType.DECIMAL))

    Logging.line_separator('as mpmath.mpf')
    Logging.log(divide(denominator, numerator, NumberType.MPMATH))
Exemplo n.º 12
0
def main():
    Logging.line_separator("FRACTION TEST", 40, '+')

    divide_test(5, 25)

    Logging.line_separator("WHOLE NUMBER TEST", 40, '+')

    divide_test(25, 5)

    Logging.line_separator("DIVIDE BY ZERO TEST", 40, '+')

    divide_test(5, 0)
Exemplo n.º 13
0
def pi_test(precision):
    # Integer
    Logging.line_separator(f'PI WITH PRECISION OF {precision}, USING INTEGERS',
                           60)
    Logging.log(get_pi(precision))

    # Float
    Logging.line_separator(f'PI WITH PRECISION OF {precision}, USING FLOATS',
                           60)
    Logging.log(get_pi(precision, PiLibType.FLOAT))

    # Decimal
    Logging.line_separator(f'PI WITH PRECISION OF {precision}, DECIMAL LIB',
                           60)
    Logging.log(get_pi(precision, PiLibType.DECIMAL))

    # MPMath
    # Set precision one higher to avoid rounding errors.
    Logging.line_separator(f'PI WITH PRECISION OF {precision + 1}, MPMATH LIB',
                           60)
    Logging.log(get_pi(precision + 1, PiLibType.MPMATH))
Exemplo n.º 14
0
def main():
    # Precision: 10
    pi_test(10)

    Logging.line_separator(None, 60, '_')

    # Precision: 25
    pi_test(25)

    Logging.line_separator(None, 60, '_')

    # Precision: 256
    pi_test(256)

    Logging.line_separator(None, 60, '_')

    # Precision: 300
    pi_test(300)
Exemplo n.º 15
0
def main():
    try:
        # Create a dictionary and populate with Books.
        series = {
            1:  Book("The Name of the Wind",    "Patrick Rothfuss",     662,    datetime.date(2007, 3, 27)),
            2:  Book("The Wise Man's Fear",     "Patrick Rothfuss",     994,    datetime.date(2011, 3, 1)),
            3:  Book("Doors of Stone",          "Patrick Rothfuss")
        }

        # Output Books in series dictionary, with and without index.
        Logging.line_separator('Series')
        log_dict(series)
        Logging.line_separator('Series w/ Order Index')
        log_dict(series, True)

        # Output book in series that doesn't exist.
        Logging.line_separator('series[len(series) + 1]')
        Logging.log(f'series[{len(series) + 1}]: {series[len(series) + 1]}')
    except KeyError as error:
        # Output expected KeyErrors.
        Logging.log_exception(error)
    except Exception as exception:
        # Output unexpected Exceptions.
        Logging.log_exception(exception, False)
Exemplo n.º 16
0
def output_buffer(view: memoryview):
    Logging.line_separator("BUFFER OUTPUT")
    Logging.log(f'tobytes(): {view.tobytes()}')
    Logging.log(f'tolist(): {view.tolist()}')
    Logging.log(f'hex(): {view.hex()}')