本文整理汇总了Python中preprocessor.Preprocessor.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Preprocessor.parse方法的具体用法?Python Preprocessor.parse怎么用?Python Preprocessor.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类preprocessor.Preprocessor
的用法示例。
在下文中一共展示了Preprocessor.parse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read
# 需要导入模块: from preprocessor import Preprocessor [as 别名]
# 或者: from preprocessor.Preprocessor import parse [as 别名]
def read(self, filename=None, preprocess=True, **defines):
"""Preprocess, read and parse itp file *filename*.
Any keywords in *defines* are use to modify the default preprocessor
variables (see
:meth:`gromacs.fileformats.preprocessor.Preprocessor.parse` for
details). Setting *preprocess* = ``False`` skips the preprocessing
step.
"""
self._init_filename(filename)
if preprocess:
kwargs = self.defines.copy()
kwargs['commentchar'] = self.commentchar
kwargs['clean'] = True
ppitp = Preprocessor(self.real_filename, **kwargs)
ppitp.parse(**defines)
itp = ppitp.StringIO()
else:
itp = open(self.real_filename)
try:
stream = OneLineBuffer(itp.next)
self.parse(stream)
finally:
itp.close()
示例2: contains_preprocessor_constructs
# 需要导入模块: from preprocessor import Preprocessor [as 别名]
# 或者: from preprocessor.Preprocessor import parse [as 别名]
def contains_preprocessor_constructs(self):
"""Check if file makes use of any preprocessor constructs.
The test is done by running the file through the
:class:`~gromacs.fileformats.preprocessor.Preprocessor` (while
stripping all empty and lines starting with a comment character. This
is compared to the original file, stripped in the same manner. If the
two stripped files differ from each other then the preprocessor altered
the file and preprocessor directives must have been involved and this
function returns ``True``.
.. versionadded: 0.3.1
"""
from itertools import izip
kwargs = self.defines.copy()
kwargs['commentchar'] = self.commentchar
kwargs['clean'] = True
kwargs['strip'] = True
ppitp = Preprocessor(self.real_filename, **kwargs)
ppitp.parse()
pp_lines = ppitp.StringIO().readlines()
def strip_line(line):
s = line.strip()
return len(s) == 0 or s.startswith(self.commentchar)
raw_lines = [line for line in open(self.real_filename) if not strip_line(line)]
if len(pp_lines) != len(raw_lines):
self.logger.debug("File %r is preprocessed (pp: %d vs raw %d lines (stripped))",
self.real_filename, len(pp_lines), len(raw_lines))
return True
for linenum, (raw, pp) in enumerate(izip(raw_lines, pp_lines)):
if raw != pp:
self.logger.debug("File %r is preprocessed. Difference at (stripped) line %d",
self.real_filename, linenum)
self.logger.debug("preprocessed: %s", pp)
self.logger.debug("original: %s", raw)
return True
self.logger.debug("File %r does not appear to contain recognized preprocessing directives",
self.real_filename)
return False