# 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. - it reads and uses its own docstring - it prints an evaluation of itself - the code starts in the middle of the file (after the docstring), goes to the end and then in the last line gets back to the start, where it starts to work again. 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. - The terminal is expected to support ANSI escape codes (for colored output) - nearly all terminal emulators do. - The program was tested successfully with CPython 3.9, 3.10 and 3.11, but should also work with later versions. - The program was tested only on linux (NixOS 22.11.3667.87edbd74246 Raccoon), but should work platform-independently (as long as CPython is available). # How does it do it? There are two parts to this program, both living in the same file: - The *inner program* is encoded as an abstract syntax tree in the modules docstring. - The *outer program* is right below and will extract, decode and run the inner program. ## How does the outer program work? Here is a clearer (and documented) version of the outer program. ```python # 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. ```python # 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.") ```