forked from mhm-ufz/mHM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlif.py
executable file
·164 lines (136 loc) · 4.75 KB
/
lif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
from __future__ import print_function
import string
def lif(file, noblank=False, comment='', skip=0, maxcol=False):
"""
Counts the numer of lines in a file.
Blank (only whitespace) and comment lines can be excluded.
Definition
----------
def lif(file, noblank=False, comment='', skip=0):
Input
-----
file source file name
Optional input parameters
-------------------------
comment line gets excluded if first character of line is
in comment sequence
sequence can be e.g. string, list or tuple
skip number of lines to skip at the beginning of
the file (default 0)
Options
-------
noblank excludes all lines that consists only of
whitespace characters
maxcol if True, return also maximum amount of characters in one line
Output
------
if maxcol:
number of lines in file, maximum characters in a line
else:
number of lines in file
Restrictions
------------
Only ascii files.
Examples
--------
# Create some data
>>> filename = 'test.dat'
>>> file = open(filename,'w')
>>> file.writelines('First line\\n')
>>> file.writelines(' \\n')
>>> file.writelines('# First comment\\n')
>>> file.writelines('! Second comment\\n')
>>> file.writelines('Last line\\n')
>>> file.close()
# Count lines
>>> print(lif(filename))
5
>>> print(lif(filename,noblank=True))
4
>>> print(lif(filename,comment='#'))
4
>>> print(lif(filename,comment='#!'))
3
>>> print(lif(filename,comment='#S'))
4
>>> print(lif(filename,comment=('#','L')))
3
>>> print(lif(filename,comment=['#','!']))
3
>>> print(lif(filename,comment='#!',noblank=True))
2
>>> print(lif(filename,skip=2))
3
>>> print(lif(filename,skip=2,maxcol=True))
(3, 16)
# Clean up
>>> import os
>>> os.remove(filename)
License
-------
This file is part of the UFZ Python library.
The UFZ Python library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The UFZ Python library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with The UFZ Python library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009-2013 Matthias Cuntz
History
-------
Written, MC, Jul 2009
Modified, MC, Nov 2012 - maxcol
MC, Feb 2013 - ported to Python 3
"""
# Open file
try:
f = open(file, 'r')
except IOError:
raise ValueError('Cannot open file '+file)
# Count lines
count = 0
if skip > 0:
iskip = 0
while iskip < skip:
l = f.readline()
iskip += 1
imax = []
if noblank and (comment != ''): # exclude blank, exclude comment
for line in f:
ll = line
imax += [len(ll)]
l = ll.strip(string.whitespace)
if (l != ''):
if (l[0] not in comment): count += 1
elif noblank and (comment == ''): # exclude blank, include comment
for line in f:
ll = line
imax += [len(ll)]
l = ll.strip(string.whitespace)
if (l != ''): count += 1
elif (not noblank) and (comment != ''):# include blank, exclude comment
for line in f:
ll = line
imax += [len(ll)]
l = ll.strip(string.whitespace)
if (l == ''):
count += 1
else:
if (l[0] not in comment): count += 1
else: # include blank, include comment
for line in f:
imax += [len(line)]
count = len(imax)
f.close()
if maxcol:
return count, max(imax)-1 # lines include \0 at the end.
else:
return count
if __name__ == '__main__':
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)