panyc 1.0.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.
- panyc-1.0.0.data/scripts/panyc.py +26 -0
- panyc-1.0.0.dist-info/METADATA +86 -0
- panyc-1.0.0.dist-info/RECORD +6 -0
- panyc-1.0.0.dist-info/WHEEL +5 -0
- panyc-1.0.0.dist-info/top_level.txt +1 -0
- panyc.py +26 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright © 2025 Matteo Benzi <matteo.benzi97@gmail.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
#
|
5
|
+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
#
|
7
|
+
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
9
|
+
import inspect
|
10
|
+
|
11
|
+
def raise_if(expr, message):
|
12
|
+
if expr:
|
13
|
+
caller = inspect.stack()[1]
|
14
|
+
raise AssertionError(f"{caller.filename}:{caller.lineno}: {message}")
|
15
|
+
|
16
|
+
def todo(message=""):
|
17
|
+
caller = inspect.stack()[1]
|
18
|
+
if len(message) > 0:
|
19
|
+
message = f" ({message})"
|
20
|
+
raise NotImplementedError(f'{caller.filename}:{caller.lineno}: Function "{caller.function}" not implemented{message}')
|
21
|
+
|
22
|
+
def unreachable(message=""):
|
23
|
+
caller = inspect.stack()[1]
|
24
|
+
if len(message) > 0:
|
25
|
+
message = f" ({message})"
|
26
|
+
raise NotImplementedError(f'{caller.filename}:{caller.lineno}: Reached unreachable code in "{caller.function}"{message}')
|
@@ -0,0 +1,86 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: panyc
|
3
|
+
Version: 1.0.0
|
4
|
+
Home-page: https://codeberg.org/bnz/panyc
|
5
|
+
Author: bnz
|
6
|
+
Author-email: matteo.benzi97@gmail.com
|
7
|
+
License: MIT
|
8
|
+
Description-Content-Type: text/markdown
|
9
|
+
Dynamic: author
|
10
|
+
Dynamic: author-email
|
11
|
+
Dynamic: description
|
12
|
+
Dynamic: description-content-type
|
13
|
+
Dynamic: home-page
|
14
|
+
Dynamic: license
|
15
|
+
|
16
|
+
# Panyc
|
17
|
+
|
18
|
+
Super simple python package to solve 2 problems:
|
19
|
+
- In Python `assert` is not guaranteed to work (see the `-O` flag)
|
20
|
+
- The very convenient `todo()`/`unreachable()`/... functions to panic in case reached, are missing
|
21
|
+
|
22
|
+
### Usage
|
23
|
+
|
24
|
+
```python
|
25
|
+
from panyc import raise_if
|
26
|
+
|
27
|
+
def is_even(x):
|
28
|
+
raise_if(type(x) != int, f"expected an integer, got {type(x)}")
|
29
|
+
return x % 2 == 0
|
30
|
+
|
31
|
+
def main():
|
32
|
+
x = 6.9
|
33
|
+
print("Is", x, "even?", is_even(x))
|
34
|
+
|
35
|
+
main()
|
36
|
+
```
|
37
|
+
Will result in `AssertionError: /path/to/file/example.py:4: expected an integer, got <class 'float'>`
|
38
|
+
|
39
|
+
> A couple of notice:
|
40
|
+
> - first of all the condition must be true to raise, in the contrary of `assert` where it must be false
|
41
|
+
> - secondly it raise an `AssertionError` as well, so the error handling remain the same
|
42
|
+
>
|
43
|
+
> So if you plan to rewrite your `assert`s you have to change the condition and nothing else
|
44
|
+
|
45
|
+
|
46
|
+
```python
|
47
|
+
from panyc import todo
|
48
|
+
|
49
|
+
def end_world_hungry():
|
50
|
+
todo()
|
51
|
+
|
52
|
+
def main():
|
53
|
+
end_world_hungry()
|
54
|
+
|
55
|
+
main()
|
56
|
+
```
|
57
|
+
Will result in `NotImplementedError: /path/to/file/example.py:4: Function "end_world_hungry" not implemented`
|
58
|
+
|
59
|
+
|
60
|
+
```python
|
61
|
+
from panyc import unreachable
|
62
|
+
|
63
|
+
def sound(x):
|
64
|
+
if x == "cat":
|
65
|
+
return "meow"
|
66
|
+
elif x == "dog":
|
67
|
+
return "woof"
|
68
|
+
elif x == "duck":
|
69
|
+
return "quack"
|
70
|
+
unreachable() # we do not expect other animals
|
71
|
+
|
72
|
+
def main():
|
73
|
+
print(sound("crocodile"))
|
74
|
+
|
75
|
+
main()
|
76
|
+
```
|
77
|
+
Will result in `NotImplementedError: /path/to/file/example.py:10: Reached unreachable code in "sound"`
|
78
|
+
|
79
|
+
### Installation
|
80
|
+
|
81
|
+
Just copy the only file `panyc.py` in your project.
|
82
|
+
|
83
|
+
If you like the slop, there's also the package on pypi:
|
84
|
+
```console
|
85
|
+
$ pip install panyc
|
86
|
+
```
|
@@ -0,0 +1,6 @@
|
|
1
|
+
panyc.py,sha256=ZghaC8FuxZBQ5SYUQevn1qJXKQ724__8ZpARrt2MT2U,1758
|
2
|
+
panyc-1.0.0.data/scripts/panyc.py,sha256=ZghaC8FuxZBQ5SYUQevn1qJXKQ724__8ZpARrt2MT2U,1758
|
3
|
+
panyc-1.0.0.dist-info/METADATA,sha256=EolqYH6NnOHs2AILP54p54Bx-04D6MyNqLqAMYwoCrg,1932
|
4
|
+
panyc-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
5
|
+
panyc-1.0.0.dist-info/top_level.txt,sha256=qZYkGaMuDKYFeaeh764gVaQv4ZduogjeaKXSDAuGzCE,6
|
6
|
+
panyc-1.0.0.dist-info/RECORD,,
|
@@ -0,0 +1 @@
|
|
1
|
+
panyc
|
panyc.py
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright © 2025 Matteo Benzi <matteo.benzi97@gmail.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
#
|
5
|
+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
#
|
7
|
+
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
|
+
|
9
|
+
import inspect
|
10
|
+
|
11
|
+
def raise_if(expr, message):
|
12
|
+
if expr:
|
13
|
+
caller = inspect.stack()[1]
|
14
|
+
raise AssertionError(f"{caller.filename}:{caller.lineno}: {message}")
|
15
|
+
|
16
|
+
def todo(message=""):
|
17
|
+
caller = inspect.stack()[1]
|
18
|
+
if len(message) > 0:
|
19
|
+
message = f" ({message})"
|
20
|
+
raise NotImplementedError(f'{caller.filename}:{caller.lineno}: Function "{caller.function}" not implemented{message}')
|
21
|
+
|
22
|
+
def unreachable(message=""):
|
23
|
+
caller = inspect.stack()[1]
|
24
|
+
if len(message) > 0:
|
25
|
+
message = f" ({message})"
|
26
|
+
raise NotImplementedError(f'{caller.filename}:{caller.lineno}: Reached unreachable code in "{caller.function}"{message}')
|