-
Notifications
You must be signed in to change notification settings - Fork 0
/
ils.py
1301 lines (998 loc) · 42.1 KB
/
ils.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Collection of utility functions.
Author: Simon M. Hofmann | <[firstname].[lastname][ät]pm.me> | 2023
"""
# %% Imports
from __future__ import annotations
import difflib
import fileinput
import gc
import gzip
import math
import os
import pickle # noqa: S403
import platform
import re
import subprocess # noqa: S404
import sys
import warnings
from datetime import datetime, timedelta
from functools import wraps
from pathlib import Path, PosixPath
from typing import TYPE_CHECKING, Any, ClassVar, Generator, Sequence
import numpy as np
import psutil
import requests
if TYPE_CHECKING:
from collections.abc import Callable
import numpy.typing as npt
# %% Paths < o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
class DisplayablePath:
"""
Build DisplayablePath class for tree().
With honourable mention to 'abstrus':
https://stackoverflow.com/questions/9727673/list-directory-tree-structure-in-python
Note:
----
* This uses recursion. It will raise a RecursionError on really deep folder trees
* The tree is lazily evaluated. It should behave well on really wide folder trees.
Immediate children of a given folder are not lazily evaluated, though.
"""
display_filename_prefix_middle = "├──"
display_filename_prefix_last = "└──"
display_parent_prefix_middle = " "
display_parent_prefix_last = "│ "
def __init__(self, path: str | Path, parent_path: str | Path | DisplayablePath, is_last: bool) -> None:
"""Initialise DisplayablePath object."""
self.path = Path(str(path))
self.parent = parent_path
self.is_last = is_last
if self.parent:
self.depth = self.parent.depth + 1
else:
self.depth = 0
@property
def display_name(self) -> str:
"""Display path name."""
if self.path.is_dir():
return self.path.name + "/"
return self.path.name
@classmethod
def make_tree(
cls,
root: str | Path,
parent: str | Path | DisplayablePath | None = None,
is_last: bool = False,
criteria: bool | None = None,
) -> list[DisplayablePath]:
"""Display the file tree starting with given root."""
root = Path(str(root))
criteria = criteria or cls._default_criteria
displayable_root = cls(root, parent, is_last)
yield displayable_root
children = sorted([path for path in root.iterdir() if criteria(path)], key=lambda s: str(s).lower())
count = 1
for path in children:
is_last = count == len(children)
if path.is_dir():
yield from cls.make_tree(path, parent=displayable_root, is_last=is_last, criteria=criteria)
else:
yield cls(path, displayable_root, is_last)
count += 1
@classmethod
def _default_criteria(cls, path: str | Path) -> bool: # noqa: ARG003
return True
def displayable(self) -> str:
"""Provide paths which can be displayed."""
if self.parent is None:
return self.display_name
_filename_prefix = self.display_filename_prefix_last if self.is_last else self.display_filename_prefix_middle
parts = [f"{_filename_prefix!s} {self.display_name!s}"]
parent = self.parent
while parent and parent.parent is not None:
parts.append(self.display_parent_prefix_middle if parent.is_last else self.display_parent_prefix_last)
parent = parent.parent
return "".join(reversed(parts))
def tree(directory: str | Path) -> None:
"""
Generate tree of given directory.
Use this function the same way as the shell command `tree`.
This leads to outputs such as:
directory/
├── _static/
│ ├── embedded/
│ │ ├── deep_file
│ │ └── very/
│ │ └── deep/
│ │ └── folder/
│ │ └── very_deep_file
│ └── less_deep_file
├── about.rst
├── conf.py
└── index.rst
"""
paths = DisplayablePath.make_tree(Path(directory))
for path in paths:
print(path.displayable())
def find(
fname: str,
folder: str = ".",
typ: str = "file",
exclusive: bool = True,
fullname: bool = True,
abs_path: bool = False,
verbose: bool = False,
) -> str | list[str] | None:
"""
Find file(s) or folder(s) in given folder.
:param fname: full filename OR consecutive part of it
:param folder: root folder to search
:param typ: 'file' or folder 'dir'
:param exclusive: only return path when only one file was found
:param fullname: True: consider only files which exactly match the given fname
:param abs_path: False: return relative path(s); True: return absolute path(s)
:param verbose: Report findings
:return: path to file OR list of paths, OR None
"""
ctn_found = 0
findings = []
for root, dirs, files in os.walk(folder):
search_in = files if typ.lower() == "file" else dirs
for f in search_in:
if (fname == f) if fullname else (fname in f):
ffile = str(Path(root, f)) # found file
if abs_path:
ffile = str(Path(ffile).resolve())
findings.append(ffile)
ctn_found += 1
if exclusive and len(findings) > 1:
if verbose:
cprint(string=f"\nFound several {typ}s for given fname='{fname}', please specify:", col="y")
print("", *findings, sep="\n\t>> ")
return None
if not exclusive and len(findings) > 1:
if verbose:
cprint(string=f"\nFound several {typ}s for given fname='{fname}', return list of {typ} paths", col="y")
return findings
if len(findings) == 0:
if verbose:
cprint(string=f"\nDid not find any {typ} for given fname='{fname}', return None", col="y")
return None
if verbose:
cprint(string=f"\nFound this {typ}: '{findings[0]}'", col="y")
return findings[0]
# %% Timer < o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def chop_microseconds(delta: timedelta) -> timedelta:
"""Chop microseconds from given time delta."""
return delta - timedelta(microseconds=delta.microseconds)
def function_timed(dry_funct: Callable[..., Any] | None = None, ms: bool | None = None) -> Callable[..., Any]:
"""
Time the processing duration of wrapped function.
Way to use:
The following returns the duration without micro-seconds:
@function_timed
def abc():
return 2+2
The following returns micro-seconds as well:
@function_timed(ms=True)
def abcd():
return 2+2
:param dry_funct: parameter can be ignored. Results in output without micro-seconds
:param ms: if micro-seconds should be printed, set to True
:return:
"""
def _function_timed(funct: Callable[..., Any]) -> Callable[..., Any]:
@wraps(funct)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrap function to be timed."""
start_timer = datetime.now()
# whether to suppress wrapper: use functimer=False in main funct
w = kwargs.pop("functimer", True)
output = funct(*args, **kwargs)
duration = datetime.now() - start_timer
if w:
if ms:
print(f"\nProcessing time of {funct.__name__}: {duration} [h:m:s:ms]")
else:
print(f"\nProcessing time of {funct.__name__}: {chop_microseconds(duration)} [h:m:s]")
return output
return wrapper
if dry_funct:
return _function_timed(dry_funct)
return _function_timed
def loop_timer(
start_time: datetime, loop_length: int, loop_idx: int, loop_name: str | None = None, add_daytime: bool = False
) -> None:
"""
Estimate the remaining time to run through given loop.
Function must be placed at the end of the loop inside.
Before the loop, take the start time by start_time=datetime.now()
Provide position within in the loop via enumerate()
In the form:
'
start = datetime.now()
for idx, ... in enumerate(iterable):
... operations ...
loop_timer(start_time=start, loop_length=len(iterable), loop_idx=idx)
'
:param start_time: time at the start of the loop
:param loop_length: total length of iterable
:param loop_idx: position within the loop
:param loop_name: name the loop for print-out
:param add_daytime: add leading day time to print-out
"""
_idx = loop_idx
ll = loop_length
duration = datetime.now() - start_time
rest_duration = chop_microseconds(duration / (_idx + 1) * (ll - _idx - 1))
loop_name = "" if loop_name is None else " of " + loop_name
now_time = f"{datetime.now().replace(microsecond=0)} | " if add_daytime else ""
string = (
f"{now_time}Estimated time to loop over rest{loop_name}: {rest_duration} [hh:mm:ss]\t "
f"[ {'*' * int((_idx + 1) / ll * 30)}{'.' * (30 - int((_idx + 1) / ll * 30))} ] "
f"{(_idx + 1) / ll * 100:.2f} %"
)
print(string, "\r" if (_idx + 1) != ll else "\n", end="")
if (_idx + 1) == ll:
cprint(
string=f"{now_time}Total duration of loop{loop_name.split(' of')[-1]}: "
f"{chop_microseconds(duration)} [hh:mm:ss]\n",
col="b",
)
def average_time(list_of_timestamps: list, in_timedelta: bool = True) -> float | timedelta:
"""
Compute average time in a list of time-stamps.
Necessary for Python 2.
In Python3 do: np.mean([timedelta(0, 20), ..., timedelta(0, 32)])
:param list_of_timestamps: list of time-stamps
:param in_timedelta: whether to return in timedelta-format.
:return: average time
"""
mean_time = sum(list_of_timestamps, timedelta()).total_seconds() / len(list_of_timestamps)
if in_timedelta:
mean_time = timedelta(seconds=mean_time)
return mean_time
def try_funct(funct: Callable[..., Any]) -> Callable[..., Any]:
"""
Try wrapped function, if exception: tell user, but continue.
Usage:
@try_funct
def abc(a, b, c):
return a+b+c
# this runs normally
abc(1, 2, 3)
# this catches the exception, and continues nonetheless
abc(1, "no int", 3)
"""
@wraps(funct)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrap function to be tried."""
try:
return funct(*args, **kwargs) # == function()
except Exception: # noqa: BLE001
cprint(string=f"Function {funct.__name__} couldn't be successfully executed!", col="r")
return wrapper
# %% Normalizer & numerics o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def normalize(
array: npt.ArrayLike,
lower_bound: float,
upper_bound: float,
global_min: float | None = None,
global_max: float | None = None,
) -> npt.ArrayLike:
"""
Min-Max-Scaling: Normalizes the input-array to lower and upper bound.
:param array: To be transformed array
:param lower_bound: lower-bound a
:param upper_bound: upper-bound b
:param global_min: if the array is part of a larger tensor, normalize w.r.t. global min and ...
:param global_max: ... global max (i.e., tensor min/max)
:return: normalized array
"""
if not lower_bound < upper_bound:
msg = "lower_bound must be < upper_bound"
raise AssertionError(msg)
array = np.array(array)
a, b = lower_bound, upper_bound
if global_min is not None:
if global_min > np.nanmin(array):
msg = "global_min must be <= np.nanmin(array)"
raise AssertionError(msg)
mini = global_min
else:
mini = np.nanmin(array)
if global_max is not None:
if global_max < np.nanmax(array):
msg = "global_max must be >= np.nanmax(array)"
raise AssertionError(msg)
maxi = global_max
else:
maxi = np.nanmax(array)
return (b - a) * ((array - mini) / (maxi - mini)) + a
def denormalize(
array: npt.ArrayLike, denorm_minmax: tuple[float, float], norm_minmax: tuple[float, float]
) -> npt.ArrayLike:
"""
Undo normalization of given array back to previous scaling.
:param array: array to be denormalized
:param denorm_minmax: tuple of (min, max) of the denormalized (target) vector
:param norm_minmax: tuple of (min, max) of the normalized vector
:return: denormalized value
"""
array = np.array(array)
dn_min, dn_max = denorm_minmax
n_min, n_max = norm_minmax
if not n_min < n_max:
msg = "norm_minmax must be tuple (min, max), where min < max"
raise AssertionError(msg)
if not dn_min < dn_max:
msg = "denorm_minmax must be tuple (min, max), where min < max"
raise AssertionError(msg)
de_normed_array = (array - n_min) / (n_max - n_min) * (dn_max - dn_min) + dn_min
return np.array(de_normed_array)
def z_score(array: npt.ArrayLike) -> npt.ArrayLike:
"""
Create z-score of the given array.
:return: z-score array
"""
sub_mean = np.nanmean(array)
sub_std = np.nanstd(array)
z_array = (array - sub_mean) / sub_std
return np.array(z_array)
def get_factors(n: int) -> list[int]:
"""Get factors of given integer."""
return [i for i in range(1, n + 1) if n % i == 0]
def oom(number: float) -> float:
"""Return order of magnitude of given number."""
# TODO: what about 10**-x # noqa: FIX002
return math.floor(math.log(number, 10))
# %% Sorter o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def natural_sort(list_to_sort: list[str] | tuple[str] | Sequence[str] | Generator[str]) -> list[str]:
"""
Sort a list naturally.
For instance:
["Head34", "Head100", "Head8"] -> ["Head8", "Head34", "Head100"]
Source: https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort
:param list_to_sort: List to sort.
"""
convert = lambda text: int(text) if text.isdigit() else text.lower() # noqa: E731
alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)] # noqa: E731
return sorted(list_to_sort, key=alphanum_key)
def sort_row_by_row(mat: npt.NDArray[Any], mat_idx: npt.NDArray[int]) -> npt.NDArray[Any]:
"""
Sort matrix with an index-matrix row by row.
mat mat_idx sorted mat
[[1,2,3], [[1,2,0], ==> [[2,3,1],
[4,5,6]] [2,0,1]] ==> [6,4,5]]
:param mat: matrix to be sorted by rows of mat_idx
:param mat_idx: matrix with corresponding indices
:return: sorted matrix
"""
mat_idx = mat_idx.astype(int)
if mat.shape != mat_idx.shape:
msg = "Matrices must have the same shape!"
raise AssertionError(msg)
n_rows = mat.shape[0]
sorted_mat = np.zeros(shape=mat.shape)
for row in range(n_rows):
sorted_mat[row, :] = mat[row, :][mat_idx[row, :]]
return sorted_mat
def inverse_sort_row_by_row(mat: npt.NDArray[Any], mat_idx: npt.NDArray[int]) -> npt.NDArray[Any]:
"""
Inverse-sort matrix with an index-matrix row by row.
mat mat_idx sorted mat
[[2,3,1], [[1,2,0], ==> [[1,2,3],
[6,4,5]] [2,0,1]] ==> [4,5,6]]
:param mat: matrix to be sorted by rows of mat_idx
:param mat_idx: matrix with corresponding inverse indices indicating the original order
:return: sorted matrix
"""
mat_idx = mat_idx.astype(int)
if mat.shape != mat_idx.shape:
msg = "Matrices must have the same shape!"
raise AssertionError(msg)
n_rows = mat.shape[0]
sorted_mat = np.zeros(shape=mat.shape)
for row in range(n_rows):
sorted_mat[row, :] = inverse_indexing(arr=mat[row, :], idx=mat_idx[row, :])
return sorted_mat
def inverse_indexing(arr: npt.NDArray[Any], idx: list[int] | npt.NDArray[np.int_]) -> npt.NDArray[Any]:
"""
Inverse indexing of the given array.
For instance, inverse array [16., 2., 4.] to its origin [2., 4., 16.].
For this we need the index-vector [2, 0, 1].
(Note, this is different from using: [16., 2., 4.][[1, 2, 0]].)
:param arr: altered array
:param idx: former indexing vector
:return: recovered array
"""
inv_arr = np.repeat(np.nan, len(arr))
for i, ix in enumerate(idx):
inv_arr[ix] = arr[i]
return inv_arr
def split_in_n_bins(
a: list[Any] | (tuple[Any] | npt.NDArray[Any]), n: int, attribute_remainder: bool = True
) -> list[Any]:
"""Split in three bins and attribute the remainder equally: [1,2,3,4,5,6,7,8] => [1,2,7], [3,4,8], [5,6]."""
size = len(a) // n
split = np.split(a, np.arange(size, len(a), size))
if attribute_remainder and (len(split) != n):
att_i = 0
remainder = list(split.pop(-1))
while len(remainder) > 0:
split[att_i] = np.append(split[att_i], remainder.pop(0))
att_i += 1 # can't overflow
elif len(split) != n:
cprint(
string=f"{len(split[-1])} remainder were put in extra bin. Return {len(split)} bins instead of {n}.",
col="y",
)
return split
def dims_to_rectangularize(arr_length: int) -> tuple[int, int]:
"""Compute dimensions to rectangularize a 1-D array to a 2-D array."""
if arr_length % 2 != 0:
msg = "arr_length must be even!"
raise ValueError(msg)
factors = get_factors(n=arr_length)
dim_i = factors[len(factors) // 2]
dim_j = arr_length // dim_i
return dim_i, dim_j
def rectangularize_1d_array(arr: npt.NDArray[Any], wide: bool = False) -> npt.NDArray[Any]:
"""
Rectangularize a 1-D array to a 2-D array.
:param arr: 1-D array
:param wide: whether to return a wide array (i.e., more columns than rows)
:return: 2-D array
"""
arr = arr.squeeze()
if np.ndim(arr) != 1:
msg = "Array must be 1-D!"
raise ValueError(msg)
dim_i, dim_j = dims_to_rectangularize(arr_length=len(arr))
return np.reshape(arr, newshape=(dim_i, dim_j)[:: -1 if wide else 1])
def get_n_cols_and_rows(n_plots: int, square: bool = True, verbose: bool = False) -> tuple[int, int]:
"""Define figure grid-size: with rpl x cpl cells."""
factors = get_factors(n_plots)
if len(factors) <= 2 or square: # prime or square # noqa: PLR2004
rpl = 1
cpl = 1
while (rpl * cpl) < n_plots:
if rpl == cpl:
rpl += 1
else:
cpl += 1
else:
rpl = factors[len(factors) // 2]
cpl = n_plots // rpl
ndiff = rpl * cpl - n_plots
if ndiff > 0 and verbose:
cprint(string=f"There will {ndiff} empty plot slots.", col="y")
return rpl, cpl
def get_string_overlap(s1: str, s2: str) -> str:
"""
Find the longest overlap between two strings, starting from the left.
For instance:
get_string_overlap("abcdef", "abcefg") -> "abc"
:param s1: first string
:param s2: second string
:return overlap between two strings [str]
"""
s = difflib.SequenceMatcher(None, s1, s2)
pos_a, _, size = s.find_longest_match(0, len(s1), 0, len(s2)) # _ = pos_b
return s1[pos_a : pos_a + size]
# %% Color prints & I/O << o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def ol(string: str, wide_bar: bool = True) -> str:
"""
Create overline over given string or character.
ol("x") -> "x̅"
"""
bw = "\u0305" if wide_bar else "\u0304" # 0305: wide; 0304: smaller
return "".join([f"{char}{bw}" for char in string])
def ss(string_with_nr: str, sub: bool = True) -> str:
"""
Translate the following chars '0123456789()' into subscript or superscript and vice versa.
ss("H2O") -> "H₂O"
ss("This is x2", sub=False) -> "This is x²"
"""
# TODO: also for chars # noqa: FIX002
# https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts
subs = str.maketrans("0123456789()₀₁₂₃₄₅₆₇₈₉₍₎", "₀₁₂₃₄₅₆₇₈₉₍₎0123456789()")
sups = str.maketrans("0123456789()⁰¹²³⁴⁵⁶⁷⁸⁹⁽⁾", "⁰¹²³⁴⁵⁶⁷⁸⁹⁽⁾0123456789()")
return string_with_nr.translate(subs if sub else sups)
class Bcolors:
r"""
Use for color-print-commands in console.
Usage:
print(Bcolors.HEADER + "Warning: No active frommets remain. Continue?" + Bcolors.ENDC)
print(Bcolors.OKBLUE + "Warning: No active frommets remain. Continue?" + Bcolors.ENDC)
For more:
CSELECTED = '\33[7m'
CBLACK = '\33[30m'
CRED = '\33[31m'
CGREEN = '\33[32m'
CYELLOW = '\33[33m'
CBLUE = '\33[34m'
CVIOLET = '\33[35m'
CBEIGE = '\33[36m'
CWHITE = '\33[37m'
CBLACKBG = '\33[40m'
CREDBG = '\33[41m'
CGREENBG = '\33[42m'
CYELLOWBG = '\33[43m'
CBLUEBG = '\33[44m'
CVIOLETBG = '\33[45m'
CBEIGEBG = '\33[46m'
CWHITEBG = '\33[47m'
CGREY = '\33[90m'
CBEIGE2 = '\33[96m'
CWHITE2 = '\33[97m'
CGREYBG = '\33[100m'
CREDBG2 = '\33[101m'
CGREENBG2 = '\33[102m'
CYELLOWBG2 = '\33[103m'
CBLUEBG2 = '\33[104m'
CVIOLETBG2 = '\33[105m'
CBEIGEBG2 = '\33[106m'
CWHITEBG2 = '\33[107m'
# For preview type:
for i in [1, 4, 7] + list(range(30, 38)) + list(range(40, 48)) + list(range(90, 98)) + list(
range(100, 108)): # range(107+1)
print(i, '\33[{}m'.format(i) + "ABC & abc" + '\33[0m')
"""
HEADERPINK = "\033[95m"
OKBLUE = "\033[94m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
UNDERLINE = "\033[4m"
BOLD = "\033[1m"
ENDC = "\033[0m" # this is necessary in the end to reset to default print
DICT: ClassVar = {"p": HEADERPINK, "b": OKBLUE, "g": OKGREEN, "y": WARNING, "r": FAIL, "ul": UNDERLINE, "bo": BOLD}
def _col_check(col: str) -> None:
"""Check whether the given color is valid."""
if col.lower() not in {"p", "b", "g", "y", "r"}:
msg = "col must be 'p'(ink), 'b'(lue), 'g'(reen), 'y'(ellow), 'r'(ed)"
raise ValueError(msg)
def cprint(string: str, col: str | None = None, fm: str | None = None, ts: bool = False) -> None:
"""
Colorize and format print-out. Add leading time-stamp (fs) if required.
:param string: print message
:param col: color:'p'(ink), 'b'(lue), 'g'(reen), 'y'(ellow), OR 'r'(ed)
:param fm: format: 'ul'(:underline) OR 'bo'(:bold)
:param ts: add leading time-stamp
"""
if col:
col = col.lower()
_col_check(col=col)
col = Bcolors.DICT[col]
if fm:
fm = fm[0:2].lower()
if fm not in {"ul", "bo"}:
msg = "fm must be 'ul'(:underline), 'bo'(:bold)"
raise ValueError(msg)
fm = Bcolors.DICT[fm]
if ts:
pfx = "" # collecting leading indent or new line
while string.startswith(("\n", "\t")):
pfx += string[:1]
string = string[1:]
string = f"{pfx}{datetime.now():%Y-%m-%d %H:%M:%S} | {string}"
# print given string with given formatting
print(f"{col if col else ''}{fm if fm else ''}{string}{Bcolors.ENDC}")
def cinput(string: str, col: str | None = None) -> str:
"""Colorize string for input()."""
if col:
col = col.lower()
_col_check(col=col)
col = Bcolors.DICT[col]
# input(given string) with given formatting
return input("{}".format(col if col else "") + string + Bcolors.ENDC)
def block_print() -> None:
"""Disable print outs."""
sys.stdout = open(os.devnull, "w") # noqa: SIM115, PTH123, PLW1514
def enable_print() -> None:
"""Restore & enable print outs."""
# Check if sys.stdout can be closed
sys.stdout = sys.__stdout__
def suppress_print(func: Callable[..., Any]) -> Callable[..., Any]:
"""Suppresses print within given function."""
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrap function, in which print-out is to be suppressed."""
block_print()
output = func(*args, **kwargs)
enable_print()
return output
return wrapper
def true_false_request(func: Callable[..., Any]) -> Callable[..., Any]:
"""Wrap print function with true-false-request."""
@wraps(func)
def wrapper(*args: str, **kwargs: str) -> bool:
"""Wrap function for true-false-request."""
func(*args, **kwargs)
tof = input("(T)rue or (F)alse: ").lower()
if tof not in {"true", "false", "t", "f"}:
msg = "Must be 'T', 't' or 'T/true', or 'F', 'f', 'F/false'"
raise ValueError(msg)
return tof in "true"
return wrapper
@true_false_request
def ask_true_false(question: str, col: str = "b") -> None:
"""
Ask user for input for given True-or-False question.
:param question: str
:param col: print-color of question
:return: answer
"""
cprint(question, col)
def check_executor(return_shell_bool: bool = False) -> bool | None:
"""
Check from where the script that executes this function is run from.
:param return_shell_bool: True: provide boolean about result
:return: only print (None) OR bool
"""
ppid = os.getppid()
shell: bool = psutil.Process(ppid).name() in {"bash", "zsh"} # TODO: extend for other shells # noqa: FIX002
print(
"Current script{} is executed via: {}{}{}".format(
f" {Bcolors.WARNING}{sys.argv[0]}{Bcolors.ENDC}" if shell else "", # platform.sys
Bcolors.OKBLUE,
psutil.Process(ppid).name(),
Bcolors.ENDC,
)
)
return shell if return_shell_bool else None
def cln(factor: int = 1) -> None:
"""Clean the console of interactive shells that do not allow for this."""
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n" * factor)
# %% Text << o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def replace_line_in_file(
path_to_file: str | Path,
pattern: str | re.Pattern,
fill_with: str | None,
whole_line: bool = True,
verbose: bool = False,
) -> bool | None:
"""
Replace line with the matching pattern, either the whole line, or only the pattern.
Delete patterns or entire line with a pattern by setting new_line to None or "".
:param path_to_file: the path to the text-like file
:param pattern: the pattern to match per line in file, which follows the convention of re.Pattern
:param fill_with: new line which replaces the old line; None OR "" will delete pattern/line
:param whole_line: whether to replace whole line, or only matching pattern
:param verbose: verbose or not
:return: whether pattern found or not
"""
if not Path(path_to_file).exists():
if verbose:
cprint(string=f"Couldn't find file '{path_to_file}'!", col="r")
return None
fill_with = "" if fill_with is None else fill_with
if verbose and len(fill_with) == 0:
if whole_line:
cprint(string="Given patterns will be deleted from file ...", col="y")
else:
cprint(string="Lines with given pattern will be deleted ...", col="y")
if isinstance(pattern, str):
pattern = re.compile(pattern)
found = False
with fileinput.input(path_to_file, inplace=True) as file:
for old_line in file:
match = pattern.search(old_line)
if match is not None:
found = True
if whole_line:
if len(fill_with) > 0: # in the case of == 0: the line will be deleted
fill_with = fill_with if fill_with.endswith("\n") else fill_with + "\n"
sys.stdout.write(fill_with)
else:
sys.stdout.write(pattern.sub(fill_with, old_line))
else:
sys.stdout.write(old_line)
return found
# %% OS >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o >><< o
def open_folder(path: str | Path) -> None:
"""Open specific folder or file in Finder."""
if not isinstance(path, (str, Path)):
msg = f"Path must be string or Path, not {type(path)}"
raise TypeError(msg)
if not Path(path).exists():
msg = f"Path '{path}' doesn't exist."
raise FileNotFoundError(msg)
if platform.system() == "Windows": # for Windows
os.startfile(path) # noqa: S606
elif platform.system() == "Darwin": # ≈ sys.platform = 'darwin' | for Mac
subprocess.Popen(["/usr/bin/open", path]) # noqa: S603
else: # for 'Linux'
subprocess.Popen(["/usr/bin/xdg-open", path]) # noqa: S603
def browse_files(initialdir: str | Path | None = None, filetypes: str | None = None) -> str:
"""
Browse and choose a file from the finder.
:param initialdir: Where to start the search (ARG MUST BE NAMED 'initialdir')
:param filetypes: what type of file-ending (suffix, e.g., '*.jpg')
:return: path to chosen file
"""
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
kwargs = {}
if initialdir:
kwargs.update({"initialdir": str(initialdir)})
if filetypes:
kwargs.update({"filetypes": [(filetypes + " File", "*." + filetypes.lower())]})
return askopenfilename(parent=root, title="Choose the file", **kwargs)
def delete_dir_and_files(parent_path: str | Path, force: bool = False, verbose: bool = True) -> None:
"""
Delete the given folder and all subfolders and files.
os.walk() returns three values on each iteration of the loop:
i) The name of the current folder: dir path
ii) A list of folders in the current folder: dir names
iii) A list of files in the current folder: files
:param parent_path: path to parent folder
:param force: True: don't ask to remove
:param verbose: True: list files will be deleted
:return: None
"""
# Print the effected files and subfolders
if Path(parent_path).exists():
if not (not verbose and force):
print(f"\nFollowing (sub-)folders and files of parent folder '{parent_path}' would be deleted:")
for file in Path(parent_path).glob("**/*"):
cprint(string=f"{file}", col="b")
# Double check: Ask whether to delete
delete = True if force else ask_true_false("Do you want to delete this tree and corresponding files?", col="r")
if delete:
# Delete all folders and files in the tree
for dir_path, _dirn_ames, files in os.walk(parent_path, topdown=False): # start from bottom
for file_name in files:
if verbose:
cprint(string=f"Remove file: {file_name}", col="r") # f style (for Python > 3.5)
Path(dir_path, file_name).unlink()
if verbose:
cprint(string=f"Remove folder: {dir_path}", col="r")
Path(dir_path).rmdir() # os.rmdir(dir_path)
else:
cprint(string="Tree and files won't be deleted!", col="b")
else:
cprint(string=f"Given folder '{parent_path}' doesn't exist.", col="r")