Skip to content

Instantly share code, notes, and snippets.

@mazieres
Created March 15, 2016 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazieres/bcc0628f360bb72ee7f2 to your computer and use it in GitHub Desktop.
Save mazieres/bcc0628f360bb72ee7f2 to your computer and use it in GitHub Desktop.
import re
def get_imports(script):
res = []
patt = re.compile('^(?:import|from).*$', re.MULTILINE)
raw_imports = re.findall(patt, script)
if raw_imports == []:
return None
for imp in raw_imports:
if '#' in imp:
imp = imp.split('#')[0]
if 'as' in imp:
imp = imp.split(' as ')[0]
if 'from ' in imp:
if '*' not in imp.split('import')[-1]:
root_module = imp.split(' ')[1] + '.'
else:
imp = imp.split('import')[0].replace('from ', 'import ')
root_module = ''
else:
root_module = ''
for module in imp.split('import ')[-1].split(','):
neat = root_module + module.strip(' ,')
res.append(neat.strip())
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment