-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (32 loc) · 1.2 KB
/
utils.py
File metadata and controls
43 lines (32 loc) · 1.2 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
"""Utility functions for the notebooks"""
import random
from pathlib import Path
from datetime import date, timedelta
class Utils:
"""Utility class for common operations."""
@staticmethod
def project_root_path() -> Path:
"""
Returns the root path of the project.
"""
return Path(__file__).parent.parent
@staticmethod
def calculate_dob_from_age(age: int, today: date | None = None) -> date:
"""
Calculate the date of birth from the given age.
Return random month and day in the year of birth.
Args:
age (int): The age in years.
Returns:
date: The calculated date of birth.
"""
if today is None:
today = date.today()
year = today.year - age
start_date = today.replace(year=year) + timedelta(days=1)
end_date = today.replace(year=year + 1) - timedelta(days=1)
# The date will be between today +1 until this day next year -1
delta_days = (end_date - start_date).days
random_days = random.randint(0, delta_days)
dob = start_date + timedelta(days=random_days)
return dob