Add is_delegated() method to utils

This commit is contained in:
Roman Mogilatov 2016-11-04 10:41:40 +02:00
parent 55cde4729d
commit 78cb4296cc
3 changed files with 66 additions and 19 deletions

View File

@ -3,8 +3,7 @@
Cython optimized code.
"""
cpdef tuple parse_positional_injections(tuple args)
cpdef tuple parse_named_injections(dict kwargs)
cimport cython
cdef class Injection:
@ -39,25 +38,44 @@ cdef class NamedInjection(Injection):
return self.__value()
@cython.boundscheck(False)
@cython.wraparound(False)
cdef inline tuple __provide_positional_args(tuple inj_args,
int inj_args_len,
tuple args):
cdef int index
cdef list positional_args
cdef PositionalInjection injection
if inj_args_len > 0:
positional_args = list()
for index in range(inj_args_len):
injection = <PositionalInjection>inj_args[index]
positional_args.append(injection.get_value())
positional_args.extend(args)
args = positional_args
return args
if inj_args_len == 0:
return args
positional_args = list()
for index in range(inj_args_len):
injection = <PositionalInjection>inj_args[index]
positional_args.append(injection.get_value())
positional_args.extend(args)
return positional_args
@cython.boundscheck(False)
@cython.wraparound(False)
cdef inline dict __provide_keyword_args(tuple inj_kwargs,
int inj_kwargs_len,
dict kwargs):
cdef int index
cdef NamedInjection kw_injection
if inj_kwargs_len > 0:
for index in range(inj_kwargs_len):
kw_injection = <NamedInjection>inj_kwargs[index]
kwargs[kw_injection.get_name()] = kw_injection.get_value()
if inj_kwargs_len == 0:
return kwargs
for index in range(inj_kwargs_len):
kw_injection = <NamedInjection>inj_kwargs[index]
kwargs[kw_injection.get_name()] = kw_injection.get_value()
return kwargs
cpdef tuple parse_positional_injections(tuple args)
cpdef tuple parse_named_injections(dict kwargs)

View File

@ -3,8 +3,12 @@
Cython optimized code.
"""
# TODO: replace to cimport
from .utils import is_provider
cimport cython
from .utils import (
is_provider,
is_delegated,
)
cdef class Injection:
@ -18,13 +22,17 @@ cdef class PositionalInjection(Injection):
"""Initializer."""
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = 0 # TODO: use utils.is_delegated()
self.__is_delegated = <int>is_delegated(value)
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
def get_value(self):
"""Return injection value."""
return self.__get_value()
def get_original_value(self):
"""Return original value."""
return self.__value
cdef class NamedInjection(Injection):
"""Keyword injection class."""
@ -34,7 +42,7 @@ cdef class NamedInjection(Injection):
self.__name = name
self.__value = value
self.__is_provider = <int>is_provider(value)
self.__is_delegated = 0 # TODO: use utils.is_delegated()
self.__is_delegated = <int>is_delegated(value)
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
def get_name(self):
@ -45,14 +53,20 @@ cdef class NamedInjection(Injection):
"""Return injection value."""
return self.__get_value()
def get_original_value(self):
"""Return original value."""
return self.__value
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef tuple parse_positional_injections(tuple args):
"""Parse positional injections."""
cdef list injections = list()
cdef int args_len = len(args)
cdef object arg
cdef int index
cdef object arg
cdef PositionalInjection injection
for index in range(args_len):
@ -63,6 +77,8 @@ cpdef tuple parse_positional_injections(tuple args):
return tuple(injections)
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef tuple parse_named_injections(dict kwargs):
"""Parse named injections."""
cdef list injections = list()

View File

@ -52,6 +52,19 @@ def ensure_is_provider(instance):
return instance
def is_delegated(instance):
"""Check if instance is delegated provider.
:param instance: Instance to be checked.
:type instance: object
:rtype: bool
"""
return (not isinstance(instance, six.class_types) and
hasattr(instance, '__IS_DELEGATED__') and
getattr(instance, '__IS_DELEGATED__') is True)
def is_container(instance):
"""Check if instance is container instance.