# flytekit.core.checkpointer

## Directory

### Classes

| Class | Description |
|-|-|
| [`Checkpoint`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.checkpointer/page.md#flytekitcorecheckpointercheckpoint) | Base class for Checkpoint system. |
| [`SyncCheckpoint`](https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.checkpointer/page.md#flytekitcorecheckpointersynccheckpoint) | This class is NOT THREAD-SAFE!. |

## flytekit.core.checkpointer.Checkpoint

Base class for Checkpoint system. Checkpoint system allows reading and writing custom checkpoints from user
scripts

### Methods

| Method | Description |
|-|-|
| [`prev_exists()`](#prev_exists) |  |
| [`read()`](#read) | This should only be used if there is a singular checkpoint file written. |
| [`restore()`](#restore) | Given a path, if a previous checkpoint exists, will be downloaded to this path. |
| [`save()`](#save) | Usage: If you have a io. |
| [`write()`](#write) | This will overwrite the checkpoint. |

#### prev_exists()

```python
def prev_exists()
```
#### read()

```python
def read()
```
This should only be used if there is a singular checkpoint file written. If more than one checkpoint file is
found, this will raise a ValueError

#### restore()

```python
def restore(
    path: typing.Union[pathlib.Path, str],
) -> typing.Optional[pathlib.Path]
```
Given a path, if a previous checkpoint exists, will be downloaded to this path.
If download is successful the downloaded path is returned

> [!NOTE]
> Download will not be performed, if the checkpoint was previously restored. The method will return the
  previously downloaded path.

| Parameter | Type | Description |
|-|-|-|
| `path` | `typing.Union[pathlib.Path, str]` | |

#### save()

```python
def save(
    cp: typing.Union[pathlib.Path, str, _io.BufferedReader],
)
```
Usage: If you have a io.BufferedReader then the following should work

```python
with input_file.open(mode="rb") as b:
    checkpointer.save(b)
```

| Parameter | Type | Description |
|-|-|-|
| `cp` | `typing.Union[pathlib.Path, str, _io.BufferedReader]` | Checkpoint file (path, str path or a io.BufferedReader) |

#### write()

```python
def write(
    b: bytes,
)
```
This will overwrite the checkpoint. It can be retrieved using read or restore

| Parameter | Type | Description |
|-|-|-|
| `b` | `bytes` | |

## flytekit.core.checkpointer.SyncCheckpoint

This class is NOT THREAD-SAFE!
Sync Checkpoint, will synchronously checkpoint a user given file or folder.
It will also synchronously download / restore previous checkpoints, when restore is invoked.

TODO: Implement an async checkpoint system

### Parameters

```python
class SyncCheckpoint(
    checkpoint_dest: str,
    checkpoint_src: typing.Optional[str],
)
```
checkpoint_src: If a previous checkpoint should exist, this path should be set to the folder that contains the checkpoint information
checkpoint_dest: Location where the new checkpoint should be copied to

| Parameter | Type | Description |
|-|-|-|
| `checkpoint_dest` | `str` | |
| `checkpoint_src` | `typing.Optional[str]` | |

### Methods

| Method | Description |
|-|-|
| [`prev_exists()`](#prev_exists) |  |
| [`read()`](#read) | This should only be used if there is a singular checkpoint file written. |
| [`restore()`](#restore) | Given a path, if a previous checkpoint exists, will be downloaded to this path. |
| [`save()`](#save) | Usage: If you have a io. |
| [`write()`](#write) | This will overwrite the checkpoint. |

#### prev_exists()

```python
def prev_exists()
```
#### read()

```python
def read()
```
This should only be used if there is a singular checkpoint file written. If more than one checkpoint file is
found, this will raise a ValueError

#### restore()

```python
def restore(
    path: typing.Union[pathlib.Path, str, NoneType],
) -> typing.Optional[pathlib.Path]
```
Given a path, if a previous checkpoint exists, will be downloaded to this path.
If download is successful the downloaded path is returned

> [!NOTE]
> Download will not be performed, if the checkpoint was previously restored. The method will return the
  previously downloaded path.

| Parameter | Type | Description |
|-|-|-|
| `path` | `typing.Union[pathlib.Path, str, NoneType]` | |

#### save()

```python
def save(
    cp: typing.Union[pathlib.Path, str, _io.BufferedReader],
)
```
Usage: If you have a io.BufferedReader then the following should work

```python
with input_file.open(mode="rb") as b:
    checkpointer.save(b)
```

| Parameter | Type | Description |
|-|-|-|
| `cp` | `typing.Union[pathlib.Path, str, _io.BufferedReader]` | Checkpoint file (path, str path or a io.BufferedReader) |

#### write()

```python
def write(
    b: bytes,
)
```
This will overwrite the checkpoint. It can be retrieved using read or restore

| Parameter | Type | Description |
|-|-|-|
| `b` | `bytes` | |

---
**Source**: https://github.com/unionai/unionai-docs/blob/main/content/api-reference/flytekit-sdk/packages/flytekit.core.checkpointer.md
**HTML**: https://www.union.ai/docs/v1/union/api-reference/flytekit-sdk/packages/flytekit.core.checkpointer/
