Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Last active August 29, 2015 14:07
Show Gist options
  • Save RemyPorter/441e5d01d5b828152297 to your computer and use it in GitHub Desktop.
Save RemyPorter/441e5d01d5b828152297 to your computer and use it in GitHub Desktop.
import sys
def lift_to_module(module_name, types):
"""Take a list of dynamically generated types, or anything really,
and lift them to the root level of the module.
I mostly use this when I do something like this:
>>> some_data_source = ["Foo", "Bar", "Goo"]
>>> types = [
SomeMetaClass("TypeName"+ i, (object,), inputs)
for (i,inputs) in enumerate(some_data_source)
]
>>> lift_to_module(__name__, types)
This creates new classes, based on the data source,
then "lifts" them to export the types by name.
The module now contains TypeName0, TypeName1, TypeName2 as
new classes.
"""
mod = sys.modules[module_name]
for i in types:
setattr(mod, i.__name__, i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment