Пример #1
0
def dotcall(name, *args, **kwargs):
    """Creates a function that accepts an object and invokes a member function
    (a "method") on it. The object can be a class instance, a class, a type,
    or even a module.

    :param name: Name of member function to invoke

    The rest of positional and keyword arguments will be passed
    to the member function as its parameters.

    :return: Unary function invoking member function ``name`` on its argument
    """
    ensure_string(name)

    get_member_func = attr_func(name)

    def call(obj):
        member_func = ensure_callable(get_member_func(obj))
        return member_func(*args, **kwargs)

    # through :func:`attr_func`, we may support ``name`` containing dots,
    # but we need to turn it into valid Python identifier for function's name
    call.__name__ = name.replace('.', '__')

    return call
Пример #2
0
def dotcall(name, *args, **kwargs):
    """Creates a function that accepts an object and invokes a member function
    (a "method") on it. The object can be a class instance, a class, a type,
    or even a module.

    :param name: Name of member function to invoke

    The rest of positional and keyword arguments will be passed
    to the member function as its parameters.

    :return: Unary function invoking member function ``name`` on its argument
    """
    ensure_string(name)

    get_member_func = attr_func(name)

    def call(obj):
        member_func = ensure_callable(get_member_func(obj))
        return member_func(*args, **kwargs)

    # through :func:`attr_func`, we may support ``name`` containing dots,
    # but we need to turn it into valid Python identifier for function's name
    call.__name__ = name.replace('.', '__')

    return call
Пример #3
0
def has_identifier_form(s):
    """Check whether given string has a form of a Python identifier.

    Note that this includes Python language keywords, because they exhibit
    a general form of an identifier. See also :func:`is_identifier`.
    """
    ensure_string(s)
    return bool(IDENTIFIER_FORM_RE.match(s))
Пример #4
0
def has_identifier_form(s):
    """Check whether given string has a form of a Python identifier.

    Note that this includes Python language keywords, because they exhibit
    a general form of an identifier. See also :func:`is_identifier`.
    """
    ensure_string(s)
    return bool(IDENTIFIER_FORM_RE.match(s))
Пример #5
0
def is_identifier(s):
    """Check whether given string is a valid Python identifier.

    Note that this excludes language keywords, even though they exhibit
    a general form of an identifier. See also :func:`has_identifier_form`.

    :param s: String to check
    :return: Whether ``s`` is a valid Python identifier
    """
    ensure_string(s)

    if not IDENTIFIER_FORM_RE.match(s):
        return False
    if is_keyword(s):
        return False

    # ``None`` is not part of ``keyword.kwlist`` in Python 2.x,
    # so we need to check for it explicitly
    if s == 'None' and not IS_PY3:
        return False

    return True
Пример #6
0
def is_identifier(s):
    """Check whether given string is a valid Python identifier.

    Note that this excludes language keywords, even though they exhibit
    a general form of an identifier. See also :func:`has_identifier_form`.

    :param s: String to check
    :return: Whether ``s`` is a valid Python identifier
    """
    ensure_string(s)

    if not IDENTIFIER_FORM_RE.match(s):
        return False
    if is_keyword(s):
        return False

    # ``None`` is not part of ``keyword.kwlist`` in Python 2.x,
    # so we need to check for it explicitly
    if s == 'None' and not IS_PY3:
        return False

    return True
Пример #7
0
 def test_bytestring__py2(self):
     __unit__.ensure_string(self.BYTE_STRING)
Пример #8
0
 def test_bytestring__py3(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(self.BYTE_STRING)
Пример #9
0
 def test_bytestring__py2(self):
     __unit__.ensure_string(self.BYTE_STRING)
Пример #10
0
 def test_unicode_string(self):
     __unit__.ensure_string(self.UNICODE_STRING)
Пример #11
0
 def test_default_string(self):
     __unit__.ensure_string(self.DEFAULT_STRING)
Пример #12
0
 def test_some_object(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(object())
Пример #13
0
 def test_none(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(None)
Пример #14
0
 def __init__(self, method, stage, order):
     self.method = method
     self.stage = ensure_string(stage)
     self.order = order
Пример #15
0
 def test_bytestring__py3(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(self.BYTE_STRING)
Пример #16
0
 def test_unicode_string(self):
     __unit__.ensure_string(self.UNICODE_STRING)
Пример #17
0
 def test_default_string(self):
     __unit__.ensure_string(self.DEFAULT_STRING)
Пример #18
0
 def test_some_object(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(object())
Пример #19
0
 def test_none(self):
     with self.assertRaises(TypeError):
         __unit__.ensure_string(None)
Пример #20
0
 def __init__(self, method, stage, order):
     self.method = method
     self.stage = ensure_string(stage)
     self.order = order