iterlab 0.1.0__tar.gz

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.
iterlab-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Zachary Einck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
iterlab-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,17 @@
1
+ Metadata-Version: 2.1
2
+ Name: iterlab
3
+ Version: 0.1.0
4
+ Summary: Collection of helper functions that perform operations on iterables.
5
+ License: MIT
6
+ Author: Zachary Einck
7
+ Author-email: zacharyeinck@gmail.com
8
+ Requires-Python: >=3.11,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Description-Content-Type: text/markdown
14
+
15
+ # iterlab
16
+
17
+ Collection of helper functions that perform operations on iterables.
@@ -0,0 +1,3 @@
1
+ # iterlab
2
+
3
+ Collection of helper functions that perform operations on iterables.
@@ -0,0 +1,4 @@
1
+ from .iterlab import *
2
+
3
+ __version__ = '0.1.0'
4
+ __author__ = 'Zachary T. Einck'
@@ -0,0 +1,143 @@
1
+ import re
2
+
3
+
4
+
5
+ #+---------------------------------------------------------------------------+
6
+ # Freestanding functions
7
+ #+---------------------------------------------------------------------------+
8
+
9
+ def natural_sort(array, inplace=False):
10
+ '''
11
+ Description
12
+ ----------
13
+ Sorts list via natural sorting as opposed to lexicographical sorting. For example, consider a
14
+ list of file names starting with numbers ['101 Dalmatians.xlsx', '3 Blind Mice.xlsx']. Sorting
15
+ using the default method will yield the same list because the first character "1" < "3". Conversely,
16
+ natural sorting will intuitively place the name beginning with "3" before "101" since "3" < "101".
17
+
18
+ Parameters
19
+ ----------
20
+ array : list
21
+ list to sort
22
+ inplace : bool
23
+ if True, sorting will be done inplace
24
+
25
+ Returns
26
+ ----------
27
+ out : list | None
28
+ list if inplace is False otherwise None
29
+ '''
30
+
31
+ def alphanumeric_key(element):
32
+ ''' converts string into a list of string and number chunks (e.g. 'z23a' -> ['z', 23, 'a']) '''
33
+
34
+ def try_int(x):
35
+ try:
36
+ return int(x)
37
+ except:
38
+ return x
39
+
40
+ out = list(map(try_int, re.split('([0-9]+)', str(element))))
41
+ return out
42
+
43
+ if inplace:
44
+ array.sort(key=alphanumeric_key)
45
+ else:
46
+ return sorted(array, key=alphanumeric_key)
47
+
48
+
49
+
50
+ def iter_get(array, index=0, default=None):
51
+ ''' extends dictionary's .get() to other iterables such as lists '''
52
+ if array is None: return
53
+ try:
54
+ return array[index]
55
+ except IndexError:
56
+ return default
57
+
58
+
59
+
60
+ def to_iter(x):
61
+ ''' converts variable to a list '''
62
+ return x if isinstance(x, (list, tuple)) else [x]
63
+
64
+
65
+
66
+ def delimit_iter(x, typ=str, delimiter=', ', encase=True):
67
+ x = [str(typ(z)) for z in x]
68
+ if typ == str and encase: x = ["'%s'" % z for z in x]
69
+ return delimiter.join(x)
70
+
71
+
72
+
73
+ def lower_iter(iterable):
74
+ return [x.lower() for x in iterable]
75
+
76
+
77
+
78
+ def text_to_iter(text, transform=str, delimiter='\n'):
79
+ '''
80
+ Description
81
+ ----------
82
+ Converts a list of items in text format to a Python list.
83
+
84
+ Parameters
85
+ ----------
86
+ text : str
87
+ Delimited string
88
+ transform : func
89
+ Function applied to each item as list is constructed
90
+ delimiter : str
91
+ text delimiter
92
+
93
+ Returns
94
+ ----------
95
+ out : list
96
+ list if items in text file
97
+ '''
98
+ return [transform(x.strip()) for x in text.split(delimiter) if x.strip()]
99
+
100
+
101
+
102
+ def iter_window(x, left=0, right=0, strict=False, step=False, include_index=False):
103
+ '''
104
+ iterates over a window of values in a list
105
+
106
+ Parameters
107
+ ----------
108
+ x : list | other iterable
109
+ list to iterate over
110
+ left : int
111
+ how many indices to the left of the current index to include in each returned slice
112
+ right : int
113
+ how many indices to the right of the current index to include in each returned slice
114
+ strict : bool
115
+ if True, only complete slices will be returned. (e.g. x=[1,2,3,4,5] and left=2 the first
116
+ slice returned will be [1,2,3] at index 2. Index 0 and 1 would be partial slices and not returned.
117
+ step : bool
118
+ if True, the right-most index of a slice will define the boundary line for the next slice so that
119
+ no values repeat. (e.g. x=[1,2,3,4,5] and right=2 will return slices [1,2,3] and [4,5])
120
+ include_index : bool
121
+ if True, the index of the slice is also returned
122
+ '''
123
+ n, start = len(x) - 1, -1
124
+ for i,v in enumerate(x):
125
+ a, b = max(0, i - left), min(n, i + right)
126
+ #print('i =', i, 'v =', v, 'start again =', start, 'a =', a, 'b =', b)
127
+ if step:
128
+ if left > 0 and i == n: a = max(start, 0)
129
+ if a < start: continue
130
+ subset = x[ a : b + 1 ]
131
+ #print('subset =',subset,a,b+1)
132
+ if strict and len(subset) < left + right + 1: continue
133
+ start = b + 1
134
+ #color.print(str(subset),'o')
135
+ yield (i, subset) if include_index else subset
136
+
137
+
138
+
139
+
140
+
141
+ if __name__ == '__main__':
142
+ for x in iter_window([0,1,3,4], left=1, strict=True, step=True):
143
+ print('x =', x)
@@ -0,0 +1,15 @@
1
+ [tool.poetry]
2
+ name = "iterlab"
3
+ version = "0.1.0"
4
+ description = "Collection of helper functions that perform operations on iterables."
5
+ authors = ["Zachary Einck <zacharyeinck@gmail.com>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+
9
+ [tool.poetry.dependencies]
10
+ python = "^3.11"
11
+
12
+
13
+ [build-system]
14
+ requires = ["poetry-core"]
15
+ build-backend = "poetry.core.masonry.api"