BINP python API

class binp.BINP(kv=<factory>, journal=<factory>, action=<factory>, service=<factory>)[source]
action: binp.action.Action

UI exposed actions (buttons)

property app

Creates FastAPI applications and caches result.

journal: binp.journals.Journals

Journal for operation tracing

kv: binp.kv.KV

Key-Value default storage

service: binp.service.Service

Background services

class binp.Action[source]

Expose user-defined action as ‘button’ in UI

Expose async function as button to ui.

It will not be automatically journaled: it’s up to you add @binp.journal annotation or not.

Example

from binp import BINP
from asyncio import sleep

binp = BINP()

@binp.action
async def invoke():
    '''
    Do something
    '''
    await sleep(3) # emulate some work
    print("done")

By default, action will be exposed with name equal to fully-qualified function name and description from doc-string (if exists).

Exposed name could by optionally defined manually.

from binp import BINP
from asyncio import sleep

binp = BINP()

@binp.action(name='Do Something', description='Emulate some heavy work')
async def invoke():
    await sleep(3)
    print("done")
Conflicts

Actions are indexed by name. If multiple actions defined with the same name - the latest one will be used.

property actions

Copy of list of defined actions prepared for serialization.

Return type

List[ActionInfo]

async invoke(name)[source]

Invoke action by name or ignore. If handler will raise an error, the error will NOT be suppressed.

Parameters

name (str) – action name

Return type

bool

Returns

true if action invoked

class binp.KV(namespace='default', db=None)[source]

Basic Key-Value storage with namespace.

By default - ‘default’ namespace used. All values are serialized to JSON by standard JSON module or in case value is subclass of pydantic BaseMode - by pydantic .json().

Example

from binp import BINP

binp = BINP()

async def save_something():
    # save multiple values to storage
    await binp.kv.set(name="reddec", repo="binp", year=2020)
    # get single value
    name = await binp.kv.get('name')
    assert name == 'reddec'

There is some optimization for saving/loading Pydantic classes when class name itself a key.

Example

from binp import BINP
from pydantic.main import BaseModel

binp = BINP()

class Author(BaseModel):
    name: str
    repo: str
    year: int

async def save_something():
    value = Author(name="reddec", repo="binp", year=2020)
    # save class with key name equal to fully-qualified class name
    await binp.kv.save(value)
    # restore
    saved_value = await binp.kv.load(Author)
    assert value == saved_value
async get(name)[source]

Get save value by name.

Return type

Union[str, int, float, bool, dict, None]

async load(klass)[source]

Load and parse value by key name equal to class name (fqdn)

Parameters

klass (Type[~T]) – BaseModel inherited class to load and parse

Return type

Optional[~T]

async namespaces()[source]

Fetch all namespaces in selected database.

Return type

List[str]

async remove(*names)[source]

Remove multiple values by names

async save(value)[source]

Save value with key name equal to class name (fqdn)

select(namespace)[source]

Get accessor to another namespace in a same database

Return type

KV

async set(**values)[source]

Sav multiple values into storage. All values should be serializable to JSON.