Skip to content

Instantly share code, notes, and snippets.

@davidszotten
Last active April 3, 2020 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidszotten/c2229079d22ba782b9bb2835fa9f4fd0 to your computer and use it in GitHub Desktop.
Save davidszotten/c2229079d22ba782b9bb2835fa9f4fd0 to your computer and use it in GitHub Desktop.
import ast
import sys
filename = sys.argv[1]
with open(filename) as handle:
source = handle.read()
sourcelines = source.splitlines()
tree = ast.parse(source)
bytestrings = []
class FuncLister(ast.NodeVisitor):
def visit_Bytes(self, node):
# 0 index vs 1 index
bytestrings.append((node.lineno - 1, node.col_offset))
self.generic_visit(node)
FuncLister().visit(tree)
# go through backwards so multiple edits to the same line don't move offsets
# yet to be used
for lineno, col_offset in reversed(bytestrings):
line = sourcelines[lineno]
line = line[:col_offset] + line[col_offset + 1:]
sourcelines[lineno] = line
with open(filename, 'w') as handle:
handle.write('\n'.join(sourcelines))
if sourcelines:
handle.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment