Skip to content

Instantly share code, notes, and snippets.

@tsgiannis
Last active February 18, 2022 22:25
Show Gist options
  • Save tsgiannis/deb93320199d2617514fa9776aa3cd02 to your computer and use it in GitHub Desktop.
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.
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