File: iterutils.py

package info (click to toggle)
python-aioinflux 0.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 320 kB
  • sloc: python: 1,402; makefile: 41
file content (48 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import inspect

from typing import Optional, Iterator, Callable, Any


def iterpoints(resp: dict, parser: Optional[Callable] = None) -> Iterator[Any]:
    """Iterates a response JSON yielding data point by point.

    Can be used with both regular and chunked responses.
    By default, returns just a plain list of values representing each point,
    without column names, or other metadata.

    In case a specific format is needed, an optional ``parser`` argument can be passed.
    ``parser`` is a function/callable that takes data point values
    and, optionally, a ``meta`` parameter containing which takes a
    dictionary containing all or a subset of the following:
    ``{'columns', 'name', 'tags', 'statement_id'}``.

    Sample parser functions:

    .. code:: python

       # Function optional meta argument
       def parser(*x, meta):
           return dict(zip(meta['columns'], x))

       # Namedtuple (callable)
       from collections import namedtuple
       parser = namedtuple('MyPoint', ['col1', 'col2', 'col3'])


    :param resp: Dictionary containing parsed JSON (output from InfluxDBClient.query)
    :param parser: Optional parser function/callable
    :return: Generator object
    """
    for statement in resp['results']:
        if 'series' not in statement:
            continue
        for series in statement['series']:
            if parser is None:
                return (x for x in series['values'])
            elif 'meta' in inspect.signature(parser).parameters:
                meta = {k: series[k] for k in series if k != 'values'}
                meta['statement_id'] = statement['statement_id']
                return (parser(*x, meta=meta) for x in series['values'])
            else:
                return (parser(*x) for x in series['values'])
    return iter([])