Skip to content

Commit

Permalink
Merge 6870db9 into 5955d09
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwoerpel committed Jun 17, 2024
2 parents 5955d09 + 6870db9 commit 496e33d
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 112 deletions.
7 changes: 4 additions & 3 deletions anystore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from anystore.settings import Settings
from anystore.store import get_store

cli = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=False)
console = Console(stderr=True)

settings = Settings()
cli = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=settings.debug)
console = Console(stderr=True)

state = {"uri": settings.uri, "pickle": False}

Expand All @@ -23,6 +22,8 @@ def __enter__(self):

def __exit__(self, e, msg, _):
if e is not None:
if settings.debug:
raise e
console.print(f"[red][bold]{e.__name__}[/bold]: {msg}[/red]")
raise typer.Exit(code=1)

Expand Down
5 changes: 4 additions & 1 deletion anystore/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def smart_read(uri: Uri, mode: str | None = DEFAULT_MODE, **kwargs) -> Any:


def smart_write(
uri, content: bytes | str, mode: str | None = DEFAULT_WRITE_MODE, **kwargs
uri: Uri, content: bytes | str, mode: str | None = DEFAULT_WRITE_MODE, **kwargs
) -> None:
if uri == "-":
if isinstance(content, str):
content = content.encode()
with smart_open(uri, mode, **kwargs) as fh:
fh.write(content)
6 changes: 3 additions & 3 deletions anystore/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
log = logging.getLogger(__name__)


@lru_cache(1000)
@lru_cache(128)
def cached_from_uri(uri: Uri) -> str:
"""
Cache remote sources on runtime
Expand All @@ -30,7 +30,7 @@ class JsonMixin:

@classmethod
def from_json_str(cls, data: str, **kwargs) -> Self:
loaded = clean_dict({**orjson.loads(data), **kwargs})
loaded = clean_dict({**orjson.loads(data), **clean_dict(kwargs)})
return cls(**loaded)

@classmethod
Expand All @@ -46,7 +46,7 @@ class YamlMixin:

@classmethod
def from_yaml_str(cls, data: str, **kwargs) -> Self:
loaded = clean_dict({**yaml.safe_load(data), **kwargs})
loaded = clean_dict({**yaml.safe_load(data), **clean_dict(kwargs)})
return cls(**loaded)

@classmethod
Expand Down
5 changes: 5 additions & 0 deletions anystore/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ def from_store(value: bytes, serialization_mode: Mode | None = "auto") -> Any:
try:
return cloudpickle.loads(value)
except Exception:
if isinstance(value, bytes):
try:
return value.decode()
except UnicodeDecodeError:
pass
return value
3 changes: 3 additions & 0 deletions anystore/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
from anystore.serialize import Mode

Expand All @@ -11,6 +12,8 @@ class Settings(BaseSettings):
serialization_mode: Mode | None = "auto"
raise_on_nonexist: bool = True

debug: bool = Field(alias="debug", default=False)


class SqlSettings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="anystore_sql_")
Expand Down
198 changes: 99 additions & 99 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_data(*args, **kwargs):
key = make_signature_key("x")
assert store.get(key) is None
assert get_data("x") == "data"
assert store.get(key) == b"data"
assert store.get(key) == "data"

# store as arg, custom key func
@anycache(store=store, key_func=lambda *args, **kwargs: args[0].upper())
Expand All @@ -21,7 +21,7 @@ def get_data2(*args, **kwargs):

assert store.get("X") is None
assert get_data2("x") == "data2"
assert store.get("X") == b"data2"
assert store.get("X") == "data2"

# not yet existing store
@anycache(uri=tmp_path / "foo", key_func=lambda x: x)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

def test_serialize():
# mode: auto
assert serialize.from_store(serialize.to_store("hello")) == b"hello"
assert serialize.from_store(serialize.to_store(b"hello")) == b"hello"
assert serialize.from_store(serialize.to_store("hello")) == "hello"
assert serialize.from_store(serialize.to_store(b"hello")) == "hello"
assert serialize.from_store(serialize.to_store(1)) == 1
assert serialize.from_store(serialize.to_store(1.1)) == 1.1
assert serialize.from_store(serialize.to_store(None)) is None
assert serialize.from_store(serialize.to_store(True)) is True
assert serialize.from_store(serialize.to_store(False)) is False
assert serialize.from_store(serialize.to_store("")) == b""
assert serialize.from_store(serialize.to_store("")) == ""

assert serialize.from_store(serialize.to_store({"a": 1})) == {"a": 1}
assert serialize.from_store(serialize.to_store({"a": "1"})) == {"a": "1"}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _test_store(uri: str) -> bool:
assert isinstance(store, BaseStore)
key = "test"
store.put(key, "foo")
assert store.get(key) == b"foo"
assert store.get(key) == "foo"
assert store.get(key, mode="r") == "foo"
# overwrite
store.put(key, False)
Expand Down

0 comments on commit 496e33d

Please sign in to comment.