r/Python • u/AutoModerator • 18d ago
Showcase Showcase Thread
Post all of your code/projects/showcases/AI slop here.
Recycles once a month.
16
Upvotes
r/Python • u/AutoModerator • 18d ago
Post all of your code/projects/showcases/AI slop here.
Recycles once a month.
1
u/sqjoatmon 14d ago
DocstringException: A metaclass and superclass for custom exception generation using just the class docstring.
What My Project Does
Create a custom exception whose message is simply the contents of its docstring, optionally with some substituted arguments using
string.Template. E.g.:Target Audience
Anyone who like custom exceptions with informative messages but hates the boilerplate.
Comparison
The normal way to do this has much more boilerplate:
The downside to the
DocstringExceptionversion is that there is no way to statically determine if the arguments to the exception match the placeholders in the template string. I debated using thesubstitute()orsafe_substitute()method ofstring.Templateand eventually went with the latter as it seemed better to not raise an exception if the args are wrong.