Skip to main content

Templated docstrings for Python classes.

Project description

documented

Build Status Coverage Python Version wemake-python-styleguide PyPI - License

Templated docstrings for Python classes.

Features

  • Describe your business logic in docstrings of your classes and exceptions;
  • When printing an object or an exception, the library will substitute the placeholders in the docstring text with runtime values,
  • And you (or your user) will see a human readable text.

Installation

pip install documented

Example

from dataclasses import dataclass
from documented import DocumentedError


@dataclass
class InsufficientWizardryLevel(DocumentedError):
    """
    🧙 Your level of wizardry is insufficient ☹

        Spell: {self.spell}
        Minimum level required: {self.required_level}
        Actual level: {self.actual_level} {self.comment}

    Unseen University will be happy to assist in your training! 🎓
    """

    spell: str
    required_level: int
    actual_level: int

    @property
    def comment(self) -> str:
        if self.actual_level <= 0:
            return '(You are Rincewind, right? Hi!)'
        else:
            return ''


raise InsufficientWizardryLevel(
    spell='Animal transformation',
    required_level=8,
    actual_level=0,
)

which prints:

---------------------------------------------------------------------
InsufficientWizardryLevel           Traceback (most recent call last)
<ipython-input-1-d8ccdb953cf6> in <module>
     27 
     28 
---> 29 raise InsufficientWizardryLevel(
     30     spell='Animal transformation',
     31     required_level=8,

InsufficientWizardryLevel: 
🧙 Your level of wizardry is insufficient ☹

    Spell: Animal transformation
    Minimum level required: 8
    Actual level: 0 (You are Rincewind, right? Hi!)

Unseen University will be happy to assist in your training! 🎓

Usage

  • Template rendering is done using str.format().
  • That function receives the object instance as self keyword argument.
  • From template, you can't call methods of the object, but you can access its fields and properties.
  • textwrap.dedent() is applied to the result, thus Python indentation rules do not corrupt the resulting message.

Dynamically computed pieces of content may be introduced using:

You can also access elements of lists and dicts by index, for example: {self.countries[US]}.

Making your exceptions sane

  • Create your own exception classes in terms of your domain, to play a part in your business logic.

  • Do not use the word Exception or Error in their names. Your code should raise things like:

    • BalanceInsufficient
    • PlanetNotFound
    • TetOffline
    • OrderDeclined

    And should not:

    • ValueError
    • Exception
    • CatastrophicalError
  • Store meaningful properties of your errors in fields of the exception classes.

  • Use dataclasses, attrs or pydantic to save yourself from boilerplate in __init__() — and to get IDE support.

  • Maintain docstrings of your exceptions to contain up-to-date, human readable descriptions of what they mean.

  • You will be stimulated to do this by documented: when an exception happens, the docstring becomes actually useful.

Links

Which actually explains the meaning of this little helper: if we're stuck with exceptions in Python, why not at least make them friendlier?

This project was generated with wemake-python-package. Current template version is: 5840464a31423422d7523897d854e92408eee6b8. See what is updated since then.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

documented-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

documented-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file documented-0.1.1.tar.gz.

File metadata

  • Download URL: documented-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.8.3 Linux/5.4.0-47-generic

File hashes

Hashes for documented-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c7a6f9907212b53176f2c29a0171ab257cfca0a3ee6f9d0b9b7528200e8238c5
MD5 aeaf429b45adcf70c1950dc8ccf7c77d
BLAKE2b-256 0e23b4ee2f9748eaf998b3f2522cd405028313722bb1939d8b2168e294ee839b

See more details on using hashes here.

File details

Details for the file documented-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: documented-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.3 CPython/3.8.3 Linux/5.4.0-47-generic

File hashes

Hashes for documented-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f55daee8476ca89eacfddf7cf6467cab663e12e0726aec7424d9980ed5d1167b
MD5 c8133dc6af969e3da836a2c031d5d1c8
BLAKE2b-256 5f4d24e0d335c7da6a426b6622d4c357429d0c7ba02a8b8520bde0625bdd0d7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page