コード例 #1
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__keyword(self):
     self.assertFalse(__unit__.is_identifier('if'))
     self.assertFalse(__unit__.is_identifier('for'))
     self.assertFalse(__unit__.is_identifier('from'))
     self.assertFalse(__unit__.is_identifier('yield'))
     self.assertFalse(__unit__.is_identifier('return'))
     self.assertFalse(__unit__.is_identifier('finally'))
コード例 #2
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__keyword(self):
     self.assertFalse(__unit__.is_identifier('if'))
     self.assertFalse(__unit__.is_identifier('for'))
     self.assertFalse(__unit__.is_identifier('from'))
     self.assertFalse(__unit__.is_identifier('yield'))
     self.assertFalse(__unit__.is_identifier('return'))
     self.assertFalse(__unit__.is_identifier('finally'))
コード例 #3
0
def attr_func(*attrs, **kwargs):
    """Creates an "attribute function" for given attribute name(s).

    Resulting function will retrieve attributes with given names, in order,
    from the object that has been passed to it.
    For example, ``attr_func('a', 'b')(foo)`` yields the same as ``foo.a.b``

    :param attrs: Attribute names
    :param default: Optional keyword argument specifying default value
                    that will be returned when some attribute is not present

    :return: Unary attribute function
    """
    ensure_argcount(attrs, min_=1)
    ensure_keyword_args(kwargs, optional=('default', ))

    # preprocess argument list:
    # * allow dots in arguments, interpreting them as multiple attributes,
    #   e.g. ``attr_func('a.b')`` as ``attr_func('a', 'b')``
    # * make sure the attribute names are valid Python identifiers
    attrs = map(ensure_string, attrs)
    attrs = flatten(
        attr.split('.') if '.' in attr else [attr] for attr in attrs)
    for attr in attrs:
        if not is_identifier(attr):
            raise ValueError("'%s' is not a valid attribute name", attr)

    if 'default' in kwargs:
        default = kwargs['default']
        if len(attrs) == 1:
            getattrs = lambda obj: getattr(obj, attrs[0], default)
        else:

            def getattrs(obj):
                for attr in attrs:
                    try:
                        obj = getattr(obj, attr)
                    except AttributeError:
                        return default
                return obj
    else:
        if len(attrs) == 1:
            getattrs = operator.attrgetter(attrs[0])
        else:

            def getattrs(obj):
                for attr in attrs:
                    obj = getattr(obj, attr)
                return obj

    return getattrs
コード例 #4
0
ファイル: functions.py プロジェクト: Xion/taipan
def attr_func(*attrs, **kwargs):
    """Creates an "attribute function" for given attribute name(s).

    Resulting function will retrieve attributes with given names, in order,
    from the object that has been passed to it.
    For example, ``attr_func('a', 'b')(foo)`` yields the same as ``foo.a.b``

    :param attrs: Attribute names
    :param default: Optional keyword argument specifying default value
                    that will be returned when some attribute is not present

    :return: Unary attribute function
    """
    ensure_argcount(attrs, min_=1)
    ensure_keyword_args(kwargs, optional=('default',))

    # preprocess argument list:
    # * allow dots in arguments, interpreting them as multiple attributes,
    #   e.g. ``attr_func('a.b')`` as ``attr_func('a', 'b')``
    # * make sure the attribute names are valid Python identifiers
    attrs = map(ensure_string, attrs)
    attrs = flatten(attr.split('.') if '.' in attr else [attr]
                    for attr in attrs)
    for attr in attrs:
        if not is_identifier(attr):
            raise ValueError("'%s' is not a valid attribute name", attr)

    if 'default' in kwargs:
        default = kwargs['default']
        if len(attrs) == 1:
            getattrs = lambda obj: getattr(obj, attrs[0], default)
        else:
            def getattrs(obj):
                for attr in attrs:
                    try:
                        obj = getattr(obj, attr)
                    except AttributeError:
                        return default
                return obj
    else:
        if len(attrs) == 1:
            getattrs = operator.attrgetter(attrs[0])
        else:
            def getattrs(obj):
                for attr in attrs:
                    obj = getattr(obj, attr)
                return obj

    return getattrs
コード例 #5
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__underscore_prefix(self):
     self.assertTrue(__unit__.is_identifier('_42'))
コード例 #6
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__number(self):
     self.assertFalse(__unit__.is_identifier('42'))
コード例 #7
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__number_prefix(self):
     self.assertFalse(__unit__.is_identifier('42foo'))
コード例 #8
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__underscore_prefix(self):
     self.assertTrue(__unit__.is_identifier('_42'))
コード例 #9
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__empty(self):
     self.assertFalse(__unit__.is_identifier(''))
コード例 #10
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__snake_case(self):
     self.assertTrue(__unit__.is_identifier('foo_bar'))
コード例 #11
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__self(self):
     self.assertTrue(__unit__.is_identifier('self'))
コード例 #12
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__global_constants__py2(self):
     self.assertTrue(__unit__.is_identifier('True'))
     self.assertTrue(__unit__.is_identifier('False'))
     self.assertFalse(__unit__.is_identifier('None'))  # can't assign to it
コード例 #13
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__global_constants__py3(self):
     self.assertFalse(__unit__.is_identifier('True'))  # keyword in py3
     self.assertFalse(__unit__.is_identifier('False'))  # keyword in py3
     self.assertFalse(__unit__.is_identifier('None'))  # keyword in py3
コード例 #14
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__number(self):
     self.assertFalse(__unit__.is_identifier('42'))
コード例 #15
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__self(self):
     self.assertTrue(__unit__.is_identifier('self'))
コード例 #16
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__snake_case(self):
     self.assertTrue(__unit__.is_identifier('foo_bar'))
コード例 #17
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__with_whitespace(self):
     self.assertFalse(__unit__.is_identifier('foo bar'))
     self.assertFalse(__unit__.is_identifier('foo\tbar'))
コード例 #18
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__letters(self):
     self.assertTrue(__unit__.is_identifier('FooBar'))
     self.assertTrue(__unit__.is_identifier('fooBar'))
コード例 #19
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__letters(self):
     self.assertTrue(__unit__.is_identifier('FooBar'))
     self.assertTrue(__unit__.is_identifier('fooBar'))
コード例 #20
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__global_constants__py3(self):
     self.assertFalse(__unit__.is_identifier('True'))  # keyword in py3
     self.assertFalse(__unit__.is_identifier('False'))  # keyword in py3
     self.assertFalse(__unit__.is_identifier('None'))  # keyword in py3
コード例 #21
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__with_whitespace(self):
     self.assertFalse(__unit__.is_identifier('foo bar'))
     self.assertFalse(__unit__.is_identifier('foo\tbar'))
コード例 #22
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_some_object(self):
     with self.assertRaises(TypeError):
         __unit__.is_identifier(object())
コード例 #23
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__empty(self):
     self.assertFalse(__unit__.is_identifier(''))
コード例 #24
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_some_object(self):
     with self.assertRaises(TypeError):
         __unit__.is_identifier(object())
コード例 #25
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_string__global_constants__py2(self):
     self.assertTrue(__unit__.is_identifier('True'))
     self.assertTrue(__unit__.is_identifier('False'))
     self.assertFalse(__unit__.is_identifier('None'))  # can't assign to it
コード例 #26
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_string__number_prefix(self):
     self.assertFalse(__unit__.is_identifier('42foo'))
コード例 #27
0
ファイル: test_lang.py プロジェクト: Xion/taipan
 def test_none(self):
     with self.assertRaises(TypeError):
         __unit__.is_identifier(None)
コード例 #28
0
ファイル: test_lang.py プロジェクト: movermeyer/taipan
 def test_none(self):
     with self.assertRaises(TypeError):
         __unit__.is_identifier(None)