Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
Loading...
Searching...
No Matches
iterables.py
1from typing import Union, Callable, Iterable, Literal
2
3
4# TODO: tests
5def drop_duplicates(iterable, method:Literal['set', 'sorted set', 'generator', 'manual', 'other']='sorted set'):
6 method = method.lower()
7 if method == 'set':
8 return type(iterable)(set(iterable))
9 elif method == 'sorted set':
10 return list(sorted(set(iterable), key=lambda x: iterable.index(x)))
11 elif method == 'generator':
12 seen = set()
13 for item in seq:
14 if item not in seen:
15 seen.add(item)
16 yield item
17 elif method == 'manual':
18 dups = {}
19 newlist = []
20 for x in biglist:
21 if x['link'] not in dups:
22 newlist.append(x)
23 dups[x['link']] = None
24 elif method == 'other':
25 seen_links = set()
26 for index in len(biglist):
27 link = biglist[index]['link']
28 if link in seen_links:
29 del(biglist[index])
30 seen_links.add(link)
31 else:
32 raise ValueError(f'Unknown method {method}. Options are: (set, sorted set, generator, manual, other)')
33
34# TODO: I guess this is useful? Figure out how the dict + operator works first
35def addDicts(*dicts):
36 """ Basically the same thing as Dict.update(), except returns a copy
37 (and can take multiple parameters)
38 """
39 rtn = {}
40 for d in dicts:
41 rtn.update(d)
42 return rtn
43
44# TODO: make this actually work
45class MultiAccessDict(dict):
46 """ Exactly the same thing as a regular dict, except you can get and set multiple items
47 at once, and it returns a list of the asked for items, or sets all of the items to
48 the specified value
49 """
50 def __getitem__(self, *keys):
51 if len(keys) == 0:
52 raise KeyError("No input parameters given")
53 return [super().__getitem__(key) for key in keys]
54
55 def __setitem__(self, *keys, value):
56 pass
57 # todo('figure out how the setitem parameters work')
58 # return [super().__setitem__(key) for key in keys]
Exactly the same thing as a regular dict, except you can get and set multiple items at once,...
Definition: iterables.py:45