Giter Site home page Giter Site logo

miloth / guardian Goto Github PK

View Code? Open in Web Editor NEW

This project forked from roeiohayon/guardian

0.0 0.0 0.0 207 KB

Use builtin guard clauses, and easily create custom guard clauses to improve code quality in python

License: MIT License

Python 100.00%

guardian's Introduction

Guardian Logo

Python PyPI PyPI Tweet

Guardian

A Guard Clause is a validation of information used to avoid errors during execution, and enables to "fail fast".
This package provides:

  • Function decorator guard clauses (All function arguments will be checked)
  • General use case guard clauses
  • Ability to create custom guard clauses

Install

pip install guardian-python

Usage

from guardian import guard


@guard.none()
def buy_item(username: str, users_repository: UsersRepository, products_repository: ProductsRepository):
    guard.not_alphabetic(username)
    ...

Supported Guard Clauses

  • numbers: zero, not_negative, not_positive, in_range, out_of_range
  • strings: matches_regex, not_matches_regex, whitespace, uppercase, lowercase, not_numeric, not_alphabetic, not_alphanumeric, empty_or_whitespace
  • dates: in_date_range, out_of_date_range, before, after
  • general: none, contains, equals, empty (list, str, tuple...)

Create a Custom Guard

custom_guard.py:

from guardian import Guard

dead = Guard(name="dead", predicate=lambda v: v.HP + v.armor <= 0, description="Don't Perform if dead")
alive = Guard(name="alive", predicate=lambda v: v.HP + v.armor > 0, description="Don't Perform if alive")

main.py:

import custom_guard

@custom_guard.dead()
def shoot(enemy: Enemy):
  ...

@custom_guard.alive()
def revive(player: Player):
  ... 

Additional Parameters

  • property: extracts property from objects
  • key: get value at key
  • transformer: function to extract wanted data to guard against
@guard.none(property="name")
def login(user: User):
  # if user.name is None an exception will be thrown

@guard.none(key="name")
def login(user: dict):
  # if user["name"] is None an exception will be thrown

@guard.none(transformer=lambda v: v.name)
def login(user: User):
  # if user.name is None an exception will be thrown

More Code Examples:

guard.matches_regex(".*el$")
def greet_user(username: str):
  # username = "Daniel" will throw an exception
  # username = "John" won't throw an exception
  ...

# Alternatively you can use:
def greet_user(username: str):
  guard.matches_regex(username, ".*el$")
  ...

@guard.out_of_range([0, 255])
def rgb_to_hsv(r: int, g: int, b: int) -> Tuple[int, int, int]:
  ...

Feel free to give a โญ if you enjoy using this project ๐Ÿ˜Š

guardian's People

Contributors

roeiohayon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.