Skip to content

Instantly share code, notes, and snippets.

@afq984
Last active April 9, 2021 16:51
Show Gist options
  • Save afq984/a239b16eb92dfc4e40775f6c3367e376 to your computer and use it in GitHub Desktop.
Save afq984/a239b16eb92dfc4e40775f6c3367e376 to your computer and use it in GitHub Desktop.
import argparse
import sys
import tokenize
def professional(filename):
result = []
line = 0
displacement = 0
with open(filename, "rb") as file:
for tok in tokenize.tokenize(file.readline):
if line != tok.line:
line = tok.line
displacement = 0
if displacement != 0:
tok = tok._replace(start=(tok.start[0], tok.start[1] + displacement))
if (
tok.type == tokenize.NUMBER
and not tok.string.startswith("0")
and len(tok.string) > 1
):
try:
value = int(tok.string)
except ValueError:
pass
else:
string = hex(value)
displacement += len(string) - len(tok.string)
tok = tok._replace(string=string)
if displacement != 0:
tok = tok._replace(end=(tok.end[0], tok.end[1] + displacement))
result.append(tok)
sys.stdout.buffer.write(tokenize.untokenize(result))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("filename")
args = parser.parse_args()
professional(args.filename)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment