本文整理汇总了Python中fileinput.isfirstline方法的典型用法代码示例。如果您正苦于以下问题:Python fileinput.isfirstline方法的具体用法?Python fileinput.isfirstline怎么用?Python fileinput.isfirstline使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileinput
的用法示例。
在下文中一共展示了fileinput.isfirstline方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_version
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def test_version(self):
message = "\nFound a different version at line %d or file %r: %r (may be %r)."
files = [os.path.join(self.source_dir, '__init__.py')]
if self.package_dir is not None:
files.extend([
os.path.join(self.package_dir, 'setup.py'),
os.path.join(self.package_dir, 'doc/conf.py'),
])
version = filename = None
for line in fileinput.input(files):
if fileinput.isfirstline():
filename = fileinput.filename()
lineno = fileinput.filelineno()
match = self.get_version.search(line)
if match is not None:
if version is None:
version = match.group(1).strip('\'\"')
else:
self.assertTrue(
version == match.group(1).strip('\'\"'),
message % (lineno, filename, match.group(1).strip('\'\"'), version)
)
示例2: isfirstline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def isfirstline(self):
self.invocation_counts["isfirstline"] += 1
return self.return_values["isfirstline"]
示例3: test_state_is_None
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def test_state_is_None(self):
"""Tests fileinput.isfirstline() when fileinput._state is None.
Ensure that it raises RuntimeError with a meaningful error message
and does not modify fileinput._state"""
fileinput._state = None
with self.assertRaises(RuntimeError) as cm:
fileinput.isfirstline()
self.assertEqual(("no active input()",), cm.exception.args)
self.assertIsNone(fileinput._state)
示例4: test_state_is_not_None
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def test_state_is_not_None(self):
"""Tests fileinput.isfirstline() when fileinput._state is not None.
Ensure that it invokes fileinput._state.isfirstline() exactly once,
returns whatever it returns, and does not modify fileinput._state
to point to a different object."""
isfirstline_retval = object()
instance = MockFileInput()
instance.return_values["isfirstline"] = isfirstline_retval
fileinput._state = instance
retval = fileinput.isfirstline()
self.assertExactlyOneInvocation(instance, "isfirstline")
self.assertIs(retval, isfirstline_retval)
self.assertIs(fileinput._state, instance)
示例5: main
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('files', nargs='*', help='files to sort')
ap.add_argument('-r', '--reverse', action='store_true', default=False, help='reverse the result of comparisons')
ns = ap.parse_args(args)
def _print(lines):
if lines is not None:
lines = sorted(lines)
if ns.reverse:
lines = lines[::-1]
print(''.join(lines))
fileinput.close() # in case it is not closed
try:
lines = None
for line in fileinput.input(ns.files, openhook=fileinput.hook_encoded("utf-8")):
if fileinput.isfirstline():
_print(lines)
lines = []
lines.append(line)
_print(lines)
finally:
fileinput.close()
示例6: main
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('files', nargs='*', help='files to unique (must be sorted first)')
ns = ap.parse_args(args)
def _print(lines):
if lines is not None:
print(''.join(lines))
fileinput.close() # in case it is not closed
try:
prev_line = None
lines = None
for line in fileinput.input(ns.files, openhook=fileinput.hook_encoded("utf-8")):
if fileinput.isfirstline():
_print(lines)
lines = []
prev_line = None
if prev_line is None or line != prev_line:
lines.append(line)
prev_line = line
_print(lines)
finally:
fileinput.close()
示例7: test_missing_debug_statements
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def test_missing_debug_statements(self):
# Exclude explicit debug statements written in the code
exclude = {
'regex.py': [240, 241],
'codepoints.py': [534],
'cli.py': [117, 133, 137, 140],
}
message = "\nFound a debug missing statement at line %d or file %r: %r"
filename = None
file_excluded = []
files = glob.glob(os.path.join(self.source_dir, '*.py')) + \
glob.glob(os.path.join(self.source_dir, 'validators/*.py'))
for line in fileinput.input(files):
if fileinput.isfirstline():
filename = fileinput.filename()
file_excluded = exclude.get(os.path.basename(filename), [])
lineno = fileinput.filelineno()
if lineno in file_excluded:
continue
match = self.missing_debug.search(line)
if match is None or filename.endswith('/cli.py') and match.group(0) == 'print(':
continue
self.assertIsNone(match, message % (lineno, filename, match.group(0)))
示例8: _generate_parsed_logs
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def _generate_parsed_logs(caffe_home, log_path, output_log_file):
"""
Takes the raw Caffe output created while training the model in order
to generate reduced statistics, such as giving iterations vs. test loss.
"""
print("\tParsing logs...")
process = subprocess.Popen([caffe_home + "/tools/extra/parse_log.py",
output_log_file, log_path], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
sys.stdout.write(line)
shutil.rmtree(output_log_file + ".validate", ignore_errors=True)
shutil.move(output_log_file + ".test", output_log_file + ".validate")
# Convert the commas in the files into tabs to make them easier to read.
log_files = [output_log_file + ".train", output_log_file + ".validate"]
for line in fileinput.input(log_files, inplace=True):
line = line.replace(u",", u"\t")
if fileinput.isfirstline():
# HACK(neuberg): The column headers with tabs don't quite line up, so shorten
# some column names and add a tab.
line = line.replace(u"NumIters", u"Iters")
line = line.replace(u"LearningRate", u"\tLR")
sys.stdout.write(line)
fileinput.close()
logs = [
{"title": "Testing", "filename": "train"},
{"title": "Validation", "filename": "validate"}
]
for log in logs:
print("\n\t\tParsed %s log:" % log["title"])
with open(output_log_file + "." + log["filename"], "r") as f:
lines = f.read().split("\n")
for line in lines:
print("\t\t\t%s" % line)
print("\t\tParsed training log saved to %s" % (output_log_file + ".train"))
print("\t\tParsed validation log saved to %s\n" % (output_log_file + ".validate"))
示例9: generate_parsed_logs
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import isfirstline [as 别名]
def generate_parsed_logs():
"""
Takes the raw Caffe output created while training the model in order
to generate reduced statistics, such as giving iterations vs. test loss.
"""
print("\tParsing logs...")
process = subprocess.Popen([constants.CAFFE_HOME + "/tools/extra/parse_log.py",
constants.OUTPUT_LOG_PATH, constants.LOG_DIR], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
sys.stdout.write(line)
shutil.rmtree(constants.OUTPUT_LOG_PATH + ".validate", ignore_errors=True)
shutil.move(constants.OUTPUT_LOG_PATH + ".test",
constants.OUTPUT_LOG_PATH + ".validate")
# Convert the commas in the files into tabs to make them easier to read.
log_files = [constants.OUTPUT_LOG_PATH + ".train",
constants.OUTPUT_LOG_PATH + ".validate"]
for line in fileinput.input(log_files, inplace=True):
line = line.replace(u",", u"\t")
if fileinput.isfirstline():
# HACK(neuberg): The column headers with tabs don't quite line up, so shorten
# some column names and add a tab.
line = line.replace(u"NumIters", u"Iters")
line = line.replace(u"LearningRate", u"\tLR")
sys.stdout.write(line)
fileinput.close()
logs = [
{"title": "Testing", "filename": "train"},
{"title": "Validation", "filename": "validate"}
]
for log in logs:
print("\n\t\tParsed %s log:" % log["title"])
with open(constants.OUTPUT_LOG_PATH + "." + log["filename"], "r") as f:
lines = f.read().split("\n")
for line in lines:
print("\t\t\t%s" % line)
print("\t\tParsed training log saved to %s" % (constants.OUTPUT_LOG_PATH + ".train"))
print("\t\tParsed validation log saved to %s\n" % (constants.OUTPUT_LOG_PATH + ".validate"))