Exemple #1
0
    def extract(self, key=None, fields=None, lang='en'):
        "Returns sub-data anchored by name. Similar to reversing DataMap.merge()"

        if not key and not fields:
            raise ValueError("Either a key or a list of fields " +
                             "must be given when persisting a data map")

        result = {}

        for entry in self.values():
            name = entry.name(lang)

            # If root is supplied, nest. If doesn't exist, skip to next item
            if key:
                if not entry.get(key, None):
                    continue
                entry = entry[key]

            # If we re-rooted, we might be looking at a list now
            # Lists are extracted as-is
            # TODO: what if we get a list here and fields is supplied?
            if not isinstance(entry, collections.Mapping):
                result[name] = entry
                continue

            # If fields is given, extract them
            if fields:
                result[name] = extract_fields(entry, *fields)
            else:
                result[name] = copy.deepcopy(entry)

        return result
Exemple #2
0
    def save_data_csv(self,
                      location,
                      data_map: DataMap,
                      *,
                      key_join='name_en',
                      nest_additional=[],
                      groups=[],
                      key=None,
                      fields=None,
                      schema=None):
        """Write a DataMap to a location in the data directory.

        If key is given, then the saving is restricted to what's inside that key.
        If fields are given, only fields within the list are exported.

        At least one of key or fields is required.

        TODO: Write about nest_additional and groups
        """

        if not key and not fields:
            raise ValueError("Either a key or a list of fields " +
                             "must be given when persisting a data map")

        flattened_rows = []
        for entry in data_map.values():
            ex_values = {k: entry[k] for k in data_map.keys_ex}
            row_base = {'base_' + key_join: entry[key_join], **ex_values}

            items = None
            if key and key in entry and entry[key]:
                items = entry[key]
                if not typecheck.is_flat_iterable(items):
                    items = [items]
            elif not key and fields:
                items = [entry.to_dict()]

            if not items:
                continue

            for item in items:
                # If fields is given, extract them
                if fields:
                    values = extract_fields(item, *fields)
                else:
                    values = copy.deepcopy(item)
                new_row = {**row_base, **values}
                flattened_rows.append(new_row)

        flattened_rows = [
            ungroup_fields(v, groups=groups) for v in flattened_rows
        ]
        self.save_csv(location, flattened_rows, schema=schema)