A 1st IOPCC Winner

Author:

Judges' Comments

Most Introspective.

Very well put together, top-notch obfuscation. Tongue-in-cheek, comments on itself (very meta, which we like).

Author's Comments

What does it do?

The program shows off it's own sourcecode and then prints an evaluation of itself. For that walks through Tim Peter's "The Zen of Python".

The program violates most of the 19 guiding principles. Therefore the program comments only on those principles that are not violated.

Why does it do it?

The program is meant to demonstrate the power of abstract syntax trees. The fact that you can parse (and manipulate) python syntax trees with just the standard library is amazing to me. It is also really easy, as shown by the final statement in the outer program (see below). In fact, it is so simple that I had to obfuscate the syntax tree, in fear that the code might be too readable otherwise.

The program is trying to be as circular as possible.

Also shaping the code into a big "PY" was really fun. It is sad how seldom we as programmers get to do that.

How to run it?

Simply run python main.py in a terminal.

There are no dependencies other then python itself.

How does it do it?

There are two parts to this program, both living in the same file:

How does the outer program work?

Here is a clearer (and documented) version of the outer program.

# Import everything from the ast module.
from ast import *

# Defines alias that will be used in the inner function.
# Those aliases are there to obfuscate the code,
# make it more dense and to make it look mysterious.
# The unicode characters from the "Unified Canadian Aboriginal Syllabics"
# were chosen for their awesome geometric look
= alias
= Import
= Constant
= NamedExpr
= Call
= Name
= Attribute
= Load
= Module
= []
= Store
= Expr
= IfExp
= Continue
= If
= Not
= UnaryOp
= JoinedStr
= FormattedValue

# Here comes the hart of the program, where the outer code retrieves the inner code and runs it.

exec(
    unparse(
        fix_missing_locations(
            eval(
                 __doc__        # get this module's docstring (which happens to be the code of the inner program as one big syntax_tree expression)
            )                   # evaluate the string into a syntax_tree object
        )                       # add `lineno` and `col_offset` attributes (which are missing to make the code denser)
    )                           # convert the syntax_tree into a string of normal python code
)                               # execute that code

# Name of the author
# Johannes Lippmann

How does the inner program work?

The inner program is not coded so densly. You could say it is rather normal python code. That's because it is not presented directly, but in an encoded (as abstract syntax tree) and obfuscated (with aliases). That is why we can get away with somewhat ordinary code.

Here is a nicer version of the inner code.

# Show the code itself, to show off the awesome PY shape.
print(open(__file__, "r").read())
print()


# Import the zen of python into a string variable.
# We do that by running the well known easteregg "import this",
# which will print out the zen of python.
# Since we don't want to print it yet we temporarly redirect stdout.
# This is honestly the simplest way I could get the zen of python into a string
# without putting it into a constant.
import contextlib, io, operator, time
filehandle = io.StringIO()
with contextlib.redirect_stdout(filehandle):
    import this
zen_of_python = filehandle.getvalue()


# Comments on those zen-principles that are not violated
# The format is LINE_NUMBER: COMMENT
# If there is no comment then the principle is known violated.
comments_on_principles = {
    11: "no errors were silenced",
    12: "no errors were thrown",
    13: "everything is crystal clear",
    17: "for this code, never would have been better",
    19: "does not apply",
}


# ANSI escape sequences for colored output.
print_in_green = '\033[32m'
print_in_red = '\033[91m'
print_in_default_color = '\033[0m'


for line_number, zen_principle in enumerate(zen_of_python.splitlines()):

    # The zen of python starts with a headline
    # we will transform it into a question to give context to the rest
    if line_number == 0:
        print(f"How does this code do in terms of >{zen_principle.replace(', ','< ')}?")
        continue

    # preserve the next (empty) line
    elif not zen_principle:
        print()
        continue

    # Print the principles, with color, check- or x-mark and a comment where applicable.
    # This is intended to be an evaluation.
    # The colors are an integral part of making it look like that.

    if (comment := comments_on_principles.get(line_number)):
        print(f'{print_in_green}{zen_principle}{print_in_default_color}  # {comment}.')
    else:
        print(f'{print_in_red}{zen_principle}')

    time.sleep(0.3)

# print comment on the codes poor preformance in regard to this evaluation
print(print_in_default_color)
print("Well...")
time.sleep(2)

print("I tried.")