https://github.com/fluentpython/abacus
Metaprogramming in service of user experience
https://github.com/fluentpython/abacus
Last synced: 8 months ago
JSON representation
Metaprogramming in service of user experience
- Host: GitHub
- URL: https://github.com/fluentpython/abacus
- Owner: fluentpython
- License: bsd-3-clause
- Created: 2024-02-20T15:24:01.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-20T19:16:38.000Z (almost 2 years ago)
- Last Synced: 2025-04-09T09:11:20.509Z (9 months ago)
- Language: Python
- Size: 7.81 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# abacus
The `abacus` module is an example of module metaprogramming
in service of developer experience.
The end-user APIs for `plain_abacus` and `abacus` are the same.
Please view their module-level docstrings for examples.
But `abacus` uses metaprogramming to elliminate the code
duplication in the `add` and `sub` functions of `plain_abacus`.
The metaprogramming features are:
* `@command`: decorator to register a method name as a command;
* `@command_alias`: decorator to register a method name and aliases as a command;
* `_make_command`: builds a stand-alone function from an `Abacus` method;
* `_install_commands`: makes commands and installs them as global functions;
**Note**:
`abacus` has more lines of code and is much harder to understand than `plain_abacus`.
However, this example was inspired by the
[jupyturtle](https://github.com/ramalho/jupyturtle) project,
where the `Turtle` class has many more methods than `Abacus`,
therefore coding each global function by hand would require
a lot of duplication and many more lines of code.
When using metaprogramming, it's always a good idea to consider
whether the added complexity is worthwhile.