-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-list.py
More file actions
56 lines (48 loc) · 2.13 KB
/
process-list.py
File metadata and controls
56 lines (48 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
#[x-cmds]: UPDATE
"""Process a list of items and display some statistics."""
from typing import Callable
from xulbux import FormatCodes, Console
ARGS = Console.get_args({
"list_items": "before",
"separator": {"-s", "--sep", "--separator"},
})
def main() -> None:
sep = ARGS.separator.values[0] if ARGS.separator.values else ""
if sep != "":
if not ARGS.list_items.exists:
input_str = input("> ")
else:
input_str = " ".join(ARGS.list_items.values)
lst = [x for x in input_str.split(sep) if x.strip() not in {"", None}]
else:
lst = [str(val) for val in ARGS.list_items.values]
if len(lst) >= 1 and lst[0].strip() not in {"", None}:
FormatCodes.print(f"\n[b|bg:black]([in]( PROCESSED ) {len(lst)} [in]( LIST ENTRIES ))\n")
FormatCodes.print(f"[bright:cyan]{'\n'.join(lst)}[_]\n")
if all(e.isnumeric() for e in lst):
lst = [int(e) if e.replace("_", "").isdigit() else float(e) for e in lst]
average: Callable[[list[int | float]], float] = lambda nums: sum(nums) / len(nums)
Console.log_box_bordered(
f"[b](Min) : [br:cyan]({min(lst)})",
f"[b](Max) : [br:cyan]({max(lst)})",
f"[b](Sum) : [br:cyan]({sum(lst)})",
f"[b](Average) : [br:cyan]({average(lst)})",
)
else:
lst = [str(x) for x in lst]
box_content = f"[b](Unique entries) : {' '.join(f'[br:cyan|bg:black]({e})' for e in sorted(set(lst)))}"
if any(not e.replace("_", "").isdigit() for e in lst):
upper = sum(1 for e in lst if e.isupper())
lower = sum(1 for e in lst if e.islower())
box_content += f"\n[b](Uppercase) : {upper / len(lst) * 100:.1f}%"
box_content += f"\n[b](Lowercase) : {lower / len(lst) * 100:.1f}%"
Console.log_box_bordered(box_content)
print()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()
except Exception as e:
Console.fail(e, start="\n", end="\n\n")