Skip to content

Commit

Permalink
Refactor code and improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Jun 1, 2024
1 parent 67aa85a commit f9001ea
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file. The format
## Second Release Version 1.1.0

- added new functionality
- Function added to check if a given EAN-13 barcode is valid or not
- Array validation multiple functions added

- documentation added
- code refactored and code quality improved
- test cases and test coverage added
Expand Down
2 changes: 0 additions & 2 deletions sanatio/array_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ def isContains(self, value, element) -> bool: # TODO: test cases is pending
if element in value:
return True
return False


2 changes: 1 addition & 1 deletion sanatio/base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def isvalidBoolean(self, value)-> bool:
return False

if isinstance(value, bool):
return True
return True
2 changes: 1 addition & 1 deletion sanatio/date_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dateutil.parser import parse
from dateutil.parser import parse


class DateValidator:
def __init__(self) -> None:
pass
Expand Down Expand Up @@ -59,4 +60,3 @@ def toDate(self, value, format='%Y-%m-%d'): # need to improve this function
return date
else:
return None

3 changes: 1 addition & 2 deletions sanatio/document_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sanatio.utils.utils import all_country, regexs
from sanatio.utils.checksum import checksum_aadhar, checksum_credit_card


class DocumentValidator:
def __init__(self) -> None:
pass
Expand Down Expand Up @@ -53,5 +54,3 @@ def isCreditCard(self, value: str) -> bool: # checksum not implemented
if checksum_credit_card(value):
return True
return False


2 changes: 0 additions & 2 deletions sanatio/file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,3 @@ def isPerl(self, value) -> bool: # TODO: test cases is pending
def isShell(self, value) -> bool: # TODO: test cases is pending
""" check if the string is Shell or not """
pass


2 changes: 1 addition & 1 deletion sanatio/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class Validator(StringValidator, DocumentValidator, UsernameValidator, NumberValidator,
DateValidator, EmailValidator, PasswordValidator, OtherValidator):
DateValidator, EmailValidator, PasswordValidator, OtherValidator, ArrayValidator):
""" Validator class for validating the data """
def __init__(self):
super().__init__()
1 change: 0 additions & 1 deletion sanatio/number_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ def toInt(self, value):
value = 0

return value

16 changes: 3 additions & 13 deletions sanatio/other_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@

from sanatio.utils.utils import all_country, regexs
from sanatio.base_class import BaseValidator

from sanatio.utils.checksum import checksum_ean

class OtherValidator(BaseValidator):
def __init__(self):
super().__init__()

def isEAN13(self, value) -> bool:
""" check if the string is EAN or not """
if isinstance(value, int):
value = str(value)
if not self.isLength(value, 13, 13):
return False

last_digit = int(value[-1])
remaining_numbers = value[:-1]
total_sum = sum(int(remaining_numbers[i]) * 3 if i % 2 != 0 else \
int(remaining_numbers[i]) for i in range(len(remaining_numbers)))
unit_digit = 0 if total_sum % 10 == 0 else 10 - (total_sum % 10)
if unit_digit == last_digit:
return True
value = str(value) if isinstance(value, int) else value
return self.isLength(value, 13, 13) and checksum_ean(value)

def isHash(self, value) -> bool:
""" check if the string is hash or not """
Expand Down
1 change: 0 additions & 1 deletion sanatio/string_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,3 @@ def removeWhiteSpace(self, value):
""" remove white space from string """
if self.isvalidString(value):
return value.replace(" ", "")

15 changes: 6 additions & 9 deletions sanatio/utils/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@
sys.path.append('.')
from sanatio.utils.checksum_algorithms.verhoeff_algorithm import VerhoeffAlgorithm
from sanatio.utils.checksum_algorithms.luhn_algorithm import LuhnAlgorithm

verhoeff = VerhoeffAlgorithm()
luhn = LuhnAlgorithm()
from sanatio.utils.checksum_algorithms.ean_checksum import EANCheckSum


def checksum_aadhar(aadhar_number):
""" validates aadhar number """
return verhoeff.verify(aadhar_number)
return VerhoeffAlgorithm().verify(aadhar_number)

def checksum_pan(pan_number):
""" pancard checksum """
pass

def checksum_credit_card(credit_card_number):
""" credit card checksum """
return luhn.verify(credit_card_number)


return LuhnAlgorithm().verify(credit_card_number)

# res = checksum_credit_card('4578423013769219')
# print(res)
def checksum_ean(ean_number):
""" ean checksum """
return EANCheckSum().is_valid(ean_number)
21 changes: 21 additions & 0 deletions sanatio/utils/checksum_algorithms/ean_checksum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class EANCheckSum:
def __init__(self):
pass

def checksum_ean13(self, value: str) -> bool:
"""Calculate the checksum for the EAN code"""
last_digit = int(value[-1])
remaining_numbers = value[:-1]
total_sum = sum(int(remaining_numbers[i]) * 3 if i % 2 != 0 else \
int(remaining_numbers[i]) for i in range(len(remaining_numbers)))
unit_digit = 0 if total_sum % 10 == 0 else 10 - (total_sum % 10)
if unit_digit == last_digit:
return True

def checksum_ean8(self, value: str) -> bool:
"""Calculate the checksum for the EAN code"""
pass

def is_valid(self, value: str) -> bool:
"""Check if the EAN code is valid"""
return self.checksum_ean13(value) if len(value) == 13 else self.checksum_ean8(value)
4 changes: 1 addition & 3 deletions sanatio/utils/checksum_algorithms/luhn_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def __checksum(self, numbers: str) -> int:
def verify(self, numbers: str) -> bool:
"""Verify a number using Luhn algorithm."""
if isinstance(numbers, int):
number = str(numbers)
numbers = str(numbers)

return self.__checksum(numbers)



2 changes: 1 addition & 1 deletion sanatio/utils/checksum_algorithms/verhoeff_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def verify(self, numbers):
number = str(numbers)

numbers = numbers.replace(' ', '')
return self.__checksum(numbers)
return self.__checksum(numbers)
40 changes: 40 additions & 0 deletions tests/array_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import unittest
sys.path.append('.')
from sanatio import Validator
validator = Validator()


class ArrayTest(unittest.TestCase):
def test_isArray_True(self):
self.assertTrue(validator.isArray([]))
self.assertTrue(validator.isArray([1, 2, 3]))
self.assertTrue(validator.isArray(['a', 'b', 'c']))

def test_isArray_False(self):
self.assertFalse(validator.isArray('abc'))
self.assertFalse(validator.isArray(123))

def test_isLength_True(self):
self.assertTrue(validator.isLength([1, 2, 3], 1, 3))
self.assertTrue(validator.isLength([1, 2, 3], 3, 3))
self.assertTrue(validator.isLength([1, 2, 3], 3, 3))

def test_isLength_False(self):
self.assertFalse(validator.isLength([1, 2, 3], 1, 2))
self.assertFalse(validator.isLength([1, 2, 3], 4, 5))
self.assertFalse(validator.isLength([1, 2, 3], 1, 2))

def test_isContains_True(self):
self.assertTrue(validator.isContains([1, 2, 3], 1))
self.assertTrue(validator.isContains([1, 2, 3], 2))
self.assertTrue(validator.isContains([1, 2, 3], 3))

def test_isContains_False(self):
self.assertFalse(validator.isContains([1, 2, 3], 4))
self.assertFalse(validator.isContains([1, 2, 3], 5))
self.assertFalse(validator.isContains([1, 2, 3], 6))


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions tests/date_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def test_isDate_true(self):

def test_isDate_false(self):
self.assertFalse(validator.isDate('2017-01-32'))
self.assertFalse(validator.isDate('2017-02-29'))
self.assertFalse(validator.isDate('2017-02-30'))

def test_toDate(self):
self.assertEqual(validator.toDate('2017-01-01'), datetime.datetime(2017, 1, 1, 0, 0))
Expand Down

0 comments on commit f9001ea

Please sign in to comment.