| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Various utilities |
| 5 |
|
| 6 |
Author: Gertjan van den Burg |
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
import hashlib |
| 11 |
|
| 12 |
from typing import Iterable |
| 13 |
from typing import Iterator |
| 14 |
from typing import Tuple |
| 15 |
from typing import TypeVar |
| 16 |
|
| 17 |
from clevercsv._types import AnyPath |
| 18 |
|
| 19 |
T = TypeVar("T") |
| 20 |
|
| 21 |
|
| 22 |
def pairwise(iterable: Iterable[T]) -> Iterator[Tuple[T, T]]: |
| 23 |
"s - > (s0, s1), (s1, s2), (s2, s3), ..." |
| 24 |
a = iter(iterable) |
| 25 |
b = iter(iterable) |
| 26 |
next(b, None) |
| 27 |
return zip(a, b) |
| 28 |
|
| 29 |
|
| 30 |
def sha1sum(filename: AnyPath) -> str: |
| 31 |
"""Compute the SHA1 checksum of a given file |
| 32 |
|
| 33 |
Parameters |
| 34 |
---------- |
| 35 |
filename : str |
| 36 |
Path to a file |
| 37 |
|
| 38 |
Returns |
| 39 |
------- |
| 40 |
checksum : str |
| 41 |
The SHA1 checksum of the file contents. |
| 42 |
""" |
| 43 |
blocksize = 1 << 16 |
| 44 |
hasher = hashlib.sha1() |
| 45 |
with open(filename, "rb") as fp: |
| 46 |
buf = fp.read(blocksize) |
| 47 |
while len(buf) > 0: |
| 48 |
hasher.update(buf) |
| 49 |
buf = fp.read(blocksize) |
| 50 |
return hasher.hexdigest() |