Last active
April 3, 2020 14:02
-
-
Save davidszotten/c2229079d22ba782b9bb2835fa9f4fd0 to your computer and use it in GitHub Desktop.
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 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