Last active
October 11, 2023 09:38
-
-
Save a-n-d-r-e-w-l/c3fd7067debe512d8b02ca70ae712871 to your computer and use it in GitHub Desktop.
Source code for my talk on cursed Python (https://www.youtube.com/watch?v=t863QfAOmlY).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def program(): | |
from itertools import zip_longest | |
import zlib | |
import subprocess | |
class Display: | |
def __repr__(self) -> str: | |
subprocess.run([ | |
"feh", | |
"-xYFqZ", | |
"/home/alees/.python-talk/talk_res/image.png" | |
]) | |
return "" | |
compressed = b'x\x9c\x8d\x92A\x0e\x84 \x0cE\xf7\x9c\xa2\x07h\xfe\x01\xda\xe3\xb0%\xe9\xfdw\x03\xcc8mQ\x88oaL\xdfC\x89B\xe4\x80\xa56\x1b\xb4\xaa\x8c\x12\x94\xf6\x99\x05$Z\xb5\xda\xaf\xc9G-f\x0c\xb6\x9df\xb9\xc8\x1a\xac\x92pM\xe8\x14Bze\xe6\xdb\x8c\x0c\xfb(d(r\xc8\xf4\x9f\xa1\xc4yS\x0e\xc0\x9f6B\xdf\x1dS\x14+E\xef\xbby\xec\xe8\xea\x8e\xd9\xf80\xf5E7\x7f\xc1\xbc\xa3e\xf8;\r\x9b%Ov\x0e\xe2y9\xdbuyv\xae?\x07\xa1\xa1\xb5' | |
lines = zlib.decompress(compressed).decode("utf8").splitlines() | |
czen = b'x\x9c}RAn\xdc0\x0c\xbc\xfb\x15\xeci\x81 \xeb?4i\n\xf4\x92C\xd2<\x80\x92i\x9b\x88,\x1a\x12\x95\x8d\x7f\x1fJ\xde\x14\xedn\xd0\x83\r\x9bC\x0egF\xba#,\xcac\t\xc0\x19\x1c\xa9R\x02\x9d1B\x99\xc2\xd6w\x0f\xefk`\xcfz\x89\xf2\xb2\xd7\xfb\xee\xb9~\xd2%\xee\xa5V\xdf\xfb\xee~\xff\xf8\x12g\x8fJC\xdf\xfd\x0cx\xb5 Rn\xd8\xf3\x8a)_\xd1\x0f\x143\xf5\xdd\x13\xe1\x80\x8e\x03\xebf\x8c%j\xae\x03\xe4\x19\x03x\xcc\x94\x01\x13\xc5\x83B>\x17)J\x99fP\x01\x97\x08_\x8d\x8c \x95@6\xf7=\xe8\xdc\xc05\xa1W\x93\xd6X\x1d\xa1fXK\xb2\x1fK#%I\x19\xb25\x86\xc1$\xbe\x99\x9e\x15\xb3U8P\xd4\x1a\xd8K4\xb6\x0ct\xce-l;\xe6\xab\x97_\xb1-\x1c\xd1\x13\xc8\x08\xb88\x9e\x8a\x11\xdfB\xa2\xb1\x98\xcb\x8a*-\xab\xa2\xb2\xc4*s*\xc6\xd6w\xbfgJ\xf4\xb9\xd7\xd9t\xa4\xe3\x110\x0e\xa6\x96FJ\xe8l\x91\xc4\xf6"8\x1e\xc5\xbd\xb1\x94\x0c\'\xdc*\xcb P\x8f\xea\x8fE\xcbP\x1b\xb6\xd8\x13E\x1b\xe5y\xc4\x90\x91SV(\xbb\x93M\xca\xc1v\xff(\xea\xe7\xbe{\x94\xd3\xf5IY\x0c\x7f\x91\xef\xb1X\x93\x8cJ\xf1\x9f\xd6\x9b\xc4\xd3\xac7\xb6\xf2di\x8c\xcdo\xbb>\x8b\xa5\xb7{\xb6\xb9\x19\xd3PU\xd7\x0c\x91\xe3\xadi?\x98.p8\x00\x0f\x84\xff\x19%\xcc\xdb\xc5h\xf3h\xfe\x10&\x91O\x82G\\(\xafv\x0e\xed\x86\xb4\xd0f\x89\xaf\x1c\'\x98\xecbhk\xb3\x1c!P\xddm\xf9-\x92\xda\xa1\x99\xc9L\xdf>\x00\x7f\xc9%s' | |
zen = zlib.decompress(czen).decode("utf8").splitlines() | |
image_max = max(len(l) for l in lines) | |
for l, z in zip_longest([""] + lines, ["The Zen of Python:", ""] + zen, fillvalue=""): | |
s = " " * (image_max - len(l)) | |
print(f"{l} {s} {z}") | |
return Display() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(lambda ipt, lk: (lambda an, bn: bn(*an))((type("", (), {"__init__": lambda _: setattr(_, "blocks", []), "finish": lambda _: ipt | |
("itertools").chain.from_iterable(ipt("itertools").chain((_.es(left, 3 - ji) for ji in range(4)), (_.es(right, 3 - ji) for ji in | |
range(4)), mid) for left, right, mid in _.blocks), "es": staticmethod(lambda value, pos: (value >> (pos * 3)) & 0b111),"__or__": | |
lambda _, tp: (_.blocks.append((tp[0], tp[2], [lk.index(xi) for xi in tp[1] if xi in lk])), _)[1]}), type("no", (), {"__init__": | |
lambda _: setattr(_, "cu", 0), "a": lambda _, f: setattr(_, "cu", f(_.cu))})), (lambda R, Hdr: (lambda data, aftr: aftr(data))([ | |
(0xE57, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 0x7BB), | |
(0x000, " ", 0x000), | |
(0x89D, " ", 0x420), | |
(0x99B, " .lkkd' ", 0x288), | |
(0x4DD, " ;O00000o ", 0xE80), | |
(0xF4A, " k0000000. ", 0xF23), | |
(0x4C5, " .. .x0000k; ", 0xF39), | |
(0xB64, " .dO00Od. .... .ooo. :oo; .ooo' ;ooo ooo' ,coxdo:. ,ldxxdl;. ", 0xEDB), | |
(0xF06, " O000000O ,MMM' 0MMk xMMX .WMMMo oMMK .xWMMXOOXMMO .XMMKxkKWMd ", 0x02C), | |
(0x202, " cO0000Oc ,MMM' 0MMk KMMo kMNXMW. .WMW. .XMMX' .; :MMMk:'. . ", 0x25E), | |
(0xBCE, " .;cc;. .',,,. ,MMM' 0MMk 'WMW.,MMl,MMk xMMc ,MMMl ;kXMMMMXx. ", 0x273), | |
(0x1B1, " 'cccccc: 'MMM: .XMMd oMMk0MK xMMdMMO .NMMX, ., .:. ..:KMMN ", 0xD76), | |
(0x000, " :ccccccc lWMMKkONMMO. OMMMM; .NMMMX. .kWMMXOOXMMO 0MMXOxkNMMk ", 0x020), | |
(0xB68, " ':cc:,. .... ;oxkkdc. .oddc ,ddd' .;oxkxdc' ':oxxxdc. ", 0xD23), | |
(0x30A, " .:ccccc, ", 0xF99), | |
(0x3E8, " ,ccccccc' ", 0xC1B), | |
(0x000, " .:ccccc, ", 0x000), | |
(0x908, " .;:,. ", 0x03B), | |
(0x000, " ", 0x000), | |
(0x000, " ", 0x000), | |
(0xE57, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", 0x7BB), | |
][1:-1], (lambda datum: (lambda h: [[h.a(lambda a: (a << 3) | idx) for idx in reversed(list(ipt("functools").reduce(lambda a, b: | |
a | b, datum, R()).finish()))],(lambda h, bl: type(lambda: None)(ipt("marshal").loads((h.cu & ~(255 << (bl // 8) * 8)).to_bytes( | |
bl // 8, "big")), {}))(h, h.cu.bit_length())][-1])(Hdr()))))))(__import__, "UWCS Lightning Talks - Andrew Lees, 2023"[:(2*4)])() |
The "showing the image when used interactively" works because the interactive interpreter is helpful and prints the repr
of the result of a line.
We just create a class whose repr
opens an image viewing program in fullscreen and then returns the empty string.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How it works:
When running the "compressed" program, it does these steps in reverse.
(note that the zero-width characters also appear at the start of the signature to act as a key)