Produce the "row processing" function for this :class:`.Bundle`. May be overridden by subclasses to provide custom behaviors when results are fetched. The method is passed the statement object and a set of "row processor" functions at query execution time; these proc
(
self,
query: Select[Any],
procs: Sequence[Callable[[Row[Any]], Any]],
labels: Sequence[str],
)
| 1709 | return cloned |
| 1710 | |
| 1711 | def create_row_processor( |
| 1712 | self, |
| 1713 | query: Select[Any], |
| 1714 | procs: Sequence[Callable[[Row[Any]], Any]], |
| 1715 | labels: Sequence[str], |
| 1716 | ) -> Callable[[Row[Any]], Any]: |
| 1717 | """Produce the "row processing" function for this :class:`.Bundle`. |
| 1718 | |
| 1719 | May be overridden by subclasses to provide custom behaviors when |
| 1720 | results are fetched. The method is passed the statement object and a |
| 1721 | set of "row processor" functions at query execution time; these |
| 1722 | processor functions when given a result row will return the individual |
| 1723 | attribute value, which can then be adapted into any kind of return data |
| 1724 | structure. |
| 1725 | |
| 1726 | The example below illustrates replacing the usual :class:`.Row` |
| 1727 | return structure with a straight Python dictionary:: |
| 1728 | |
| 1729 | from sqlalchemy.orm import Bundle |
| 1730 | |
| 1731 | |
| 1732 | class DictBundle(Bundle): |
| 1733 | def create_row_processor(self, query, procs, labels): |
| 1734 | "Override create_row_processor to return values as dictionaries" |
| 1735 | |
| 1736 | def proc(row): |
| 1737 | return dict(zip(labels, (proc(row) for proc in procs))) |
| 1738 | |
| 1739 | return proc |
| 1740 | |
| 1741 | A result from the above :class:`_orm.Bundle` will return dictionary |
| 1742 | values:: |
| 1743 | |
| 1744 | bn = DictBundle("mybundle", MyClass.data1, MyClass.data2) |
| 1745 | for row in session.execute(select(bn)).where(bn.c.data1 == "d1"): |
| 1746 | print(row.mybundle["data1"], row.mybundle["data2"]) |
| 1747 | |
| 1748 | """ # noqa: E501 |
| 1749 | keyed_tuple = result_tuple(labels, [() for l in labels]) |
| 1750 | |
| 1751 | def proc(row: Row[Any]) -> Any: |
| 1752 | return keyed_tuple([proc(row) for proc in procs]) |
| 1753 | |
| 1754 | return proc |
| 1755 | |
| 1756 | |
| 1757 | def _orm_annotate(element: _SA, exclude: Optional[Any] = None) -> _SA: |
no test coverage detected