Skip to main contentNedix.io

Magic

Parent import replacer

class ParentImportReplacer():
def _parent_import_replacements_callback(self, function, aliases):
def callback(*args, **kwargs):
original_globals = function.__globals__
function.__globals__.update(aliases)
value = function(*args, **kwargs)
function.__globals__.update(original_globals)
return value
return callback

Usage

class Apple(Fruit, ParentImportReplacer):
def __init__(self, *args, **kwargs):
self.parent_import_replacements = {
'FunctionClass': {
'OriginalImport': ReplacementImplementation,
}
}
super().__init__(*args, **kwargs)