Skip to content

Commit

Permalink
Helper valitor for lists (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Tiago Gandarez <[email protected]>
  • Loading branch information
tiagogandarezff and gandarezt authored Jan 18, 2022
1 parent 99f2f18 commit 8d1cada
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 3 deletions.
2 changes: 1 addition & 1 deletion checkarg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__doc__ = "__init__"
__version__ = "0.1.0"
__version__ = "0.2.0"
1 change: 1 addition & 0 deletions checkarg/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .error_messages import (
DefaultErrorMessages,
ListErrorMessages,
NoneTypeErrorMessages,
NumberErrorMessages,
TextErrorMessages,
Expand Down
34 changes: 34 additions & 0 deletions checkarg/exceptions/error_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,37 @@ def is_equal_to_message(argument_name, value, conditional_value):
@staticmethod
def is_not_equal_to_message(argument_name, value):
return f"Argument {argument_name} shouldn't be equal to {value}"


class ListErrorMessages:
@staticmethod
def is_not_empty_message(argument_name):
return f"Argument {argument_name} shouldn't be empty"

@staticmethod
def has_length_lower_message(argument_name, condition_value):
return f"The length of argument {argument_name} should be lower than {condition_value}"

@staticmethod
def has_length_greater_message(argument_name, condition_value):
return f"The length of argument {argument_name} should be higher than {condition_value}"

@staticmethod
def has_length_equals_message(argument_name, condition_value):
return f"The length of argument {argument_name} should be equals to {condition_value}"

@staticmethod
def contains_message(argument_name, element):
return f"The element {element} is not present in the argument {argument_name}"

@staticmethod
def all_elements_of_same_type_message(argument_name):
return f"All the elements of the argument {argument_name} should be of the same type"

@staticmethod
def all_elements_of_type_message(argument_name, type):
return f"All the elements of the argument {argument_name} should be of the type {type}"

@staticmethod
def has_not_repeated_elements_message(argument_name):
return f"Argument {argument_name} shouldn't have repeated values"
130 changes: 130 additions & 0 deletions checkarg/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from typing import List, TypeVar

import checkarg.none_type as NoneType
from checkarg.exceptions import ArgumentError, ListErrorMessages

T = TypeVar("T")


def is_not_empty(
list: List, argument_name: str = None, exception: Exception = None
) -> None:
""" Check if the given list is not empty """
NoneType.is_not_none(list, argument_name, exception)
if not list:
raise ArgumentError(
ListErrorMessages.is_not_empty_message(argument_name), argument_name
) if exception is None else exception


def has_length_lower(
list: List,
condition_value: int,
argument_name: str = None,
exception: Exception = None,
) -> None:
""" Check if the given list length is lower than the condition_value """
NoneType.is_not_none(list, argument_name, exception)
if len(list) >= condition_value:
raise ArgumentError(
ListErrorMessages.has_length_lower_message(argument_name, condition_value),
argument_name,
) if exception is None else exception


def has_length_greater(
list: List,
condition_value: int,
argument_name: str = None,
exception: Exception = None,
) -> None:
""" Check if the given list length is greater than the condition_value """
NoneType.is_not_none(list, argument_name, exception)
if len(list) <= condition_value:
raise ArgumentError(
ListErrorMessages.has_length_greater_message(
argument_name, condition_value
),
argument_name,
) if exception is None else exception


def has_length_between(
list: List,
min_length: int,
max_length: int,
argument_name: str = None,
exception: Exception = None,
) -> None:
""" Check if the given list length is between min_length and max_length """

has_length_greater(list, min_length - 1, argument_name, exception)
has_length_lower(list, max_length + 1, argument_name, exception)


def is_length_equals(
list: List,
condition_value: int,
argument_name: str = None,
exception: Exception = None,
) -> None:
""" Check if the given list length is equals than the condition_value """
NoneType.is_not_none(list, argument_name, exception)
if len(list) != condition_value:
raise ArgumentError(
ListErrorMessages.has_length_equals_message(argument_name, condition_value),
argument_name,
) if exception is None else exception


def contains(
list: List, element: T, argument_name: str = None, exception: Exception = None
) -> None:
""" Check if a given element is present in the list """
NoneType.is_not_none(list, argument_name, exception)
if element not in list:
raise ArgumentError(
ListErrorMessages.contains_message(argument_name, element), argument_name
) if exception is None else exception


def has_all_elements_of_same_type(
list: List, argument_name: str = None, exception: Exception = None
) -> None:
""" Check if the elements' list are all of the same type """
NoneType.is_not_none(list, argument_name, exception)

if len(set(map(type, list))) != 1:
raise ArgumentError(
ListErrorMessages.all_elements_of_same_type_message(argument_name),
argument_name,
) if exception is None else exception


def has_all_elements_of_type(
list: List,
expected_type: type,
argument_name: str = None,
exception: Exception = None,
) -> None:
""" Check if the elements' list are all of a given type """
NoneType.is_not_none(list, argument_name, exception)
if any(type(element) != expected_type for element in list):
raise ArgumentError(
ListErrorMessages.all_elements_of_type_message(
argument_name, expected_type
),
argument_name,
) if exception is None else exception


def has_no_repeated_elements(
list: List, argument_name: str = None, exception: Exception = None
) -> None:
""" Check if the list has not repeated values """
NoneType.is_not_none(list, argument_name, exception)
if len(list) != len(set(list)):
raise ArgumentError(
ListErrorMessages.has_not_repeated_elements_message(argument_name),
argument_name,
) if exception is None else exception
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.0
current_version = 0.2.0

[bumpversion:file:checkarg/__init__.py]

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name=package_name,
version="0.1.0",
version="0.2.0",
url="https://github.com/Farfetch/checkarg.git",
license="MIT License (MIT)",
include_package_data=True,
Expand Down
Loading

0 comments on commit 8d1cada

Please sign in to comment.