Skip to content

Take control of those forbidden Python wildcard * imports! Star Namer will replace star imports with function names. Star Wrangler will scrape imported modules for only the required code and copy it along with dependencies. from_to_import will convert `from modname import` with `modname.function_name`

Notifications You must be signed in to change notification settings

SurpriseDog/Star-Wrangler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SurpriseDog presents a collection of scripts to help you tame those forbidden wildcard * imports and clean up your imports.

Star Namer

Will go through a list of wildcard * imports for a script and replace them with the actual functions to be imported.

For example: from common import *

will become: from common import fuction1, function2, function3...

Usage: ./star_namer.py module_filename script_filename

Example: ./star_name.py common.py script_name.py

Get full help with: ./star_namer.py -h


Star Wrangler

Do you want to publish a script that imports a couple of functions, but not have to post the entire module with thousands of lines, just to import 2 functions? Wouldn't it be nice to only copy the code from the functions you need (and all of their dependencies?

Star Wrangler will scrape through a list of imported modules and generate a .py file containing all of the required code and do so in the correct order. Then it will examine each function individually so that any dependencies are also copied over. Finally a list of necessary imports are generated at the top.

The way this works is that each section of code is examined with pylint to see if there are any undefined functions, then it compares that list with the list of all functions in the target module.

Look at sd/common.py for an example of this; a code file generated by this very script. If you have multiple modules to copy from, then you can do what I do and create a module called "common" with a bunch of "from mod_name import *..." to tie together multiple modules in one group.

Usage:

  ./star_wrangler.py module_filename script 1 script 2 ...

For Example: ./star_wrangler.py sd/common.py *.py

will run the script on itself, pulling every function used in common.py and generating a new file.

Get help with ./star_wrangler.py -h


From_to_import.py

Convert a script with: from modname import function to import modname so you can have lines of code with modname.function which is considered to be more explicit than wondering where each function was declared.

For example, if we have a script with :

  from universe import function
  ...
  ...
  ...
  function()

It will be converted to:

  import universe
  ...
  ...
  ...
  universe.function()

Usage: from_to_import.py script_name modname


Issues that may be fixed later, :

  • Text alignment issues caused by inserting characters. Comments don't match up anymore.
  • Special handling for aliased functions. For example: import quickrun as qrun doesn't get handled correctly and ends up with modname.qrun in the code instead of the actual modname.quickrun

If anyone is interested let me know or better yet, sumbit a patch.

Requirements:

  • pylint - Must be updated to at least version 2.4

It’s easy to make a mistake and end up with multiple versions of pylint running around the system, so if the script doesn’t work, double check the path for pylint in shared.py

About

Take control of those forbidden Python wildcard * imports! Star Namer will replace star imports with function names. Star Wrangler will scrape imported modules for only the required code and copy it along with dependencies. from_to_import will convert `from modname import` with `modname.function_name`

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages