afscgap.model_util

Utility functions to create implementors of afscgap.model interfaces.

(c) 2024 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.

This file is part of afscgap released under the BSD 3-Clause License. See LICENSE.md.

 1"""
 2Utility functions to create implementors of afscgap.model interfaces.
 3
 4(c) 2024 Regents of University of California / The Eric and Wendy Schmidt Center
 5for Data Science and the Environment at UC Berkeley.
 6
 7This file is part of afscgap released under the BSD 3-Clause License. See
 8LICENSE.md.
 9"""
10from afscgap.typesdef import OPT_FLOAT
11from afscgap.typesdef import OPT_INT
12
13
14def get_opt_float(target) -> OPT_FLOAT:
15    """Attempt to parse a value as a float, returning None if there is an error.
16
17    Args:
18        target: The value to try to interpret as a float.
19
20    Returns:
21        The value of target as a float or None if there was an issue in parsing
22        like that target is None.
23    """
24    if target:
25        try:
26            return float(target)
27        except ValueError:
28            return None
29    else:
30        return None
31
32
33def get_opt_int(target) -> OPT_INT:
34    """Attempt to parse a value as an int, returning None if there is an error.
35
36    Args:
37        target: The value to try to interpret as an int.
38
39    Returns:
40        The value of target as an int or None if there was an issue in parsing
41        like that target is None.
42    """
43    if target:
44        try:
45            return int(target)
46        except ValueError:
47            return None
48    else:
49        return None
50
51
52def assert_float_present(target: OPT_FLOAT) -> float:
53    """Assert that a value is non-None before returning that value.
54
55    Args:
56        target: The value to check if not None.
57
58    Raises:
59        AssertionError: Raised if target is None.
60
61    Returns:
62        The value of target if not None.
63    """
64    if target is None:
65        raise ValueError('Encountered unexpected None.')
66
67    return target
68
69
70def assert_int_present(target: OPT_INT) -> int:
71    """Assert that a value is non-None before returning that value.
72
73    Args:
74        target: The value to check if not None.
75
76    Raises:
77        AssertionError: Raised if target is None.
78
79    Returns:
80        The value of target if not None.
81    """
82    if target is None:
83        raise ValueError('Encountered unexpected None.')
84
85    return target
def get_opt_float(target) -> Optional[float]:
15def get_opt_float(target) -> OPT_FLOAT:
16    """Attempt to parse a value as a float, returning None if there is an error.
17
18    Args:
19        target: The value to try to interpret as a float.
20
21    Returns:
22        The value of target as a float or None if there was an issue in parsing
23        like that target is None.
24    """
25    if target:
26        try:
27            return float(target)
28        except ValueError:
29            return None
30    else:
31        return None

Attempt to parse a value as a float, returning None if there is an error.

Arguments:
  • target: The value to try to interpret as a float.
Returns:

The value of target as a float or None if there was an issue in parsing like that target is None.

def get_opt_int(target) -> Optional[int]:
34def get_opt_int(target) -> OPT_INT:
35    """Attempt to parse a value as an int, returning None if there is an error.
36
37    Args:
38        target: The value to try to interpret as an int.
39
40    Returns:
41        The value of target as an int or None if there was an issue in parsing
42        like that target is None.
43    """
44    if target:
45        try:
46            return int(target)
47        except ValueError:
48            return None
49    else:
50        return None

Attempt to parse a value as an int, returning None if there is an error.

Arguments:
  • target: The value to try to interpret as an int.
Returns:

The value of target as an int or None if there was an issue in parsing like that target is None.

def assert_float_present(target: Optional[float]) -> float:
53def assert_float_present(target: OPT_FLOAT) -> float:
54    """Assert that a value is non-None before returning that value.
55
56    Args:
57        target: The value to check if not None.
58
59    Raises:
60        AssertionError: Raised if target is None.
61
62    Returns:
63        The value of target if not None.
64    """
65    if target is None:
66        raise ValueError('Encountered unexpected None.')
67
68    return target

Assert that a value is non-None before returning that value.

Arguments:
  • target: The value to check if not None.
Raises:
  • AssertionError: Raised if target is None.
Returns:

The value of target if not None.

def assert_int_present(target: Optional[int]) -> int:
71def assert_int_present(target: OPT_INT) -> int:
72    """Assert that a value is non-None before returning that value.
73
74    Args:
75        target: The value to check if not None.
76
77    Raises:
78        AssertionError: Raised if target is None.
79
80    Returns:
81        The value of target if not None.
82    """
83    if target is None:
84        raise ValueError('Encountered unexpected None.')
85
86    return target

Assert that a value is non-None before returning that value.

Arguments:
  • target: The value to check if not None.
Raises:
  • AssertionError: Raised if target is None.
Returns:

The value of target if not None.