-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
43 lines (31 loc) · 1.01 KB
/
Copy pathutil.py
File metadata and controls
43 lines (31 loc) · 1.01 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
# File: util.py
# Created Date: Sunday January 22nd 2023
# Author: Steven Atkinson (steven@atkinson.mn)
"""
Helpful utilities
"""
import warnings
from datetime import datetime
def timestamp() -> str:
t = datetime.now()
return f"{t.year:04d}-{t.month:02d}-{t.day:02d}-{t.hour:02d}-{t.minute:02d}-{t.second:02d}"
class _FilterWarnings(object):
"""
Context manager.
Kinda hacky since it doesn't restore to what it was before, but to what the
global default is.
"""
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def __enter__(self):
warnings.filterwarnings(*self._args, **self._kwargs)
def __exit__(self, exc_type, exc_val, exc_tb):
warnings.resetwarnings()
def filter_warnings(*args, **kwargs):
"""
Simple-but-kinda-hacky context manager that allows you to use
`warnings.filterwarnings()` / `warnings.resetwarnings()` as if it were a
context manager.
"""
return _FilterWarnings(*args, **kwargs)