Skip to content

Commit

Permalink
Add __repr__ to store classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Oct 30, 2024
1 parent c318a53 commit 2469d5b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2 deletions.
7 changes: 6 additions & 1 deletion obstore/python/obstore/store/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# TODO: move to reusable types package
from pathlib import Path

from ._aws import S3ConfigKey as S3ConfigKey
from ._aws import S3Store as S3Store
from ._azure import AzureConfigKey as AzureConfigKey
Expand All @@ -16,11 +18,14 @@ class LocalStore:
Can optionally be created with a directory prefix.
```py
from pathlib import Path
store = LocalStore()
store = LocalStore(prefix="/path/to/directory")
store = LocalStore(prefix=Path("."))
```
"""
def __init__(self, prefix: str | None = None) -> None: ...
def __init__(self, prefix: str | Path | None = None) -> None: ...

class MemoryStore:
"""A fully in-memory implementation of ObjectStore.
Expand Down
5 changes: 5 additions & 0 deletions pyo3-object_store/src/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ impl PyS3Store {
}
Ok(Self(Arc::new(builder.build()?)))
}

fn __repr__(&self) -> String {
let repr = self.0.to_string();
repr.replacen("AmazonS3", "S3Store", 1)
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down
5 changes: 5 additions & 0 deletions pyo3-object_store/src/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ impl PyAzureStore {
}
Ok(Self(Arc::new(builder.build()?)))
}

fn __repr__(&self) -> String {
let repr = self.0.to_string();
repr.replacen("MicrosoftAzure", "AzureStore", 1)
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down
5 changes: 5 additions & 0 deletions pyo3-object_store/src/gcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ impl PyGCSStore {
}
Ok(Self(Arc::new(builder.build()?)))
}

fn __repr__(&self) -> String {
let repr = self.0.to_string();
repr.replacen("GoogleCloudStorage", "GCSStore", 1)
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down
4 changes: 4 additions & 0 deletions pyo3-object_store/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl PyHttpStore {
pub fn into_inner(self) -> Arc<HttpStore> {
self.0
}

fn __repr__(&self) -> String {
self.0.to_string()
}
}

#[pymethods]
Expand Down
5 changes: 5 additions & 0 deletions pyo3-object_store/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ impl PyLocalStore {
};
Ok(Self(Arc::new(fs)))
}

fn __repr__(&self) -> String {
let repr = self.0.to_string();
repr.replacen("LocalFileSystem", "LocalStore", 1)
}
}
8 changes: 7 additions & 1 deletion pyo3-object_store/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::Arc;

use object_store::memory::InMemory;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyString;

/// A Python-facing wrapper around an [`InMemory`].
#[pyclass(name = "MemoryStore")]
Expand All @@ -13,11 +15,15 @@ impl AsRef<Arc<InMemory>> for PyMemoryStore {
}
}

impl PyMemoryStore {
impl<'py> PyMemoryStore {
/// Consume self and return the underlying [`InMemory`].
pub fn into_inner(self) -> Arc<InMemory> {
self.0
}

fn __repr__(&'py self, py: Python<'py>) -> &Bound<'py, PyString> {
intern!(py, "MemoryStore")
}
}

#[pymethods]
Expand Down
Empty file added tests/store/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/store/test_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pathlib import Path

import obstore as obs
from obstore.store import LocalStore


def test_local_store():
here = Path(".")
store = LocalStore(here)
list_result = obs.list(store).collect()
assert any(x["path"] == "test_local.py" for x in list_result)


def test_repr():
here = Path(".")
store = LocalStore(here)
assert repr(store).startswith("LocalStore")

0 comments on commit 2469d5b

Please sign in to comment.