-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMy_Utils.py
822 lines (697 loc) · 24.9 KB
/
My_Utils.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
#!/usr/bin/python
import os
import re
import sys
import time
import json
import shutil
import ctypes
import psutil
import random
import string
import datetime as TM
import platform
import traceback
import tracemalloc
import logging
from typing import Union
from functools import wraps
# DECORATORS
# XXX: color codes: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
'''
\033[91m : Red
\033[92m : Green
\033[93m : Yellow
\033[94m : Blue
\033[95m : Purple
\033[96m : Cyan
\033[0m : Reset color
'''
# XXX: DECORATORS
def color_print(fg: int = 37, bg: int = 40):
def decorator(func):
def wrapper(text):
print(f"\033[{fg}m\033[{bg}m{func(text)}\033[0m")
return wrapper
return decorator
def name_time(func):
"""
A decorator that prints the function's name, start time, end time,
and the duration it took to execute the function.
"""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
print(f" +{func.__name__} Start: {time.strftime('%H:%M:%S')}")
result = func(*args, **kwargs)
end_time = time.perf_counter()
duration = end_time - start_time
hours, remainder = divmod(duration, 3600)
minutes, seconds = divmod(remainder, 60)
# print(f" -Time: {int(hours)}h:{int(minutes)}m:{seconds:.2f}s")
return result
return wrapper
def debug(func):
def wrapper(*args, **kwargs):
calling_function = sys._getframe().f_back.f_code.co_name if sys._getframe().f_back is not None else "Top Level"
try:
result = func(*args, **kwargs)
return result
except Exception as e:
traceback.print_exc()
print(f"Exception in {calling_function}: {e}")
return wrapper
def perf_monitor(func):
debug = False
if not debug:
# If running in optimized mode, return the original function
return func
@wraps(func)
def wrapper(*args, **kwargs):
calling_function = sys._getframe().f_back.f_code.co_name if sys._getframe().f_back is not None else "Top Level"
strt_time = time.perf_counter()
cpu_percent_prev = psutil.cpu_percent(interval=0.05, percpu=False)
tracemalloc.start()
try:
return func(*args, **kwargs)
except Exception as e:
traceback.print_exc()
print(f"Exception in {calling_function}: {e}")
finally:
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
cpu_percent = psutil.cpu_percent(interval=None, percpu=False)
cpu_percnt = cpu_percent - cpu_percent_prev
end_time = time.perf_counter()
duration = end_time - strt_time
msj = f"{calling_function}\t\tUsed {abs(cpu_percnt):>5.1f} % CPU: {duration:.2f} sec\t Mem: [avr:{current}, max:{peak}]\t({func.__doc__})"
logging.info(msj)
return wrapper
def perf_monitor_temp(func):
""" Measure performance of a function """
@wraps(func)
def wrapper(*args, **kwargs):
strt_time = time.perf_counter()
cpu_percent_prev = psutil.cpu_percent(interval=0.05, percpu=False)
tracemalloc.start()
try:
return func(*args, **kwargs)
except Exception as e:
logging.exception(f"Exception in {func.__name__}: {e}",exc_info=True, stack_info=True)
finally:
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
cpu_percent = psutil.cpu_percent(interval=None, percpu=False)
cpu_percnt = cpu_percent - cpu_percent_prev
# New code to measure CPU temperature
cpu_temp = psutil.sensors_temperatures().get('coretemp')[0].current
print(f"CPU temperature: {cpu_temp}°C")
end_time = time.perf_counter()
duration = end_time - strt_time
msj = f"{func.__name__}\t\tUsed {abs(cpu_percnt):>5.1f} % CPU: {hm_time(duration)}\t Mem: [avr:{hm_sz(current):>8}, max:{hm_sz(peak):>8}]\t({func.__doc__})"
logging.info(msj)
return wrapper
def measure_cpu_time(func):
def wrapper(*args, **kwargs):
start_time = time.time()
cpu_percent = psutil.cpu_percent(interval=None, percpu=True)
result = func(*args, **kwargs)
elapsed_time = time.time() - start_time
cpu_percent = [p - c for p, c in zip(psutil.cpu_percent(interval=None, percpu=True), cpu_percent)]
print(f"Function {func.__name__} used {sum(cpu_percent)/len(cpu_percent)}% CPU over {elapsed_time:.2f} seconds")
return result
return wrapper
def logit(logfile='out.log', de_bug=False):
def logging_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
with open(logfile, 'a') as f:
if len(kwargs) > 0:
f.write(f"\n{func.__name__}{args} {kwargs} = {result}\n")
else:
f.write(f"\n{func.__name__}{args} = {result}\n")
if de_bug:
if len(kwargs) > 0:
print(f"{func.__name__}{args} {kwargs} = {result}")
else:
print(f"{func.__name__}{args} = {result}")
return result
return wrapper
return logging_decorator
def handle_exception(func):
"""Decorator to handle exceptions."""
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"Exception in {func.__name__}: {e}")
logging.exception(f"Exception in {func.__name__}: {e}",exc_info=True, stack_info=True)
# sys.exit(1)
except TypeError :
print(f"{func.__name__} wrong data types")
except IOError:
print("Could not write to file.")
except :
print("Someting Else?")
else:
print("No Exceptions")
finally:
logging.error("Error: ", exc_info=True)
logging.error("uncaught exception: %s", traceback.format_exc())
return wrapper
def measure_cpu_utilization(func):
"""Measure CPU utilization, number of cores used, and their capacity."""
@wraps(func)
def wrapper(*args, **kwargs):
cpu_count = psutil.cpu_count(logical=True)
cpu_prcnt = psutil.cpu_percent(interval=0.1, percpu=True)
result = func(*args, **kwargs)
cpu_percnt = sum(cpu_prcnt) / cpu_count
return result, cpu_percnt, cpu_prcnt
return wrapper
def log_exceptions(func):
"""Log exceptions that occur within a function."""
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"Exception in {func.__name__}: {e}")
logging.exception(f"Exception in {func.__name__}: {e}",exc_info=True, stack_info=True)
return wrapper
def measure_execution_time(func):
"""Measure the execution time of a function."""
@wraps(func)
def wrapper(*args, **kwargs):
strt_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
duration = end_time - strt_time
print(f"{func.__name__}: Execution time: {duration:.5f} sec")
return result
return wrapper
def measure_memory_usage(func):
"""Measure the memory usage of a function."""
@wraps(func)
def wrapper(*args, **kwargs):
tracemalloc.start()
result = func(*args, **kwargs)
current, peak = tracemalloc.get_traced_memory()
print(f"{func.__name__}: Mem usage: {current / 10**6:.6f} MB (avg), {peak / 10**6:.6f} MB (peak)")
tracemalloc.stop()
return result
return wrapper
def performance_check(func):
"""Measure performance of a function"""
@log_exceptions
@measure_execution_time
@measure_memory_usage
@measure_cpu_utilization
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def temperature ():
sensors = psutil.sensors_temperatures()
for name, entries in sensors.items():
print(f"{name}:")
for entry in entries:
print(f" {entry.label}: {entry.current}°C")
##>>============-------------------< End >------------------==============<<##
class Tee:
def __init__(self, *files, error_on=False):
self.files = files
self.error_on = error_on
self.original_stdout = sys.stdout
self.original_stderr = sys.stderr
def write(self, obj):
if not isinstance(obj, str):
obj = str(obj)
for file in self.files:
file.write(obj)
file.flush()
def flush(self):
for file in self.files:
file.flush()
def close(self):
for file in self.files:
if file not in (self.original_stdout, self.original_stderr):
file.close()
def __enter__(self):
sys.stdout = self
if self.error_on:
sys.stderr = self
return self
def __exit__(self, exc_type, exc_value, traceback):
sys.stdout = self.original_stdout
if self.error_on:
sys.stderr = self.original_stderr
self.close()
##>>============-------------------< End >------------------==============<<##
class Spinner:
def __init__(self, spin_text="|/-o+\\", indent=0, delay=0.1):
self.spinner_count = 0
self.spin_text = spin_text
self.spin_length = len(spin_text)
self.prefix = " " * indent # Indentation string
self.last_message_length = 0 # To keep track of the length of the last printed message
self.cursor_hidden = False
self.delay = delay # Delay between spinner updates
self.last_update_time = 0
def hide_cursor(self):
if not self.cursor_hidden:
sys.stderr.write("\033[?25l") # Hide cursor
sys.stderr.flush()
self.cursor_hidden = True
def show_cursor(self):
if self.cursor_hidden:
sys.stderr.write("\033[?25h") # Show cursor
sys.stderr.flush()
self.cursor_hidden = False
def abbreviate_path(self, path: str, max_length: int) -> str:
"""Abbreviates a path to fit within the terminal width."""
if len(path) <= max_length:
return path
else:
return f"{path[:max_length//2]}...{path[-max_length//2:]}"
def print_spin(self, extra: str = "") -> None:
"""Prints a spinner with optional extra text."""
current_time = time.time()
if current_time - self.last_update_time < self.delay:
return # Skip updating the spinner if it's too soon
self.last_update_time = current_time # Update the last update time
# Hide the cursor
self.hide_cursor()
# Get terminal width
terminal_width = shutil.get_terminal_size().columns
# Abbreviate the extra text to fit the terminal
extra = self.abbreviate_path(extra, terminal_width - 10)
spin_char = self.spin_text[self.spinner_count % self.spin_length]
message = f"\r{self.prefix}| {spin_char} | {extra}"
# Calculate the number of spaces needed to clear the previous message
clear_spaces = max(self.last_message_length - len(message), 0)
# Print the spinner and the extra text, followed by enough spaces to clear any leftover characters
sys.stderr.write(f"{message}{' ' * clear_spaces}")
sys.stderr.flush()
# Update the length of the last message
self.last_message_length = len(message)
self.spinner_count += 1
def stop(self):
"""Stops the spinner and shows the cursor."""
# Show the cursor
self.show_cursor()
sys.stderr.write("\n") # Move to the next line after stopping
sys.stderr.flush()
'''
# Example usage:
if __name__ == "__main__":
# Testing with different indentation values
spinner_no_indent = Spinner(indent=0)
spinner_with_indent = Spinner(indent=4)
for _ in range(100): # Simulate a task with 10 iterations
spinner_no_indent.print_spin(f" {_} Processing without indent...")
time.sleep(0.1) # Simulate work being done
print("\n")
for _ in range(100): # Simulate a task with 10 iterations
spinner_with_indent.print_spin(f" {_} Processing with indent...")
time.sleep(0.1) # Simulate work being done
print("\nTask completed!")
'''
##>>============-------------------< End >------------------==============<<##
class RunningAverage:
''' Compute the running average of a value '''
def __init__(self):
self.n = 0
self.avg = 0
def update(self, x):
self.avg = (self.avg * self.n + x) / (self.n + 1)
self.n += 1
def get_avg(self):
return self.avg
def reset(self):
self.n = 0
self.avg = 0
##>>============-------------------< End >------------------==============<<##
class RunningStats:
def __init__(self):
self.total = 0.0
self.count = 0
self.min_val = float('inf')
self.max_val = float('-inf')
def update(self, num):
self.total += num
self.count += 1
self.min_val = min(self.min_val, num)
self.max_val = max(self.max_val, num)
self.print_stats()
def print_stats(self):
running_average = self.total / self.count if self.count != 0 else 0.0
print(f"Current number: {num}, Running average: {running_average}")
print(f"Minimum value: {self.min_val}")
print(f"Maximum value: {self.max_val}")
##>>============-------------------< End >------------------==============<<##
class Color:
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
RESET = "\033[0m"
def __init__(self, color, bright=False):
self.color = color
self.bright = bright
def __str__(self):
return f"\033[{1 if self.bright else ''};{self.color}m" if self.bright else f"{self.color}m"
# Usage:
#print(f"{Color(Color.RED, bright=True)}This is bright red text!{Color(Color.RESET)}")
#print(f"{Color(Color.BLUE)}This is normal blue text!{Color(Color.RESET)}")
## XXX: Functions :XXX
##==============------------------- Start -------------------==============##
def hm_sz(numb: Union[str, int, float], type: str = "B") -> str:
"""
Convert a file size to human-readable format.
Parameters:
- numb (str, int, float): File size.
- type (str): Type of file size (default is "B" for bytes).
Returns:
- str: Human-readable file size with sign.
"""
# Handle different input types for numb
if isinstance(numb, str):
numb = float(numb)
elif not isinstance(numb, (int, float)):
raise ValueError("Invalid type for numb. Must be str, int, or float.")
sign = '-' if numb < 0 else ''
numb = abs(numb) # Convert to absolute value for calculations
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
for unit in units:
if numb < 1024.0:
return f"{sign}{numb:.2f} {unit}"
numb /= 1024.0
return f"{sign}{numb:.2f} {units[-1]}"
##==============------------------- End -------------------==============##
def hm_time(timez) -> str:
"""Converts time in seconds to a human-readable format."""
# Handle invalid input types
if not isinstance(timez, (int, float)):
return f"Invalid input: {type(timez).__name__}"
units = [
('year', 31536000),
('month', 2592000),
('week', 604800),
('day', 86400),
('hour', 3600),
('minute', 60),
('second', 1)
]
# Handle edge cases
if timez < 0:
return "Error: time cannot be negative."
if timez == 0:
return "Zero time."
if timez < 0.001:
return f"{timez * 1000:.3f} ms"
if timez < 60:
return f"{timez:.3f} second{'s' if timez != 1 else ''}"
result = []
# Convert time into larger units
for unit, seconds_in_unit in units:
value = int(timez // seconds_in_unit)
if value > 0:
result.append(f"{value} {unit}{'s' if value > 1 else ''}")
timez %= seconds_in_unit
# Join the result with 'and' for the last unit
if len(result) > 1:
return ", ".join(result[:-1]) + " and " + result[-1]
else:
return result[0]
##==============------------------- End -------------------==============##
def copy_move(src: str, dst: str, keep_original: bool = True, verbose: bool = False) -> bool:
"""
Copy or move a file.
Parameters:
- src (str): Source file path.
- dst (str): Destination file path.
- keep_original (bool, optional): If True, keep the original file.
- verbose (bool optional): If True print message. Defaults to False.
"""
# if verbose:
# print(f"\ncopy_move called with src: {src}, dst: {dst}, keep_original: {keep_original}\n")
if os.path.exists(dst) and os.path.samefile(src, dst):
if not keep_original:
os.remove(src)
if verbose:
print(f"Source {src} and destination {dst} are the same. Source has been deleted.")
else:
if verbose:
print(f"{src} and {dst} are the same, nothing to do.")
return True
try:
(action, transfer_func) = ("_Copy", shutil.copy2) if keep_original else ("_Move", shutil.move)
transfer_func(src, dst)
if verbose:
print(f"{action}: {src}\nTo: {dst}")
return True
except (PermissionError, IOError, OSError) as err:
print(f"\ncopy_move Error: {err}\n{action}: {src} to {dst}")
return False
##==============------------------- End -------------------==============##
def ordinal(n: int ) -> str :
if 10 <= n % 100 <= 20:
suffix = 'th'
else:
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')
return f"{n}'{suffix}"
##==============------------------- End -------------------==============##
def flatten_list_of_lists(lst):
"""Flatten a list of lists (or a mix of lists and other elements) into a single list."""
result = []
for item in lst:
if isinstance(item, list):
result.extend(item)
elif isinstance(item, tuple):
# If the item is a tuple, only extend the first element (which should be a list)
result.extend(item[0])
else:
result.append(item)
return result
##==============------------------- End -------------------==============##
def divd_strn(val: str ) -> float:
'''
Returns floating point resul for string (n/d) or val it's fp '.'
'''
# input ( val )
r = 1
if '/' in val:
n, d = val.split('/')
n = float(n)
d = float(d)
if n != 0 and d != 0 :
r = n / d
elif '.' in val:
r = float(val)
return round( r, 3)
##==============------------------- End -------------------==============##
def vis_compr(string1, string2, no_match_c='|', match_c='='):
''' Visualy show diferences between sting1 graphx string2 '''
str_t = TM.datetime.now()
message = sys._getframe().f_code.co_name + ':'
print(f" +{message}=: Start: {str_t:%T}")
# print (f"\n1: {string1}\n2: {string2}\n ??")
# XXX: # TODO: location of differences , chunking
graphx = ''
n_diff = 0
if len(string2) < len(string1):
string1, string2 = string2, string1
for c1, c2 in zip(string1, string2):
if c1 == c2:
graphx += match_c
else:
graphx += no_match_c
n_diff += 1
delta = len(string2) - len(string1)
graphx += delta * no_match_c
n_diff += delta
if n_diff :
print(f"{n_diff} Differences \n1: {string1}\n {graphx}\nMove: {string2}\n")
return graphx, n_diff
#>=-------------------------------------------------------------------------=<#
def print_alighned(list: str) -> None :
'''
print a formated table with the {list} values provided
'''
lens = []
for col in zip(*list):
lens.append(max([len(v) for v in col]))
format = " ".join(["{:<" + str(l) + "}" for l in lens])
for row in list:
print(format.format(*row))
##==============------------------- End -------------------==============##
def test_filename(filename: str) -> None:
legal_chars = '[A-Za-z0-9._-]+'
if re.fullmatch(legal_chars, filename):
print(f'{filename} is a legal filename.')
else:
print(f'{filename} is NOT a legal filename.')
out_file = re.sub(r'[^\w\s_-]+', '', filename).strip().replace(' ', '_')
print ( f'{out_file} is rename it')
#test_filename("myfile.txt")
#test_filename("my file.txt")
##==============------------------- End -------------------==============##
def stmpd_rad_str(length=13, prefix=''):
"""
Generate a random string + prefix + timestamp.
Parameters:
length (int): The length of the random string to generate. Default is 13.
prefix (str): A prefix to add to the generated string. Default is an empty string.
Returns:
str: The generated string with the timestamp prefix and random characters.
"""
# Get the current time and format it to include minutes and seconds
current_time = TM.datetime.now()
timestamp = current_time.strftime("_%M_%S_")
# Generate random characters from ascii letters and hex digits
random_chars = ''.join(random.sample(string.ascii_letters + string.digits, length))
# Combine the prefix, timestamp, and random characters to form the final string
random_string = prefix + timestamp + random_chars
return random_string
##==============------------------- End -------------------==============##
def get_tree_size(path: str) -> int:
"""Return total size of files in path and subdirs. If is_dir() or stat() fails, print an error message to stderr
and assume zero size (for example, file has been deleted).
"""
total_size = 0
for entry in os.scandir(path):
try:
if entry.is_file(follow_symlinks=False):
total_size += entry.stat(follow_symlinks=False).st_size
elif entry.is_dir(follow_symlinks=False):
total_size += get_tree_size(entry.path)
except (OSError, ValueError) as e:
logging.error(f" {e}", exc_info=True)
print(f"Error in {get_tree_size.__name__} when processing {entry.name}: {e}", file=sys.stderr)
return total_size
##==============------------------- End -------------------==============##
def safe_options(strm, opts ):
safe = {}
# Only copy options that are expected and of correct type
# (and do typecasting on them)
for k, v in opts.items():
if k in opts and v is not None:
typ = opts[k]
try:
safe[k] = typ(v)
except ValueError:
pass
return safe
##==============------------------- End -------------------==============##
def Trace(message: str, exception: Exception, debug: bool = False) -> None:
"""Prints a traceback and debug info for a given exception"""
max_chars = 42
print("+-" * max_chars)
print(f"Msg: {message}\nErr: {exception}\nRep: {repr(exception)}")
print("-+" * max_chars)
max_chars *= 2
print("Stack")
print("=" * max_chars)
stack = traceback.extract_stack()
template = " {filename:<26} | {lineno:5} | {funcname:<20} | {source:>12}"
for filename, lineno, funcname, source in stack:
if funcname != "<module>":
funcname = funcname + "()"
print(
template.format(
filename=os.path.basename(filename),
lineno=lineno,
source=source,
funcname=funcname,
)
)
print("=" * max_chars)
print("Sys Exec_Info")
exc_type, exc_value, exc_traceback = sys.exc_info()
print("-" * max_chars)
for frame in traceback.extract_tb(exc_traceback):
print(
f" {os.path.basename(frame.filename):<26} | {frame.lineno:5} | {frame.name:20} | {frame.line:12} "
)
print("-" * max_chars)
print("=" * max_chars)
logging.exception(f"Msg: {message} Err: {exception}", exc_info=True, stack_info=True)
# logging.error (f"Msg: {message} Err: {exception}", exc_info=True, stack_info=True))
time.sleep(3)
##==============------------------- End -------------------==============##
def res_chk(folder='.'):
msj = sys._getframe().f_code.co_name
print("=" * 60)
print(TM.datetime.now().strftime('\n%a:%b:%Y %T %p'))
print('\n:>', msj)
print(os.getcwd())
print("Python is:", '\n'.join(sorted(sys.path)), '\n')
print('\nFile :', __file__)
print('Access time :', time.ctime(os.path.getatime(__file__)))
print('Modified time:', time.ctime(os.path.getmtime(__file__)))
print('Change time :', time.ctime(os.path.getctime(__file__)))
print('Size :', hm_sz(os.path.getsize(__file__)))
if os.path.isfile(folder):
print('\n', folder, " is a File")
elif os.path.isdir(folder):
print('\n', folder, " is a folder")
elif os.path.islink(folder):
print('\n', folder, " is a Link")
elif os.path.ismount(folder):
print('\n', folder, " is a Mountpoint")
else:
print('\n', folder, " is a WTF?")
try:
sys_is = platform.uname()
print('\nSystem : ', sys_is.node, sys_is.system, sys_is.release,
'(', sys_is.version, ')', sys_is.processor)
print("FFmpeg :", ffmpath)
if not (os.path.exists(ffmpath)):
print(msj, " ffMpeg Path Does not Exist:")
return False
print("Log File :", Log_File)
total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong()
if sys.version_info >= (3,) or isinstance(path, unicode):
fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW
else:
fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA
ret = fun(None, None, ctypes.byref(total), ctypes.byref(free))
if ret == 0:
raise ctypes.WinError()
if (free.value / total.value) < 0.30:
print(msj, " Less that 30% Space on Disk")
return False
print("\nTotal : %s Free %s %s %s"
% (hm_sz(total.value), hm_sz(free.value), round(free.value / total.value * 100), '%'))
except Exception as e:
logging.exception(f"Error {e}", exc_info=True, stack_info=True, extra=msj)
msj += " WTF? Exception "
Trace (msj, e)
finally:
print("\nResources OK\n")
return True
##==============------------------- End -------------------==============##
def calculate_total_bits(width, height, pixel_format):
bits_per_pixel = {
'yuv420p8': 12, # 8-bit YUV 4:2:0
'p010le': 15, # 10-bit Packed YUV 4:2:0
'yuv420p10le': 15, # 10-bit YUV 4:2:0
'yuv422p8': 16, # 8-bit YUV 4:2:2
'yuv422p10le': 20, # 10-bit YUV 4:2:2
'yuv444p8': 24, # 8-bit YUV 4:4:4
'yuv444p10le': 30, # 10-bit YUV 4:4:4
# Add more pixel formats as needed
}
bpp = bits_per_pixel.get(pixel_format, None)
if bpp is None:
raise ValueError(f"Unsupported pixel format: {pixel_format}")
total_bits = width * height * bpp
print(f"Total bits for a frame with pixel format '{pixel_format}': {total_bits}")
return total_bits