File: sizeof.py

package info (click to toggle)
dask.distributed 2024.12.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,588 kB
  • sloc: python: 96,954; javascript: 1,549; sh: 390; makefile: 220
file content (24 lines) | stat: -rw-r--r-- 670 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import annotations

import logging

from dask.sizeof import sizeof
from dask.utils import format_bytes, typename

logger = logging.getLogger(__name__)


def safe_sizeof(obj: object, default_size: float = 1e6) -> int:
    """Safe variant of sizeof that captures and logs exceptions

    This returns a default size of 1e6 if the sizeof function fails
    """
    try:
        return sizeof(obj)
    except Exception:
        logger.warning(
            f"Sizeof calculation for object of type '{typename(obj)}' failed. "
            f"Defaulting to {format_bytes(int(default_size))}",
            exc_info=True,
        )
        return int(default_size)