Package: publicize
PublicizeSet of utilities to help manage how modules are imported or exportedWorks with both of the current major CPython versions, 2.7 and 3.7.Detailed error messages make finding name conflicts easy.Significantly reduce boilerplate, especially usingstar_export,star_import,public_constants, andimport_from_object.Simplifies dynamically reloading modules when source code has changed but restarting the interpreter is impossible or undesirableInstallation$ pip install publicizeFunctionsimport_as_copy(mod_or_name:[str,object])->module:Import a shallow copy ofmod_or_nameimport_from_object(obj, overwrite=False)->dictPretendsobjis a module and imports its public attributespublic(*objects:[str,object], overwrite=False)->objects[0]Mark objects (or names) as public and automatically append them to__all__.
Can be used as a decorator with or without closing parentheses.There are 2 different ways to use it:@public(overwrite=False) <- (with or without parentheses)Simple decorator for functions/classes. Adds the wrapped object’s__name__attribute to__all__public(name_or_object, *names_or_objects)If an object is passed as an argument, all names that refer to that
object will be added to__all__. If a string is passed, the name
will be added assuming the reference actually exists.publicconstants(**constants)->constants:Define public global variables, adding their names to__all__.public_from_import(module:[str,ModuleType], *names, overwrite=False)->{**imported}:from module import namesand add the imported names to__all__.publish_module(module:[str,ModuleType], overwrite=False)->module:Publish everything that would be gotten withfrom module import *reimport_module(module:[str,ModuleType])->module:Clear a module’s dict and reimport it.reverse_star_import(module)->None:Removes what would be imported withfrom module import *safe_star_import(module)->{**imported}:from module import *that will not overwrite existing namesstar_export(*private_modules,ignore_list=None, export_private=True, export_metadata=False, caller=None):Forceexporter(caller by default) to export almost everythingstar_import(module:[str,ModuleType], overwrite=False, module=False, prefix=None, id_mod=None, ignore_private=False, import_metadata=False)->[dict,{**imported}]:Ignore default* importmechanics to import almost everything.Ifprefixis True, it will be prepended to imported names.Ifoverwriteis False, an error is raised instead of overwriting.Ifignore_privateis True then _private names prepended with an
underscore are ignored.Ifimport_metadatais True, then module metadata attributes such as__author__and__version__are imported.Ifmoduleis True, return the module itself, otherwise a dict
mapping of imported:object names.Special attributes of modules such as__path__,__file__, and__all__are never imported.Example usage# in colors.py
from publicize import *
public_from_import('enum', 'Enum')
@public
class Colors(Enum):
    RED = red = 'red'
    BLUE = blue = 'blue'
    GREEN = green = 'green'
COLOR_LIST = [*Colors]
COLOR_DICT=public_constants(**Colors.__members__)
CHARTREUSE = GREEN
CYAN = BLUE
CARMINE = RED
public(CHARTREUSE, CYAN, CARMINE)
random = star_import('random', prefix='random_', module=True)

def random_color():
    return random_choice(COLOR_LIST)

def _get_valid_colors():
    return {k for k, v in globals().items() if isinstance(v, Colors)}>>> from publicize import *>>> import colors>>> colors_namespace =star_import('colors')>>> colors.__all__['BLUE', 'CARMINE', 'CHARTREUSE', 'CYAN', 'Colors', 'Enum', 'GREEN', 'RED', 'blue', 'green', 'red']>>> CHARTREUSE<Colors.GREEN: 'green'>>>>COLOR_DICT['blue']<Colors.BLUE: 'blue>>>> old = CARMINE>>>reverse_star_import('colors')>>>reimport_module('colors').CARMINEis not oldTrue>>> random_color()<Colors.BLUE: 'blue'>>>> _get_colors()>>> {'BLUE', 'CHARTREUSE', 'GREEN', 'CARMINE', 'red', 'CYAN', 'green', 'blue', 'RED'}