How to use the awe.chart.Transformer function in awe

To help you get started, we’ve selected a few awe examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dankilman / awe / tests / test_chart.py View on Github external
def test_get_transformer():
    get = chart.Chart._get_transformer

    for t in [None, 'noop']:
        noop_transformer = get(t)
        assert noop_transformer is chart.transformers['noop']
        assert isinstance(noop_transformer, chart.NoOpTransformer)

    assert isinstance(get('numbers'), chart.NumberSequenceTransformer)

    class MockTransformer(chart.Transformer):
        def transform(self, data):
            pass

    mock_transformer = MockTransformer()
    assert get(mock_transformer) is mock_transformer

    flat_transformer = get({
        'type': 'flat',
        'chart_mapping': ['one', 'two'],
        'series_mapping': ['three', 'four'],
        'value_key': 'value'
    })
    assert isinstance(flat_transformer, chart.FlatDictTransformer)
    assert flat_transformer._chart_mapping == ['one', 'two']
    assert flat_transformer._series_mapping == ['three', 'four']
    assert flat_transformer._value_key == 'value'
github dankilman / awe / awe / chart.py View on Github external
def _get_transformer(transform):
        transformer = transform or 'noop'
        if isinstance(transformer, Transformer):
            return transformer
        elif isinstance(transformer, dict):
            transformer_key = transformer.pop('type')
            return transformer_classes[transformer_key](**transformer)
        if isinstance(transformer, six.string_types):
            if transformer in transformers:
                return transformers[transformer]
            maybe_dict_transformer = DictLevelsTransformer.from_str(transformer)
            if maybe_dict_transformer:
                transformers[maybe_dict_transformer.key] = maybe_dict_transformer
                return maybe_dict_transformer
        raise ValueError('No matching transformer found for {}'.format(transform))
github dankilman / awe / awe / chart.py View on Github external
series_data = current_chart.setdefault(series_key, [])
            series_data.append((now, value))
        for chart_key, series in chart_dict.items():
            for series_key, series_data in series.items():
                (result.setdefault(chart_key, {
                    'series': [],
                    'title': chart_key,
                    'type': 'line'
                })['series'].append({
                    'name': series_key,
                    'data': series_data
                }))
        return result


class DictLevelsTransformer(Transformer):

    def __init__(self, chart_mapping, series_mapping):
        """
        A transformer that handles nested dictionaries data items.

        Usually instantiated by supplying a transform key in this format: ``[chart levels]to[series levels]``.

        For example, a key of ``23to1`` assumes a "3 level" nested dictionary where the charts will be generated
        from the different combinations of keys in the 2nd and 3rd levels and the series for each chart will be
        generated from each key in the 1st level.

        key: ``[Ns]to[Ms]``
        """

        self._chart_mapping = chart_mapping
        self._series_mapping = series_mapping
github dankilman / awe / awe / chart.py View on Github external
class NoOpTransformer(Transformer):
    """
    The default transformer.

    Assumes data is already in appropriate highcharts format.

    key: ``noop``
    """

    key = 'noop'

    def transform(self, data):
        return data


class NumberSequenceTransformer(Transformer):
    """
    A numbers transformer.

    Assumes each item in data is a single number or a list of numbers.

    If a list of numbers is supplied, each number in the list is assumed to belong to a different series.

    key: ``numbers``
    """

    key = 'numbers'

    def transform(self, data):
        now = now_ms()
        series_dict = {}
        for item in data:
github dankilman / awe / awe / chart.py View on Github external
for item in data:
            now, item = self._extract_timed_tuple(now, item)
            if isinstance(item, number_types):
                item = [item]
            for index, value in enumerate(item):
                series_dict.setdefault(index, {'name': index + 1, 'data': []})['data'].append((now, value))
        return {
            '': {
                'series': [series_dict[index] for index in sorted(series_dict)],
                'title': '',
                'type': 'line'
            }
        }


class FlatDictTransformer(Transformer):

    key = 'flat'

    def __init__(self, chart_mapping, series_mapping, value_key):
        """
        A transformer that expects data items to be dictionaries.

        key: ``flat``

        :param chart_mapping: A list of keys to build charts from. (combinations of them)
        :param series_mapping: A list of keys to build chart series from. (combinations of them)
        :param value_key: The key to the value of the data item.
        """
        self._chart_mapping = chart_mapping
        self._series_mapping = series_mapping
        self._value_key = value_key
github dankilman / awe / awe / chart.py View on Github external
if series['name'] in new_series_dict:
                    series['data'].extend(new_series_dict[series['name']])
        return transformed_added_data

    def transform(self, data):
        raise NotImplementedError

    @staticmethod
    def _extract_timed_tuple(now, item):
        if isinstance(item, timed_tuple_types) and len(item) == 2:
            now, item = item
            now = to_ms(now)
        return now, item


class NoOpTransformer(Transformer):
    """
    The default transformer.

    Assumes data is already in appropriate highcharts format.

    key: ``noop``
    """

    key = 'noop'

    def transform(self, data):
        return data


class NumberSequenceTransformer(Transformer):
    """