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
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))
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
def test_bytestring__py2(self): __unit__.ensure_string(self.BYTE_STRING)
def test_bytestring__py3(self): with self.assertRaises(TypeError): __unit__.ensure_string(self.BYTE_STRING)
def test_unicode_string(self): __unit__.ensure_string(self.UNICODE_STRING)
def test_default_string(self): __unit__.ensure_string(self.DEFAULT_STRING)
def test_some_object(self): with self.assertRaises(TypeError): __unit__.ensure_string(object())
def test_none(self): with self.assertRaises(TypeError): __unit__.ensure_string(None)
def __init__(self, method, stage, order): self.method = method self.stage = ensure_string(stage) self.order = order