-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
44 lines (38 loc) · 1.45 KB
/
utils.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
def get_file_size(file_name):
"""
:rtype numeric: the number of bytes in a file
"""
bytes = os.path.getsize(file_name)
return humanize_bytes(bytes)
def humanize_bytes(bytes, precision=1):
"""Return a humanized string representation of a number of bytes."""
# This was stollen from http://code.activestate.com/recipes/577081/
abbrevs = (
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
(1, 'bytes')
)
if bytes == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytes >= factor:
break
return '%.*f %s' % (precision, bytes / factor, suffix)
def get_file_line_count(filename):
""" Return the number of lines in a file
The idea is to simumalate a `while` loop by reading and returning
pices of the file in a buffer, and counting `\n` characters
"""
# Stollen from SO/questions/845058/how-to-get-line-count-cheaply-in-python
from itertools import (takewhile, repeat)
with open(filename, 'rb') as f:
# Make an iterator that returns elements from
# the iterable as long as the predicate is true:
# takewhile(predicate, iterable)
fragment_iter = takewhile(lambda frag: frag,
(f.raw.read(1024 * 1024)
for _ in repeat(None)))
return sum(frag.count(b'\n') for frag in fragment_iter)