Skip to content

Models

apple_health_parser.models.parsed.ParsedData dataclass

Dataclass to store parsed data from the Apple Health export file.

Source code in apple_health_parser/models/parsed.py
 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
@dataclass
class ParsedData:
    """
    Dataclass to store parsed data from the Apple Health export file.
    """

    flag: str
    sources: list[str]
    dates: set[date]
    records: pd.DataFrame

    def __str__(self) -> str:
        """
        String representation of the ParsedData class.
        Includes the flag, sources, dates, and number of records.

        Example:
        ```bash
        =====================ParsedData=====================
        Flag:       HKQuantityTypeIdentifierRestingHeartRate
        Sources:    3 sources
        Dates:      144 dates
        Records:    145 records
        ```

        Returns:
            str: String representation of the ParsedData class
        """

        description = [
            f"{'Flag:':<12}{self.flag}",
            f"{'Sources:':<12}{len(self.sources)} sources",
            f"{'Dates:':<12}{len(self.dates)} dates",
            f"{'Records:':<12}{len(self.records)} records",
        ]

        max_len = len(max(description, key=len))
        description = [f"{'ParsedData':=^{max_len}}"] + description

        return "\n".join(description)

__str__()

String representation of the ParsedData class. Includes the flag, sources, dates, and number of records.

Example:

=====================ParsedData=====================
Flag:       HKQuantityTypeIdentifierRestingHeartRate
Sources:    3 sources
Dates:      144 dates
Records:    145 records

Returns:

Name Type Description
str str

String representation of the ParsedData class

Source code in apple_health_parser/models/parsed.py
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
def __str__(self) -> str:
    """
    String representation of the ParsedData class.
    Includes the flag, sources, dates, and number of records.

    Example:
    ```bash
    =====================ParsedData=====================
    Flag:       HKQuantityTypeIdentifierRestingHeartRate
    Sources:    3 sources
    Dates:      144 dates
    Records:    145 records
    ```

    Returns:
        str: String representation of the ParsedData class
    """

    description = [
        f"{'Flag:':<12}{self.flag}",
        f"{'Sources:':<12}{len(self.sources)} sources",
        f"{'Dates:':<12}{len(self.dates)} dates",
        f"{'Records:':<12}{len(self.records)} records",
    ]

    max_len = len(max(description, key=len))
    description = [f"{'ParsedData':=^{max_len}}"] + description

    return "\n".join(description)

apple_health_parser.models.records.HealthData

Bases: BaseModel

Source code in apple_health_parser/models/records.py
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
class HealthData(BaseModel):
    type: str = Field(
        alias="type",
        title="Type",
        description="Type of health data",
        examples=["HKQuantityTypeIdentifierHeartRate"],
    )
    source_name: str = Field(
        alias="sourceName",
        title="Source Name",
        description="Name of the source",
        examples=["Apple Watch"],
    )
    source_version: str = Field(
        alias="sourceVersion",
        title="Source Version",
        description="Version of the source",
        examples=["10.2"],
    )
    unit: str = Field(
        alias="unit",
        title="Unit",
        description="Unit of the health data",
        examples=["count/min", "kcal", "%"],
    )
    creation_date: datetime = Field(
        alias="creationDate",
        title="Creation Date",
        description="Date of creation",
        examples=["2021-01-01 00:00:00 +0200"],
    )
    start_date: datetime = Field(
        alias="startDate",
        title="Start Date",
        description="Date of measurement start",
        examples=["2021-01-01 00:00:00 +0200"],
    )
    end_date: datetime = Field(
        alias="endDate",
        title="End Date",
        description="Date of measurement end",
        examples=["2021-01-01 00:00:00 +0200"],
    )
    value: int | float = Field(
        alias="value",
        title="Value",
        description="Value of the health data",
        examples=[60, 120, 15.5],
    )

    @field_validator("creation_date", "start_date", "end_date", mode="before")
    @classmethod
    def check_date(cls, v: str) -> datetime:
        return datetime.strptime(v, "%Y-%m-%d %H:%M:%S %z")

    @field_validator("value", mode="before")
    @classmethod
    def str_to_numeric(cls, v) -> int | float:
        if type(v) is str:
            try:
                return int(v)
            except ValueError:
                return float(v)
        return v

apple_health_parser.models.records.HeartRateData

Bases: HealthData

Source code in apple_health_parser/models/records.py
79
80
81
82
83
84
85
86
87
88
89
90
91
class HeartRateData(HealthData):
    device: str = Field(title="Device", description="Device used for measurement")
    motion_context: str = Field(
        alias="motionContext",
        title="Motion Context",
        description="Context of motion (e.g. sedentary, active, unset)",
        examples=["Unset", "Sedentary", "Active"],
    )

    @field_validator("motion_context", mode="before")
    @classmethod
    def check_motion_context(cls, v: str) -> str:
        return MotionContext(v).name.lower().capitalize()