本文整理匯總了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