Created
February 7, 2019 21:11
-
-
Save orez-/9c7796fb2262db39d72247c88761688e to your computer and use it in GitHub Desktop.
Codec to enable ++x, x++, --x, and x-- in python3.8+
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
import plusplus | |
import module | |
module.main() |
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
# -*- coding: plusplus -*- | |
def main(): | |
x = 0 | |
while x < 3: | |
print(x++) | |
print("--") | |
x = 0 | |
while x < 3: | |
print(++x) |
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
import codecs | |
import re | |
encode = codecs.utf_8_encode | |
swap_sign = {'-': '+', '+': '-'}.__getitem__ | |
def xpp_translate(match): | |
sign = match[2][0] | |
back = swap_sign(sign) | |
return f"(({match[1]}:={match[1]}{sign}1){back}1)" | |
def decode(input, errors='strict'): | |
file_bytes = input.tobytes().split(b'\n') | |
# Replace the first line with an encoding that isn't this one to avoid RecursionError. | |
# Gross! | |
file_bytes[0] = b'# -*- coding: utf-8 -*-' | |
ppx = re.compile(r"(\+\+|\-\-)([a-z_]\w*)") | |
xpp = re.compile(r"([a-z_]\w*)(\+\+|\-\-)") | |
file_bytes = [ | |
xpp.sub( | |
xpp_translate, | |
ppx.sub(lambda m: f"({m[2]}:={m[2]}{m[1][0]}1)", line.decode('utf8')) | |
) | |
for line in file_bytes | |
] | |
return '\n'.join(file_bytes), 0 | |
class IncrementalEncoder(codecs.IncrementalEncoder): | |
def encode(self, input, final=False): | |
return codecs.utf_8_encode(input, self.errors)[0] | |
class IncrementalDecoder(codecs.BufferedIncrementalDecoder): | |
_buffer_decode = codecs.utf_8_decode | |
class StreamWriter(codecs.StreamWriter): | |
encode = codecs.utf_8_encode | |
class StreamReader(codecs.StreamReader): | |
decode = codecs.utf_8_decode | |
def getregentry(name): | |
if name == 'plusplus': | |
return codecs.CodecInfo( | |
name='plusplus', | |
encode=encode, | |
decode=decode, | |
incrementalencoder=IncrementalEncoder, | |
incrementaldecoder=IncrementalDecoder, | |
streamreader=StreamReader, | |
streamwriter=StreamWriter, | |
) | |
codecs.register(getregentry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment