Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
1214 lines (914 loc) · 32.4 KB
/
Copy pathutils.py
File metadata and controls
1214 lines (914 loc) · 32.4 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
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
"""
utils.py - Utility Functions and Helpers for Neuron Framework
This module provides various utility functions and helpers that are used
throughout the Neuron framework, including serialization, validation,
logging utilities, and common operations.
"""
import asyncio
import functools
import inspect
import json
import logging
import os
import signal
import sys
import threading
import time
import traceback
import uuid
from contextlib import contextmanager
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union
logger = logging.getLogger(__name__)
# Type variables for generic functions
T = TypeVar('T')
R = TypeVar('R')
def setup_logging(log_level: str = "INFO",
log_file: Optional[str] = None,
log_format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s") -> None:
"""
Set up logging configuration.
Args:
log_level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
log_file: Optional path to log file
log_format: Format string for log messages
"""
# Convert log level string to constant
level = getattr(logging, log_level.upper(), logging.INFO)
# Configure logging
handlers = []
# Add console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(log_format))
handlers.append(console_handler)
# Add file handler if log file is specified
if log_file:
log_path = Path(log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(logging.Formatter(log_format))
handlers.append(file_handler)
# Configure root logger
logging.basicConfig(
level=level,
handlers=handlers,
format=log_format
)
logger.info(f"Logging configured with level {log_level}")
def generate_id() -> str:
"""
Generate a unique ID.
Returns:
Unique ID string
"""
return str(uuid.uuid4())
def current_timestamp() -> float:
"""
Get the current timestamp.
Returns:
Current timestamp in seconds since epoch
"""
return time.time()
def format_timestamp(timestamp: float, format_str: str = "%Y-%m-%d %H:%M:%S") -> str:
"""
Format a timestamp as a string.
Args:
timestamp: Timestamp in seconds since epoch
format_str: Format string for strftime
Returns:
Formatted timestamp string
"""
return time.strftime(format_str, time.localtime(timestamp))
def parse_timestamp(timestamp_str: str, format_str: str = "%Y-%m-%d %H:%M:%S") -> float:
"""
Parse a timestamp string to a timestamp.
Args:
timestamp_str: Timestamp string
format_str: Format string for strptime
Returns:
Timestamp in seconds since epoch
Raises:
ValueError: If the timestamp string cannot be parsed
"""
return time.mktime(time.strptime(timestamp_str, format_str))
def serialize_to_json(obj: Any, pretty: bool = False) -> str:
"""
Serialize an object to JSON.
Args:
obj: Object to serialize
pretty: Whether to format the JSON with indentation
Returns:
JSON string
Raises:
TypeError: If the object cannot be serialized to JSON
"""
indent = 2 if pretty else None
# Handle Enum values
def enum_serializer(obj):
if isinstance(obj, Enum):
return obj.value
raise TypeError(f"Type {type(obj)} not serializable")
return json.dumps(obj, indent=indent, default=enum_serializer)
def deserialize_from_json(json_str: str) -> Any:
"""
Deserialize an object from JSON.
Args:
json_str: JSON string
Returns:
Deserialized object
Raises:
json.JSONDecodeError: If the JSON string is invalid
"""
return json.loads(json_str)
def serialize_to_file(obj: Any, file_path: Union[str, Path], pretty: bool = False) -> None:
"""
Serialize an object to a JSON file.
Args:
obj: Object to serialize
file_path: Path to the file
pretty: Whether to format the JSON with indentation
Raises:
TypeError: If the object cannot be serialized to JSON
OSError: If the file cannot be written
"""
file_path = Path(file_path)
file_path.parent.mkdir(parents=True, exist_ok=True)
with open(file_path, 'w') as f:
json_str = serialize_to_json(obj, pretty)
f.write(json_str)
def deserialize_from_file(file_path: Union[str, Path]) -> Any:
"""
Deserialize an object from a JSON file.
Args:
file_path: Path to the file
Returns:
Deserialized object
Raises:
json.JSONDecodeError: If the JSON is invalid
FileNotFoundError: If the file does not exist
OSError: If the file cannot be read
"""
with open(file_path, 'r') as f:
json_str = f.read()
return deserialize_from_json(json_str)
def validate_type(value: Any, expected_type: Type[T]) -> T:
"""
Validate that a value is of the expected type.
Args:
value: Value to validate
expected_type: Expected type
Returns:
The value if it is of the expected type
Raises:
TypeError: If the value is not of the expected type
"""
if not isinstance(value, expected_type):
raise TypeError(f"Expected {expected_type.__name__}, got {type(value).__name__}")
return value
def validate_range(value: Union[int, float],
min_value: Optional[Union[int, float]] = None,
max_value: Optional[Union[int, float]] = None) -> Union[int, float]:
"""
Validate that a numeric value is within a range.
Args:
value: Value to validate
min_value: Minimum allowed value (inclusive)
max_value: Maximum allowed value (inclusive)
Returns:
The value if it is within the range
Raises:
ValueError: If the value is outside the range
"""
if min_value is not None and value < min_value:
raise ValueError(f"Value {value} is less than minimum {min_value}")
if max_value is not None and value > max_value:
raise ValueError(f"Value {value} is greater than maximum {max_value}")
return value
def validate_not_none(value: Optional[T], name: str = "value") -> T:
"""
Validate that a value is not None.
Args:
value: Value to validate
name: Name of the value for the error message
Returns:
The value if it is not None
Raises:
ValueError: If the value is None
"""
if value is None:
raise ValueError(f"{name} cannot be None")
return value
def safe_call(func: Callable[..., T], *args, **kwargs) -> Tuple[Optional[T], Optional[Exception]]:
"""
Safely call a function, catching any exceptions.
Args:
func: Function to call
*args: Positional arguments for the function
**kwargs: Keyword arguments for the function
Returns:
Tuple of (result, exception), where exception is None if no exception was raised
"""
try:
result = func(*args, **kwargs)
return result, None
except Exception as e:
return None, e
def retry(max_attempts: int = 3,
delay: float = 1.0,
backoff_factor: float = 2.0,
exceptions: Tuple[Type[Exception], ...] = (Exception,)) -> Callable:
"""
Decorator to retry a function on failure.
Args:
max_attempts: Maximum number of attempts
delay: Initial delay between attempts (seconds)
backoff_factor: Factor to multiply delay by after each attempt
exceptions: Tuple of exception types to catch and retry on
Returns:
Decorated function
Example:
@retry(max_attempts=3, delay=1.0, exceptions=(ConnectionError,))
def fetch_data():
# Function that might fail
pass
"""
def decorator(func: Callable[..., T]) -> Callable[..., T]:
@functools.wraps(func)
def wrapper(*args, **kwargs) -> T:
current_delay = delay
for attempt in range(1, max_attempts + 1):
try:
return func(*args, **kwargs)
except exceptions as e:
if attempt == max_attempts:
raise
logger.warning(
f"Attempt {attempt}/{max_attempts} for {func.__name__} failed: {e}. "
f"Retrying in {current_delay:.2f} seconds."
)
time.sleep(current_delay)
current_delay *= backoff_factor
return wrapper
return decorator
def async_retry(max_attempts: int = 3,
delay: float = 1.0,
backoff_factor: float = 2.0,
exceptions: Tuple[Type[Exception], ...] = (Exception,)) -> Callable:
"""
Decorator to retry an async function on failure.
Args:
max_attempts: Maximum number of attempts
delay: Initial delay between attempts (seconds)
backoff_factor: Factor to multiply delay by after each attempt
exceptions: Tuple of exception types to catch and retry on
Returns:
Decorated function
Example:
@async_retry(max_attempts=3, delay=1.0, exceptions=(ConnectionError,))
async def fetch_data():
# Async function that might fail
pass
"""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Any:
current_delay = delay
for attempt in range(1, max_attempts + 1):
try:
return await func(*args, **kwargs)
except exceptions as e:
if attempt == max_attempts:
raise
logger.warning(
f"Attempt {attempt}/{max_attempts} for {func.__name__} failed: {e}. "
f"Retrying in {current_delay:.2f} seconds."
)
await asyncio.sleep(current_delay)
current_delay *= backoff_factor
return wrapper
return decorator
def with_timeout(timeout: float) -> Callable:
"""
Decorator to add a timeout to a function.
Args:
timeout: Timeout in seconds
Returns:
Decorated function
Example:
@with_timeout(5.0)
def long_running_function():
# Function that might take too long
pass
"""
def decorator(func: Callable[..., T]) -> Callable[..., T]:
@functools.wraps(func)
def wrapper(*args, **kwargs) -> T:
result = [None]
exception = [None]
def target():
try:
result[0] = func(*args, **kwargs)
except Exception as e:
exception[0] = e
thread = threading.Thread(target=target)
thread.daemon = True
thread.start()
thread.join(timeout)
if thread.is_alive():
raise TimeoutError(f"Function {func.__name__} timed out after {timeout} seconds")
if exception[0]:
raise exception[0]
return result[0]
return wrapper
return decorator
def async_with_timeout(timeout: float) -> Callable:
"""
Decorator to add a timeout to an async function.
Args:
timeout: Timeout in seconds
Returns:
Decorated function
Example:
@async_with_timeout(5.0)
async def long_running_function():
# Async function that might take too long
pass
"""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Any:
try:
return await asyncio.wait_for(func(*args, **kwargs), timeout=timeout)
except asyncio.TimeoutError:
raise TimeoutError(f"Function {func.__name__} timed out after {timeout} seconds")
return wrapper
return decorator
def measure_time(func: Callable[..., T]) -> Callable[..., Tuple[T, float]]:
"""
Decorator to measure the execution time of a function.
Args:
func: Function to measure
Returns:
Decorated function that returns (result, elapsed_time)
Example:
@measure_time
def my_function():
# Function to measure
pass
result, elapsed = my_function()
"""
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Tuple[T, float]:
start_time = time.time()
result = func(*args, **kwargs)
elapsed_time = time.time() - start_time
return result, elapsed_time
return wrapper
def async_measure_time(func: Callable[..., Any]) -> Callable[..., Any]:
"""
Decorator to measure the execution time of an async function.
Args:
func: Async function to measure
Returns:
Decorated function that returns (result, elapsed_time)
Example:
@async_measure_time
async def my_function():
# Async function to measure
pass
result, elapsed = await my_function()
"""
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Tuple[Any, float]:
start_time = time.time()
result = await func(*args, **kwargs)
elapsed_time = time.time() - start_time
return result, elapsed_time
return wrapper
def memoize(func: Callable[..., T]) -> Callable[..., T]:
"""
Decorator to memoize (cache) function results.
Args:
func: Function to memoize
Returns:
Decorated function
Example:
@memoize
def expensive_function(x, y):
# Expensive computation
pass
"""
cache = {}
@functools.wraps(func)
def wrapper(*args, **kwargs) -> T:
# Create a key from the arguments
key = str(args) + str(sorted(kwargs.items()))
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
# Add a method to clear the cache
wrapper.clear_cache = lambda: cache.clear()
return wrapper
def log_exceptions(logger_instance: Optional[logging.Logger] = None) -> Callable:
"""
Decorator to log exceptions raised by a function.
Args:
logger_instance: Logger to use, or None to use the module logger
Returns:
Decorated function
Example:
@log_exceptions()
def my_function():
# Function that might raise exceptions
pass
"""
log = logger_instance or logger
def decorator(func: Callable[..., T]) -> Callable[..., T]:
@functools.wraps(func)
def wrapper(*args, **kwargs) -> T:
try:
return func(*args, **kwargs)
except Exception as e:
log.exception(f"Exception in {func.__name__}: {e}")
raise
return wrapper
return decorator
def async_log_exceptions(logger_instance: Optional[logging.Logger] = None) -> Callable:
"""
Decorator to log exceptions raised by an async function.
Args:
logger_instance: Logger to use, or None to use the module logger
Returns:
Decorated function
Example:
@async_log_exceptions()
async def my_function():
# Async function that might raise exceptions
pass
"""
log = logger_instance or logger
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> Any:
try:
return await func(*args, **kwargs)
except Exception as e:
log.exception(f"Exception in {func.__name__}: {e}")
raise
return wrapper
return decorator
@contextmanager
def capture_exceptions() -> None:
"""
Context manager to capture and log exceptions.
Example:
with capture_exceptions():
# Code that might raise exceptions
pass
"""
try:
yield
except Exception as e:
logger.exception(f"Captured exception: {e}")
raise
@contextmanager
def temporary_attribute(obj: Any, attribute: str, value: Any) -> None:
"""
Context manager to temporarily set an attribute on an object.
Args:
obj: Object to modify
attribute: Attribute name
value: Temporary value
Example:
with temporary_attribute(my_object, 'attribute', 'temporary_value'):
# Code using the temporary attribute
pass
"""
original = getattr(obj, attribute, None)
setattr(obj, attribute, value)
try:
yield
finally:
if original is None:
delattr(obj, attribute)
else:
setattr(obj, attribute, original)
def run_async(func: Callable[..., Any]) -> Callable[..., Any]:
"""
Run an async function in the current event loop.
If no event loop is running, a new one is created.
Args:
func: Async function to run
Returns:
Function that runs the async function and returns its result
Example:
@run_async
async def my_async_function():
# Async code
pass
result = my_async_function() # No need for await
"""
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Any:
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if loop.is_running():
# Create a new loop for this call
new_loop = asyncio.new_event_loop()
try:
return new_loop.run_until_complete(func(*args, **kwargs))
finally:
new_loop.close()
else:
return loop.run_until_complete(func(*args, **kwargs))
return wrapper
def is_async(func: Callable) -> bool:
"""
Check if a function is asynchronous.
Args:
func: Function to check
Returns:
True if the function is asynchronous, False otherwise
"""
return inspect.iscoroutinefunction(func) or inspect.isawaitable(func)
def to_async(func: Callable[..., T]) -> Callable[..., Any]:
"""
Convert a synchronous function to an asynchronous one.
Args:
func: Synchronous function
Returns:
Asynchronous function
Example:
sync_func = lambda x: x * 2
async_func = to_async(sync_func)
result = await async_func(5) # Returns 10
"""
@functools.wraps(func)
async def wrapper(*args, **kwargs) -> T:
return func(*args, **kwargs)
return wrapper
def to_sync(func: Callable[..., Any]) -> Callable[..., Any]:
"""
Convert an asynchronous function to a synchronous one.
Args:
func: Asynchronous function
Returns:
Synchronous function
Example:
async def async_func(x):
return x * 2
sync_func = to_sync(async_func)
result = sync_func(5) # Returns 10, no await needed
"""
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Any:
return run_async(func)(*args, **kwargs)
return wrapper
def deep_merge(dict1: Dict[str, Any], dict2: Dict[str, Any]) -> Dict[str, Any]:
"""
Deeply merge two dictionaries.
The second dictionary takes precedence when there are conflicts.
Args:
dict1: First dictionary
dict2: Second dictionary
Returns:
Merged dictionary
"""
result = dict1.copy()
for key, value in dict2.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge(result[key], value)
else:
result[key] = value
return result
def flatten_dict(d: Dict[str, Any], parent_key: str = '', separator: str = '.') -> Dict[str, Any]:
"""
Flatten a nested dictionary.
Args:
d: Dictionary to flatten
parent_key: Key of the parent dictionary
separator: Separator for keys
Returns:
Flattened dictionary
Example:
flatten_dict({'a': {'b': 1, 'c': 2}, 'd': 3})
# Returns {'a.b': 1, 'a.c': 2, 'd': 3}
"""
items = []
for key, value in d.items():
new_key = f"{parent_key}{separator}{key}" if parent_key else key
if isinstance(value, dict):
items.extend(flatten_dict(value, new_key, separator).items())
else:
items.append((new_key, value))
return dict(items)
def unflatten_dict(d: Dict[str, Any], separator: str = '.') -> Dict[str, Any]:
"""
Unflatten a flattened dictionary.
Args:
d: Flattened dictionary
separator: Separator for keys
Returns:
Nested dictionary
Example:
unflatten_dict({'a.b': 1, 'a.c': 2, 'd': 3})
# Returns {'a': {'b': 1, 'c': 2}, 'd': 3}
"""
result = {}
for key, value in d.items():
parts = key.split(separator)
current = result
for part in parts[:-1]:
if part not in current:
current[part] = {}
current = current[part]
current[parts[-1]] = value
return result
def get_nested_value(d: Dict[str, Any], key_path: str, separator: str = '.', default: Any = None) -> Any:
"""
Get a value from a nested dictionary using a key path.
Args:
d: Dictionary to get the value from
key_path: Path to the key, separated by separator
separator: Separator for keys
default: Default value if the key doesn't exist
Returns:
Value at the key path, or default if not found
Example:
get_nested_value({'a': {'b': {'c': 42}}}, 'a.b.c') # Returns 42
"""
parts = key_path.split(separator)
current = d
try:
for part in parts:
current = current[part]
return current
except (KeyError, TypeError):
return default
def set_nested_value(d: Dict[str, Any], key_path: str, value: Any, separator: str = '.') -> Dict[str, Any]:
"""
Set a value in a nested dictionary using a key path.
Args:
d: Dictionary to set the value in
key_path: Path to the key, separated by separator
value: Value to set
separator: Separator for keys
Returns:
Modified dictionary
Example:
set_nested_value({'a': {'b': {}}}, 'a.b.c', 42)
# Returns {'a': {'b': {'c': 42}}}
"""
parts = key_path.split(separator)
current = d
for part in parts[:-1]:
if part not in current or not isinstance(current[part], dict):
current[part] = {}
current = current[part]
current[parts[-1]] = value
return d
def get_thread_stack_traces() -> Dict[int, str]:
"""
Get stack traces for all threads.
Returns:
Dictionary mapping thread IDs to stack traces
"""
traces = {}
for thread_id, frame in sys._current_frames().items():
trace = ''.join(traceback.format_stack(frame))
traces[thread_id] = trace
return traces
def log_thread_stack_traces(log_level: int = logging.INFO) -> None:
"""
Log stack traces for all threads.
Args:
log_level: Logging level
"""
traces = get_thread_stack_traces()
for thread_id, trace in traces.items():
thread_name = None
for thread in threading.enumerate():
if thread.ident == thread_id:
thread_name = thread.name
break
identifier = f"{thread_name} ({thread_id})" if thread_name else f"Thread-{thread_id}"
logger.log(log_level, f"Stack trace for thread {identifier}:\n{trace}")
def register_signal_handler(sig: signal.Signals, handler: Callable[[int, Any], None]) -> None:
"""
Register a signal handler.
Args:
sig: Signal to handle
handler: Signal handler function
Example:
def handle_sigterm(signum, frame):
print("Received SIGTERM")
register_signal_handler(signal.SIGTERM, handle_sigterm)
"""
try:
signal.signal(sig, handler)
except (ValueError, OSError):
logger.warning(f"Failed to register signal handler for {sig.name}")
@contextmanager
def open_atomic(file_path: Union[str, Path], mode: str = 'w', **kwargs) -> None:
"""
Context manager for atomic file operations.
This writes to a temporary file and then renames it to the target file,
which is an atomic operation on most file systems.
Args:
file_path: Path to the file
mode: File mode
**kwargs: Additional arguments for open()
Example:
with open_atomic('data.txt', 'w') as f:
f.write('Hello, world!')
"""
file_path = Path(file_path)
temp_path = file_path.with_suffix(f"{file_path.suffix}.tmp")
try:
with open(temp_path, mode, **kwargs) as f:
yield f
# Ensure the file is flushed to disk
os.fsync(f.fileno())
# Atomic rename
os.replace(temp_path, file_path)
except Exception:
# Clean up the temporary file
if temp_path.exists():
temp_path.unlink()
raise
def chunked_iterable(iterable, chunk_size: int):
"""
Break an iterable into chunks.
Args:
iterable: Iterable to chunk
chunk_size: Size of each chunk
Yields:
Chunks of the iterable
Example:
for chunk in chunked_iterable(range(10), 3):
print(chunk)
# Prints: [0, 1, 2], [3, 4, 5], [6, 7, 8], [9]
"""
chunk = []