Coder Social home page Coder Social logo

interlinked's Introduction

Quickstart

This initial example shows first pattern matching and then dependency resolution.

from interlinked import run, provide, depend

@provide('echo')
@provide('echo.{name}')
def echo(name='default'):
    return name

@depend(value='echo.test')
@provide('many_echo')
def many_echo(value, repeat=2):
    return ' '.join([value] * repeat)


result = run("echo.spam")
print(result)  # -> spam

result = run("many_echo", repeat=4)
print(result)  # -> test test test test

This second example shows custom dependency resolution (based on previous code).

from interlinked.workflow import default_workflow as wkf

wkf.resolve = lambda target, **kw: wkf.run(target, **kw).upper()
print(wkf.run("many_echo"))  # -> TEST TEST

As you can see, we rebind the resolve method of the workflow.

The workflow object is instanciated for us when interlinked is imported. This object is responsible to keep track of dependencies (based on depend decorator) and to run our code (based on provide decorator).

Resolve is the method that is invoked by the workflow to reify the parameters defined in depend, by default it simply runs the corresponding function and reuse the returned values as input.

By rebinding the resolve method we can inject custom logic at each step of the workflow.

Advanced usages

Load parameters from config dict

You can provide a config dict to a workflow (or declare add it to the default workflow with set_config):

cfg = {
    "hello.{world}" : {
        "param" : " from conf",
    },
    "hello.ham" : {
        "param" : " FROM CONF"
    }
}
wkf = Workflow(config=cfg)

It will be implicitly used when a "provide" route is matched:

@wkf.provide("hello")
@wkf.provide("hello.{world}")
def echo(world, param=""):
    return f"hello {world} {param}"


res = wkf.run("hello.spam")
assert res  == "hello spam from conf"

res = wkf.run("hello.ham")
assert res == "hello ham FROM CONF"

Command line

TODO

Multi workflow

Above examples use the default workflow object. We can use explicit ones like this

wkf_a = Workflow("wkf-a")
wkf_b = Workflow("wkf-b")

@wkf_a.provide('echo-one')
def echo_one():
    return 'one A'

@wkf_b.provide('echo-one')
def echo_one():
    return 'one B'

assert wkf_b.run('echo-one') == 'one B'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.