Skip to content

Python good practices | Prevent unwanted bugs

installation

To install ensure run the following command:

pip install ensure

Code used in the demo

Code part 01 used in the video
demo.py
def is_product_is_positive(x: int, y: int) -> bool:
    if isinstance(x, int) and isinstance(y, int):
        if x*y >= 0:
            return True
        return False
    else:
        raise TypeError("x and y must be integers")

result = is_product_is_positive(x=2, y=2)
print(f"Case1: {result}")

result = is_product_is_positive(x=2, y=22.2)
print(f"Case2: {result}")

result = is_product_is_positive(x=2, y="test")
print(f"Case2: {result}")
Code part 02 used in the video
demo_ensure.py
from ensure import ensure_annotations

@ensure_annotations
def is_product_is_positive(x: int, y: int) -> bool:
    if x*y >= 0:
        return True
    return False

# result = is_product_is_positive(x=2, y=2)
# print(f"Case1: {result}")

# result = is_product_is_positive(x=2, y=22)
# print(f"Case2: {result}")

# result = is_product_is_positive(x=2, y="test")
# print(f"Case2: {result}")

@ensure_annotations
def get_product(x: int, y: int) -> int:
    return "x*y"

result = get_product(x=2, y=22)
print(f"Case1: {result}")