checkpointing.hash.stream
1import hashlib 2from checkpointing.config import defaults 3from typing import Iterable 4from io import RawIOBase 5 6 7class HashStream(RawIOBase): 8 """ 9 Binary input stream hasher. It implements a file-like interface, so that it can be used with 10 pickle easily, without needing a copy of the bytes representation of an object and thus saving memory. 11 """ 12 13 def __init__(self, algorithm: str = None) -> None: 14 """ 15 Args: 16 algorithm: the hash algorithm. Must be supported by the hashlib. 17 If it's not specified, use the global default `hash.algorithm`. 18 """ 19 20 if algorithm is None: 21 algorithm = defaults["hash.algorithm"] 22 23 self.__hash = hashlib.new(algorithm) 24 25 def readable(self) -> bool: 26 return False 27 28 def seekable(self) -> bool: 29 return False 30 31 def writable(self) -> bool: 32 return True 33 34 def writelines(self, lines: Iterable[bytes]) -> None: 35 for line in lines: 36 self.write(line) 37 38 def write(self, b: bytes) -> int: 39 self.__hash.update(b) 40 return len(b) 41 42 def hexdigest(self) -> str: 43 """ 44 Returns: 45 The hexdigest of the all the bytes data written to this hash stream 46 """ 47 48 return self.__hash.hexdigest()
class
HashStream(io.RawIOBase):
8class HashStream(RawIOBase): 9 """ 10 Binary input stream hasher. It implements a file-like interface, so that it can be used with 11 pickle easily, without needing a copy of the bytes representation of an object and thus saving memory. 12 """ 13 14 def __init__(self, algorithm: str = None) -> None: 15 """ 16 Args: 17 algorithm: the hash algorithm. Must be supported by the hashlib. 18 If it's not specified, use the global default `hash.algorithm`. 19 """ 20 21 if algorithm is None: 22 algorithm = defaults["hash.algorithm"] 23 24 self.__hash = hashlib.new(algorithm) 25 26 def readable(self) -> bool: 27 return False 28 29 def seekable(self) -> bool: 30 return False 31 32 def writable(self) -> bool: 33 return True 34 35 def writelines(self, lines: Iterable[bytes]) -> None: 36 for line in lines: 37 self.write(line) 38 39 def write(self, b: bytes) -> int: 40 self.__hash.update(b) 41 return len(b) 42 43 def hexdigest(self) -> str: 44 """ 45 Returns: 46 The hexdigest of the all the bytes data written to this hash stream 47 """ 48 49 return self.__hash.hexdigest()
Binary input stream hasher. It implements a file-like interface, so that it can be used with pickle easily, without needing a copy of the bytes representation of an object and thus saving memory.
HashStream(algorithm: str = None)
14 def __init__(self, algorithm: str = None) -> None: 15 """ 16 Args: 17 algorithm: the hash algorithm. Must be supported by the hashlib. 18 If it's not specified, use the global default `hash.algorithm`. 19 """ 20 21 if algorithm is None: 22 algorithm = defaults["hash.algorithm"] 23 24 self.__hash = hashlib.new(algorithm)
Args
- algorithm: the hash algorithm. Must be supported by the hashlib.
If it's not specified, use the global default
hash.algorithm.
def
readable(self) -> bool:
Return whether object was opened for reading.
If False, read() will raise OSError.
def
seekable(self) -> bool:
Return whether object supports random access.
If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().
def
writable(self) -> bool:
Return whether object was opened for writing.
If False, write() will raise OSError.
def
writelines(self, lines: Iterable[bytes]) -> None:
Write a list of lines to stream.
Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.
def
hexdigest(self) -> str:
43 def hexdigest(self) -> str: 44 """ 45 Returns: 46 The hexdigest of the all the bytes data written to this hash stream 47 """ 48 49 return self.__hash.hexdigest()
Returns
The hexdigest of the all the bytes data written to this hash stream
Inherited Members
- _io._RawIOBase
- read
- readall
- readinto
- _io._IOBase
- seek
- tell
- truncate
- flush
- close
- fileno
- isatty
- readline
- readlines
- closed