当前位置: 首页>>代码示例>>Python>>正文


Python Promise.resolve方法代码示例

本文整理汇总了Python中promise.Promise.resolve方法的典型用法代码示例。如果您正苦于以下问题:Python Promise.resolve方法的具体用法?Python Promise.resolve怎么用?Python Promise.resolve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在promise.Promise的用法示例。


在下文中一共展示了Promise.resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_filter_set

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def get_filter_set(cls, info: 'ResolveInfo') -> 'FilterSet':
        """
        Get field filter set.

        Args:
            info: Graphene resolve info object.

        Returns:
            FilterSet class from field args.

        """
        field_name = info.field_asts[0].name.value
        schema_field = info.parent_type.fields.get(field_name)
        filters_type = schema_field.args[cls.filter_arg].type
        filters: 'FilterSet' = filters_type.graphene_type
        return filters 
开发者ID:art1415926535,项目名称:graphene-sqlalchemy-filter,代码行数:18,代码来源:connection_field.py

示例2: _get_filter_set

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def _get_filter_set(cls, info: 'ResolveInfo') -> 'FilterSet':
        """
        Get field filter set.

        Args:
            info: Graphene resolve info object.

        Returns:
            FilterSet class from field args.

        """
        field_name = info.field_asts[0].name.value
        schema_field = info.parent_type.fields.get(field_name)
        filters_type = schema_field.args[cls.filter_arg].type
        filters: 'FilterSet' = filters_type.graphene_type
        return filters 
开发者ID:art1415926535,项目名称:graphene-sqlalchemy-filter,代码行数:18,代码来源:connection_field.py

示例3: test_3_2_1_2

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_3_2_1_2():
    """
    That that the second argument to 'then' is ignored if it
    is not a function.
    """
    results = {}
    nonFunctions = [None, False, 5, {}, []]

    def testNonFunction(nonFunction):
        def foo(k, r):
            results[k] = r

        p1 = Promise.resolve("Error: " + str(nonFunction))
        p2 = p1.then(lambda r: foo(str(nonFunction), r), nonFunction)
        p2._wait()

    for v in nonFunctions:
        testNonFunction(v)

    for v in nonFunctions:
        assert "Error: " + str(v) == results[str(v)] 
开发者ID:syrusakbary,项目名称:promise,代码行数:23,代码来源:test_spec.py

示例4: test_3_2_2_1

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_3_2_2_1():
    """
    The first argument to 'then' must be called when a promise is
    fulfilled.
    """

    c = Counter()

    def check(v, c):
        assert v == 5
        c.tick()

    p1 = Promise.resolve(5)
    p2 = p1.then(lambda v: check(v, c))
    p2._wait()
    assert 1 == c.value() 
开发者ID:syrusakbary,项目名称:promise,代码行数:18,代码来源:test_spec.py

示例5: test_3_2_5_1_when

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_3_2_5_1_when():
    """
    Then can be called multiple times on the same promise
    and callbacks must be called in the order of the
    then calls.
    """

    def add(l, v):
        l.append(v)

    p1 = Promise.resolve(2)
    order = []
    p2 = p1.then(lambda v: add(order, "p2"))
    p3 = p1.then(lambda v: add(order, "p3"))
    p2._wait()
    p3._wait()
    assert 2 == len(order)
    assert "p2" == order[0]
    assert "p3" == order[1] 
开发者ID:syrusakbary,项目名称:promise,代码行数:21,代码来源:test_spec.py

示例6: test_3_2_5_1_if

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_3_2_5_1_if():
    """
    Then can be called multiple times on the same promise
    and callbacks must be called in the order of the
    then calls.
    """

    def add(l, v):
        l.append(v)

    p1 = Promise.resolve(2)
    order = []
    p2 = p1.then(lambda v: add(order, "p2"))
    p3 = p1.then(lambda v: add(order, "p3"))
    p2._wait()
    p3._wait()
    assert 2 == len(order)
    assert "p2" == order[0]
    assert "p3" == order[1] 
开发者ID:syrusakbary,项目名称:promise,代码行数:21,代码来源:test_spec.py

示例7: test_3_2_6_2_when

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_3_2_6_2_when():
    """
    Promises returned by then must be rejected when any of their
    callbacks throw an exception.
    """

    def fail(v):
        raise AssertionError("Exception Message")

    p1 = Promise.resolve(5)
    pf = p1.then(fail)
    pf._wait()
    assert pf.is_rejected
    assert_exception(pf.reason, AssertionError, "Exception Message")

    p2 = Promise.reject(Exception("Error"))
    pr = p2.then(None, fail)
    pr._wait()
    assert pr.is_rejected
    assert_exception(pr.reason, AssertionError, "Exception Message") 
开发者ID:syrusakbary,项目名称:promise,代码行数:22,代码来源:test_spec.py

示例8: test_benchmark_promise_creation_with_reject

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_benchmark_promise_creation_with_reject(benchmark):
    do_resolve = lambda resolve, reject: reject(Exception("Error"))

    def create_promise():  # unnecessary function call
        p = Promise(do_resolve)
        # p._wait()
        return p

    with raises(Exception) as exc_info:
        result = benchmark(create_promise).get()

    assert str(exc_info.value) == "Error"


# def test_benchmark_promisify_promise(benchmark):
#     instance = Promise()

#     def create_promise():  # unnecessary function call
#         return promisify(instance)

#     result = benchmark(create_promise)

#     assert isinstance(result, Promise) 
开发者ID:syrusakbary,项目名称:promise,代码行数:25,代码来源:test_benchmark.py

示例9: test_issue_11

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_issue_11():
    # https://github.com/syrusakbary/promise/issues/11
    def test(x):
        def my(resolve, reject):
            if x > 0:
                resolve(x)
            else:
                reject(Exception(x))

        return Promise(my)

    promise_resolved = test(42).then(lambda x: x)
    assert promise_resolved.get() == 42

    promise_rejected = test(-42).then(lambda x: x, lambda e: str(e))
    assert promise_rejected.get() == "-42" 
开发者ID:syrusakbary,项目名称:promise,代码行数:18,代码来源:test_issues.py

示例10: test_issue_75

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_issue_75():
    def function_with_local_type():
        class A:
            pass

        a = A()
        assert a == Promise.resolve(a).get()

        return weakref.ref(A)

    weak_reference = function_with_local_type()

    # The local type 'A' from the function is still kept alive by reference cycles.
    gc.collect()

    # Now the local type should have been garbage collected,
    # such that the weak reference should be invalid.
    assert not weak_reference() 
开发者ID:syrusakbary,项目名称:promise,代码行数:20,代码来源:test_issues.py

示例11: test_supports_loading_multiple_keys_in_one_call

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_supports_loading_multiple_keys_in_one_call():
    def call_fn(keys):
        return Promise.resolve(keys)

    identity_loader = DataLoader(call_fn)

    promise_all = identity_loader.load_many([1, 2])
    assert isinstance(promise_all, Promise)

    values = promise_all.get()
    assert values == [1, 2]

    promise_all = identity_loader.load_many([])
    assert isinstance(promise_all, Promise)

    values = promise_all.get()
    assert values == [] 
开发者ID:syrusakbary,项目名称:promise,代码行数:19,代码来源:test_dataloader.py

示例12: test_can_represent_failures_and_successes_simultaneously

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_can_represent_failures_and_successes_simultaneously():
    @Promise.safe
    def do():
        def resolve(keys):
            mapped_keys = [
                key if key % 2 == 0 else Exception("Odd: {}".format(key))
                for key in keys
            ]
            return Promise.resolve(mapped_keys)

        even_loader, load_calls = id_loader(resolve=resolve)

        promise1 = even_loader.load(1)
        promise2 = even_loader.load(2)

        with raises(Exception) as exc_info:
            promise1.get()

        assert str(exc_info.value) == "Odd: 1"
        value2 = promise2.get()
        assert value2 == 2
        assert load_calls == [[1, 2]]

    do().get() 
开发者ID:syrusakbary,项目名称:promise,代码行数:26,代码来源:test_dataloader.py

示例13: test_caches_failed_fetches

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_caches_failed_fetches():
    @Promise.safe
    def do():
        def resolve(keys):
            mapped_keys = [Exception("Error: {}".format(key)) for key in keys]
            return Promise.resolve(mapped_keys)

        error_loader, load_calls = id_loader(resolve=resolve)

        with raises(Exception) as exc_info:
            error_loader.load(1).get()

        assert str(exc_info.value) == "Error: 1"

        with raises(Exception) as exc_info:
            error_loader.load(1).get()

        assert str(exc_info.value) == "Error: 1"

        assert load_calls == [[1]]

    do().get() 
开发者ID:syrusakbary,项目名称:promise,代码行数:24,代码来源:test_dataloader.py

示例14: test_wrong_loader_return_type_does_not_block_async_instance

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def test_wrong_loader_return_type_does_not_block_async_instance():
    @Promise.safe
    def do():
        def do_resolve(x):
            return x

        a_loader, a_load_calls = id_loader(resolve=do_resolve)

        with raises(Exception):
            a_loader.load("A1").get()
        assert async_instance.have_drained_queues
        with raises(Exception):
            a_loader.load("A2").get()
        assert async_instance.have_drained_queues

    do().get() 
开发者ID:syrusakbary,项目名称:promise,代码行数:18,代码来源:test_dataloader.py

示例15: social_auth

# 需要导入模块: from promise import Promise [as 别名]
# 或者: from promise.Promise import resolve [as 别名]
def social_auth(f):
    @psa
    @wraps(f)
    def wrapper(cls, root, info, social, **kwargs):
        def on_resolve(payload):
            payload.social = social
            return payload

        result = f(cls, root, info, social, **kwargs)

        if is_thenable(result):
            return Promise.resolve(result).then(on_resolve)
        return on_resolve(result)
    return wrapper 
开发者ID:flavors,项目名称:django-graphql-social-auth,代码行数:16,代码来源:decorators.py


注:本文中的promise.Promise.resolve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。