paramtrack 0.1.0__py3-none-any.whl
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.
paramtrack/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from html import escape
|
|
4
|
+
|
|
5
|
+
__all__ = ["track_class"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# "name = value" lines with the "=" signs lined up, drawn as a tree.
|
|
9
|
+
def tree(arguments):
|
|
10
|
+
width = max(map(len, arguments), default=0)
|
|
11
|
+
lines = [f"{k:<{width}} = {v}" for k, v in arguments.items()]
|
|
12
|
+
return [f"├─ {line}" for line in lines[:-1]] + [f"└─ {line}" for line in lines[-1:]]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Parameters(dict):
|
|
16
|
+
# Text version, used by print() and the terminal.
|
|
17
|
+
def __repr__(self):
|
|
18
|
+
blocks = []
|
|
19
|
+
for name, arguments in self.items():
|
|
20
|
+
blocks.append("\n".join([f"\033[1m{name}\033[0m", *tree(arguments)])) # \033[1m = bold
|
|
21
|
+
return "\n\n".join(blocks)
|
|
22
|
+
|
|
23
|
+
# Clickable version, used by Jupyter; click a method name to unfold it.
|
|
24
|
+
def _repr_html_(self):
|
|
25
|
+
html = ""
|
|
26
|
+
for name, arguments in self.items():
|
|
27
|
+
lines = escape("\n".join(tree(arguments)))
|
|
28
|
+
html += f"<details><summary><b>{name}</b></summary><pre style='margin: 0 0 0 1.2em'>{lines}</pre></details>"
|
|
29
|
+
return html
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def track(method, parameters):
|
|
33
|
+
@wraps(method)
|
|
34
|
+
def wrapper(*args, **kwargs):
|
|
35
|
+
bound = inspect.signature(method).bind(*args, **kwargs)
|
|
36
|
+
bound.apply_defaults()
|
|
37
|
+
parameters[method.__name__] = {k: repr(v) for k, v in bound.arguments.items() if k != "self"}
|
|
38
|
+
return method(*args, **kwargs)
|
|
39
|
+
return wrapper
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# Class decorator: track every method and store the arguments as cls.parameters.
|
|
43
|
+
def track_class(cls):
|
|
44
|
+
cls.parameters = Parameters()
|
|
45
|
+
for name, method in list(vars(cls).items()):
|
|
46
|
+
if inspect.isfunction(method):
|
|
47
|
+
setattr(cls, name, track(method, cls.parameters))
|
|
48
|
+
return cls
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: paramtrack
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Remembers the arguments of each method's last call, defaults included.
|
|
5
|
+
Author: Alessio Popovic
|
|
6
|
+
Author-email: Alessio Popovic <alessiopopovic@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Project-URL: Repository, https://github.com/AlessioPop/paramtrack
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# paramtrack
|
|
14
|
+
|
|
15
|
+
Remembers the arguments of each method's last call, defaults included.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install paramtrack
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from paramtrack import track_class
|
|
27
|
+
|
|
28
|
+
@track_class
|
|
29
|
+
class Telescope:
|
|
30
|
+
def observe(self, target, exposure=30, filter="V"):
|
|
31
|
+
...
|
|
32
|
+
|
|
33
|
+
telescope = Telescope()
|
|
34
|
+
telescope.observe("Vega", exposure=60)
|
|
35
|
+
|
|
36
|
+
print(telescope.parameters)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
observe
|
|
41
|
+
├─ target = 'Vega'
|
|
42
|
+
├─ exposure = 60
|
|
43
|
+
└─ filter = 'V'
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
In Jupyter, put `telescope.parameters` on the last line of a cell for a foldable view.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
paramtrack/__init__.py,sha256=GPvB1GQt-HwzjbkP6C4DOOC3ueFZFKW82N41ZKTG__E,1709
|
|
2
|
+
paramtrack-0.1.0.dist-info/licenses/LICENSE,sha256=lT4HaNpCiJIS0dr5MlpK84IoiZx_UTM69cXEf0UxmkA,1072
|
|
3
|
+
paramtrack-0.1.0.dist-info/WHEEL,sha256=CoDSoyhtC_eO_tlxRYzsTraPv1fPJRXFx91k6ISeAvA,81
|
|
4
|
+
paramtrack-0.1.0.dist-info/METADATA,sha256=q8KZDrx05BqfBUcEWYyML52isnzlfg1-Q93mj1rsae4,949
|
|
5
|
+
paramtrack-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alessio Popovic
|
|
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.
|