Last active
February 18, 2022 22:25
-
-
Save tsgiannis/deb93320199d2617514fa9776aa3cd02 to your computer and use it in GitHub Desktop.
Split a code file into smaller ones ...e.g you have a python file that has multiple classes ..and for easier studying you want to split it.
This file contains hidden or 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
matches = ["class", "(", ":"] | |
with open("sailor.py") as f: | |
lines = f.readlines() | |
currentclassname = '' | |
currentlines =[] | |
for line in lines : | |
if all(x in line for x in matches) : # we have found a class | |
classname = line.split(" ", 2)[1].split("(",1)[0] | |
print(classname) | |
currentclassname=classname | |
if len(currentclassname): | |
with open(classname+'.py', 'a') as the_file: | |
the_file.write(line) | |
else: | |
if len(currentclassname): | |
with open(currentclassname+'.py', 'a') as the_file: | |
the_file.write(line) | |
else: | |
with open('_initial.py', 'a') as the_file: | |
the_file.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment