-
-
Save a-n-d-r-e-w-l/9f09b901a879bad5cc46d0c9607201d9 to your computer and use it in GitHub Desktop.
Source for my talk's (https://www.youtube.com/watch?v=t863QfAOmlY) final demo.
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
"""custom codec to screw with people""" | |
import codecs | |
### Codec APIs | |
replacement = r""" | |
import subprocess | |
from time import sleep | |
TEXT = "The End." | |
delay = { | |
3: 0.6, | |
7: 1 | |
} | |
for i in range(len(TEXT) + 1): | |
subprocess.run(["clear"]) | |
print("\n" * 10) | |
subprocess.run(["figlet", "-f", "big", TEXT[:i]]) | |
sleep(delay.get(i, 0.3)) | |
input("") | |
subprocess.run(["clear"]) | |
""" | |
def encode(input, errors): | |
return codecs.utf_8_encode(input, errors) | |
def decode(input, errors): | |
text, l = codecs.utf_8_decode(input, errors) | |
if "3" in text: | |
return replacement, l | |
return "", l | |
class Codec(codecs.Codec): | |
# Note: Binding these as C functions will result in the class not | |
# converting them to methods. This is intended. | |
encode = encode | |
decode = decode | |
class IncrementalEncoder(codecs.IncrementalEncoder): | |
def encode(self, input, final=False): | |
return encode(input, self.errors)[0] | |
class IncrementalDecoder(codecs.IncrementalDecoder): | |
def decode(self, input, final=False): | |
return decode(input, self.errors)[0] | |
class StreamWriter(Codec,codecs.StreamWriter): | |
pass | |
class StreamReader(Codec,codecs.StreamReader): | |
pass | |
def getregentry(): | |
return codecs.CodecInfo( | |
name='false_encoding', | |
encode=Codec.encode, | |
decode=Codec.decode, | |
incrementalencoder=IncrementalEncoder, | |
incrementaldecoder=IncrementalDecoder, | |
streamreader=StreamReader, | |
streamwriter=StreamWriter, | |
) |
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
# "The Worst Python Techniques You Might Get Away With" | |
# rate my coding: 1/10 usefulness, 10/10 style | |
# UWCS Lightning Talks - Andrew Lees, 2023 | |
The Zen of Python: | |
.,:clooooolc;,. | |
;ooloooooooooooo:. Beautiful is better than ugly. | |
;oc loooooooooooo. Explicit is better than implicit. | |
:oo,.,ooooooooooooo. Simple is better than complex. | |
,:::::::::ooooooooo. Complex is better than complicated. | |
.,;::::::::::::::ooooooooo. .... Flat is better than nested. | |
.loooooooooooooooooooooooooo. ....... Sparse is better than dense. | |
.oooooooooooooooooooooooooooo. ........ Readability counts. | |
:ooooooooooooooooooooooooooo; ......... Special cases aren't special enough to break the rules. | |
ooooooooooool;,,,,,,,,,,,,.. ........... Although practicality beats purity. | |
looooooooo, ........................... Errors should never pass silently. | |
;oooooooo. ............................ Unless explicitly silenced. | |
oooooooo ............................. In the face of ambiguity, refuse the temptation to guess. | |
.coooooo ............................ There should be one-- and preferably only one --obvious way to do it. | |
..... .......... Although that way may not be obvious at first unless you're Dutch. | |
................... Now is better than never. | |
.............. .. Although never is often better than *right* now. | |
............. .. If the implementation is hard to explain, it's a bad idea. | |
.............. If the implementation is easy to explain, it may be a good idea. | |
....... Namespaces are one honking great idea -- let's do more of those! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took me ages to find out what the hell was going on here (I am not well versed in the field of torturing programming languages). Finally found this: https://docs.python.org/3/tutorial/interpreter.html#source-code-encoding
According to this doc, this is how to specify the format:
# -*- coding: encoding -*-
This clearly fails both counts: it's not in that format, and it's the second line while the first is not a shebang. Therefore the CPython interpreter must have a looser requirement.
Digging further, I found this more authoritative reference: https://docs.python.org/3/reference/lexical_analysis.html#encoding-declarations, which states (emphasis mine):
So I guess that first thing I found, which was a tutorial page, deliberately gave a stricter rule to not encourage people to do the awful things you've done.