Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __repr__ to store classes #61

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 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,15 @@ 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: ...
def __repr__(self) -> str: ...

class MemoryStore:
"""A fully in-memory implementation of ObjectStore.
Expand All @@ -31,6 +37,7 @@ class MemoryStore:
```
"""
def __init__(self) -> None: ...
def __repr__(self) -> str: ...

ObjectStore = AzureStore | GCSStore | HTTPStore | S3Store | LocalStore | MemoryStore
"""All supported ObjectStore implementations."""
2 changes: 2 additions & 0 deletions obstore/python/obstore/store/_aws.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,5 @@ class S3Store:
Returns:
S3Store
"""

def __repr__(self) -> str: ...
2 changes: 2 additions & 0 deletions obstore/python/obstore/store/_azure.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ class AzureStore:
Returns:
AzureStore
"""

def __repr__(self) -> str: ...
2 changes: 2 additions & 0 deletions obstore/python/obstore/store/_gcs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ class GCSStore:
Returns:
GCSStore
"""

def __repr__(self) -> str: ...
2 changes: 2 additions & 0 deletions obstore/python/obstore/store/_http.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ class HTTPStore:
Returns:
HTTPStore
"""

def __repr__(self) -> str: ...
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("test_local.py" in x["path"] for x in list_result)


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