-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathutils.py
More file actions
66 lines (47 loc) · 1.6 KB
/
Copy pathutils.py
File metadata and controls
66 lines (47 loc) · 1.6 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
import subprocess
import os
import shutil
from pathlib import Path
from datetime import datetime, timedelta
import typing as T
try:
import psutil
except ImportError:
psutil = None
# pip install psutil will improve CPU utilization.
git = shutil.which("git")
Pathlike = T.Union[str, Path]
__all__ = ["gitrev", "get_cpu_count", "ymdhourdec2datetime", "datetime2ymd_hourdec"]
def gitrev() -> str:
if not git:
return ""
return subprocess.check_output([git, "rev-parse", "--short", "HEAD"], universal_newlines=True).strip()
def get_cpu_count() -> int:
""" get a physical CPU count
Returns
-------
count: int
detect number of physical CPU
"""
max_cpu = None
# without psutil, hyperthreaded CPU may overestimate physical count by factor of 2 (or more)
if psutil is not None:
max_cpu = psutil.cpu_count(logical=False)
extradiv = 1
if max_cpu is None:
max_cpu = psutil.cpu_count()
extradiv = 2
if max_cpu is None:
max_cpu = os.cpu_count()
extradiv = 2
return max_cpu // extradiv
def ymdhourdec2datetime(year: int, month: int, day: int, hourdec: float) -> datetime:
"""
convert year,month,day + decimal hour HH.hhh to time
"""
return datetime(year, month, day, int(hourdec), int((hourdec * 60) % 60)) + timedelta(seconds=(hourdec * 3600) % 60)
def datetime2ymd_hourdec(dt: datetime) -> str:
"""
convert datetime to ymd_hourdec string for filename stem
"""
return dt.strftime("%Y%m%d") + f"_{dt.hour*3600 + dt.minute*60 + dt.second + dt.microsecond/1e6:12.6f}"