anatoly-scherbakov / documented Goto Github PK
View Code? Open in Web Editor NEWTemplated docstrings for Python classes
Home Page: https://anatoly-scherbakov.github.io/documented/
License: MIT License
Templated docstrings for Python classes
Home Page: https://anatoly-scherbakov.github.io/documented/
License: MIT License
For example, we should be able to pretty-print dataclasses and dictionaries. This should also be customizeable.
It can be actually achieved using properties though, but the special syntax can be nevertheless useful due to how often this can be helpful.
│ in __setattr__:4 │
│ ╭──────────────────────────────────── locals ─────────────────────────────────────╮ │
│ │ cls = <class 'todoist_costs.errors.MissingDueTime'> │ │
│ │ FrozenInstanceError = <class 'dataclasses.FrozenInstanceError'> │ │
│ │ name = '__traceback__' │ │
│ │ self = MissingDueTime( │ │
│ │ │ …
│ │ ) │ │
│ │ value = <traceback object at 0x7f00779cd280> │ │
│ ╰─────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
FrozenInstanceError: cannot assign to field '__traceback__'
I do not think this can be fixed, it just has to be documented at https://anatoly-scherbakov.github.io/documented/compatibility/dataclasses/ page.
@documented
def calculate_customers_report(customers: List[Customer], requested_by: User) -> CustomerReport:
"""
Build customers report.
Requested by: {requested_by}.
"""
When this function is executed, the following message can be printed:
Build customers report.
Requested by: John Doe.
Before executing the function, its wrapper will substitute the arguments of the function call into the docstring and then will print the docstring using built-in Python logging system.
Purpose of this is to illustrate what is happening in the program in a human readable way in its log.
Run source "$HOME/.poetry/env"
mypy documented tests/*.py
bash: mypy: command not found
make: *** [lint] Error 127
Makefile:5: recipe for target 'lint' failed
Error: Process completed with exit code 2.
…on each subclass of DocumentedError
.
I want to create a rich
Table and render it in the output. How do I do that?
What if I want to combine it with console markup?
The library documentation refers to a few articles about how exceptions might be used in controlling the control flow of an application. This is an interesting topic to explore further.
I personally very much like to write exceptions as dataclasses
and inherit them from DocumentedError
. This allows me to encapsulate business logic related information in those exceptions, and then to easily retrieve it in other parts of the application. For instance, I can do something like this:
def ask_hal(question: str, user_name: str):
try:
return hal.ask(question)
except AnswerToAnswer as err:
raise dataclasses.replace(
err,
user_name=user_name,
)
In this question, we do not provide user_name
to the internal layer of the application which is responsible for communication with HAL9000 computer, but we catch the error, enhance it with information available in the API layer, and reraise. Voilà.
ExceptionList
, a new feature introduced into Python recently, can be mentioned in relation to this topic.
No idea; perhaps at some point I get inspired by a particular usage example and will get to writing this piece.
Should probably be left aligned instead.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.