File: utils.py

package info (click to toggle)
python-clevercsv 0.8.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,076 kB
  • sloc: python: 6,184; ansic: 870; makefile: 90
file content (50 lines) | stat: -rw-r--r-- 969 bytes parent folder | download
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
# -*- coding: utf-8 -*-

"""
Various utilities

Author: Gertjan van den Burg

"""

import hashlib

from typing import Iterable
from typing import Iterator
from typing import Tuple
from typing import TypeVar

from clevercsv._types import AnyPath

T = TypeVar("T")


def pairwise(iterable: Iterable[T]) -> Iterator[Tuple[T, T]]:
    "s - > (s0, s1), (s1, s2), (s2, s3), ..."
    a = iter(iterable)
    b = iter(iterable)
    next(b, None)
    return zip(a, b)


def sha1sum(filename: AnyPath) -> str:
    """Compute the SHA1 checksum of a given file

    Parameters
    ----------
    filename : str
        Path to a file

    Returns
    -------
    checksum : str
        The SHA1 checksum of the file contents.
    """
    blocksize = 1 << 16
    hasher = hashlib.sha1()
    with open(filename, "rb") as fp:
        buf = fp.read(blocksize)
        while len(buf) > 0:
            hasher.update(buf)
            buf = fp.read(blocksize)
    return hasher.hexdigest()