File: auto.py

package info (click to toggle)
python-eth-hash 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308 kB
  • sloc: python: 659; makefile: 233
file content (28 lines) | stat: -rw-r--r-- 781 bytes parent folder | download | duplicates (2)
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
from typing import (
    Union,
)

from eth_hash.abc import (
    BackendAPI,
    PreImageAPI,
)
from eth_hash.utils import (
    auto_choose_backend,
)


class AutoBackend(BackendAPI):
    def _initialize(self) -> None:
        backend = auto_choose_backend()
        # Use setattr to circumvent mypy's confusion, see:
        # https://github.com/python/mypy/issues/2427
        setattr(self, "keccak256", backend.keccak256)  # noqa: B010
        setattr(self, "preimage", backend.preimage)  # noqa: B010

    def keccak256(self, in_data: Union[bytearray, bytes]) -> bytes:
        self._initialize()
        return self.keccak256(in_data)

    def preimage(self, in_data: Union[bytearray, bytes]) -> PreImageAPI:
        self._initialize()
        return self.preimage(in_data)