stranded 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stranded-0.0.1/LICENSE +21 -0
- stranded-0.0.1/PKG-INFO +173 -0
- stranded-0.0.1/README.md +142 -0
- stranded-0.0.1/pyproject.toml +83 -0
- stranded-0.0.1/setup.cfg +4 -0
- stranded-0.0.1/src/stranded/__init__.py +51 -0
- stranded-0.0.1/src/stranded/abc/__init__.py +21 -0
- stranded-0.0.1/src/stranded/abc/composer_.py +191 -0
- stranded-0.0.1/src/stranded/argparse/__init__.py +24 -0
- stranded-0.0.1/src/stranded/argparse/abc/__init__.py +21 -0
- stranded-0.0.1/src/stranded/argparse/abc/argument_parser_.py +539 -0
- stranded-0.0.1/src/stranded/argparse/argument_parser_.py +13 -0
- stranded-0.0.1/src/stranded/argparse/asyncio/__init__.py +24 -0
- stranded-0.0.1/src/stranded/argparse/asyncio/argument_parser_.py +50 -0
- stranded-0.0.1/src/stranded/argparse/threading/__init__.py +24 -0
- stranded-0.0.1/src/stranded/argparse/threading/argument_parser_.py +52 -0
- stranded-0.0.1/src/stranded/asyncio/__init__.py +21 -0
- stranded-0.0.1/src/stranded/asyncio/composer_.py +76 -0
- stranded-0.0.1/src/stranded/builtins/__init__.py +21 -0
- stranded-0.0.1/src/stranded/builtins/exception_.py +6 -0
- stranded-0.0.1/src/stranded/composer_.py +27 -0
- stranded-0.0.1/src/stranded/execution/__init__.py +30 -0
- stranded-0.0.1/src/stranded/execution/abc/__init__.py +21 -0
- stranded-0.0.1/src/stranded/execution/abc/scheduler_.py +59 -0
- stranded-0.0.1/src/stranded/execution/asyncio/__init__.py +21 -0
- stranded-0.0.1/src/stranded/execution/asyncio/scheduler_.py +86 -0
- stranded-0.0.1/src/stranded/execution/scheduler_.py +59 -0
- stranded-0.0.1/src/stranded/execution/threading/__init__.py +21 -0
- stranded-0.0.1/src/stranded/execution/threading/scheduler_.py +116 -0
- stranded-0.0.1/src/stranded/functools/__init__.py +51 -0
- stranded-0.0.1/src/stranded/functools/abc/__init__.py +39 -0
- stranded-0.0.1/src/stranded/functools/abc/herd_.py +96 -0
- stranded-0.0.1/src/stranded/functools/abc/lru_cache_.py +192 -0
- stranded-0.0.1/src/stranded/functools/abc/retry_.py +60 -0
- stranded-0.0.1/src/stranded/functools/abc/throttle_.py +97 -0
- stranded-0.0.1/src/stranded/functools/asyncio/__init__.py +51 -0
- stranded-0.0.1/src/stranded/functools/asyncio/herd_.py +89 -0
- stranded-0.0.1/src/stranded/functools/asyncio/lru_cache_.py +92 -0
- stranded-0.0.1/src/stranded/functools/asyncio/retry_.py +43 -0
- stranded-0.0.1/src/stranded/functools/asyncio/throttle_.py +74 -0
- stranded-0.0.1/src/stranded/functools/herd_.py +18 -0
- stranded-0.0.1/src/stranded/functools/lru_cache_.py +18 -0
- stranded-0.0.1/src/stranded/functools/retry_.py +13 -0
- stranded-0.0.1/src/stranded/functools/threading/__init__.py +51 -0
- stranded-0.0.1/src/stranded/functools/threading/herd_.py +88 -0
- stranded-0.0.1/src/stranded/functools/threading/lru_cache_.py +90 -0
- stranded-0.0.1/src/stranded/functools/threading/retry_.py +42 -0
- stranded-0.0.1/src/stranded/functools/threading/throttle_.py +75 -0
- stranded-0.0.1/src/stranded/functools/throttle_.py +13 -0
- stranded-0.0.1/src/stranded/logging/__init__.py +24 -0
- stranded-0.0.1/src/stranded/logging/abc/__init__.py +21 -0
- stranded-0.0.1/src/stranded/logging/abc/logger_.py +96 -0
- stranded-0.0.1/src/stranded/logging/asyncio/__init__.py +21 -0
- stranded-0.0.1/src/stranded/logging/asyncio/logger_.py +41 -0
- stranded-0.0.1/src/stranded/logging/logger_.py +13 -0
- stranded-0.0.1/src/stranded/logging/threading/__init__.py +21 -0
- stranded-0.0.1/src/stranded/logging/threading/logger_.py +41 -0
- stranded-0.0.1/src/stranded/py.typed +0 -0
- stranded-0.0.1/src/stranded/sqlite3/__init__.py +24 -0
- stranded-0.0.1/src/stranded/sqlite3/abc/__init__.py +21 -0
- stranded-0.0.1/src/stranded/sqlite3/abc/db_.py +149 -0
- stranded-0.0.1/src/stranded/sqlite3/asyncio/__init__.py +24 -0
- stranded-0.0.1/src/stranded/sqlite3/asyncio/db_.py +43 -0
- stranded-0.0.1/src/stranded/sqlite3/db_.py +13 -0
- stranded-0.0.1/src/stranded/sqlite3/threading/__init__.py +24 -0
- stranded-0.0.1/src/stranded/sqlite3/threading/db_.py +43 -0
- stranded-0.0.1/src/stranded/threading/__init__.py +21 -0
- stranded-0.0.1/src/stranded/threading/composer_.py +74 -0
- stranded-0.0.1/src/stranded/types/__init__.py +21 -0
- stranded-0.0.1/src/stranded/types/constraint.py +7 -0
- stranded-0.0.1/src/stranded/types/convert.py +158 -0
- stranded-0.0.1/src/stranded.egg-info/PKG-INFO +173 -0
- stranded-0.0.1/src/stranded.egg-info/SOURCES.txt +74 -0
- stranded-0.0.1/src/stranded.egg-info/dependency_links.txt +1 -0
- stranded-0.0.1/src/stranded.egg-info/requires.txt +7 -0
- stranded-0.0.1/src/stranded.egg-info/top_level.txt +1 -0
stranded-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Cameron Evans
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
stranded-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stranded
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Composable decorators for caching, retrying, throttling, logging, and CLIs, for both sync and asyncio code
|
|
5
|
+
Author-email: Cameron Evans <c.d.evans87@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/cevans87/Stranded
|
|
8
|
+
Project-URL: Issues, https://github.com/cevans87/Stranded/issues
|
|
9
|
+
Project-URL: Repository, https://github.com/cevans87/Stranded.git
|
|
10
|
+
Keywords: asyncio,back pressure,cache,memoize,rate limit,retry,throttle
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Topic :: System :: Logging
|
|
16
|
+
Classifier: Framework :: AsyncIO
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: mypy; extra == "test"
|
|
26
|
+
Requires-Dist: pytest; extra == "test"
|
|
27
|
+
Requires-Dist: pytest-asyncio; extra == "test"
|
|
28
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
29
|
+
Requires-Dist: pytest-mypy; extra == "test"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# Stranded
|
|
33
|
+
|
|
34
|
+
Composable decorators for caching, retrying, throttling, logging, and building
|
|
35
|
+
command-line interfaces. Every decorator works on both plain and `async def`
|
|
36
|
+
functions, picking the threading or asyncio implementation from the function it
|
|
37
|
+
wraps.
|
|
38
|
+
|
|
39
|
+
Stranded is alpha software. The API changes between releases.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install stranded
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python 3.13 or newer is required. There are no runtime dependencies.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
Each decorator is a frozen dataclass. Configure it with keyword arguments and
|
|
52
|
+
apply it to a function. The same decorator works for sync and async code.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import asyncio
|
|
56
|
+
|
|
57
|
+
from stranded.functools import LruCache, Retry, Throttle
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@LruCache(size=128)
|
|
61
|
+
def fib(n: int) -> int:
|
|
62
|
+
return n if n < 2 else fib(n - 1) + fib(n - 2)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@Retry(n=3)
|
|
66
|
+
@Throttle(max_running=4)
|
|
67
|
+
async def fetch(url: str) -> str:
|
|
68
|
+
await asyncio.sleep(0)
|
|
69
|
+
return url
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
print(fib(40))
|
|
73
|
+
print(asyncio.run(fetch('https://example.com')))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### functools
|
|
77
|
+
|
|
78
|
+
- `LruCache(size=...)` memoizes by argument, sharing one in-flight result
|
|
79
|
+
between concurrent callers.
|
|
80
|
+
- `Herd()` collapses concurrent calls with the same arguments into one call,
|
|
81
|
+
without memoizing the result.
|
|
82
|
+
- `Retry(n=...)` re-invokes the function when it raises, up to `n` times.
|
|
83
|
+
- `Throttle(max_running=..., max_waiting=...)` bounds concurrency. The running
|
|
84
|
+
cap grows additively while calls succeed and halves when a call raises.
|
|
85
|
+
|
|
86
|
+
Each also has an instance with default settings, spelled in lowercase:
|
|
87
|
+
`lru_cache`, `herd`, `retry`, `throttle`.
|
|
88
|
+
|
|
89
|
+
### logging
|
|
90
|
+
|
|
91
|
+
`Logger` logs a function's call, return value, and exceptions at configurable
|
|
92
|
+
levels.
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import logging
|
|
96
|
+
|
|
97
|
+
from stranded.logging import Logger
|
|
98
|
+
|
|
99
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@Logger(call_level='DEBUG', return_level='INFO', exception_level='ERROR')
|
|
103
|
+
def add(a: int, b: int) -> int:
|
|
104
|
+
return a + b
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
add(1, 2)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### sqlite3
|
|
111
|
+
|
|
112
|
+
`Db` memoizes a function's results to a SQLite database on disk, so results
|
|
113
|
+
survive across processes.
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import pathlib
|
|
117
|
+
import tempfile
|
|
118
|
+
|
|
119
|
+
from stranded.sqlite3 import Db
|
|
120
|
+
|
|
121
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
122
|
+
|
|
123
|
+
@Db(path=pathlib.Path(tmp) / 'db')
|
|
124
|
+
def expensive(key: str) -> str:
|
|
125
|
+
print('computing', key)
|
|
126
|
+
return key.upper()
|
|
127
|
+
|
|
128
|
+
print(expensive('a'))
|
|
129
|
+
print(expensive('a'))
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### argparse
|
|
133
|
+
|
|
134
|
+
`ArgumentParser` builds a command-line parser from a function's signature and
|
|
135
|
+
type hints. Flags come before positionals, a boolean flag is spelled with one
|
|
136
|
+
dash, and a valued flag with two. Parsers compose with `|`, so flags shared
|
|
137
|
+
across subcommands can be written once.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from stranded.argparse import ArgumentParser
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@ArgumentParser()
|
|
144
|
+
def verbose_flag(*, verbose: bool = False) -> None:
|
|
145
|
+
if verbose:
|
|
146
|
+
print('verbose on')
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@ArgumentParser()
|
|
150
|
+
def main(name: str, /, *, count: int = 1) -> None:
|
|
151
|
+
for _ in range(count):
|
|
152
|
+
print(f'hello, {name}')
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
(verbose_flag | main)('-verbose', '--count', '2', 'world')
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Explicit sync and async variants
|
|
159
|
+
|
|
160
|
+
The top-level decorators dispatch on whether the wrapped function is a
|
|
161
|
+
coroutine function. To pin one, import from the `asyncio` or `threading`
|
|
162
|
+
subpackage instead, for example `stranded.functools.asyncio.LruCache`.
|
|
163
|
+
|
|
164
|
+
## Development
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pip install -e '.[test]'
|
|
168
|
+
pytest
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## License
|
|
172
|
+
|
|
173
|
+
MIT. See `LICENSE`.
|
stranded-0.0.1/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Stranded
|
|
2
|
+
|
|
3
|
+
Composable decorators for caching, retrying, throttling, logging, and building
|
|
4
|
+
command-line interfaces. Every decorator works on both plain and `async def`
|
|
5
|
+
functions, picking the threading or asyncio implementation from the function it
|
|
6
|
+
wraps.
|
|
7
|
+
|
|
8
|
+
Stranded is alpha software. The API changes between releases.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install stranded
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Python 3.13 or newer is required. There are no runtime dependencies.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Each decorator is a frozen dataclass. Configure it with keyword arguments and
|
|
21
|
+
apply it to a function. The same decorator works for sync and async code.
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
import asyncio
|
|
25
|
+
|
|
26
|
+
from stranded.functools import LruCache, Retry, Throttle
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@LruCache(size=128)
|
|
30
|
+
def fib(n: int) -> int:
|
|
31
|
+
return n if n < 2 else fib(n - 1) + fib(n - 2)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@Retry(n=3)
|
|
35
|
+
@Throttle(max_running=4)
|
|
36
|
+
async def fetch(url: str) -> str:
|
|
37
|
+
await asyncio.sleep(0)
|
|
38
|
+
return url
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
print(fib(40))
|
|
42
|
+
print(asyncio.run(fetch('https://example.com')))
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### functools
|
|
46
|
+
|
|
47
|
+
- `LruCache(size=...)` memoizes by argument, sharing one in-flight result
|
|
48
|
+
between concurrent callers.
|
|
49
|
+
- `Herd()` collapses concurrent calls with the same arguments into one call,
|
|
50
|
+
without memoizing the result.
|
|
51
|
+
- `Retry(n=...)` re-invokes the function when it raises, up to `n` times.
|
|
52
|
+
- `Throttle(max_running=..., max_waiting=...)` bounds concurrency. The running
|
|
53
|
+
cap grows additively while calls succeed and halves when a call raises.
|
|
54
|
+
|
|
55
|
+
Each also has an instance with default settings, spelled in lowercase:
|
|
56
|
+
`lru_cache`, `herd`, `retry`, `throttle`.
|
|
57
|
+
|
|
58
|
+
### logging
|
|
59
|
+
|
|
60
|
+
`Logger` logs a function's call, return value, and exceptions at configurable
|
|
61
|
+
levels.
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import logging
|
|
65
|
+
|
|
66
|
+
from stranded.logging import Logger
|
|
67
|
+
|
|
68
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@Logger(call_level='DEBUG', return_level='INFO', exception_level='ERROR')
|
|
72
|
+
def add(a: int, b: int) -> int:
|
|
73
|
+
return a + b
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
add(1, 2)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### sqlite3
|
|
80
|
+
|
|
81
|
+
`Db` memoizes a function's results to a SQLite database on disk, so results
|
|
82
|
+
survive across processes.
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import pathlib
|
|
86
|
+
import tempfile
|
|
87
|
+
|
|
88
|
+
from stranded.sqlite3 import Db
|
|
89
|
+
|
|
90
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
91
|
+
|
|
92
|
+
@Db(path=pathlib.Path(tmp) / 'db')
|
|
93
|
+
def expensive(key: str) -> str:
|
|
94
|
+
print('computing', key)
|
|
95
|
+
return key.upper()
|
|
96
|
+
|
|
97
|
+
print(expensive('a'))
|
|
98
|
+
print(expensive('a'))
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### argparse
|
|
102
|
+
|
|
103
|
+
`ArgumentParser` builds a command-line parser from a function's signature and
|
|
104
|
+
type hints. Flags come before positionals, a boolean flag is spelled with one
|
|
105
|
+
dash, and a valued flag with two. Parsers compose with `|`, so flags shared
|
|
106
|
+
across subcommands can be written once.
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from stranded.argparse import ArgumentParser
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@ArgumentParser()
|
|
113
|
+
def verbose_flag(*, verbose: bool = False) -> None:
|
|
114
|
+
if verbose:
|
|
115
|
+
print('verbose on')
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@ArgumentParser()
|
|
119
|
+
def main(name: str, /, *, count: int = 1) -> None:
|
|
120
|
+
for _ in range(count):
|
|
121
|
+
print(f'hello, {name}')
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
(verbose_flag | main)('-verbose', '--count', '2', 'world')
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Explicit sync and async variants
|
|
128
|
+
|
|
129
|
+
The top-level decorators dispatch on whether the wrapped function is a
|
|
130
|
+
coroutine function. To pin one, import from the `asyncio` or `threading`
|
|
131
|
+
subpackage instead, for example `stranded.functools.asyncio.LruCache`.
|
|
132
|
+
|
|
133
|
+
## Development
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
pip install -e '.[test]'
|
|
137
|
+
pytest
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT. See `LICENSE`.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = 'setuptools.build_meta'
|
|
3
|
+
requires = ['setuptools>=77']
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
authors = [
|
|
7
|
+
{name = 'Cameron Evans', email = 'c.d.evans87@gmail.com'},
|
|
8
|
+
]
|
|
9
|
+
classifiers = [
|
|
10
|
+
'Development Status :: 3 - Alpha',
|
|
11
|
+
'Intended Audience :: Developers',
|
|
12
|
+
'Topic :: Software Development :: Libraries',
|
|
13
|
+
'Topic :: Software Development :: Libraries :: Python Modules',
|
|
14
|
+
'Topic :: System :: Logging',
|
|
15
|
+
'Framework :: AsyncIO',
|
|
16
|
+
'Programming Language :: Python :: 3',
|
|
17
|
+
'Programming Language :: Python :: 3.13',
|
|
18
|
+
'Programming Language :: Python :: 3.14',
|
|
19
|
+
'Typing :: Typed',
|
|
20
|
+
]
|
|
21
|
+
dependencies = []
|
|
22
|
+
description = 'Composable decorators for caching, retrying, throttling, logging, and CLIs, for both sync and asyncio code'
|
|
23
|
+
keywords = [
|
|
24
|
+
'asyncio',
|
|
25
|
+
'back pressure',
|
|
26
|
+
'cache',
|
|
27
|
+
'memoize',
|
|
28
|
+
'rate limit',
|
|
29
|
+
'retry',
|
|
30
|
+
'throttle',
|
|
31
|
+
]
|
|
32
|
+
license = 'MIT'
|
|
33
|
+
license-files = ['LICENSE']
|
|
34
|
+
name = 'stranded'
|
|
35
|
+
readme = 'README.md'
|
|
36
|
+
requires-python = '>=3.13'
|
|
37
|
+
version = '0.0.1'
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
test = ['mypy', 'pytest', 'pytest-asyncio', 'pytest-cov', 'pytest-mypy']
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = 'https://github.com/cevans87/Stranded'
|
|
44
|
+
Issues = 'https://github.com/cevans87/Stranded/issues'
|
|
45
|
+
Repository = 'https://github.com/cevans87/Stranded.git'
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.packages.find]
|
|
48
|
+
include = ['*']
|
|
49
|
+
where = ['src']
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.package-data]
|
|
52
|
+
stranded = ['py.typed']
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
addopts = [
|
|
56
|
+
'--cov=stranded',
|
|
57
|
+
'--cov-branch',
|
|
58
|
+
'--cov-report=term-missing',
|
|
59
|
+
'--mypy',
|
|
60
|
+
]
|
|
61
|
+
pythonpath = ['src']
|
|
62
|
+
testpaths = ['test', 'src']
|
|
63
|
+
|
|
64
|
+
[tool.coverage.run]
|
|
65
|
+
branch = true
|
|
66
|
+
source = ['stranded']
|
|
67
|
+
|
|
68
|
+
[tool.coverage.report]
|
|
69
|
+
exclude_also = [
|
|
70
|
+
'if typing\\.TYPE_CHECKING:',
|
|
71
|
+
'\\.\\.\\.\\s*$',
|
|
72
|
+
'raise NotImplementedError',
|
|
73
|
+
'@(abc\\.)?abstractmethod',
|
|
74
|
+
]
|
|
75
|
+
show_missing = true
|
|
76
|
+
skip_covered = false
|
|
77
|
+
|
|
78
|
+
[tool.mypy]
|
|
79
|
+
strict = true
|
|
80
|
+
files = ['src', 'test']
|
|
81
|
+
mypy_path = '$MYPY_CONFIG_FILE_DIR/src'
|
|
82
|
+
explicit_package_bases = true
|
|
83
|
+
namespace_packages = true
|
stranded-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib as _importlib
|
|
4
|
+
import typing as _typing
|
|
5
|
+
|
|
6
|
+
if _typing.TYPE_CHECKING:
|
|
7
|
+
from . import abc
|
|
8
|
+
from . import argparse
|
|
9
|
+
from . import asyncio
|
|
10
|
+
from . import builtins
|
|
11
|
+
from . import composer_
|
|
12
|
+
from .composer_ import Composer
|
|
13
|
+
from . import execution
|
|
14
|
+
from . import functools
|
|
15
|
+
from . import logging
|
|
16
|
+
from . import sqlite3
|
|
17
|
+
from . import threading
|
|
18
|
+
from . import types
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def __getattr__(name: str) -> _typing.Any:
|
|
22
|
+
match name:
|
|
23
|
+
case 'abc': return _importlib.import_module('.abc', __name__)
|
|
24
|
+
case 'argparse': return _importlib.import_module('.argparse', __name__)
|
|
25
|
+
case 'asyncio': return _importlib.import_module('.asyncio', __name__)
|
|
26
|
+
case 'builtins': return _importlib.import_module('.builtins', __name__)
|
|
27
|
+
case 'composer_': return _importlib.import_module('.composer_', __name__)
|
|
28
|
+
case 'Composer': return _importlib.import_module('.composer_', __name__).Composer
|
|
29
|
+
case 'execution': return _importlib.import_module('.execution', __name__)
|
|
30
|
+
case 'functools': return _importlib.import_module('.functools', __name__)
|
|
31
|
+
case 'logging': return _importlib.import_module('.logging', __name__)
|
|
32
|
+
case 'sqlite3': return _importlib.import_module('.sqlite3', __name__)
|
|
33
|
+
case 'threading': return _importlib.import_module('.threading', __name__)
|
|
34
|
+
case 'types': return _importlib.import_module('.types', __name__)
|
|
35
|
+
case _: raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
__all__ = (
|
|
39
|
+
'abc',
|
|
40
|
+
'argparse',
|
|
41
|
+
'asyncio',
|
|
42
|
+
'builtins',
|
|
43
|
+
'composer_',
|
|
44
|
+
'Composer',
|
|
45
|
+
'execution',
|
|
46
|
+
'functools',
|
|
47
|
+
'logging',
|
|
48
|
+
'sqlite3',
|
|
49
|
+
'threading',
|
|
50
|
+
'types',
|
|
51
|
+
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib as _importlib
|
|
4
|
+
import typing as _typing
|
|
5
|
+
|
|
6
|
+
if _typing.TYPE_CHECKING:
|
|
7
|
+
from . import composer_
|
|
8
|
+
from .composer_ import Composer
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def __getattr__(name: str) -> _typing.Any:
|
|
12
|
+
match name:
|
|
13
|
+
case 'composer_': return _importlib.import_module('.composer_', __name__)
|
|
14
|
+
case 'Composer': return _importlib.import_module('.composer_', __name__).Composer
|
|
15
|
+
case _: raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
__all__ = (
|
|
19
|
+
'composer_',
|
|
20
|
+
'Composer',
|
|
21
|
+
)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import abc
|
|
4
|
+
import annotationlib
|
|
5
|
+
import dataclasses
|
|
6
|
+
import inspect
|
|
7
|
+
import types
|
|
8
|
+
import typing
|
|
9
|
+
|
|
10
|
+
type Instance = object
|
|
11
|
+
|
|
12
|
+
type ValueT[**ParamT_, RetT_] = Param[ParamT_] | Raise | Return[RetT_] | Stop
|
|
13
|
+
type StackT = tuple[
|
|
14
|
+
ValueT[typing.Any, typing.Any]
|
|
15
|
+
| Composee[typing.Any, typing.Any]
|
|
16
|
+
| Connect[typing.Any, typing.Any]
|
|
17
|
+
| Exit[typing.Any, typing.Any]
|
|
18
|
+
| Enter[typing.Any, typing.Any]
|
|
19
|
+
| Composed[typing.Any, typing.Any],
|
|
20
|
+
...,
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def replace[T](obj: T, /, **changes: typing.Any) -> T:
|
|
25
|
+
"""Copy `obj` - a frozen dataclass - with `changes` applied.
|
|
26
|
+
|
|
27
|
+
`dataclasses.replace` re-runs `__init__`, which costs several times as much as copying the
|
|
28
|
+
instance dict does. Binding a composition to an instance happens on every attribute access, so
|
|
29
|
+
it is worth copying directly.
|
|
30
|
+
"""
|
|
31
|
+
copy = object.__new__(type(obj))
|
|
32
|
+
copy.__dict__.update(obj.__dict__, **changes)
|
|
33
|
+
return copy
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclasses.dataclass(frozen=True)
|
|
37
|
+
class Raise:
|
|
38
|
+
exc_type: type[BaseException]
|
|
39
|
+
exc_val: BaseException
|
|
40
|
+
exc_tb: types.TracebackType | None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclasses.dataclass(frozen=True)
|
|
44
|
+
class Stop(BaseException): ...
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
48
|
+
class Param[**ParamT]:
|
|
49
|
+
args: ParamT.args
|
|
50
|
+
kwargs: ParamT.kwargs
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclasses.dataclass(frozen=True)
|
|
54
|
+
class Return[RetT]:
|
|
55
|
+
ret: RetT
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Composee[**ParamT, RetT](typing.Protocol):
|
|
59
|
+
__doc__: str
|
|
60
|
+
__module__: str
|
|
61
|
+
__name__: str
|
|
62
|
+
__qualname__: str
|
|
63
|
+
def __call__(self, *args: ParamT.args, **kwargs: ParamT.kwargs) -> RetT: ...
|
|
64
|
+
def __get__(self, instance: Instance, owner: type[object] | None) -> typing.Self: ...
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
68
|
+
class Connect[**ParamT, RetT](abc.ABC):
|
|
69
|
+
sender: Composed[ParamT, RetT]
|
|
70
|
+
receiver: Composed[typing.Any, typing.Any]
|
|
71
|
+
composer: Composer[ParamT, RetT]
|
|
72
|
+
|
|
73
|
+
def __call__(self, value: ValueT[ParamT, RetT], /) -> StackT:
|
|
74
|
+
match value:
|
|
75
|
+
case Param() as param_: return *self.receiver.stack, param_
|
|
76
|
+
case Raise() as raise_: return raise_,
|
|
77
|
+
case Return() as return_: return *self.receiver.stack, Param(args=(return_.ret,), kwargs={}),
|
|
78
|
+
case Stop() as stop_: return stop_,
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
82
|
+
class Exit[**ParamT, RetT](abc.ABC):
|
|
83
|
+
enter: Enter[ParamT, RetT]
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def composer(self) -> Composer[typing.Any, typing.Any]: return self.enter.composer
|
|
87
|
+
|
|
88
|
+
def __call__(self, value: ValueT[ParamT, RetT], /) -> StackT:
|
|
89
|
+
return ()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
93
|
+
class Enter[**ParamT, RetT](abc.ABC):
|
|
94
|
+
composer: Composer[ParamT, RetT]
|
|
95
|
+
composee: Composee[ParamT, RetT]
|
|
96
|
+
|
|
97
|
+
def __call__(self, value: ValueT[ParamT, RetT], /) -> StackT:
|
|
98
|
+
match value, self.composee:
|
|
99
|
+
case Param(), Composed() as composed_: return self.composer.Exit(enter=self), *composed_.stack
|
|
100
|
+
case Param(), composee_: return self.composer.Exit(enter=self), composee_,
|
|
101
|
+
case _: return ()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
105
|
+
class Composed[**ParamT, RetT](abc.ABC):
|
|
106
|
+
__doc__: str
|
|
107
|
+
__module__: str
|
|
108
|
+
__name__: str
|
|
109
|
+
__qualname__: str
|
|
110
|
+
__signature__: inspect.Signature
|
|
111
|
+
composer: Composer[ParamT, RetT]
|
|
112
|
+
stack: StackT
|
|
113
|
+
owner: type[object] | None = None
|
|
114
|
+
name: str | None = None
|
|
115
|
+
|
|
116
|
+
@abc.abstractmethod
|
|
117
|
+
def __call__(self, *args: ParamT.args, **kwargs: ParamT.kwargs) -> RetT: ...
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def enter(self) -> Enter[ParamT, RetT]:
|
|
121
|
+
"""The Enter at the bottom of the stack, which holds whatever state the composition has."""
|
|
122
|
+
match self.stack[-1]:
|
|
123
|
+
case Enter() as enter_: return enter_
|
|
124
|
+
assert False, "unreachable"
|
|
125
|
+
|
|
126
|
+
def create_enter(self, instance: Instance) -> Enter[ParamT, RetT]:
|
|
127
|
+
"""Return the Enter that `instance`'s copy of this composition composes with.
|
|
128
|
+
|
|
129
|
+
Composers that keep per-instance state override this to hand each instance its own. Note
|
|
130
|
+
that whatever they hold it in must not refer back to `instance`, or the instance can never
|
|
131
|
+
be collected - which is why they hold Enters rather than whole bound compositions.
|
|
132
|
+
"""
|
|
133
|
+
return self.enter
|
|
134
|
+
|
|
135
|
+
def __set_name__(self, owner: type[object], name: str) -> None:
|
|
136
|
+
object.__setattr__(self, 'owner', owner)
|
|
137
|
+
object.__setattr__(self, 'name', name)
|
|
138
|
+
|
|
139
|
+
def __get__(self, instance: Instance, owner: type[object] | None) -> typing.Self:
|
|
140
|
+
if instance is None:
|
|
141
|
+
return self
|
|
142
|
+
enter = self.create_enter(instance)
|
|
143
|
+
return replace(
|
|
144
|
+
self, stack=(*self.stack[:-1], replace(enter, composee=enter.composee.__get__(instance, owner))),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
def __or__[**ReceiverParamT, ReceiverRetT](
|
|
148
|
+
self,
|
|
149
|
+
receiver: Composed[ReceiverParamT, ReceiverRetT],
|
|
150
|
+
/,
|
|
151
|
+
) -> Composed[ParamT, ReceiverRetT]:
|
|
152
|
+
return dataclasses.replace(
|
|
153
|
+
self,
|
|
154
|
+
__doc__=f"{self.__doc__}\n\n{receiver.__doc__}",
|
|
155
|
+
__signature__=inspect.Signature().replace(
|
|
156
|
+
parameters=tuple(self.__signature__.parameters.values()),
|
|
157
|
+
return_annotation=receiver.__signature__.return_annotation,
|
|
158
|
+
),
|
|
159
|
+
stack=(self.composer.Connect(composer=self.composer, receiver=receiver, sender=self), *self.stack),
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# Aliases for annotation use. The Composer ClassVars below shadow the class names
|
|
164
|
+
# within Composer's scope, so annotations must reference these instead.
|
|
165
|
+
type ComposeeT[**ParamT, RetT] = Composee[ParamT, RetT]
|
|
166
|
+
type ConnectT[**ParamT, RetT] = Connect[ParamT, RetT]
|
|
167
|
+
type ExitT[**ParamT, RetT] = Exit[ParamT, RetT]
|
|
168
|
+
type EnterT[**ParamT, RetT] = Enter[ParamT, RetT]
|
|
169
|
+
type ComposedT[**ParamT, RetT] = Composed[ParamT, RetT]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@dataclasses.dataclass(frozen=True, kw_only=True)
|
|
173
|
+
class Composer[**ParamT, RetT](abc.ABC):
|
|
174
|
+
Composee: typing.ClassVar[type[Composee[typing.Any, typing.Any]]]
|
|
175
|
+
Connect: typing.ClassVar[type[Connect[typing.Any, typing.Any]]]
|
|
176
|
+
Exit: typing.ClassVar[type[Exit[typing.Any, typing.Any]]]
|
|
177
|
+
Enter: typing.ClassVar[type[Enter[typing.Any, typing.Any]]]
|
|
178
|
+
Composed: typing.ClassVar[type[Composed[typing.Any, typing.Any]]]
|
|
179
|
+
|
|
180
|
+
def __call__[**CallParamT, CallRetT](
|
|
181
|
+
self, composee: ComposeeT[CallParamT, CallRetT], /,
|
|
182
|
+
) -> ComposedT[CallParamT, CallRetT]:
|
|
183
|
+
return self.Composed(
|
|
184
|
+
__doc__=str(composee.__doc__),
|
|
185
|
+
__module__=str(composee.__module__),
|
|
186
|
+
__name__=str(composee.__name__),
|
|
187
|
+
__qualname__=str(composee.__qualname__),
|
|
188
|
+
__signature__=inspect.signature(composee, annotation_format=annotationlib.Format.FORWARDREF),
|
|
189
|
+
composer=self,
|
|
190
|
+
stack=(self.Enter(composer=self, composee=composee),)
|
|
191
|
+
)
|