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


Python InputReader.add_line_key方法代码示例

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


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

示例1: test_line_repeat_in_definition

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_repeat_in_definition():
    # You cannot repeat keys
    r = InputReader()
    r.add_line_key(str('red'))
    with raises(ReaderError) as e:
        r.add_line_key('red')
    assert search(r'The keyname "\w+" has been defined twice', str(e.value))
开发者ID:SethMMorton,项目名称:input_reader,代码行数:9,代码来源:test_line.py

示例2: test_line_case_definition

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_case_definition():
    r = InputReader()
    a = r.add_line_key('red', case=True)
    assert a._case
    with raises(ValueError) as e:
        r.add_line_key('blue', case='True')
    assert 'case must be bool' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:9,代码来源:test_line.py

示例3: test_line_read_case_sensitive

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_read_case_sensitive():
    r = InputReader()
    r.add_line_key('blue', type=str)
    inp = r.read_input(['blue HeLLo'])
    assert inp.blue == 'hello'
    r.add_line_key('red', type=str, case=True)
    inp = r.read_input(['red HeLLo'])
    assert inp.red == 'HeLLo'
开发者ID:SethMMorton,项目名称:input_reader,代码行数:10,代码来源:test_line.py

示例4: test_unknown_keys_cause_failure

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_unknown_keys_cause_failure(setup):
    # Don't ignore unknown keys
    parse_string = setup[-1]
    reader = InputReader(comment='//', ignoreunknown=False)
    reader.add_boolean_key('red')
    reader.add_line_key('path')
    with raises(ReaderError) as e:
        reader.read_input(parse_string)
    assert 'Unrecognized key' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:11,代码来源:test_read_file.py

示例5: test_ignoreunknown_actually_ignores_unknown

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_ignoreunknown_actually_ignores_unknown(setup):
    # Ignore unknown keys
    parse_string = setup[-1]
    reader = InputReader(comment='//', ignoreunknown=True)
    reader.add_boolean_key('red')
    reader.add_line_key('path')
    inp = reader.read_input(parse_string)
    with raises(AttributeError):
        inp.blue
开发者ID:SethMMorton,项目名称:input_reader,代码行数:11,代码来源:test_read_file.py

示例6: test_comments_are_handled_correctly

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_comments_are_handled_correctly(setup):
    parse_string = setup[-1]
    reader = InputReader(comment='#')
    reader.add_boolean_key('red')
    reader.add_boolean_key('blue')
    reader.add_line_key('path')
    with raises(ReaderError) as e:
        reader.read_input(parse_string)
    regex = r'expected \d+ arguments, got \d+'
    assert search(regex, str(e.value))
开发者ID:SethMMorton,项目名称:input_reader,代码行数:12,代码来源:test_read_file.py

示例7: test_line_read_using_defaults

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_read_using_defaults():
    r = InputReader()
    r.add_line_key('blue')
    inp = r.read_input(['blue bird'])
    assert inp.blue == 'bird'
    with raises(ReaderError) as e:
        inp = r.read_input(['blue'])
    assert search(r'expected .*\d+ arguments, got \d+', str(e.value))
    with raises(ReaderError) as e:
        inp = r.read_input(['blue bird egg'])
    assert search(r'expected .*\d+ arguments, got \d+', str(e.value))
开发者ID:SethMMorton,项目名称:input_reader,代码行数:13,代码来源:test_line.py

示例8: test_line_missing_keyname

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_missing_keyname():
    r = InputReader()
    with raises(TypeError):
        r.add_line_key()
    with raises(TypeError):
        r.add_line_key(type=str)
    with raises(TypeError):
        r.add_line_key(glob={})
    with raises(TypeError):
        r.add_line_key(keywords={})
    with raises(TypeError):
        r.add_line_key(case=False)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:14,代码来源:test_line.py

示例9: test_line_correct_call

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_correct_call():
    r = InputReader()
    a = r.add_line_key('red')
    assert a.name == 'red'
    assert a._type == [str]
    assert a._glob == {}
    assert a._keywords == {}
    assert not a._case
    with raises(TypeError) as e:
        r.add_line_key('blue', glob={'len':'*'},
                               keywords={'bird':{}})
    assert 'Cannot define both glob and keywords' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('pink', type=None, 
                               glob=None, keywords=None)
    assert 'type, glob and keywords cannot all be empty' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:18,代码来源:test_line.py

示例10: test_case_sensitivity_at_class_level

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_case_sensitivity_at_class_level():
    ir = InputReader(case=True)
    assert ir._case
    l = ir.add_line_key('RED')
    assert l._case
    with raises(ValueError):
        ir2 = InputReader(case='True')
开发者ID:SethMMorton,项目名称:input_reader,代码行数:9,代码来源:test_keywords.py

示例11: test_line_read_using_keywords

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_read_using_keywords():
    r = InputReader()
    r.add_line_key('red', type=int, 
                          keywords={'robin':{}})
    inp = r.read_input(['red 5 robin=early'])
    assert inp.red == (5, {'robin':'early'})
    inp = r.read_input(['red 5'])
    assert inp.red == (5, {})
    with raises(ReaderError) as e:
        r.read_input(['red 5 robin=early light=special'])
    assert 'Unknown keyword' in str(e.value)
    
    r.add_line_key('blue', type=[int, int],
                           keywords={'egg':{'type':int, 'default':1},
                                     'ice':{'type':(4, 5)}})
    inp = r.read_input(['blue 5 7 egg=6 ice=4'])
    assert inp.blue == (5, 7, {'egg':6, 'ice':4})
    inp = r.read_input(['blue 5 7 ice=5'])
    assert inp.blue == (5, 7, {'egg':1, 'ice':5})
    inp = r.read_input(['blue 5 7'])
    assert inp.blue == (5, 7, {'egg':1})

    r.add_line_key('cyan', type=None, keywords={'by':{'type':int},
                                                'to':{'type':int}})
    inp = r.read_input(['cyan by=14 to=11'])
    assert inp.cyan == {'by':14, 'to':11}
    inp = r.read_input(['cyan by=14'])
    assert inp.cyan == {'by':14}
    inp = r.read_input(['cyan'])
    assert inp.cyan == {}
    with raises(ReaderError) as e:
        inp = r.read_input(['cyan by = 3'])
    assert 'Error reading keyword argument' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:35,代码来源:test_line.py

示例12: test_line_name_definition

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_name_definition():
    r = InputReader()
    with raises(ValueError) as e:
        r.add_line_key(23)
    assert 'keyname must be str' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('hello goodbye')
    assert 'String cannot contain spaces' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('')
    assert 'String cannot be of zero length' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:13,代码来源:test_line.py

示例13: test_line_glob_definitions

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_glob_definitions():
    # Test that glob checking is OK
    r = InputReader()
    a = r.add_line_key('red', glob={'len':'*'})
    assert a._glob == {'len':'*', 'type':str, 'join':False}
    b = r.add_line_key('blue', glob={str('len'):str('?')})
    assert b._glob == {'len':'?', 'type':str, 'join':False}
    c = r.add_line_key('green', glob={'len':'+', 'join':True})
    assert c._glob == {'len':'+', 'type':str, 'join':True}
    e = r.add_line_key('pink', glob={'len':'?', 'type':int})
    assert e._glob == {'len':'?', 'type':int, 'join':False}
    f = r.add_line_key('gray', glob={'len':'*', 'type':(int,"hey",str("hi"),str)})
    assert f._glob == {'len':'*', 'type':(int,"hey","hi",str), 'join':False}
    # Test that glob checking is OK for bad input
    with raises(ValueError) as e:
        r.add_line_key('black', glob='wrong')
    assert 'glob must be a dict' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'len':'1'})
    assert search(r'"len" must be one of "\*", "\+", or "\?" in glob', str(e.value))
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'join':True})
    assert '"len" required for glob' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'len':'*', 'join':'True'})
    assert '"join" must be a bool in glob' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'len':'*', 'type':complex})
    assert 'type must be one of' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'len':'*', 'type':[str]})
    assert 'list not allowed in type for glob or keywords' in str(e.value)
    with raises(TypeError) as e:
        r.add_line_key('black', glob={'len':'*', 'dumb':True})
    assert 'Unknown key in glob' in str(e.value)
    with raises(ValueError) as e:
        r.add_line_key('black', glob={'len':'?', 'join':True})
    assert r'"join=True" makes no sense for "len=?"' in str(e.value)
开发者ID:SethMMorton,项目名称:input_reader,代码行数:40,代码来源:test_line.py

示例14: test_line_reading_types

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_reading_types():
    r = InputReader()
    r.add_line_key('blue', type=int)
    inp = r.read_input(['blue 23'])
    assert inp.blue == 23
    with raises(ReaderError) as e:
        inp = r.read_input(['blue bird'])
    assert search(r'expected \w+, got "\w+"', str(e.value))

    r.add_line_key('red', type=(0, 1, 2, 3))
    inp = r.read_input(['red 3'])
    assert inp.red == 3
    with raises(ReaderError) as e:
        inp = r.read_input(['red 4'])
    assert search(r'expected one of .+, got "\w+"', str(e.value))

    r.add_line_key('green', type=(float, None))
    inp = r.read_input(['green 3'])
    assert inp.green == 3
    inp = r.read_input(['green 3.5'])
    assert inp.green == 3.5
    inp = r.read_input(['green none'])
    assert inp.green is None
    with raises(ReaderError) as e:
        inp = r.read_input(['green bird'])
    assert search(r'expected one of .+, got "\w+"', str(e.value))

    r.add_line_key('cyan', type=[str, (None, str), float])
    inp = r.read_input(['cyan cat dog 6'])
    assert inp.cyan == ('cat', 'dog', 6)
    with raises(ReaderError) as e:
        inp = r.read_input(['cyan cat dog 7 8'])
    assert search('expected .+, got \w+', str(e.value))
    with raises(ReaderError) as e:
        inp = r.read_input(['cyan cat none bird'])
    assert search('expected \w+, got "\w+"', str(e.value))
    inp = r.read_input(['cyan cat none 7.8'])
    assert inp.cyan == ('cat', None, 7.8)

    r.add_line_key('black', type=('hey', str('hi'), 4, 2.8))
    inp = r.read_input(['black 4'])
    assert inp.black == 4
    inp = r.read_input(['black 2.8'])
    assert inp.black == 2.8
    inp = r.read_input(['black hey'])
    assert inp.black == 'hey'
    inp = r.read_input(['black hi'])
    assert inp.black == 'hi'
    with raises(ReaderError) as e:
        inp = r.read_input(['black whoops'])
    assert search('expected one of [a-zA-Z0-9, ".]+, got "\w+"', str(e.value))
    with raises(ReaderError) as e:
        inp = r.read_input(['black 2'])
    assert search('expected one of [a-zA-Z0-9, ".]+, got "\d+"', str(e.value))

    import re
    r.add_line_key('pink', type=re.compile(r'neat\d+'))
    inp = r.read_input(['pink neat68'])
    assert inp.pink == 'neat68'
    with raises(ReaderError) as e:
        inp = r.read_input(['pink near68'])
    assert search('expected regex\(.*\)', str(e.value))
开发者ID:SethMMorton,项目名称:input_reader,代码行数:64,代码来源:test_line.py

示例15: test_line_read_using_globs

# 需要导入模块: from input_reader import InputReader [as 别名]
# 或者: from input_reader.InputReader import add_line_key [as 别名]
def test_line_read_using_globs():
    r = InputReader()
    r.add_line_key('blue', type=[int, int],
                        glob={'len':'*', 'type':int})
    inp = r.read_input(['blue 1 2 3 4 5 6 7'])
    assert inp.blue == (1, 2, 3, 4, 5, 6, 7)
    inp = r.read_input(['blue 1 2'])
    assert inp.blue == (1, 2)

    r.add_line_key('red', type=[int, int],
                        glob={'len':'*', 'type':int, 'join':True})
    inp = r.read_input(['red 1 2 3 4 5 6 7'])
    assert inp.red == (1, 2, '3 4 5 6 7')
    inp = r.read_input(['red 1 2'])
    assert inp.red == (1, 2)

    r.add_line_key('pink', type=[int, int],
                        glob={'len':'+', 'type':float, 'join':True})
    inp = r.read_input(['pink 1 2 3 4 5 6 7'])
    assert inp.pink == (1, 2, '3.0 4.0 5.0 6.0 7.0')
    inp = r.read_input(['pink 1 2 3'])
    assert inp.pink == (1, 2, '3.0')

    r.add_line_key('cyan', type=[int, int],
                        glob={'len':'?', 'type':int})
    inp = r.read_input(['cyan 1 2 3'])
    assert inp.cyan == (1, 2, 3)
    inp = r.read_input(['cyan 1 2'])
    assert inp.cyan == (1, 2)

    r.add_line_key('yellow', type=int, glob={'len':'?', 'type':int})
    inp = r.read_input(['yellow 1 2'])
    assert inp.yellow == (1, 2)
    inp = r.read_input(['yellow 1'])
    assert inp.yellow == (1,)

    r.add_line_key('teal', type=int, glob={'len':'?', 'type':int, 'default':3})
    inp = r.read_input(['teal 1 2'])
    assert inp.teal == (1, 2)
    inp = r.read_input(['teal 1'])
    assert inp.teal == (1, 3)

    r.add_line_key('gray', type=None, glob={'len':'?'})
    inp = r.read_input(['gray bye'])
    assert inp.gray == 'bye'
    inp = r.read_input(['gray'])
    assert inp.gray == ''

    r.add_line_key('white', type=None, glob={'len':'*'})
    inp = r.read_input(['white hi lo'])
    assert inp.white == ('hi', 'lo')
    inp = r.read_input(['white'])
    assert inp.white == ()

    r.add_line_key('green', type=None, glob={'len':'+', 'join':True})
    inp = r.read_input(['green hi lo'])
    assert inp.green == 'hi lo'

    r.add_line_key('orange', type=None, glob={'len':'*', 'join':True})
    inp = r.read_input(['orange'])
    assert inp.orange == ''

    r.add_line_key('black', type=int, glob={'len':'?', 'type':int})
    with raises(ReaderError) as e:
        r.read_input(['black 1 2 3'])
    assert search(r'expected at most \d+ arguments, got \d+', str(e.value))
    r.add_line_key('maroon', type=int, glob={'len':'+', 'type':int})
    with raises(ReaderError) as e:
        r.read_input(['maroon 1'])
    assert search(r'expected at least \d+ arguments, got \d+', str(e.value))
开发者ID:SethMMorton,项目名称:input_reader,代码行数:72,代码来源:test_line.py


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