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


Python Block.get_name方法代码示例

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


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

示例1: parse

# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import get_name [as 别名]
def parse(file_name):
    '''function that takes a file name, and returns a list of blocks,
       instances of class Block.'''
    from block import Block
# open file, specified on command line
    block_file = open(file_name, 'r')
# compile the regular expressions to be used for performance reasons
    comment_pattern = re.compile(r'\s*#.*')
    block_start_pattern = re.compile(r'\s*begin\s+(\w+)')
    block_end_pattern = re.compile(r'\s*end\s+(\w+)')
# current_block holds an instance of Block for the block that is being
# parsed, its # value is None when outside a block, note that it doubles
# as state variable
    current_block = None
# list to hold the blocks
    blocks = []
    for line in block_file:
        # remove leading/triailing spaces, and comments (i.e., everything
        # following a '#'
        line = comment_pattern.sub('', line.strip())
# ignore blank lines
        if len(line) == 0:
            continue
# check for begin block
        match = block_start_pattern.match(line)
        if match is not None:
            # if current_block is not None, we are already in a block,
            # raise error
            if current_block is not None:
                raise ParseError(
                    'block {0} is not close when opening {1}'.format(
                        current_block.get_name(), match.group(1)))
# now in a block, so change state
            current_block = Block(match.group(1))
            continue
# check for end block
        match = block_end_pattern.match(line)
        if match is not None:
            # if the end block name is not the current block, raise error
            if match.group(1) != current_block.get_name():
                raise ParseError(
                    'block {0} is closed with {1}'.format(
                        match.group(1), current_block.get_name()))
# now out of a block, add current block to the list, and change state
            blocks.append(current_block)
            current_block = None
            continue
# if not in a block, ignore the line
        if current_block is None:
            continue
        else:
            # store block data
            current_block.add_data(line)
# close the file
    block_file.close()
    return blocks
开发者ID:bizatheo,项目名称:training-material,代码行数:58,代码来源:oo_fs_parser.py

示例2: __init__

# 需要导入模块: from block import Block [as 别名]
# 或者: from block.Block import get_name [as 别名]
class BlockPyParser:
    '''pyparsing based parser class for block-structured data'''

    def __init__(self):
        '''Constructor'''
        self._current_block = None
        self._blocks = []
        self._define_grammar()

    def _create_block(self, toks):
        self._current_block = Block(toks[0])

    def _finish_block(self, parsed_str, location, toks):
        if toks[0] == self._current_block.get_name():
            self._blocks.append(self._current_block)
            self._current_block = None
        else:
            message = 'mismatched begin {0}/end {1}'.format(
                self._current_block.get_name(), toks[0])
            raise ParseFatalException(parsed_str, loc=location, msg=message)

    def _handle_data(self, toks):
        self._current_block.add_data(toks[0].strip())

    def _define_actions(self):
        '''Internal method to define the various parse actions by currying
           with parser object.  To be called from grammar initializing
           method'''
        self._begin_block_action = lambda toks: self._create_block(toks)
        self._end_block_action = lambda s, l, t: self._finish_block(s, l, t)
        self._data_value_action = lambda toks: self._handle_data(toks)

    def _define_grammar(self):
        '''define the grammar to be used, and add actions'''
        self._define_actions()
        eol = LineEnd().suppress()
        white = Optional(White()).suppress()
        begin = Keyword('begin').suppress()
        end = Keyword('end').suppress()
        comment = (Literal('#') + restOfLine).suppress()
        data_value = Combine(OneOrMore(CharsNotIn('#\n\r')))
        data_line = (LineStart() + white + Optional(data_value) +
                     Optional(comment) + eol)
        block_name = Word(alphas, alphanums + '_')
        begin_block = (LineStart() + begin + block_name +
                       Optional(comment) + eol)
        end_block = LineStart() + end + block_name + Optional(comment) + eol
        junk = ZeroOrMore(LineStart() + white + NotAny(begin) +
                          restOfLine + eol).suppress()
        data = Group(ZeroOrMore(NotAny(end) + data_line))
        block_def = begin_block + data + end_block
        block_defs = junk + OneOrMore(block_def + junk)
        self._grammar = block_defs
        begin_block.addParseAction(self._begin_block_action)
        end_block.addParseAction(self._end_block_action)
        data_value.addParseAction(self._data_value_action)

    def parse(self, block_str):
        '''parse the given string'''
        return self._grammar.parseString(block_str)

    def get_blocks(self):
        '''return the blocks that were parsed'''
        return self._blocks
开发者ID:bizatheo,项目名称:training-material,代码行数:66,代码来源:pyparsing_block_parser.py


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