MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / create_row_processor

Method create_row_processor

lib/sqlalchemy/orm/util.py:1730–1779  ·  view source on GitHub ↗

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[Unpack[TupleAny]],
        procs: Sequence[Callable[[Row[Unpack[TupleAny]]], Any]],
        labels: Sequence[str],
    )

Source from the content-addressed store, hash-verified

1728 return cloned
1729
1730 def create_row_processor(
1731 self,
1732 query: Select[Unpack[TupleAny]],
1733 procs: Sequence[Callable[[Row[Unpack[TupleAny]]], Any]],
1734 labels: Sequence[str],
1735 ) -> Callable[[Row[Unpack[TupleAny]]], Any]:
1736 """Produce the "row processing" function for this :class:`.Bundle`.
1737
1738 May be overridden by subclasses to provide custom behaviors when
1739 results are fetched. The method is passed the statement object and a
1740 set of "row processor" functions at query execution time; these
1741 processor functions when given a result row will return the individual
1742 attribute value, which can then be adapted into any kind of return data
1743 structure.
1744
1745 The example below illustrates replacing the usual :class:`.Row`
1746 return structure with a straight Python dictionary::
1747
1748 from sqlalchemy.orm import Bundle
1749
1750
1751 class DictBundle(Bundle):
1752 def create_row_processor(self, query, procs, labels):
1753 "Override create_row_processor to return values as dictionaries"
1754
1755 def proc(row):
1756 return dict(zip(labels, (proc(row) for proc in procs)))
1757
1758 return proc
1759
1760 A result from the above :class:`_orm.Bundle` will return dictionary
1761 values::
1762
1763 bn = DictBundle("mybundle", MyClass.data1, MyClass.data2)
1764 for row in session.execute(select(bn)).where(bn.c.data1 == "d1"):
1765 print(row.mybundle["data1"], row.mybundle["data2"])
1766
1767 The above example is available natively using :class:`.DictBundle`
1768
1769 .. seealso::
1770
1771 :class:`.DictBundle`
1772
1773 """ # noqa: E501
1774 keyed_tuple = result_tuple(labels, [() for l in labels])
1775
1776 def proc(row: Row[Unpack[TupleAny]]) -> Any:
1777 return keyed_tuple([proc(row) for proc in procs])
1778
1779 return proc
1780
1781
1782class DictBundle(Bundle[_T]):

Callers 2

row_processorMethod · 0.45
_instance_processorFunction · 0.45

Calls 1

result_tupleFunction · 0.85

Tested by

no test coverage detected