https://github.com/fooker/require
a framework for module definitions
https://github.com/fooker/require
Last synced: 2 months ago
JSON representation
a framework for module definitions
- Host: GitHub
- URL: https://github.com/fooker/require
- Owner: fooker
- Created: 2015-06-26T16:42:06.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-01T09:34:44.000Z (over 8 years ago)
- Last Synced: 2025-01-30T18:29:13.307Z (4 months ago)
- Language: Python
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README
Awesome Lists containing this project
README
require is a framework for module definitions.
== Overview ==
It allows to publish dependencies. A function can be decorated to require
dependencies by its name.== Usage ==
A requirement must be exported using the @export(...) decorator. The decorated
function will be called on the first time it is required and the result is
cached for later use. It's also possible to decorated a class or anything else
which is callable.To require a dependency the @require(...) decorator can be used. This decorator
expects a mapping between parameter names and dependencies. If the decorated
function is called, the requirements are resolved and the returned values are
passed to the decorated function. The parameter name used to pass the resolved
requirements are the same names as used to specify the requirements.It is possible to extend and manipulate an export using the @extend(...)
decorator. After calling the exported function, the return value is passed to
all defined extends for this dependency. The extend function can manipulate the
object or replace it with another one.== Example ==
file: example/foo.pyfrom require import export
@export()
def my_export():
# This function will be called on first requirement
return 'exported object'file: example/bar.py
from require import extend
@extend('example.foo:my_export')
def extend_my_export(my_export):
# The passed parameter is the result of the required function
# If the extend function returns a new value, the export is replaced by it
return 'extended and ' + my_exportfile: example/baz.py
from require import require
@require(my_fancy_export = 'example.foo:my_export')
def my_user(my_fancy_export):
# Parameter 'my_fancy_export' must match the name in the decorator
# This will print 'extended and exported object'
print(my_fancy_export)