<jupyter_start><jupyter_text>Usefull decorators for dimensionfullness functions  Plenty of decorators are available.  Basic dimension checkingThe first decorator simply checks the dimension of the inputs and/or outputs.This can be used to : - avoid timy calculation that end up on a DimensionError - check the dimension of the output at its creation, not later when used with other quantities - quickly check that the function you implemented returns the expected dimension - restrict a function use to a specific dimensionTo specify the dimension: - a quantity can be used - a string represnetating the dimension,  like "L" - a Dimension object<jupyter_code>@check_dimension(("L", "L"), ("L"))
def sum_length(x, y):
    return x+y+1*m

print(sum_length(1*m, 1*m))


@check_dimension((m, m), (m))
def sum_length(x, y):
    "This function could be used on any Quantity, but here restricted to lengths."
    return x+y

print(sum_length(1*m, 1*m))


@check_dimension((Dimension("L"), Dimension("L")), (Dimension("L")))
def sum_length(x, y):
    return x+y

print(sum_length(1*m, 1*m))<jupyter_output>3 m
3 m
3 m<jupyter_text>Favunit settingThis decorator simply sets the favunit of the outputs<jupyter_code>mm = units["mm"]

@set_favunit(mm)
def sum_length(x, y):
    return x+y+1*m
print(sum_length(1*m, 1.123*m))<jupyter_output>3123.0 mm<jupyter_text>Dimension checks and favunit setting This decorator is a wrapper on `set_favunit` and `check_dimension`. The outputs' object will be used to check dimension and set as favunit<jupyter_code>@dimension_and_favunit((m, m), mm)
def sum_length(x, y):
    return x+y+1*m
print(sum_length(1*m, 1.123*m))<jupyter_output>3123.0 mm<jupyter_text>Convert quantitys to dimensionless quantitiesWrap functions that expect floats value in a certain unit<jupyter_code>@convert_to_unit(mm, mm)
def sum_length_from_floats(x_mm, y_mm):
    """Expects values as floats in mm"""
    return x_mm + y_mm + 1
print(sum_length_from_floats(1.2*m, 2*m))<jupyter_output>3201.0<jupyter_text>Drop dimensionSend the si value<jupyter_code>@drop_dimension
def sum_length_from_floats(x, y):
    """Expect dimensionless objects"""
    return x + y + 1
print(sum_length_from_floats(1.2*m, 2*m))<jupyter_output>4.2<jupyter_text>Adding units to ouputs<jupyter_code>@add_back_unit_param(m, s)
def timed_sum(x_m, y_m):
    time = 10
    return x_m + y_m + 1, time
print(timed_sum(1, 2))<jupyter_output>(<Quantity : 4 m>, <Quantity : 10 s>)<jupyter_text>Enforce consistents dimensionForce same dimension for inputs, without specifying which dimension<jupyter_code>@decorate_with_various_unit(('A', 'A'), 'A')
def another_sum(x, y):
    return x + y
print(another_sum(2*m, 1*m))<jupyter_output>3 m
{'fraction_non_alphanumeric': 0.11053604436229206, 'fraction_numerical': 0.02144177449168207, 'mean_word_length': 6.102362204724409, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '389908', 'license': 'MIT', 'n_tokens_mistral': 1000, 'n_tokens_neox': 956, 'n_words': 279, 'path': 'docs/Decorators.ipynb', 'repo_name': 'mocquin/physipy', 'size': '2705'}