Edit on GitHub

common.lib.item_mapping

Classes for mapped items, i.e. complex objects mapped to simple dictionaries for 4CAT processing

  1"""
  2Classes for mapped items, i.e. complex objects mapped to simple dictionaries
  3for 4CAT processing
  4"""
  5
  6
  7class MissingMappedField:
  8    """
  9    Class for a missing field in a mapped item
 10
 11    Used if e.g. a metric is missing in the underlying data object, and
 12    processors might want to know this instead of using a default value
 13    """
 14
 15    def __init__(self, default):
 16        """
 17        Constructor
 18
 19        :param default:  Value to use as the value of this field unless the
 20        processor decides otherwise.
 21        """
 22        self.value = default
 23
 24
 25class MappedItem:
 26    """
 27    Class for mapped items
 28
 29    Mapped items are complex objects mapped to simple dictionaries for 4CAT
 30    processing. But a dictionary has limited room for annotation, so this
 31    class allows for additionally passing messages, warnings, etc.
 32    """
 33
 34    def __init__(self, data, message=""):
 35        """
 36        Constructor
 37        :param dict data:  Mapped item data
 38        :param str message:  Optionally, a message, e.g. a raised warning
 39        """
 40        self.data = data
 41        self.message = message
 42        self.missing = [k for k in self.data if type(self.data[k]) is MissingMappedField]
 43
 44    def get_item_data(self, safe=False):
 45        """
 46        Get mapped item data
 47
 48        :param bool safe:  Replace MissingMappedFields with their default value
 49        :return dict:
 50        """
 51        data = self.data.copy()
 52
 53        # replace MissingMappedFields
 54        if safe:
 55            for field, value in data.items():
 56                if type(value) is MissingMappedField:
 57                    data[field] = value.value
 58
 59        return self.data
 60
 61    def get_message(self):
 62        """
 63        Get mapped item message
 64        :return str:
 65        """
 66        return self.message
 67
 68    def get_missing_fields(self):
 69        """
 70        Get missing data fields
 71        :return list:
 72        """
 73        return self.missing
 74
 75
 76class DatasetItem(dict):
 77    """
 78    An item, from a dataset
 79
 80    This is a dict, with two special properties: 'original' and 'mapped_object'
 81    which store the unmapped version of the item and the MappedItem
 82    representation of the item, respectively. These can be used as alternative
 83    views on the same data which may offer useful capabilities in some contexts.
 84
 85    :todo: consider just-in-time mapping by only storing the original and
 86    calling the mapper only when the object is accessed as a dict
 87    """
 88    def __init__(self, mapper, original, mapped_object, *args, **kwargs):
 89        """
 90        DatasetItem init
 91
 92        :param callable mapper:  Mapper for this item. Currently unused, could
 93        be used for above-mentioned just-in-time mapping.
 94        :param dict original:  Original item, e.g. from the csv or ndjson
 95        :param MappedItem mapped_object:  Mapped item, before resolving any
 96        potential missing data
 97        """
 98        super().__init__(*args, **kwargs)
 99
100        self._mapper = mapper
101        self._original = original
102        self._mapped_object = mapped_object
103
104        if hasattr(mapped_object, "get_missing_fields"):
105            self.missing_fields = mapped_object.get_missing_fields()
106            self["missing_fields"] = ", ".join(self.missing_fields)
107
108    @property
109    def original(self):
110        """
111        Return original unmapped data
112
113        :return dict:
114        """
115        return self._original
116
117    @property
118    def mapped_object(self):
119        """
120        Return mapped item object
121
122        :return MappedItem:
123        """
124        return self._mapped_object
class MissingMappedField:
 8class MissingMappedField:
 9    """
10    Class for a missing field in a mapped item
11
12    Used if e.g. a metric is missing in the underlying data object, and
13    processors might want to know this instead of using a default value
14    """
15
16    def __init__(self, default):
17        """
18        Constructor
19
20        :param default:  Value to use as the value of this field unless the
21        processor decides otherwise.
22        """
23        self.value = default

Class for a missing field in a mapped item

Used if e.g. a metric is missing in the underlying data object, and processors might want to know this instead of using a default value

MissingMappedField(default)
16    def __init__(self, default):
17        """
18        Constructor
19
20        :param default:  Value to use as the value of this field unless the
21        processor decides otherwise.
22        """
23        self.value = default

Constructor

Parameters
  • default: Value to use as the value of this field unless the processor decides otherwise.
value
class MappedItem:
26class MappedItem:
27    """
28    Class for mapped items
29
30    Mapped items are complex objects mapped to simple dictionaries for 4CAT
31    processing. But a dictionary has limited room for annotation, so this
32    class allows for additionally passing messages, warnings, etc.
33    """
34
35    def __init__(self, data, message=""):
36        """
37        Constructor
38        :param dict data:  Mapped item data
39        :param str message:  Optionally, a message, e.g. a raised warning
40        """
41        self.data = data
42        self.message = message
43        self.missing = [k for k in self.data if type(self.data[k]) is MissingMappedField]
44
45    def get_item_data(self, safe=False):
46        """
47        Get mapped item data
48
49        :param bool safe:  Replace MissingMappedFields with their default value
50        :return dict:
51        """
52        data = self.data.copy()
53
54        # replace MissingMappedFields
55        if safe:
56            for field, value in data.items():
57                if type(value) is MissingMappedField:
58                    data[field] = value.value
59
60        return self.data
61
62    def get_message(self):
63        """
64        Get mapped item message
65        :return str:
66        """
67        return self.message
68
69    def get_missing_fields(self):
70        """
71        Get missing data fields
72        :return list:
73        """
74        return self.missing

Class for mapped items

Mapped items are complex objects mapped to simple dictionaries for 4CAT processing. But a dictionary has limited room for annotation, so this class allows for additionally passing messages, warnings, etc.

MappedItem(data, message='')
35    def __init__(self, data, message=""):
36        """
37        Constructor
38        :param dict data:  Mapped item data
39        :param str message:  Optionally, a message, e.g. a raised warning
40        """
41        self.data = data
42        self.message = message
43        self.missing = [k for k in self.data if type(self.data[k]) is MissingMappedField]

Constructor

Parameters
  • dict data: Mapped item data
  • str message: Optionally, a message, e.g. a raised warning
data
message
missing
def get_item_data(self, safe=False):
45    def get_item_data(self, safe=False):
46        """
47        Get mapped item data
48
49        :param bool safe:  Replace MissingMappedFields with their default value
50        :return dict:
51        """
52        data = self.data.copy()
53
54        # replace MissingMappedFields
55        if safe:
56            for field, value in data.items():
57                if type(value) is MissingMappedField:
58                    data[field] = value.value
59
60        return self.data

Get mapped item data

Parameters
  • bool safe: Replace MissingMappedFields with their default value
Returns
def get_message(self):
62    def get_message(self):
63        """
64        Get mapped item message
65        :return str:
66        """
67        return self.message

Get mapped item message

Returns
def get_missing_fields(self):
69    def get_missing_fields(self):
70        """
71        Get missing data fields
72        :return list:
73        """
74        return self.missing

Get missing data fields

Returns
class DatasetItem(builtins.dict):
 77class DatasetItem(dict):
 78    """
 79    An item, from a dataset
 80
 81    This is a dict, with two special properties: 'original' and 'mapped_object'
 82    which store the unmapped version of the item and the MappedItem
 83    representation of the item, respectively. These can be used as alternative
 84    views on the same data which may offer useful capabilities in some contexts.
 85
 86    :todo: consider just-in-time mapping by only storing the original and
 87    calling the mapper only when the object is accessed as a dict
 88    """
 89    def __init__(self, mapper, original, mapped_object, *args, **kwargs):
 90        """
 91        DatasetItem init
 92
 93        :param callable mapper:  Mapper for this item. Currently unused, could
 94        be used for above-mentioned just-in-time mapping.
 95        :param dict original:  Original item, e.g. from the csv or ndjson
 96        :param MappedItem mapped_object:  Mapped item, before resolving any
 97        potential missing data
 98        """
 99        super().__init__(*args, **kwargs)
100
101        self._mapper = mapper
102        self._original = original
103        self._mapped_object = mapped_object
104
105        if hasattr(mapped_object, "get_missing_fields"):
106            self.missing_fields = mapped_object.get_missing_fields()
107            self["missing_fields"] = ", ".join(self.missing_fields)
108
109    @property
110    def original(self):
111        """
112        Return original unmapped data
113
114        :return dict:
115        """
116        return self._original
117
118    @property
119    def mapped_object(self):
120        """
121        Return mapped item object
122
123        :return MappedItem:
124        """
125        return self._mapped_object

An item, from a dataset

This is a dict, with two special properties: 'original' and 'mapped_object' which store the unmapped version of the item and the MappedItem representation of the item, respectively. These can be used as alternative views on the same data which may offer useful capabilities in some contexts.

:todo: consider just-in-time mapping by only storing the original and calling the mapper only when the object is accessed as a dict

DatasetItem(mapper, original, mapped_object, *args, **kwargs)
 89    def __init__(self, mapper, original, mapped_object, *args, **kwargs):
 90        """
 91        DatasetItem init
 92
 93        :param callable mapper:  Mapper for this item. Currently unused, could
 94        be used for above-mentioned just-in-time mapping.
 95        :param dict original:  Original item, e.g. from the csv or ndjson
 96        :param MappedItem mapped_object:  Mapped item, before resolving any
 97        potential missing data
 98        """
 99        super().__init__(*args, **kwargs)
100
101        self._mapper = mapper
102        self._original = original
103        self._mapped_object = mapped_object
104
105        if hasattr(mapped_object, "get_missing_fields"):
106            self.missing_fields = mapped_object.get_missing_fields()
107            self["missing_fields"] = ", ".join(self.missing_fields)

DatasetItem init

Parameters
  • callable mapper: Mapper for this item. Currently unused, could be used for above-mentioned just-in-time mapping.
  • dict original: Original item, e.g. from the csv or ndjson
  • MappedItem mapped_object: Mapped item, before resolving any potential missing data
original
109    @property
110    def original(self):
111        """
112        Return original unmapped data
113
114        :return dict:
115        """
116        return self._original

Return original unmapped data

Returns
mapped_object
118    @property
119    def mapped_object(self):
120        """
121        Return mapped item object
122
123        :return MappedItem:
124        """
125        return self._mapped_object

Return mapped item object

Returns