python file input line 1_Python fileinput.lineno方法代码示例

本文整理汇总了Python中fileinput.lineno方法的典型用法代码示例。如果您正苦于以下问题:Python fileinput.lineno方法的具体用法?Python fileinput.lineno怎么用?Python fileinput.lineno使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块fileinput的用法示例。

在下文中一共展示了fileinput.lineno方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_zero_byte_files

​点赞 6

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def test_zero_byte_files(self):

t1 = t2 = t3 = t4 = None

try:

t1 = writeTmp(1, [""])

t2 = writeTmp(2, [""])

t3 = writeTmp(3, ["The only line there is.\n"])

t4 = writeTmp(4, [""])

fi = FileInput(files=(t1, t2, t3, t4))

line = fi.readline()

self.assertEqual(line, 'The only line there is.\n')

self.assertEqual(fi.lineno(), 1)

self.assertEqual(fi.filelineno(), 1)

self.assertEqual(fi.filename(), t3)

line = fi.readline()

self.assertFalse(line)

self.assertEqual(fi.lineno(), 1)

self.assertEqual(fi.filelineno(), 0)

self.assertEqual(fi.filename(), t4)

fi.close()

finally:

remove_tempfiles(t1, t2, t3, t4)

开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:25,

示例2: more

​点赞 6

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def more(filenames, pagesize=10, clear=False, fmt='{line}'):

'''Display content of filenames pagesize lines at a time (cleared if specified) with format fmt for each output line'''

fileinput.close() # in case still open

try:

pageno = 1

if clear:

clear_screen()

for line in fileinput.input(filenames, openhook=fileinput.hook_encoded("utf-8")):

lineno, filename, filelineno = fileinput.lineno(), fileinput.filename(), fileinput.filelineno()

print(fmt.format(**locals()), end='')

if pagesize and lineno % pagesize == 0:

console.alert('Abort or continue', filename, 'Next page') # TODO: use less intrusive mechanism than alert

pageno += 1

if clear:

clear_screen()

finally:

fileinput.close()

# --- main

开发者ID:ywangd,项目名称:stash,代码行数:23,

示例3: main

​点赞 6

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def main(args):

parser = argparse.ArgumentParser(

description=__doc__,

epilog='This is inefficient for long input, as StaSh pipes do not multitask'

)

parser.add_argument('file', help='files to display ("-" is stdin is default)', action='store', nargs='*')

parser.add_argument('-p', '--pageno', help='number screen pages cumulatively', action='store_true')

parser.add_argument('-l', '--lineno', help='number lines cumulatively', action='store_true')

parser.add_argument('-f', '--filename', help='label lines by filename', action='store_true')

parser.add_argument('-n', '--filelineno', '-#', help='number lines per file', action='store_true')

parser.add_argument(

'-s',

'--pagesize',

help='number of lines per screen page (0 for no pagination)',

action='store',

type=int,

default=40

) # TODO: use actual number of lines on screen for dynamic screen page size

parser.add_argument('-c', '--clear', help='clear terminal screen before each screen page', action='store_true')

ns = parser.parse_args(args)

ns.line = True

fmt = ' '.join('{' + var + '}' for var in 'pageno lineno filename filelineno line'.split() if getattr(ns, var))

more(ns.file, ns.pagesize, ns.clear, fmt)

开发者ID:ywangd,项目名称:stash,代码行数:25,

示例4: _is_line_part_of_patches

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def _is_line_part_of_patches(self, lineno, line, patches):

"""

Checks if a line is part of the patch

Parameters

----------

int lineno: Line number

str line: Line text in a patch

list patches: List of patches

"""

result = False

for change in patches:

start, end = change.get("hunk")

if start <= lineno <= end:

patch = change.get("patch")

found = filter(lambda l: line.replace("\n", "") == l, patch.split("\n"))

if found:

result = True

break

# Try to catch unusual declared methods

broken_lines = line.split("\n")

if len(broken_lines) and not is_one_line_method(

broken_lines[0], self.config.get("keywords")

):

result = True

break

return result

开发者ID:Zarad1993,项目名称:dyc,代码行数:29,

示例5: test_files_that_dont_end_with_newline

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def test_files_that_dont_end_with_newline(self):

t1 = t2 = None

try:

t1 = writeTmp(1, ["A\nB\nC"])

t2 = writeTmp(2, ["D\nE\nF"])

fi = FileInput(files=(t1, t2))

lines = list(fi)

self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])

self.assertEqual(fi.filelineno(), 3)

self.assertEqual(fi.lineno(), 6)

finally:

remove_tempfiles(t1, t2)

## def test_unicode_filenames(self):

## # XXX A unicode string is always returned by writeTmp.

## # So is this needed?

## try:

## t1 = writeTmp(1, ["A\nB"])

## encoding = sys.getfilesystemencoding()

## if encoding is None:

## encoding = 'ascii'

## fi = FileInput(files=str(t1, encoding))

## lines = list(fi)

## self.assertEqual(lines, ["A\n", "B"])

## finally:

## remove_tempfiles(t1)

开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:28,

示例6: test_context_manager

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def test_context_manager(self):

try:

t1 = writeTmp(1, ["A\nB\nC"])

t2 = writeTmp(2, ["D\nE\nF"])

with FileInput(files=(t1, t2)) as fi:

lines = list(fi)

self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])

self.assertEqual(fi.filelineno(), 3)

self.assertEqual(fi.lineno(), 6)

self.assertEqual(fi._files, ())

finally:

remove_tempfiles(t1, t2)

开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:14,

示例7: lineno

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def lineno(self):

self.invocation_counts["lineno"] += 1

return self.return_values["lineno"]

开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:5,

示例8: test_state_is_None

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def test_state_is_None(self):

"""Tests fileinput.lineno() 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.lineno()

self.assertEqual(("no active input()",), cm.exception.args)

self.assertIsNone(fileinput._state)

开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:11,

示例9: error

​点赞 5

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def error( self, msg = "" ):

if ( msg ): print("\n ERROR: %s" % msg)

print("")

for line in fileinput.input(sys.argv[0]):

if ( not re.match( "#", line ) ): sys.exit(msg != "")

if ((fileinput.lineno() == 3) or (fileinput.lineno() > 4)):

print( re.sub( "^#", "", line.rstrip("\n") ) )

开发者ID:cornell-brg,项目名称:pydgin,代码行数:9,

示例10: initialize

​点赞 4

# 需要导入模块: import fileinput [as 别名]

# 或者: from fileinput import lineno [as 别名]

def initialize(self, change=None):

"""

The Builder's main method. It stores all the changes that needs to be made

in `self.details` for a file. Which would then be used to add Docstrings to.

"""

result = dict()

patches = []

if change:

patches = change.get("additions")

fileLines = list(fileinput.input(self.filename))

i = 0

for line in fileinput.input(self.filename):

filename = fileinput.filename()

lineno = fileinput.lineno()

keywords = self.config.get("keywords")

foundList = [

word.lstrip() for word in line.split(" ") if word.lstrip() in keywords

]

found = len(foundList) > 0 and not is_comment(

line, self.config.get('comments')

)

# Checking an unusual format in method declaration

if foundList:

openP = line.count("(")

closeP = line.count(")")

if openP == closeP:

pass

else:

pos = i

while openP != closeP:

pos += 1

line += fileLines[pos]

openP = line.count("(")

closeP = line.count(")")

lineno = pos + 1

i = i + 1

if change and found:

found = self._is_line_part_of_patches(lineno, line, patches)

if not self.details.get(filename):

self.details[filename] = dict()

if found:

length = get_file_lines(filename)

result = self.extract_and_set_information(

filename, lineno, line, length

)

if self.validate(result):

self.details[filename][result.name] = result

开发者ID:Zarad1993,项目名称:dyc,代码行数:55,

注:本文中的fileinput.lineno方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值