yet another guard clause library
Project description
xguard
Xguard is yet another guard clause library.
What are Guard Clauses
A guard clause is simply a check that immediately exits the function, either with a return statement or an exception. If you're used to writing functions that check to ensure everything is valid for the function to run, then you write the main function code, and then you write else statements to deal with error cases, this involves inverting your current workflow. The benefit is that your code will tend to be shorter and simpler, and less deeply indented. Credit
How to use this package
Using this package is easy. Just import the package, create an instance of the Xguard class, and start chaining your guard clauses together.
In dev mode, if you still encounter a lot of import errors, and you think you know what you are doing, try to add the xguard dir to the python path.
export PYTHONPATH=/path/to/parent/directory:$PYTHONPATH
Here's an example:
from xguard import guard
def add_ten(number: int):
guard = guard.Xguard()
guard.not_null(number) \
.is_type(number, int) \
.is_gt(number, 0)
# if we made it here, we have succeessfully used the guard clause, kudos!
return number + 10;
res: int = add_ten(5) # 120
print(res)
# Good Issue: Try to use the guard clauses in a factorial function
See? Easy!
For testing
python -m unittest discover tests/
Why I wrote the library
I wrote xguard because I wanted to create something useful for other developers like myself. As someone who has written a lot of code, I know how frustrating it can be to spend time tracking down errors caused by bad inputs. I wanted to make it easier for developers to catch these issues early on and write more robust code.
It was also an opporuinty for me to improve on a similar project by Adrian without wrecking his original idea. I am adding some extra features like custom error messages to make it more powerful and flexible. With custom error messages, developers can quickly understand what went wrong when a guard clause fails.
Overall, my main motivation for creating xguard was to write something that I found useful and that I hope other developers will find useful as well. I believe in the power of open source software and I would love for others to contribute to the project. If you have any feedback or suggestions, please feel free to share them or even open a pull request on GitHub. Together, we can make xguard even better.
Documentation
Each guard method is documented with a docstring that explains its purpose and usage, following the reST style. You can access the documentation for each method using Python's built-in help
function
License
This library is released under the GNU GPLv3 License. See the LICENSE file for details.
Acknowledgements
This library is based on the ideas and code of Adrian's similar project. Thank you, Adrian!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xguard-0.0.2.tar.gz
.
File metadata
- Download URL: xguard-0.0.2.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83d5203695788a416115ae50b71f855cfd107e4255ac70118a73e8d6ac339f5c |
|
MD5 | 8f576a88d50a6b9015b881f0ea45fe20 |
|
BLAKE2b-256 | b17c8ce27941f08e897728f16ad32a0692ea94210bf0c83a47fe2001e57f65a3 |
File details
Details for the file xguard-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: xguard-0.0.2-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0aabe6de87b4ebbaa371408451ab514581999629d00469627e67685fd22ca3d3 |
|
MD5 | 0b12164828737465cfb07f504239db3b |
|
BLAKE2b-256 | 86fec3495543b345d0fff536b500b3f1a0310c67fbdb7d9013564bfd0dfb346b |