# Path: aiohttp_json_api/helpers.py
# def first(iterable, default=None, key=None):
#     """
#     Return first element of *iterable*.
# 
#     Return first element of *iterable* that evaluates to ``True``, else
#     return ``None`` or optional *default*.
# 
#     >>> first([0, False, None, [], (), 42])
#     42
#     >>> first([0, False, None, [], ()]) is None
#     True
#     >>> first([0, False, None, [], ()], default='ohai')
#     'ohai'
#     >>> import re
#     >>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
#     >>> m.group(1)
#     'bc'
# 
#     The optional *key* argument specifies a one-argument predicate function
#     like that used for *filter()*.  The *key* argument, if supplied, should be
#     in keyword form. For example, finding the first even number in an iterable:
# 
#     >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
#     4
# 
#     Contributed by Hynek Schlawack, author of `the original standalone module`_
# 
#     .. _the original standalone module: https://github.com/hynek/first
#     """
#     return next(filter(key, iterable), default)
# 
# def make_sentinel(name='_MISSING', var_name=None):
#     """
#     Create sentinel instance.
# 
#     Creates and returns a new **instance** of a new class, suitable for
#     usage as a "sentinel", a kind of singleton often used to indicate
#     a value is missing when ``None`` is a valid input.
# 
#     >>> make_sentinel(var_name='_MISSING')
#     _MISSING
# 
#     The most common use cases here in project are as default values
#     for optional function arguments, partly because of its
#     less-confusing appearance in automatically generated
#     documentation. Sentinels also function well as placeholders in queues
#     and linked lists.
# 
#     .. note::
# 
#       By design, additional calls to ``make_sentinel`` with the same
#       values will not produce equivalent objects.
# 
#       >>> make_sentinel('TEST') == make_sentinel('TEST')
#       False
#       >>> type(make_sentinel('TEST')) == type(make_sentinel('TEST'))
#       False
# 
#     :arg str name:
#         Name of the Sentinel
#     :arg str var_name:
#         Set this name to the name of the variable in its respective
#         module enable pickleability.
#     """
#     class Sentinel(object):
#         def __init__(self):
#             self.name = name
#             self.var_name = var_name
# 
#         def __repr__(self):
#             if self.var_name:
#                 return self.var_name
#             return '%s(%r)' % (self.__class__.__name__, self.name)
# 
#         if var_name:
#             def __reduce__(self):
#                 return self.var_name
# 
#         def __nonzero__(self):
#             return False
# 
#         __bool__ = __nonzero__
# 
#     return Sentinel()


from collections import OrderedDict
from typing import NamedTuple, Union, Optional, List
from aiopg.sa import SAConnection
from sqlalchemy.sql.selectable import CTE
from trafaret.contrib.rfc_3339 import DateTime, Date
from aiohttp_json_api.helpers import first, make_sentinel
import datetime
import inflection
import trafaret as t
import sqlalchemy as sa
import examples.fantasy.tables as tbl
                book_series = Series.not_populated(book_series_id) \
                    if book_series_id \
                    else None
                book = Book.from_row(row, author=book_author, chapters=[],
                                     series=book_series,
                                     photos=[], alias=cte)
                results[book_id] = book

            photo_id = row[tbl.photos.c.id]
            if photo_id:
                photo = photos.get(photo_id, Photo.not_populated(photo_id))

                if photo not in book.photos:
                    book.photos.append(photo)

            chapter_id = row[tbl.chapters.c.id]
            if chapter_id:
                chapter = chapters.get(chapter_id,
                                       Chapter.not_populated(chapter_id))

                if chapter not in book.chapters:
                    book.chapters.append(chapter)

        return results

    @classmethod
    async def fetch_one(cls, conn: SAConnection, book_id):
        cte = cls.cte(where=(cls.Options.db_table.c.id == book_id),
                      limit=1)
        results = await cls.fetch_many(conn, cte=cte)

# Path: aiohttp_json_api/helpers.py
# def first(iterable, default=None, key=None):
#     """
#     Return first element of *iterable*.
# 
#     Return first element of *iterable* that evaluates to ``True``, else
#     return ``None`` or optional *default*.
# 
#     >>> first([0, False, None, [], (), 42])
#     42
#     >>> first([0, False, None, [], ()]) is None
#     True
#     >>> first([0, False, None, [], ()], default='ohai')
#     'ohai'
#     >>> import re
#     >>> m = first(re.match(regex, 'abc') for regex in ['b.*', 'a(.*)'])
#     >>> m.group(1)
#     'bc'
# 
#     The optional *key* argument specifies a one-argument predicate function
#     like that used for *filter()*.  The *key* argument, if supplied, should be
#     in keyword form. For example, finding the first even number in an iterable:
# 
#     >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
#     4
# 
#     Contributed by Hynek Schlawack, author of `the original standalone module`_
# 
#     .. _the original standalone module: https://github.com/hynek/first
#     """
#     return next(filter(key, iterable), default)
# 
# def make_sentinel(name='_MISSING', var_name=None):
#     """
#     Create sentinel instance.
# 
#     Creates and returns a new **instance** of a new class, suitable for
#     usage as a "sentinel", a kind of singleton often used to indicate
#     a value is missing when ``None`` is a valid input.
# 
#     >>> make_sentinel(var_name='_MISSING')
#     _MISSING
# 
#     The most common use cases here in project are as default values
#     for optional function arguments, partly because of its
#     less-confusing appearance in automatically generated
#     documentation. Sentinels also function well as placeholders in queues
#     and linked lists.
# 
#     .. note::
# 
#       By design, additional calls to ``make_sentinel`` with the same
#       values will not produce equivalent objects.
# 
#       >>> make_sentinel('TEST') == make_sentinel('TEST')
#       False
#       >>> type(make_sentinel('TEST')) == type(make_sentinel('TEST'))
#       False
# 
#     :arg str name:
#         Name of the Sentinel
#     :arg str var_name:
#         Set this name to the name of the variable in its respective
#         module enable pickleability.
#     """
#     class Sentinel(object):
#         def __init__(self):
#             self.name = name
#             self.var_name = var_name
# 
#         def __repr__(self):
#             if self.var_name:
#                 return self.var_name
#             return '%s(%r)' % (self.__class__.__name__, self.name)
# 
#         if var_name:
#             def __reduce__(self):
#                 return self.var_name
# 
#         def __nonzero__(self):
#             return False
# 
#         __bool__ = __nonzero__
# 
#     return Sentinel()


# Path: examples/fantasy/models.py
from collections import OrderedDict
from typing import NamedTuple, Union, Optional, List
from aiopg.sa import SAConnection
from sqlalchemy.sql.selectable import CTE
from trafaret.contrib.rfc_3339 import DateTime, Date
from aiohttp_json_api.helpers import first, make_sentinel
import datetime
import inflection
import trafaret as t
import sqlalchemy as sa
import examples.fantasy.tables as tbl

                book_series = Series.not_populated(book_series_id) \
                    if book_series_id \
                    else None
                book = Book.from_row(row, author=book_author, chapters=[],
                                     series=book_series,
                                     photos=[], alias=cte)
                results[book_id] = book

            photo_id = row[tbl.photos.c.id]
            if photo_id:
                photo = photos.get(photo_id, Photo.not_populated(photo_id))

                if photo not in book.photos:
                    book.photos.append(photo)

            chapter_id = row[tbl.chapters.c.id]
            if chapter_id:
                chapter = chapters.get(chapter_id,
                                       Chapter.not_populated(chapter_id))

                if chapter not in book.chapters:
                    book.chapters.append(chapter)

        return results

    @classmethod
    async def fetch_one(cls, conn: SAConnection, book_id):
        cte = cls.cte(where=(cls.Options.db_table.c.id == book_id),
                      limit=1)
        results = await cls.fetch_many(conn, cte=cte)

        return first(results.values())