content_hash.encodes.get_encode()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 19
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
"""Known encoding."""
2
3
import importlib
4
5
6
CACHE = {}
7
"""dict: a cache of known encoding"""
8
9
10
def get_encode(name):
11
    """
12
    Get encoding function by name.
13
14
    Encoding should be a function that takes
15
    a `str` input and returns a `bytes` result.
16
17
    :param str name: an encode name
18
19
    :return: the resulting encode
20
    :rtype: callable
21
    """
22
23
    encode = CACHE.get(name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable CACHE does not seem to be defined.
Loading history...
24
25
    if not encode:
26
        encode = CACHE[name] = importlib.import_module(f".{name}", __name__).encode
27
28
    return encode
29