bettergroups 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.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: bettergroups
3
+ Version: 0.1.0
4
+ Summary: Lightweight enhanced list and dictionary utilities.
5
+ Author: Finni
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.7
9
+ Description-Content-Type: text/markdown
10
+
11
+ # bettergroups
12
+
13
+ Lightweight enhanced list and dictionary utilities for Python.
14
+
15
+ ## Features
16
+
17
+ - BetterLists
18
+ - BetterDicts
19
+ - groupby
20
+ - chunk
21
+ - Lockable structures
22
+ - Map / Filter helpers
23
+
24
+ ## Installation
25
+
26
+ pip install bettergroups
@@ -0,0 +1,16 @@
1
+ # bettergroups
2
+
3
+ Lightweight enhanced list and dictionary utilities for Python.
4
+
5
+ ## Features
6
+
7
+ - BetterLists
8
+ - BetterDicts
9
+ - groupby
10
+ - chunk
11
+ - Lockable structures
12
+ - Map / Filter helpers
13
+
14
+ ## Installation
15
+
16
+ pip install bettergroups
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: bettergroups
3
+ Version: 0.1.0
4
+ Summary: Lightweight enhanced list and dictionary utilities.
5
+ Author: Finni
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.7
9
+ Description-Content-Type: text/markdown
10
+
11
+ # bettergroups
12
+
13
+ Lightweight enhanced list and dictionary utilities for Python.
14
+
15
+ ## Features
16
+
17
+ - BetterLists
18
+ - BetterDicts
19
+ - groupby
20
+ - chunk
21
+ - Lockable structures
22
+ - Map / Filter helpers
23
+
24
+ ## Installation
25
+
26
+ pip install bettergroups
@@ -0,0 +1,7 @@
1
+ README.md
2
+ bettergroups.py
3
+ pyproject.toml
4
+ bettergroups.egg-info/PKG-INFO
5
+ bettergroups.egg-info/SOURCES.txt
6
+ bettergroups.egg-info/dependency_links.txt
7
+ bettergroups.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ bettergroups
@@ -0,0 +1,295 @@
1
+ import collections as clc # if this fails your cooked
2
+
3
+ # btw 300 lines ain't that bad
4
+
5
+ # made by Finni
6
+
7
+ __all__ = ['BetterLists', 'Groups', 'BetterDicts', 'groupby', 'chunk']
8
+
9
+ __version__ = "0.1.0"
10
+
11
+ def groupby(iterable, key_func):
12
+ result = {}
13
+ for item in iterable:
14
+ key = key_func(item)
15
+ result.setdefault(key, []).append(item)
16
+ return result
17
+
18
+ def chunk(iterable, size):
19
+ return [iterable[i:i+size] for i in range(0, len(iterable), size)]
20
+
21
+ class BetterDicts:
22
+ def __init__(self, d=None):
23
+ if d is None: # check if input was made, if not keep the list empty
24
+ self.data = {}
25
+ else: # if there is input for the list make the input
26
+ self.data = dict(d)
27
+ self.locked = False
28
+ # self.original = self.data
29
+
30
+ def add(self, name, value):
31
+ '''add to a dictionary'''
32
+ if self.locked:
33
+ raise RuntimeError("Object is locked")
34
+ self.data[name] = value
35
+
36
+ def remove(self, name):
37
+ '''remove from a dictionary'''
38
+ if self.locked:
39
+ raise RuntimeError("Object is locked")
40
+ del self.data[name]
41
+
42
+ def pop(self, key, default=None):
43
+ '''pop something from a dictionary'''
44
+ if self.locked:
45
+ raise RuntimeError("Object is locked")
46
+ return self.data.pop(key, default)
47
+
48
+ def invert(self):
49
+ '''returns an inverted version of itself'''
50
+ return BetterDicts({v: k for k, v in self.data.items()})
51
+
52
+ def dupe(self):
53
+ '''duplicate a dictionary'''
54
+ return BetterDicts(self.data.copy())
55
+
56
+ def combine(self, other):
57
+ '''combine two dictionarys'''
58
+ if isinstance(other, BetterDicts):
59
+ other = other.data
60
+ elif not isinstance(other, dict):
61
+ raise TypeError("Can only combine with dict or BetterDicts")
62
+
63
+ new_data = self.data.copy()
64
+
65
+ for key, value in other.items():
66
+ if key in new_data:
67
+ if isinstance(new_data[key], list):
68
+ new_data[key].append(value)
69
+ else:
70
+ new_data[key] = [new_data[key], value]
71
+ else:
72
+ new_data[key] = value
73
+
74
+ return BetterDicts(new_data)
75
+
76
+ def get(self, keyname):
77
+ '''return the value of a key'''
78
+ return self.data.get(keyname)
79
+
80
+ # def _default(self):
81
+ # '''Reset a dictionary to the first time it was made'''
82
+ # self.data.clear()
83
+ # self.data = self.original
84
+
85
+ def empty(self):
86
+ '''Empty everything inside a dictionary'''
87
+ if self.locked:
88
+ raise RuntimeError("Object is locked")
89
+ self.data.clear()
90
+
91
+ def copy(self):
92
+ '''return a copy of itself'''
93
+ return BetterDicts(self.data.copy())
94
+
95
+ def map_values(self, func):
96
+ return BetterDicts({k: func(v) for k, v in self.data.items()})
97
+
98
+ def filter(self, func):
99
+ return BetterDicts({k: v for k, v in self.data.items() if func(k, v)})
100
+
101
+ def keys(self):
102
+ return self.data.keys()
103
+
104
+ def values(self):
105
+ return self.data.values()
106
+
107
+ def items(self):
108
+ return self.data.items()
109
+
110
+ def lock(self):
111
+ '''make an object uneditable'''
112
+ self.locked = True
113
+
114
+ def unlock(self):
115
+ '''make an object editable'''
116
+ self.locked = False
117
+
118
+ # not gonna go over the dunder methods
119
+ def __str__(self):
120
+ return str(self.data)
121
+ def __repr__(self):
122
+ return f"BetterDicts(data:{self.data},locked:{self.locked})"
123
+ def __len__(self):
124
+ return len(self.data)
125
+ def __getitem__(self, key):
126
+ return self.data[key]
127
+ def __setitem__(self, key, value):
128
+ if self.locked:
129
+ raise RuntimeError("Object is locked")
130
+ self.data[key] = value
131
+ def __delitem__(self, key):
132
+ if self.locked:
133
+ raise RuntimeError("Object is locked")
134
+ del self.data[key]
135
+ def __contains__(self, key):
136
+ return key in self.data
137
+ def __iter__(self):
138
+ return iter(self.data)
139
+ def __eq__(self, other):
140
+ if isinstance(other, BetterDicts):
141
+ return self.data == other.data
142
+ if isinstance(other, dict):
143
+ return self.data == other
144
+ return False
145
+ def __bool__(self):
146
+ return bool(self.data)
147
+ def __or__(self, other):
148
+ return self.combine(other)
149
+
150
+ class BetterLists:
151
+ def __init__(self, d=None):
152
+ if d is None: # check if input was made, if not keep the list empty
153
+ self.data = []
154
+ else: # if there is input for the list make the input
155
+ self.data = list(d)
156
+ self.locked = False
157
+ def add(self, d):
158
+ '''add something to a list'''
159
+ if self.locked:
160
+ raise RuntimeError("Object is locked")
161
+ self.data.append(d)
162
+
163
+ def remove(self,d):
164
+ '''remove something from a list'''
165
+ if self.locked:
166
+ raise RuntimeError("Object is locked")
167
+ self.data.remove(d)
168
+
169
+ def dupe(self):
170
+ '''copy a list to a new list'''
171
+ return BetterLists(self.data.copy())
172
+
173
+ def sorted(self, reverse=False):
174
+ '''return a sorted version of a list'''
175
+ return BetterLists(sorted(self.data, reverse=reverse))
176
+
177
+ def combine(self, other):
178
+ '''return a list combined with another list'''
179
+ if isinstance(other, BetterLists):
180
+ return BetterLists(self.data + other.data)
181
+ raise TypeError("Must combine with another BetterLists")
182
+
183
+ def insert(self,d,pos):
184
+ '''insert data to a position of a list'''
185
+ if self.locked:
186
+ raise RuntimeError("Object is locked")
187
+ self.data.insert(pos,d)
188
+
189
+ def count(self, char):
190
+ '''count the instances of a character'''
191
+ return self.data.count(char)
192
+
193
+ def countinfo(self, char):
194
+ '''count the instances of a character with extra info'''
195
+ return f'character: {char}, shows {self.data.count(char)} times'
196
+
197
+ def showtype(self, t):
198
+ '''return all instances of a certant type in a list'''
199
+ returns = []
200
+
201
+ if not isinstance(t, type): # check if t is a type
202
+ raise TypeError('Expected a type')
203
+
204
+ for i in range(len(self.data)):
205
+ if isinstance(self.data[i], t):
206
+ returns.append(self.data[i])
207
+
208
+ return returns
209
+
210
+ def showstrs(self):
211
+ '''return all the strings inside of a list'''
212
+ return self.showtype(str)
213
+
214
+ def showints(self):
215
+ '''return all the ints inside of a list'''
216
+ return self.showtype(int)
217
+
218
+ def showlists(self):
219
+ '''return all the lists inside of a list'''
220
+ return self.showtype(list)
221
+
222
+ def showdicts(self):
223
+ '''return all the dicts inside of a list'''
224
+ return self.showtype(dict)
225
+
226
+ def countall(self):
227
+ '''count everything inside of a list'''
228
+ return clc.Counter(self.data)
229
+
230
+ def empty(self):
231
+ '''clear everything inside a list'''
232
+ if self.locked:
233
+ raise RuntimeError("Object is locked")
234
+ self.data.clear()
235
+
236
+ def deldupes(self):
237
+ '''Deletes all duplicates in a list'''
238
+ if not self.locked:
239
+ return BetterLists(dict.fromkeys(self.data)) # dictionarys can't have duplicate keys
240
+ raise RuntimeError("Object is locked")
241
+
242
+ def extend(self, other):
243
+ '''add elements from an iterable'''
244
+ if self.locked:
245
+ raise RuntimeError("Object is locked")
246
+ if isinstance(other, BetterLists):
247
+ other = other.data
248
+ if isinstance(other, BetterDicts):
249
+ other = other.data
250
+ self.data.extend(other)
251
+
252
+ def filter(self, func):
253
+ return BetterLists([x for x in self.data if func(x)])
254
+
255
+ def map(self, func):
256
+ return BetterLists([func(x) for x in self.data])
257
+
258
+ def first(self):
259
+ '''return the first item in a list'''
260
+ return self.data[0] if self.data else None
261
+
262
+ def last(self):
263
+ '''return the last item in a list'''
264
+ return self.data[-1] if self.data else None
265
+
266
+ def lock(self):
267
+ '''make an object uneditable'''
268
+ self.locked = True
269
+
270
+ def unlock(self):
271
+ '''make an object editable'''
272
+ self.locked = False
273
+
274
+ # dunder methods im not gonna explain
275
+ def __str__(self):
276
+ return f'{self.data}'
277
+ def __len__(self):
278
+ return len(self.data)
279
+ def __getitem__(self, index):
280
+ return self.data[index]
281
+ def __iter__(self):
282
+ return iter(self.data)
283
+ def __contains__(self, item):
284
+ return item in self.data
285
+ def __repr__(self):
286
+ return f"BetterLists(data:{self.data},locked:{self.locked})"
287
+ def __bool__(self):
288
+ return bool(self.data)
289
+ def __add__(self, other):
290
+ if isinstance(other, BetterLists):
291
+ return BetterLists(self.data + other.data)
292
+ raise TypeError
293
+
294
+ if __name__ == "__main__":
295
+ pass
@@ -0,0 +1,18 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "bettergroups"
7
+ version = "0.1.0"
8
+ description = "Lightweight enhanced list and dictionary utilities."
9
+ readme = "README.md"
10
+ requires-python = ">=3.7"
11
+ authors = [
12
+ {name = "Finni"}
13
+ ]
14
+
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Operating System :: OS Independent",
18
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+