hiku.expr.core

Expression building blocks

hiku.expr.core.S = <hiku.expr.core._S object>

Helper object to represent symbols in expressions. S.foo.bar in expressions is equivalent to foo.bar in the regular Python.

hiku.expr.core.define(*types: GenericMeta, **kwargs: str) Callable[[Callable[[P], T]], Callable[[P], _Func]]

Annotates function arguments with types.

These annotations are used to type-check expressions and to analyze, which data is used from provided arguments.

Example:

@define(Record[{'id': Integer, 'name': String}])
def image_url(image):
    return 'http://example.com/{id}-{name}'.format(id=image['id'],
                                                   name=image['name'])

Here image_url function accepts an object as argument, and is using two of it’s fields: id field of type Integer and name field of type String. Hiku will check that this function will be used only with objects having at least such two fields.

This annotation also gives ability for Hiku to build a query for low-level graph.

hiku.expr.core.each(var: DotHandler, col: DotHandler, expr: DotHandler) None

Returns a list of the results of the expression evaluation for every item of the sequence provided.

Example:

each(S.x, S.collection, S.x.name)

Equivalent in the regular Python (only for reference):

[x.name for x in collection]
hiku.expr.core.if_(test: Any, then: Any, else_: Any) None

Checks condition and continues to evaluate one of the two expressions provided.

Example:

if_(S.value, 'truish', 'falsish')

Equivalent in the regular Python (only for reference):

if value:
    return 'truish'
else:
    return 'falsish'
hiku.expr.core.if_some(bind: List, then: _Func, else_: Any) None

Used to unpack values with Optional types and using them safely in expressions.

Example:

if_some([S.img, S.this.image],
        image_url(S.img),
        'http://example.com/no-photo.jpg')

Equivalent in the regular Python (only for reference):

if this.image is not None:
    img = this.image
    return image_url(img)
else:
    return 'http://example.com/no-photo.jpg'

If S.this.image has a type Optional[TypeRef['Image']], S.img variable will have a type TypeRef['Image'] and it will be available only in “then”-expression, which will be evaluated only if S.this.image wouldn’t be None. Otherwise “else”-expression will be evaluated.