Python 3.x PyOnce

Python 3.x PyOnce,python-3.x,pyparsing,Python 3.x,Pyparsing,我正在用pyparsing解析一个文件。它工作得很好,但我认为可以通过使用onlynce类而不是“parse_file=pp.OneOrMore(dbuPerMicron | diearea | components)+pp.StringEnd()行中的一个或多个类来改进处理时间。在def文件的components部分之后,还有一些其他部分对我来说是无用的,由于这些行,解析器需要很长时间才能完成。通过在pase_文件中使用onlynce,它给出了:“AttributeError:‘NoneTyp

我正在用pyparsing解析一个文件。它工作得很好,但我认为可以通过使用onlynce类而不是“parse_file=pp.OneOrMore(dbuPerMicron | diearea | components)+pp.StringEnd()行中的一个或多个类来改进处理时间。在def文件的components部分之后,还有一些其他部分对我来说是无用的,由于这些行,解析器需要很长时间才能完成。通过在pase_文件中使用onlynce,它给出了:“AttributeError:‘NoneType’对象没有属性‘searchString’”

我感谢你的建议

def parse_def(self):
        ifile = open("path_to.def",'r')
        def_string = ifile.read()
        ifile.close()

        EOL              = pp.LineEnd().suppress()
        linebreak        = pp.Suppress(";" + pp.LineEnd())
        identifier       = pp.Word(pp.alphanums+'_!<>/')
        number           = pp.Word(pp.nums + ".")
        word             = pp.Word(pp.alphas)

        # UNITS DISTANCE MICRONS
        dbuPerMicron_id  = pp.Keyword('UNITS DISTANCE MICRONS')
        dbuPerMicron     = pp.Group(dbuPerMicron_id + number('UnitsPerMicron')).setResultsName('dbuPerMicron')

        # DIEAREA
        diearea_id  = pp.Keyword('DIEAREA')
        diearea     = pp.Group(pp.Suppress(diearea_id) + pp.OneOrMore(pp.Suppress('(') + number + number + pp.Suppress(')')) + pp.Suppress(linebreak)).setResultsName('DIEAREA')

        # COMPONENTS
        components_id    = pp.Keyword('COMPONENTS')
        end_components   = pp.Keyword("END COMPONENTS").suppress()

        begin_comp       = pp.Keyword('-')
        ws_comp          = pp.Keyword('+')  # parameter division in componentes
        comment          = pp.Keyword('#')
        comp_name        = identifier
        compName         = (comp_name('comp_name') + identifier('cell')).setResultsName('compName')
        EEQMASTER        = (pp.Suppress(ws_comp) + identifier('EEQMASTER') + identifier('macroName')).setResultsName('EEQMASTER')

        SOURCE           = (pp.Suppress(ws_comp) + identifier('SOURCE') + identifier('source_type')).setResultsName('SOURCE')

        PLACEMENT_ids    = pp.Keyword('FIXED') | pp.Keyword('COVER') | pp.Keyword('PLACED') | pp.Keyword('UNPLACED')
        PLACEMENT_coord  = pp.Suppress('(') + number('placement_x') + number('placement_y') + pp.Suppress(')')
        PLACEMENT_orient = word('orientation')
        PLACEMENT        = PLACEMENT_ids + pp.ZeroOrMore(PLACEMENT_coord + PLACEMENT_orient)
        PLACEMENT        = (pp.Suppress(ws_comp) + PLACEMENT).setResultsName('PLACEMENT')

        HALO             = (pp.Suppress(ws_comp) + pp.Keyword('HALO') + pp.ZeroOrMore(pp.Keyword('SOFT')) + number('haloL') + number('haloB') + number('haloR') + number('haloT')).setResultsName('HALO')

        ROUTEHALO        = (pp.Suppress(ws_comp) + pp.Keyword('ROUTEHALO') + number('rhaloDist') + identifier('rhaloMinLayer') + identifier('rhaloMaxLayer')).setResultsName('ROUTEHALO')

        WEIGHT           = (pp.Suppress(ws_comp) + pp.Keyword('WEIGHT') + number('weight')).setResultsName('WEIGHT')

        REGION           = (pp.Suppress(ws_comp) + pp.Keyword('REGION') + identifier('region')).setResultsName('REGION')

        PROPERTY         = (pp.Suppress(ws_comp) + pp.Keyword('PROPERTY') + identifier('propName') + identifier('propVal')).setResultsName('PROPERTY')

        subcomponent     = pp.Group(pp.Suppress(begin_comp)
                                  + pp.OneOrMore(compName)
                                  + pp.ZeroOrMore(EEQMASTER)
                                  + pp.ZeroOrMore(SOURCE)
                                  + pp.OneOrMore(PLACEMENT)
                                  + pp.ZeroOrMore(HALO)
                                  + pp.ZeroOrMore(ROUTEHALO)
                                  + pp.ZeroOrMore(WEIGHT)
                                  + pp.ZeroOrMore(REGION)
                                  + pp.ZeroOrMore(PROPERTY)
                                  + pp.Suppress(linebreak)).setResultsName('subcomponents', listAllMatches=True)

        components       = pp.Group(pp.Suppress(components_id) + number('numComps') + pp.Suppress(linebreak)
                                  + pp.OneOrMore(subcomponent )
                                  + pp.Suppress(end_components)).setResultsName('components')


        dbuPerMicron.setParseAction(self.handle_dbuPerMicron)
        diearea.setParseAction(self.handle_diearea)
        components.setParseAction(self.handle_components)

        parse_file       = pp.OneOrMore(dbuPerMicron | diearea | components) + pp.StringEnd()
        # parse_file       = pp.OnlyOnce(dbuPerMicron | diearea | components) + pp.StringEnd()  # It doesn't work

        return parse_file.searchString(def_string)
def文件的示例:

VERSION 5.7 ;
DIVIDERCHAR "/" ;
BUSBITCHARS "[]" ;
DESIGN c1908 ;
UNITS DISTANCE MICRONS 2000 ;

PROPERTYDEFINITIONS
    COMPONENTPIN designRuleWidth REAL ;
    DESIGN FE_CORE_BOX_LL_X REAL 0.000 ;
    DESIGN FE_CORE_BOX_UR_X REAL 23.425 ;
    DESIGN FE_CORE_BOX_LL_Y REAL 0.000 ;
    DESIGN FE_CORE_BOX_UR_Y REAL 19.600 ;
END PROPERTYDEFINITIONS

DIEAREA ( 0 0 ) ( 46850 39200 ) ;

COMPONENTS 248 ;
- U293 NOR2_X1 + PLACED ( 6080 0 ) N
 ;
- U294 FA_X1 + PLACED ( 0 0 ) N
 ;
- U295 NAND2_X1 + PLACED ( 4560 5600 ) N
 ;
- U296 FA_X1 + PLACED ( 20520 2800 ) N
 ;
- U297 NAND2_X1 + PLACED ( 26600 2800 ) N
 ;
- U298 NAND2_X1 + PLACED ( 27740 2800 ) N
 ;
- U299 NAND2_X1 + PLACED ( 22800 8400 ) N
 ;
- U300 NOR2_X1 + PLACED ( 25460 5600 ) N
 ;
- U301 HA_X1 + PLACED ( 33440 5600 ) N
 ;
- U540 INV_X1 + PLACED ( 760 28000 ) N
 ;
END COMPONENTS

PINS 58 ;
- N1 + NET N1 + DIRECTION INPUT + USE SIGNAL
  + LAYER metal3 ( -70 0 ) ( 70 140 )
And more thousands of lines that are useless to me.

如果在自己的方法中创建解析器,我会尝试只执行解析器定义并返回它,并让调用方负责将解析器应用于输入字符串。这简化了
parser()
方法的调用接口,并使隔离测试更加容易

我将您的
parse()
方法更改为
parser()
,并将其包装在一个伪
X
类中,但内容几乎一字不差,只是将您的最终解析语句更改为:

    return dbuPerMicron | diearea | components
然后,我使用这段代码对任意长的样本(您发布的样本,加上10000000个随机字符,包括空格和换行符)运行解析器:

创建1000万字符位是最耗时的任务,但解析3个相关段后,解析能够停止。我使用
scanString
编写了显式循环,但是使用
itertools.islice
,可以将其折叠为一行

results.dump()
的输出如下所示(长列表行被截断,以便于发布简洁):


对于已知为整数或实数的项,可以使用
pyparsing.pyparsing_common
中定义的
integer
real
(或者只使用
number
,它将匹配所有数字形式)的表达式;这些表达式将使用一个快速正则表达式进行解析,并在解析时将结果转换为正确的Python类型,这样以后就不必进行这种转换。

onlynce是解析操作的修饰符,而不是解析器元素类型。通过使用
searchString
,pyparsing将解析整个文件,一次解析一个字符以查找可能的匹配项,并且它无法提前知道何时找到所有感兴趣的内容。如果改用
scanString
,您将有更多的控制权-它返回一个生成器,因此您可以处理任意数量的匹配,然后停止。谢谢您的建议。我使用searchString是因为我在使用scanString时遇到问题。当使用最后一个时,解析器给出了一个错误,指出文件开头应该是“X”字符,但我想忽略开头的许多字符。通过解析一个简单的字符链,就像pyparsing网站中的hello world示例一样,scanString工作得很好。我想我错过了什么。所以,回到searchString,我认为有零个或多个、一个或多个和唯一一个(解析将在第一次出现后停止)。无论如何,我会再看一看手册。谢谢你,保罗。我做了类似的事情,保持代码完整,但将原始文件分为两部分,一部分包含我需要的所有部分,另一部分包含我将来可能需要的部分,但现在是垃圾。我这样做是为了并行解析,在本例中仅使用2个进程。我将发布一个带有备选方案的答案。我当然也会测试您的实现。再次感谢。Paul,如果我将每个部分分开,用不同的方法进行解析,那么len(t)对于每个调用总是1。我还必须使用for循环吗?啊,一个bug!应该是
len(结果)
。for循环将扫描字符串生成器向前推进到下一个匹配。Paul,使用解析器的1 for循环实现和“返回dbuPerMicron | diearea | components”需要3分钟以上的时间。如果我用另一种方法分离“组件”,并使用2个for循环调用,一个用于“return dbuPerMicron | diearea”,另一个用于“return components”,则需要约1分钟。有什么建议吗?PS:我将“组件”内容放在文件末尾。
    return dbuPerMicron | diearea | components
parser = X().parser()

# accumulate results using scanString
results = []
for t, s, e in parser.scanString(sample):
    results.append(t)
    # BUG! (sorry)
    # if len(t) == 3:
    if len(results) == 3:
        break

# use builtin sum() function to merge all the parsed results into one
results = sum(results)

# or here is the same code as the above loop using islice to do the
# range checking for us
from itertools import islice
results = sum(t for t, s, e in islice(parser.scanString(sample), 0, 3))

# what did we get?
print(results.dump())
[['UNITS DISTANCE MICRONS', '2000'], ['0', '0', ...
- DIEAREA: ['0', '0', '46850', '39200']
- components: ['248', ['U293', 'NOR2_X1', 'PLACED', '6080', ...
  - numComps: '248'
  - subcomponents: [['U293', 'NOR2_X1', 'PLACED', '6080', ... 
    [0]:
      ['U293', 'NOR2_X1', 'PLACED', '6080', '0', 'N']
      - PLACEMENT: ['PLACED', '6080', '0', 'N']
      - cell: 'NOR2_X1'
      - compName: ['U293', 'NOR2_X1']
      - comp_name: 'U293'
      - orientation: 'N'
      - placement_x: '6080'
      - placement_y: '0'
    [1]:
      ['U294', 'FA_X1', 'PLACED', '0', '0', 'N']
      - PLACEMENT: ['PLACED', '0', '0', 'N']
      - cell: 'FA_X1'
      - compName: ['U294', 'FA_X1']
      - comp_name: 'U294'
      - orientation: 'N'
      - placement_x: '0'
      - placement_y: '0'
    [2]:
      ['U295', 'NAND2_X1', 'PLACED', '4560', '5600', 'N']
      - PLACEMENT: ['PLACED', '4560', '5600', 'N']
      - cell: 'NAND2_X1'
      - compName: ['U295', 'NAND2_X1']
      - comp_name: 'U295'
      - orientation: 'N'
      - placement_x: '4560'
      - placement_y: '5600'
    [3]:
      ['U296', 'FA_X1', 'PLACED', '20520', '2800', 'N']
      - PLACEMENT: ['PLACED', '20520', '2800', 'N']
      - cell: 'FA_X1'
      - compName: ['U296', 'FA_X1']
      - comp_name: 'U296'
      - orientation: 'N'
      - placement_x: '20520'
      - placement_y: '2800'
    [4]:
      ['U297', 'NAND2_X1', 'PLACED', '26600', '2800', 'N']
      - PLACEMENT: ['PLACED', '26600', '2800', 'N']
      - cell: 'NAND2_X1'
      - compName: ['U297', 'NAND2_X1']
      - comp_name: 'U297'
      - orientation: 'N'
      - placement_x: '26600'
      - placement_y: '2800'
    [5]:
      ['U298', 'NAND2_X1', 'PLACED', '27740', '2800', 'N']
      - PLACEMENT: ['PLACED', '27740', '2800', 'N']
      - cell: 'NAND2_X1'
      - compName: ['U298', 'NAND2_X1']
      - comp_name: 'U298'
      - orientation: 'N'
      - placement_x: '27740'
      - placement_y: '2800'
    [6]:
      ['U299', 'NAND2_X1', 'PLACED', '22800', '8400', 'N']
      - PLACEMENT: ['PLACED', '22800', '8400', 'N']
      - cell: 'NAND2_X1'
      - compName: ['U299', 'NAND2_X1']
      - comp_name: 'U299'
      - orientation: 'N'
      - placement_x: '22800'
      - placement_y: '8400'
    [7]:
      ['U300', 'NOR2_X1', 'PLACED', '25460', '5600', 'N']
      - PLACEMENT: ['PLACED', '25460', '5600', 'N']
      - cell: 'NOR2_X1'
      - compName: ['U300', 'NOR2_X1']
      - comp_name: 'U300'
      - orientation: 'N'
      - placement_x: '25460'
      - placement_y: '5600'
    [8]:
      ['U301', 'HA_X1', 'PLACED', '33440', '5600', 'N']
      - PLACEMENT: ['PLACED', '33440', '5600', 'N']
      - cell: 'HA_X1'
      - compName: ['U301', 'HA_X1']
      - comp_name: 'U301'
      - orientation: 'N'
      - placement_x: '33440'
      - placement_y: '5600'
    [9]:
      ['U540', 'INV_X1', 'PLACED', '760', '28000', 'N']
      - PLACEMENT: ['PLACED', '760', '28000', 'N']
      - cell: 'INV_X1'
      - compName: ['U540', 'INV_X1']
      - comp_name: 'U540'
      - orientation: 'N'
      - placement_x: '760'
      - placement_y: '28000'
- dbuPerMicron: ['UNITS DISTANCE MICRONS', '2000']
  - UnitsPerMicron: '2000'