Create a dask Bag from Python sequence. This sequence should be relatively small in memory. Dask Bag works best when it handles loading your data itself. Commonly we load a sequence of filenames into a Bag and then use ``.map`` to open them. Parameters ---------- seq: Ite
(seq, partition_size=None, npartitions=None)
| 1763 | |
| 1764 | |
| 1765 | def from_sequence(seq, partition_size=None, npartitions=None): |
| 1766 | """Create a dask Bag from Python sequence. |
| 1767 | |
| 1768 | This sequence should be relatively small in memory. Dask Bag works |
| 1769 | best when it handles loading your data itself. Commonly we load a |
| 1770 | sequence of filenames into a Bag and then use ``.map`` to open them. |
| 1771 | |
| 1772 | Parameters |
| 1773 | ---------- |
| 1774 | seq: Iterable |
| 1775 | A sequence of elements to put into the dask |
| 1776 | partition_size: int (optional) |
| 1777 | The length of each partition |
| 1778 | npartitions: int (optional) |
| 1779 | The number of desired partitions |
| 1780 | |
| 1781 | It is best to provide either ``partition_size`` or ``npartitions`` |
| 1782 | (though not both.) |
| 1783 | |
| 1784 | Examples |
| 1785 | -------- |
| 1786 | >>> import dask.bag as db |
| 1787 | >>> b = db.from_sequence(['Alice', 'Bob', 'Chuck'], partition_size=2) |
| 1788 | |
| 1789 | See Also |
| 1790 | -------- |
| 1791 | read_text: Create bag from text files |
| 1792 | """ |
| 1793 | seq = list(seq) |
| 1794 | if npartitions and not partition_size: |
| 1795 | if len(seq) <= 100: |
| 1796 | partition_size = int(math.ceil(len(seq) / npartitions)) |
| 1797 | else: |
| 1798 | partition_size = max(1, int(math.floor(len(seq) / npartitions))) |
| 1799 | if npartitions is None and partition_size is None: |
| 1800 | if len(seq) <= 100: |
| 1801 | partition_size = 1 |
| 1802 | else: |
| 1803 | partition_size = max(1, math.ceil(math.sqrt(len(seq)) / math.sqrt(100))) |
| 1804 | |
| 1805 | parts = list(partition_all(partition_size, seq)) |
| 1806 | token = tokenize(seq, partition_size) |
| 1807 | name = f"from_sequence-{token}" |
| 1808 | if len(parts) > 0: |
| 1809 | d = {(name, i): list(part) for i, part in enumerate(parts)} |
| 1810 | else: |
| 1811 | d = {(name, 0): []} |
| 1812 | |
| 1813 | return Bag(d, name, len(d)) |
| 1814 | |
| 1815 | |
| 1816 | def from_url(urls): |