snippyts 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
snippyts/__init__.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from doctest import testmod
|
|
2
|
+
from itertools import chain
|
|
3
|
+
from typing import Any, List
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def batched(iterable: List[Any], batch_size: int) -> List[List[Any]]:
|
|
7
|
+
"""
|
|
8
|
+
Partitions an input collection `iterable` into chunks of size `batch_size`.
|
|
9
|
+
The number of chunks is unknown at the time of calling is determined by
|
|
10
|
+
the length of `iterable`.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
iterable: List[Any]
|
|
15
|
+
|
|
16
|
+
batch_size: int
|
|
17
|
+
|
|
18
|
+
Returns
|
|
19
|
+
-------
|
|
20
|
+
List[List[Any]]
|
|
21
|
+
|
|
22
|
+
Examples
|
|
23
|
+
--------
|
|
24
|
+
>>> iterable = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
25
|
+
>>> chunks = batched(iterable, batch_size=2)
|
|
26
|
+
>>> assert len(chunks) == 4
|
|
27
|
+
>>> assert chunks[0] == [1, 2]
|
|
28
|
+
>>> assert chunks[-1] == [7, 8]
|
|
29
|
+
|
|
30
|
+
>>> iterable = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
31
|
+
>>> chunks = batched(iterable, batch_size=12)
|
|
32
|
+
>>> assert len(chunks) == 1
|
|
33
|
+
>>> assert chunks[0] == iterable
|
|
34
|
+
|
|
35
|
+
>>> iterable = [1, 2, 3]
|
|
36
|
+
>>> chunks = batched(iterable, batch_size=2)
|
|
37
|
+
>>> assert chunks == [
|
|
38
|
+
... [1, 2],
|
|
39
|
+
... [3]
|
|
40
|
+
... ]
|
|
41
|
+
|
|
42
|
+
"""
|
|
43
|
+
idxs = list(range(len(iterable)))
|
|
44
|
+
ii = [i for i in idxs[::batch_size]]
|
|
45
|
+
return [iterable[i:i + batch_size] for i in ii]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def flatten_loop(lists):
|
|
49
|
+
flattened = []
|
|
50
|
+
for l in lists:
|
|
51
|
+
flattened.extend(l)
|
|
52
|
+
return flattened
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def flatten_func(lists):
|
|
56
|
+
return list(chain(*lists))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def flatten(lists: List[List[Any]]) -> List[Any]:
|
|
60
|
+
"""
|
|
61
|
+
Given a collection of lists, concatenates all elements into a single list.
|
|
62
|
+
|
|
63
|
+
More formally, given a collection holding `n` iterables with `m` elements
|
|
64
|
+
each, this function will return a single list holding all `n * m` elements.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
List[List[Any]]
|
|
69
|
+
|
|
70
|
+
Returns
|
|
71
|
+
-------
|
|
72
|
+
List[Any]
|
|
73
|
+
|
|
74
|
+
Examples
|
|
75
|
+
--------
|
|
76
|
+
>>> example = [[1, 2, 3], [1], [2, 4, 6], [3, 6, 9], [7, 13]]
|
|
77
|
+
>>> len_example = sum(len(l) for l in example)
|
|
78
|
+
|
|
79
|
+
>>> assert len_example == len(flatten(example))
|
|
80
|
+
>>> assert len_example == len(flatten_func(example))
|
|
81
|
+
>>> assert len_example == len(flatten_loop(example))
|
|
82
|
+
|
|
83
|
+
>>> assert flatten(example) == flatten_func(example)
|
|
84
|
+
|
|
85
|
+
>>> assert flatten(example) == flatten_loop(example)
|
|
86
|
+
"""
|
|
87
|
+
return [e for l in lists for e in l]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
if __name__ == '__main__':
|
|
92
|
+
testmod()
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: snippyts
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Author-email: Jordi Carrera Ventura <jordi.carrera.ventura@gmail.com>
|
|
5
|
+
Project-URL: GitHub repository, https://github.com/JordiCarreraVentura/snippyts
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# snippyts
|
|
12
|
+
|
|
13
|
+
# Table of objects
|
|
14
|
+
|
|
15
|
+
| No. | Name | Description | Date added | Date reviewed |
|
|
16
|
+
| --- | --- | --- | --- | --- |
|
|
17
|
+
| 1 | `snippyts.__init__.batched` | Partitions an input collection `iterable` into chunks of size `batch_size`. The number of chunks is unknown at the time of calling is determined by the length of `iterable`. | September 22nd, 2024 | September 22nd, 2024 |
|
|
18
|
+
| 2 | `snippyts.__init__.flatten` | Given a collection of lists, concatenates all elements into a single list. More formally, given a collection holding `n` iterables with `m` elements each, this function will return a single list holding all `n * m` elements. | September 22nd, 2024 | September 22nd, 2024 |
|
|
19
|
+
| 3 | `create_python_simple_package.sh` | BASH script to initialize a local Python package as a local git repository with a virtual environment, project files, and standard folder structure. It takes user input into account for parameterization from the command line. | September 22nd, 2024 | September 23rd, 2024 |
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
snippyts/__init__.py,sha256=d2j1fZBjevgy1t9xZryMXhiy5sqhZx3cKvqFE94o33E,2231
|
|
2
|
+
snippyts-0.0.1.dist-info/METADATA,sha256=e8ebmV79LnY-JpOImP30KoYrSEWoOGfqyTfY-S8G3zI,1367
|
|
3
|
+
snippyts-0.0.1.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
4
|
+
snippyts-0.0.1.dist-info/top_level.txt,sha256=1g5QGM1gvR1PIQVYfhdEx4SHgOH1e5YTQdEXPYJ4RQk,9
|
|
5
|
+
snippyts-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
snippyts
|