Last active
November 22, 2018 03:08
-
-
Save mmerickel/d83ebca5a6ad1667d8f5 to your computer and use it in GitHub Desktop.
decorate a bunch of items in a package and expose them as a public api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _build_facade(): | |
import sys | |
from .meta.api import scan | |
this = sys.modules[__name__] | |
registry = {} | |
scan(this, registry=registry) | |
globals().update(registry) | |
_build_facade() | |
del _build_facade |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import venusian | |
def scan(pkg, registry=None): | |
if registry is None: | |
registry = {} | |
scanner = venusian.Scanner(registry=registry) | |
scanner.scan(pkg) | |
return registry | |
def expose(wrapped): | |
def callback(scanner, name, obj): | |
scanner.registry[name] = wrapped | |
venusian.attach(wrapped, callback) | |
return wrapped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .api import expose | |
@expose | |
class User(Base): | |
name = Column(Text, nullable=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment