How to use the descarteslabs.common.graft.syntax.is_graft function in descarteslabs

To help you get started, we’ve selected a few descarteslabs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github descarteslabs / descarteslabs-python / descarteslabs / common / graft / client / client.py View on Github external
def is_delayed(x):
    "Whether x is a delayed-like: ``x.graft`` is a graft-like mapping"
    try:
        return syntax.is_graft(x.graft)
    except AttributeError:
        return False
github descarteslabs / descarteslabs-python / descarteslabs / common / graft / client / client.py View on Github external
def is_keyref_graft(value):
    return syntax.is_graft(value) and len(value) == 1 and next(iter(value)) == "returns"
github descarteslabs / descarteslabs-python / descarteslabs / common / graft / client / client.py View on Github external
get a value graft back, regardless of whether ``graft`` was a function
        graft or a value graft.

        If False (default), function grafts are returned unmodified.

        If True and ``graft`` is a function graft, a value graft is returned
        that refers to that function.

    Returns
    -------
    isolated_graft: dict
        Value graft representing the same value as ``graft``,
        but isolated to a subscope. Or, if ``graft`` was a function graft,
        and ``wrap_function`` is False, it's returned unmodified.
    """
    graft = graft if syntax.is_graft(graft) else value_graft(graft)

    if is_function_graft(graft):
        if not wrap_function:
            return graft
        else:
            subgraft_key = guid()
            return {subgraft_key: graft, "returns": subgraft_key}
    else:
        subgraft_key = guid()
        result_key = guid()
        return {subgraft_key: graft, result_key: [subgraft_key], "returns": result_key}
github descarteslabs / descarteslabs-python / descarteslabs / common / graft / client / client.py View on Github external
pos_args_grafts = [
        arg if syntax.is_graft(arg) else value_graft(arg) for arg in args
    ]
    named_arg_grafts = {
        name: (arg if syntax.is_graft(arg) else value_graft(arg))
        for name, arg in six.iteritems(kwargs)
    }

    if is_delayed(function):
        function = function.graft

    result_graft = {}
    function_key = None
    if isinstance(function, str):
        function_key = function
    elif syntax.is_graft(function):
        if "parameters" in function:
            # function considered an actual function object, insert it as a subgraft
            param_names = function.get("parameters", [])
            syntax.check_args(len(args), six.viewkeys(kwargs), param_names)

            function_key = guid()
            result_graft[function_key] = function
        else:
            # function considered the value it returns; inline its graft.
            # this is the case with higher-order functions,
            # where `function` is an apply expression that returns another function.
            # we don't check args because that would require interpreting the graft.
            result_graft.update(function)
            function_key = function["returns"]
    else:
        raise TypeError(
github descarteslabs / descarteslabs-python / descarteslabs / common / graft / interpreter / interpreter.py View on Github external
if syntax.is_application(expr):
        func = get(expr[0], body, env, debug=debug)

        if syntax.is_key(expr[-1]):
            expr.append({})

        positional_args = tuple(get(key, body, env, debug=debug) for key in expr[1:-1])
        named_args = {
            name: get(key, body, env, debug=debug)
            for name, key in six.iteritems(expr[-1])
        }

        return func(*positional_args, **named_args)
    elif syntax.is_literal(expr):
        return expr
    elif syntax.is_graft(expr):
        return as_function(expr, body, env, debug=debug)
    elif syntax.is_quoted_json(expr):
        return expr[0]
    else:
        raise exceptions.GraftSyntaxError("Not a valid expression: {}".format(expr))
github descarteslabs / descarteslabs-python / descarteslabs / common / graft / client / client.py View on Github external
Graft representing a function that returns ``result`` and takes ``parameters``.
    """
    first_guid = kwargs.pop("first_guid", None)
    if len(kwargs) > 0:
        raise TypeError(
            "Unexpected keyword arguments {}".format(", ".join(map(repr, kwargs)))
        )
    if first_guid is not None:
        first_guid = int(first_guid)

    parameters = [
        param["returns"] if is_keyref_graft(param) else param for param in parameters
    ]
    if not syntax.is_params(parameters):
        raise ValueError("Invalid parameters for a graft: {}".format(parameters))
    result_graft = result if syntax.is_graft(result) else value_graft(result)

    if first_guid is not None:
        orig_result_graft = result_graft
        result_graft = orig_result_graft.copy()
        containing_scope = {
            k: result_graft.pop(k)
            for k in orig_result_graft
            if _is_outer_scope(k, first_guid)
        }
    else:
        containing_scope = {}

    if is_function_graft(result_graft):
        # Graft that returns a function object; i.e. has a "parameters" key
        key = guid()
        containing_scope.update(
github descarteslabs / descarteslabs-python / descarteslabs / workflows / types / core / core.py View on Github external
def _from_graft(cls, graft):
        """
        Create an instance of this class with the given graft dict, circumventing ``__init__``

        To use safely, this class must function correctly if its ``__init__`` method is not called.

        This is used to let your class present a friendly user-facing constructor,
        but still support copy-constuction or casting when necessary.
        """
        assert syntax.is_graft(
            graft
        ), "Attempted to instantiate {} from the non-graft-like object {!r}".format(
            cls.__name__, graft
        )
        # create a new, empty ``cls`` object, circumventing its __init__ method.
        new = cls.__new__(cls)
        new.graft = graft
        return new