MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / DictBundle

Class DictBundle

lib/sqlalchemy/orm/util.py:1782–1820  ·  view source on GitHub ↗

Like :class:`.Bundle` but returns ``dict`` instances instead of named tuple like objects:: bn = DictBundle("mybundle", MyClass.data1, MyClass.data2) for row in session.execute(select(bn)).where(bn.c.data1 == "d1"): print(row.mybundle["data1"], row.mybundle["data2"])

Source from the content-addressed store, hash-verified

1780
1781
1782class DictBundle(Bundle[_T]):
1783 """Like :class:`.Bundle` but returns ``dict`` instances instead of
1784 named tuple like objects::
1785
1786 bn = DictBundle("mybundle", MyClass.data1, MyClass.data2)
1787 for row in session.execute(select(bn)).where(bn.c.data1 == "d1"):
1788 print(row.mybundle["data1"], row.mybundle["data2"])
1789
1790 Differently from :class:`.Bundle`, multiple columns with the same name are
1791 not supported.
1792
1793 .. versionadded:: 2.1
1794
1795 .. seealso::
1796
1797 :ref:`bundles`
1798
1799 :class:`.Bundle`
1800 """
1801
1802 def __init__(
1803 self, name: str, *exprs: _ColumnExpressionArgument[Any], **kw: Any
1804 ) -> None:
1805 super().__init__(name, *exprs, **kw)
1806 if len(set(self.c.keys())) != len(self.c):
1807 raise sa_exc.ArgumentError(
1808 "DictBundle does not support duplicate column names"
1809 )
1810
1811 def create_row_processor(
1812 self,
1813 query: Select[Unpack[TupleAny]],
1814 procs: Sequence[Callable[[Row[Unpack[TupleAny]]], Any]],
1815 labels: Sequence[str],
1816 ) -> Callable[[Row[Unpack[TupleAny]]], dict[str, Any]]:
1817 def proc(row: Row[Unpack[TupleAny]]) -> dict[str, Any]:
1818 return dict(zip(labels, (proc(row) for proc in procs)))
1819
1820 return proc
1821
1822
1823def _orm_full_deannotate(element: _SA) -> _SA:

Calls

no outgoing calls