MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / partitions

Method partitions

lib/sqlalchemy/engine/result.py:862–918  ·  view source on GitHub ↗

Iterate through sub-lists of rows of the size given. Each list will be of the size given, excluding the last list to be yielded, which may have a small number of rows. No empty lists will be yielded. The result object is automatically closed when the iterator

(
        self, size: Optional[int] = None
    )

Source from the content-addressed store, hash-verified

860 return self._next_impl()
861
862 def partitions(
863 self, size: Optional[int] = None
864 ) -> Iterator[Sequence[Row[Unpack[_Ts]]]]:
865 """Iterate through sub-lists of rows of the size given.
866
867 Each list will be of the size given, excluding the last list to
868 be yielded, which may have a small number of rows. No empty
869 lists will be yielded.
870
871 The result object is automatically closed when the iterator
872 is fully consumed.
873
874 Note that the backend driver will usually buffer the entire result
875 ahead of time unless the
876 :paramref:`.Connection.execution_options.stream_results` execution
877 option is used indicating that the driver should not pre-buffer
878 results, if possible. Not all drivers support this option and
879 the option is silently ignored for those who do not.
880
881 When using the ORM, the :meth:`_engine.Result.partitions` method
882 is typically more effective from a memory perspective when it is
883 combined with use of the
884 :ref:`yield_per execution option <orm_queryguide_yield_per>`,
885 which instructs both the DBAPI driver to use server side cursors,
886 if available, as well as instructs the ORM loading internals to only
887 build a certain amount of ORM objects from a result at a time before
888 yielding them out.
889
890 .. versionadded:: 1.4
891
892 :param size: indicate the maximum number of rows to be present
893 in each list yielded. If None, makes use of the value set by
894 the :meth:`_engine.Result.yield_per`, method, if it were called,
895 or the :paramref:`_engine.Connection.execution_options.yield_per`
896 execution option, which is equivalent in this regard. If
897 yield_per weren&#x27;t set, it makes use of the
898 :meth:`_engine.Result.fetchmany` default, which may be backend
899 specific and not well defined.
900
901 :return: iterator of lists
902
903 .. seealso::
904
905 :ref:`engine_stream_results`
906
907 :ref:`orm_queryguide_yield_per` - in the :ref:`queryguide_toplevel`
908
909 """
910
911 getter = self._manyrow_getter
912
913 while True:
914 partition = getter(self, size)
915 if partition:
916 yield partition
917 else:
918 break
919

Callers 12

test_stream_optionsMethod · 0.45
test_partitionsMethod · 0.45
result_oneFunction · 0.45
test_partitionMethod · 0.45
test_partitionsMethod · 0.45
goMethod · 0.45

Calls

no outgoing calls

Tested by 11

test_stream_optionsMethod · 0.36
test_partitionsMethod · 0.36
test_partitionMethod · 0.36
test_partitionsMethod · 0.36
goMethod · 0.36