Completed
Pull Request — master (#31)
by Philip
01:26
created

inject_nulls()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
1
"""Functionality for manipulating dictionary records."""
2
3
from typing import Mapping
0 ignored issues
show
Configuration introduced by
The import typing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4
5
6
def rename_keys(record: Mapping, key_map: Mapping) -> dict:
7
    """New record with same keys or renamed keys if key found in key_map."""
8
9
    new_record = dict()
10
11
    for k, v in record.items():
12
        key = key_map[k] if k in key_map else k
13
        new_record[key] = v
14
15
    return new_record
16
17
18
def replace_keys(record: Mapping, key_map: Mapping) -> dict:
19
    """New record with renamed keys including keys only found in key_map."""
20
21
    return {key_map[k]: v for k, v in record.items() if k in key_map}
22
23
24
def inject_nulls(data: Mapping, field_names) -> dict:
25
    """Insert None as value for missing fields."""
26
27
    record = dict()
28
29
    for field in field_names:
30
        record[field] = data.get(field, None)
31
32
    return record
33