Skip to content

Commit a786a5a

Browse files
authored
Merge pull request #14 from ourstudio-se/13-add-compositions
Adding compositions
2 parents c289924 + d68f1cc commit a786a5a

3 files changed

Lines changed: 97 additions & 2 deletions

File tree

maz/compositions/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from dataclasses import dataclass
2+
from typing import Callable, Any
3+
from time import sleep
4+
from functools import partial
5+
from maz import compose
6+
7+
class retry_until:
8+
9+
"""
10+
Calls input function until condition is met
11+
or number of retries equals `retries`.
12+
"""
13+
14+
def __init__(self, function, retries: int, condition: Callable[[Any], bool]):
15+
if retries < 1:
16+
raise ValueError(f"`retries` must be greater or equal to 1, got {retries}")
17+
18+
self.function = function
19+
self.retries = retries
20+
self.condition = condition
21+
22+
def __call__(self, *args, **kwargs):
23+
24+
for i in range(self.retries):
25+
result = self.function(*args, **kwargs)
26+
if self.condition(result):
27+
return result
28+
29+
return result
30+
31+
class waiting:
32+
33+
"""
34+
Returns a new function that when executed will wait `in_seconds` seconds
35+
before executing the original function.
36+
"""
37+
38+
def __init__(self, function, in_seconds: float):
39+
self.function = function
40+
self.in_seconds = in_seconds
41+
42+
def __call__(self, *args, **kwargs):
43+
sleep(self.in_seconds)
44+
return self.function(*args, **kwargs)
45+
46+
class named:
47+
48+
"""
49+
Returns an object with properties name and function.
50+
"""
51+
52+
name: str
53+
function: Callable[[Any], Any]
54+
55+
def __call__(self, *args: Any, **kwds: Any) -> Any:
56+
return self.function(args, kwds)

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = maz
3-
version = 0.0.7
3+
version = 0.0.8
44
author = ourstudio
55
author_email = rikard@ourstudio.se
66
description = Functional programming tools.
@@ -9,4 +9,4 @@ long_description_content_type = text/markdown
99

1010
[options]
1111
packages = find:
12-
where = maz
12+
where = maz*

tests/test_maz_compositions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import time
2+
from functools import partial
3+
from maz.compositions import retry_until, waiting
4+
5+
def test_retryer():
6+
7+
retryer = retry_until(
8+
function=partial(
9+
next,
10+
iter([0,0,1,2,3])
11+
),
12+
retries=3,
13+
condition=lambda j: j == 1,
14+
)
15+
16+
assert retryer() == 1
17+
18+
retryer = retry_until(
19+
function=partial(
20+
next,
21+
iter([0,0,1,2,3])
22+
),
23+
retries=2,
24+
condition=lambda j: j == 1,
25+
)
26+
27+
assert retryer() == 0
28+
29+
def test_timeout():
30+
31+
waiting_fn = waiting(
32+
lambda x: x+1,
33+
1.1,
34+
)
35+
36+
start_time = time.time()
37+
assert waiting_fn(3) == 4
38+
total_time = time.time()-start_time
39+
assert total_time > 1

0 commit comments

Comments
 (0)