Пример #1
0
def dump_str(val: Optional[str]) -> str:
    """Serializes a string.

    Args:
    - val (Optional[str]): Value to serialize. Returns - if None or empty.

    Returns:
    - str: String (non-empty)
    """
    if val is None or is_undefined(val) or val.strip() == "":
        return "-"
    return val
Пример #2
0
def dump_percentage(val: Optional[float]) -> str:
    """Serializes a float as a percentage.

    Args:
    - val (Optional[float]): Value to serialize, between 0 and 1. Returns 0 if None.

    Returns:
    - str: String with 1 decimal (non-empty)
    """
    if val is None or is_undefined(val):
        return "0.0"
    return "{:.1f}".format(val * 100)
Пример #3
0
def dump_int(val: Optional[Union[int, float]]) -> str:
    """Serializes an int or float.

    Args:
    - val (Optional[Union[int, float]]): Value to serialize. Returns 0 if None

    Returns:
    - str: String (non-empty)
    """
    if val is None or is_undefined(val):
        return "0"
    return "{:.0f}".format(val)
Пример #4
0
    def dump_gps(coordinates: geoalchemy2.Geography) -> Optional[str]:
        """Serializes a POINT geography into  a lat, lng string

        Args:
        - coordinates (geoalchemy2.Geography): Value to serialize - if None will return None

        Returns:
        - Optional[str]: Serialized string
        """
        if coordinates is None or is_undefined(coordinates):
            return None
        shape = geoalchemy2.shape.to_shape(coordinates)
        return "{:f}, {:f}".format(shape.y, shape.x)
Пример #5
0
def dump_datetime(value: Optional[Union[datetime.datetime, datetime.date]],
                  fmt="json") -> Optional[str]:
    """Serializes Date and datetime objects into strings for JSON or other formats.

    Supported shorthand formats are:
    - json: "%Y-%m-%dT%H:%M:%S%z", e.g. 2021-02-01T21:49:25+00:0 (compatible ISO 8601)
    - gs: "%Y-%m-%d %H:%M:%S", e.g. 2021-02-01 21:49:25 (recognized by Google Sheets)

    Args:
    - value (Optional[Union[datetime.datetime, datetime.date]]): Value to serialize - if None will return None
    - fmt (str, optional): Format - if not recognized as shorthand format, will pass it to strftime. Defaults to "json".

    Returns:
    - Optional[str]: Serialized string
    """
    if value is None or is_undefined(value):
        return None
    if fmt == "json":
        fmt = "%Y-%m-%dT%H:%M:%S%z"
    elif fmt == "gs":
        fmt = "%Y-%m-%d %H:%M:%S"
    return value.strftime(fmt)