# Path: databot/db/utils.py
# class Row(dict):
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.__dict__ = self
# 
# Path: databot/exporters/utils.py
# def flatten_nested_lists(nested, include=None, exclude=None, field=(), context=None):
#     data, lists = separate_dicts_from_lists(nested, field, include, exclude)
#     data += (context or [])
#     if lists:
#         for key, values in lists:
#             for value in values:
#                 yield from flatten_nested_lists(value, include, exclude, key, data)
#     else:
#         yield data
# 
# def flatten_nested_dicts(nested, field=(), include=None, exclude=None):
#     skip = False
# 
#     if field and include:
#         if includes(include, field):
#             if field in include:
#                 include = None
#         else:
#             skip = True
# 
#     if field and exclude:
#         if exclude is True:
#             skip = True
#         elif field in exclude:
#             skip = True
#             exclude = True
# 
#     if skip:
#         pass
#     elif isinstance(nested, dict):
#         for k, v in nested.items():
#             yield from flatten_nested_dicts(v, field + (k,), include, exclude)
#     else:
#         yield (field, nested)
# 
# def get_level_keys(keys, field, include=()):
#     include_all = True
#     include_some = False
#     if include:
#         include_all = False
#         for item in include:
#             common = tuple(commonstart(field, item))
#             if len(field) == len(common) and len(item) > len(field):
#                 include_some = True
#                 if item[len(field)] in keys:
#                     yield item[len(field)]
#             elif len(item) <= len(common):
#                 include_all = True
# 
#     if include_some is False and include_all:
#         yield from sorted(keys)
# 
# def flatten(rows, exclude=None, include: list=None, update=None, sep='.', scan=100, key_name='key'):
#     include = [tuple(x.split(sep)) for x in include] if include else []
#     exclude = [tuple(x.split(sep)) for x in exclude] if exclude else []
# 
#     rows = (
#         x
#         for row in updated_rows(rows, update)
#         for x in flatten_nested_lists(row_to_dict(row, key_name), include, exclude)
#     )
# 
#     rows, fields = detect_fields(rows, scan)
# 
#     fields = sort_fields(fields, include)
#     yield tuple(sep.join(map(str, x)) for x in fields)
# 
#     for row in map(dict, rows):
#         yield tuple(row.get(x) for x in fields)
# 
# def sort_fields(fields, include):
#     if include:
#         fields = set(fields)
#         sorted_fields = []
#         for item in include:
#             if item in fields:
#                 sorted_fields.append(item)
#                 fields.remove(item)
#             else:
#                 unsorted_fields = []
#                 for field in fields:
#                     common = tuple(commonstart(field, item))
#                     if len(item) == len(common) and len(field) >= len(common):
#                         unsorted_fields.append(field)
#                 for field in sorted(unsorted_fields):
#                     sorted_fields.append(field)
#                     fields.remove(field)
#         return sorted_fields
#     else:
#         return sorted(fields)
# 
# Path: databot/exporters/jsonl.py
# def open(path_or_file, *args, **kwargs):
# def export(path, rows, append=False):
# 
# Path: databot/exporters/pandas.py
# def index_from_columns(columns, name):
# def index_from_row(row, index, idx):
# def rows_to_dataframe_items(rows, index):
# def export(pd, rows, exclude=None, include=None, update=None):


import io
import pytest
import databot
import pandas as pd
from databot.db.utils import Row
from databot.exporters.utils import flatten_nested_lists, flatten_nested_dicts, get_level_keys, flatten, sort_fields
from databot.exporters import jsonl
from databot.exporters import pandas
        (2, 'b'),
    ]


def test_flatten_rows_value(data):
    rows = [
        Row(key=1, value='a'),
        Row(key=2, value='b'),
    ]
    assert list(flatten(rows)) == [
        ('key', 'value'),
        (1, 'a'),
        (2, 'b'),
    ]


def test_flatten_int_key(data):
    rows = [
        Row(key=1, value={'year': {2000: 1, 2001: 2}}),
        Row(key=2, value={'year': {2000: 3, 2001: 4}}),
    ]
    assert list(flatten(rows)) == [
        ('key', 'year.2000', 'year.2001'),
        (1, 1, 2),
        (2, 3, 4),
    ]


def test_flatten_list(data):
    rows = [

# Path: databot/db/utils.py
# class Row(dict):
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.__dict__ = self
# 
# Path: databot/exporters/utils.py
# def flatten_nested_lists(nested, include=None, exclude=None, field=(), context=None):
#     data, lists = separate_dicts_from_lists(nested, field, include, exclude)
#     data += (context or [])
#     if lists:
#         for key, values in lists:
#             for value in values:
#                 yield from flatten_nested_lists(value, include, exclude, key, data)
#     else:
#         yield data
# 
# def flatten_nested_dicts(nested, field=(), include=None, exclude=None):
#     skip = False
# 
#     if field and include:
#         if includes(include, field):
#             if field in include:
#                 include = None
#         else:
#             skip = True
# 
#     if field and exclude:
#         if exclude is True:
#             skip = True
#         elif field in exclude:
#             skip = True
#             exclude = True
# 
#     if skip:
#         pass
#     elif isinstance(nested, dict):
#         for k, v in nested.items():
#             yield from flatten_nested_dicts(v, field + (k,), include, exclude)
#     else:
#         yield (field, nested)
# 
# def get_level_keys(keys, field, include=()):
#     include_all = True
#     include_some = False
#     if include:
#         include_all = False
#         for item in include:
#             common = tuple(commonstart(field, item))
#             if len(field) == len(common) and len(item) > len(field):
#                 include_some = True
#                 if item[len(field)] in keys:
#                     yield item[len(field)]
#             elif len(item) <= len(common):
#                 include_all = True
# 
#     if include_some is False and include_all:
#         yield from sorted(keys)
# 
# def flatten(rows, exclude=None, include: list=None, update=None, sep='.', scan=100, key_name='key'):
#     include = [tuple(x.split(sep)) for x in include] if include else []
#     exclude = [tuple(x.split(sep)) for x in exclude] if exclude else []
# 
#     rows = (
#         x
#         for row in updated_rows(rows, update)
#         for x in flatten_nested_lists(row_to_dict(row, key_name), include, exclude)
#     )
# 
#     rows, fields = detect_fields(rows, scan)
# 
#     fields = sort_fields(fields, include)
#     yield tuple(sep.join(map(str, x)) for x in fields)
# 
#     for row in map(dict, rows):
#         yield tuple(row.get(x) for x in fields)
# 
# def sort_fields(fields, include):
#     if include:
#         fields = set(fields)
#         sorted_fields = []
#         for item in include:
#             if item in fields:
#                 sorted_fields.append(item)
#                 fields.remove(item)
#             else:
#                 unsorted_fields = []
#                 for field in fields:
#                     common = tuple(commonstart(field, item))
#                     if len(item) == len(common) and len(field) >= len(common):
#                         unsorted_fields.append(field)
#                 for field in sorted(unsorted_fields):
#                     sorted_fields.append(field)
#                     fields.remove(field)
#         return sorted_fields
#     else:
#         return sorted(fields)
# 
# Path: databot/exporters/jsonl.py
# def open(path_or_file, *args, **kwargs):
# def export(path, rows, append=False):
# 
# Path: databot/exporters/pandas.py
# def index_from_columns(columns, name):
# def index_from_row(row, index, idx):
# def rows_to_dataframe_items(rows, index):
# def export(pd, rows, exclude=None, include=None, update=None):


# Path: tests/test_export.py
import io
import pytest
import databot
import pandas as pd
from databot.db.utils import Row
from databot.exporters.utils import flatten_nested_lists, flatten_nested_dicts, get_level_keys, flatten, sort_fields
from databot.exporters import jsonl
from databot.exporters import pandas

        (2, 'b'),
    ]


def test_flatten_rows_value(data):
    rows = [
        Row(key=1, value='a'),
        Row(key=2, value='b'),
    ]
    assert list(flatten(rows)) == [
        ('key', 'value'),
        (1, 'a'),
        (2, 'b'),
    ]


def test_flatten_int_key(data):
    rows = [
        Row(key=1, value={'year': {2000: 1, 2001: 2}}),
        Row(key=2, value={'year': {2000: 3, 2001: 4}}),
    ]
    assert list(flatten(rows)) == [
        ('key', 'year.2000', 'year.2001'),
        (1, 1, 2),
        (2, 3, 4),
    ]


def test_flatten_list(data):
    rows = [

        Row(key=1, value={'events': [