Skip to content

Commit 4f8c8f2

Browse files
author
Roman Kitaev
committed
add default argument to Moka.get
1 parent 5da2d6f commit 4f8c8f2

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "moka-py"
3-
version = "0.1.9"
3+
version = "0.1.10"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

moka_py/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import asyncio
22
from functools import wraps, _make_key
3-
from .moka_py import Moka
3+
from .moka_py import Moka, get_version
44

55

6-
__all__ = ["Moka", "cached"]
6+
__all__ = ["Moka", "cached", "VERSION"]
7+
8+
VERSION = get_version()
79

810

911
def cached(maxsize=128, typed=False, *, ttl=None, tti=None, wait_concurrent=False):
1012
cache = Moka(maxsize, ttl, tti)
13+
empty = object()
1114

1215
def dec(fn):
1316
if asyncio.iscoroutinefunction(fn):
@@ -17,8 +20,8 @@ def dec(fn):
1720
@wraps(fn)
1821
async def inner(*args, **kwargs):
1922
key = _make_key(args, kwargs, typed)
20-
maybe_value = cache.get(key)
21-
if maybe_value is not None:
23+
maybe_value = cache.get(key, empty)
24+
if maybe_value is not empty:
2225
return maybe_value
2326
value = await fn(*args, **kwargs)
2427
cache.set(key, value)
@@ -30,8 +33,8 @@ def inner(*args, **kwargs):
3033
if wait_concurrent:
3134
return cache.get_with(key, lambda: fn(*args, **kwargs))
3235
else:
33-
maybe_value = cache.get(key)
34-
if maybe_value is not None:
36+
maybe_value = cache.get(key, empty)
37+
if maybe_value is not empty:
3538
return maybe_value
3639
value = fn(*args, **kwargs)
3740
cache.set(key, value)

moka_py/__init__.pyi

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import TypeVar, Optional, Generic, Hashable, Union, Callable, Any
1+
from typing import TypeVar, Optional, Generic, Hashable, Union, Callable, Any, overload
22

33

44
K = TypeVar("K", bound=Hashable)
55
V = TypeVar("V")
6+
D = TypeVar("D")
67
Fn = TypeVar("Fn", bound=Callable[..., Any])
78

89

@@ -16,7 +17,11 @@ class Moka(Generic[K, V]):
1617

1718
def set(self, key: K, value: V) -> None: ...
1819

19-
def get(self, key: K) -> Optional[V]: ...
20+
@overload
21+
def get(self, key: K, default: D) -> Union[V, D]: ...
22+
23+
@overload
24+
def get(self, key: K, default: Optional[D] = None) -> Optional[Union[V, D]]: ...
2025

2126
def get_with(self, key: K, initializer: Callable[[], V]) -> V:
2227
"""

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,19 @@ impl Moka {
123123
Ok(())
124124
}
125125

126-
fn get(&self, py: Python, key: PyObject) -> PyResult<Option<PyObject>> {
126+
#[pyo3(signature = (key, default=None))]
127+
fn get(
128+
&self,
129+
py: Python,
130+
key: PyObject,
131+
default: Option<PyObject>,
132+
) -> PyResult<Option<PyObject>> {
127133
let hashable_key = AnyKey::new_with_gil(key, py)?;
128134
let value = py.allow_threads(|| self.0.get(&hashable_key));
129-
Ok(value.map(|obj| obj.clone_ref(py)))
135+
Ok(match value.map(|obj| obj.clone_ref(py)) {
136+
None => default.map(|v| v.clone_ref(py)),
137+
Some(v) => Some(v),
138+
})
130139
}
131140

132141
fn get_with(&self, py: Python, key: PyObject, initializer: PyObject) -> PyResult<PyObject> {
@@ -155,8 +164,14 @@ impl Moka {
155164
}
156165
}
157166

167+
#[pyfunction]
168+
fn get_version() -> &'static str {
169+
env!("CARGO_PKG_VERSION")
170+
}
171+
158172
#[pymodule]
159173
fn moka_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
160174
m.add_class::<Moka>()?;
175+
m.add_function(wrap_pyfunction!(get_version, m)?)?;
161176
Ok(())
162177
}

tests/test_moka.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
from time import monotonic, sleep
33
import threading
4+
45
import moka_py
56

67

@@ -78,3 +79,10 @@ def target():
7879
t2.join()
7980

8081
assert len(calls) == 1
82+
83+
84+
def test_default() -> None:
85+
moka = moka_py.Moka(128)
86+
moka.set("hello", [1, 2, 3])
87+
assert moka.get("world") is None
88+
assert moka.get("world", "foo") == "foo"

0 commit comments

Comments
 (0)