为什么python Lark语法这么慢?

2024-10-01 22:43:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析“ypcat-k netgroup”的输出 输出类似于此格式的许多行:

group1 (host1,user1,domain1) (host2,user2,domain2) (host3,user3,domain3) ...

或者有时候

group2 groupa groupb groupc ...

我第一次尝试使用这种百灵鸟语法:

def getNetgroups():
  parser = Lark(ypcat_grammer)
  res = subprocess.check_output(['ypcat -k netgroup'], shell=True).decode('utf-8')
  print(parser.parse(res).pretty())

ypcat_grammer = r"""
  ?start: _line+
  _line: groupname members NEWLINE
  members: (member|groupname)*
  member: "(" hostname? "," username? "," domainname? ")"
  
  username: _name
  domainname: _name
  groupname: _name
  hostname: _name
  _name: /([a-zA-Z0-9_\.\-]+)/
  %import common.WS_INLINE
  %import common.NUMBER
  %import common.NEWLINE
  %ignore WS_INLINE
"""

解析4000行代码需要60秒!!? 这似乎太长了,所以我写了一个手工编码的解析器:

member = re.compile('\(([^,]*),([^,]*),([^,]*)\)')

def parseNetGroups():
  res = subprocess.check_output(['ypcat -k netgroup'], shell=True).decode('utf-8')
  rows = []
  for line in res.split('\n'):
    words = re.split('\s+', line)
    groupname = words.pop(0)
    members = []
    for word in words:
      if m:=member.match(word):
        members.append((m.group(1),m.group(2),m.group(3)))
      else:
        members.append(word)
    rows.append({'GROUPNAME':groupname, 'MEMBERS':members})
  return pd.DataFrame(rows)

这花了0.8秒。 我做错了什么


Tags: nameimportlinegrouprescommonwordrows

热门问题