Skip to content

MultifokalHirn/ornaments

Repository files navigation

ornaments

Maturity badge - level 1 Tests Coverage GitHub last commit

`ornaments` helps to *future-proofly* document the intent of one's `python` code.

It aims to do so through meaningful @decorator functions that help other developers understand, (re-)use, and debug your code.


Contents

Overview

Why decorators?

A @decorator 'talks about' other code in a way that is understandable for both the human reader as well as the interpreter. By being meaningful to both, @decorator based documentation can bypass the (all to common) disconnect between documentation and code.

Scope

ornaments provides functionality for adding various

  • value/type checks (as well limiters for input/execution duration) during runtime
  • context-dependend logging functionalities (i.e. execution time)
  • testable meta information (i.e. deprecation warnings)

Usage

from ornaments.invariants import only_called_once

@only_called_once(scope="session", enforce=True)
def only_once_callable_function() -> None:
    return None

# -----

only_once_callable_function()
>>> None

# This should raise an error
only_once_callable_function()
>>> Traceback (most recent call last):
    File "<input>", line 1, in <module>
      only_once_callable_function()
    File "./ornaments/src/ornaments/invariants/only_called_once.py", line 45, in wrapper
      raise CalledTooOftenError(msg)
    ornaments._exceptions.CalledTooOftenError: Function only_once_callable_function has already been called in session. call_scope=(4522676512, <function only_once_callable_function at 0x10d929120>)

But what if I want to use the same function in different contexts? πŸ€”

from ornaments.invariants import only_called_once

def my_reusable_function() -> None:
    return None

@only_called_once(scope="session", enforce=True)
def only_once_callable_function() -> None:
    return my_reusable_function()

# -----

my_reusable_function()
>>> None

my_reusable_function()
>>> None

only_once_callable_function()
>>> None

# This should raise an error
only_once_callable_function()
>>> Traceback (most recent call last):
    File "<input>", line 1, in <module>
      only_once_callable_function()
    File "./ornaments/src/ornaments/invariants/only_called_once.py", line 45, in wrapper
      raise CalledTooOftenError(msg)
      ...

πŸš€

Goal Structure

ornaments
β”œβ”€β”€ helpers
β”‚Β Β  β”œβ”€β”€ catch_all_exceptions.py
β”‚Β Β  β”œβ”€β”€ normalized_exceptions.py
β”‚Β Β  └── retry.py
β”œβ”€β”€ invariants
β”‚Β Β  β”œβ”€β”€ conditional_execution.py
β”‚Β Β  └── only_called_once.py
β”œβ”€β”€ limits
β”‚Β Β  β”œβ”€β”€ call_limit.py
β”‚Β Β  └── execution_time_limit.py
β”œβ”€β”€ logging
β”‚Β Β  β”œβ”€β”€ log_calls.py
β”‚Β Β  β”œβ”€β”€ log_execution_time.py
β”‚Β Β  β”œβ”€β”€ log_parameters.py
β”‚Β Β  β”œβ”€β”€ log_return.py
β”‚Β Β  └── log_value.py
β”œβ”€β”€ markers
β”‚Β Β  β”œβ”€β”€ stable.py
β”‚Β Β  β”œβ”€β”€ wobbly.py
β”‚Β Β  └── deprecated.py
β”œβ”€β”€ monitoring
β”‚Β Β  β”œβ”€β”€ monitored_attribute.py
β”‚Β Β  └── monitored_calls.py
β”œβ”€β”€ runtime_checks
β”‚Β Β  β”œβ”€β”€ checked_return_type.py
β”‚Β Β  └── parameter_validation.py
└── safety
 Β Β  └── fallback_function.py

Development

See CONTRIBUTING.md for information on how to setup and contribute to this project.

asciicast

Addendum

License


           .::=+=--++=::.
       .-+++*-+++ =*+*=++=-:
     :+#+-=:-.:+--+++=.=+:=**=.
   .+#==::--+#*-...:=%%+-:**==*=
  .@%=*=+:*#*.       .#@#-==-==-*.
  #@--+=-##::          %@#=:=:+::=
 -@@:==+:%+-.          =@=:++=:: -.
 *@@:-==:%*-.          =@*+=+-+-.-.
 :@@:.-==+@%-          %@===+==: =.
  #@-++-=--#%=       :%%----=:. :=
   #@=.:-:+++++==:--++=.::=++..:+
   :*%#*=:*+: -==:#%#*.==+::..--
   ::-+**=++-:=--.*%%%==--.:::.
        :--:==-:--:--:-:::::
            . . :  : ...

Author - MultifokalHirn

About

A library of decorators to communicate, monitor, debug and enforce behavior

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors