Skip to content

capturegraph.data.save.markers #

Save Markers#

Marker classes that signal save intent when traversing CaptureTarget data. When a traversal function encounters these markers, it serializes the contained value and writes it to the appropriate file location.

Example
import capturegraph.data as cg

# Always overwrite
session.location = cg.SaveOverWith(
    cg.Location(latitude=42.0, longitude=-76.0, altitude=100.0)
)

# Only write if file doesn't exist
session.weather = cg.SaveIfMissing(weather)

# Commit changes to disk
cg.commit(target)

SaveOverWith #

Marker that signals the value should always be saved.

When traversing a CaptureTarget, if a SaveOverWith is encountered, the contained value will be serialized and written to the corresponding file location, overwriting any existing file.

Attributes:

Name Type Description
value

The value to be saved.

Example
import capturegraph.data as cg

save_marker = cg.SaveOverWith(cg.Location(-76.0, 42.0, 100.0))
save_marker.value  # Location(lat=42.000000, lon=-76.000000, alt=100.0m)
Source code in capturegraph-lib/capturegraph/data/save/markers.py
class SaveOverWith:
    """Marker that signals the value should always be saved.

    When traversing a CaptureTarget, if a SaveOverWith is encountered,
    the contained value will be serialized and written to the
    corresponding file location, overwriting any existing file.

    Attributes:
        value: The value to be saved.

    Example:
        ```python
        import capturegraph.data as cg

        save_marker = cg.SaveOverWith(cg.Location(-76.0, 42.0, 100.0))
        save_marker.value  # Location(lat=42.000000, lon=-76.000000, alt=100.0m)
        ```
    """

    def __init__(self, value: Any):
        """Create a SaveOverWith marker.

        Args:
            value: The value to be saved. Must have a registered saver
                   or a to_json() method.
        """
        self.value = value
        self.json = save_to_json(value)

    def to_json(self) -> dict:
        """Convert the wrapped value to JSON."""
        return self.json

    def __repr__(self) -> str:
        return f"SaveOverWith({self.value!r})"

__init__(value) #

Create a SaveOverWith marker.

Parameters:

Name Type Description Default
value Any

The value to be saved. Must have a registered saver or a to_json() method.

required
Source code in capturegraph-lib/capturegraph/data/save/markers.py
def __init__(self, value: Any):
    """Create a SaveOverWith marker.

    Args:
        value: The value to be saved. Must have a registered saver
               or a to_json() method.
    """
    self.value = value
    self.json = save_to_json(value)

to_json() #

Convert the wrapped value to JSON.

Source code in capturegraph-lib/capturegraph/data/save/markers.py
def to_json(self) -> dict:
    """Convert the wrapped value to JSON."""
    return self.json

SaveIfMissing #

Marker that signals the value should only be saved if no file exists.

When traversing a CaptureTarget, if a SaveIfMissing is encountered, the value will only be saved if the corresponding file does not already exist on disk.

Attributes:

Name Type Description
value

The value to be saved if no file exists.

Example
import capturegraph.data as cg

save_marker = cg.SaveIfMissing(weather)
save_marker.value  # Weather(temp=22.5°C, condition=cloudy)
Source code in capturegraph-lib/capturegraph/data/save/markers.py
class SaveIfMissing:
    """Marker that signals the value should only be saved if no file exists.

    When traversing a CaptureTarget, if a SaveIfMissing is encountered,
    the value will only be saved if the corresponding file does not
    already exist on disk.

    Attributes:
        value: The value to be saved if no file exists.

    Example:
        ```python
        import capturegraph.data as cg

        save_marker = cg.SaveIfMissing(weather)
        save_marker.value  # Weather(temp=22.5°C, condition=cloudy)
        ```
    """

    def __init__(self, value: Any):
        """Create a SaveIfMissing marker.

        Args:
            value: The value to be saved. Must have a registered saver
                   or a to_json() method.
        """
        self.value = value
        self.json = save_to_json(value)

    def to_json(self) -> dict:
        """Convert the wrapped value to JSON."""
        return self.json

    def __repr__(self) -> str:
        return f"SaveIfMissing({self.value!r})"

__init__(value) #

Create a SaveIfMissing marker.

Parameters:

Name Type Description Default
value Any

The value to be saved. Must have a registered saver or a to_json() method.

required
Source code in capturegraph-lib/capturegraph/data/save/markers.py
def __init__(self, value: Any):
    """Create a SaveIfMissing marker.

    Args:
        value: The value to be saved. Must have a registered saver
               or a to_json() method.
    """
    self.value = value
    self.json = save_to_json(value)

to_json() #

Convert the wrapped value to JSON.

Source code in capturegraph-lib/capturegraph/data/save/markers.py
def to_json(self) -> dict:
    """Convert the wrapped value to JSON."""
    return self.json