File: sizeof.py

package info (click to toggle)
dask.distributed 2022.12.1%2Bds.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,164 kB
  • sloc: python: 81,938; javascript: 1,549; makefile: 228; sh: 100
file content (23 lines) | stat: -rw-r--r-- 607 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
from __future__ import annotations

import logging

from dask.sizeof import sizeof
from dask.utils import format_bytes

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 failed. Defaulting to {format_bytes(int(default_size))}",
            exc_info=True,
        )
        return int(default_size)