Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2e2d424

Browse files
Fixing codecov issues - validate.py
1 parent 6bdca79 commit 2e2d424

File tree

2 files changed

+67
-54
lines changed

2 files changed

+67
-54
lines changed

algopy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
AlgoPy: A library for many different algorithms and other utilities.
33
"""
44

5-
from . import find, log, sort, validate, convert, faker
5+
from . import find, log, validate, convert, faker, sort
66

77
find = find.Find
88
log = log.Log

algopy/validate.py

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
class Validate:
66
@staticmethod
77
def this_email(email_address: str) -> bool:
8-
if (" " in email_address or "@" not in email_address) and (not (1 <= len(email_address) <= 320)):
9-
return False
10-
local_part, domain_part = email_address.rsplit("@", 1)
11-
if not local_part or not domain_part or "." not in domain_part:
12-
return False
13-
domain_labels = domain_part.split(".")
14-
if (not all(label.isalnum() for label in domain_labels)) and (not 2 <= len(domain_labels[-1]) <= 63):
15-
return False
16-
return True
8+
if (1 <= len(email_address) <= 320) and " " not in email_address and "@" in email_address:
9+
local_part, domain_part = email_address.rsplit("@", 1)
10+
if local_part and domain_part and "." in domain_part:
11+
domain_labels = domain_part.split(".")
12+
if all(label.isalnum() for label in domain_labels) and 2 <= len(domain_labels[-1]) <= 63:
13+
return True
14+
return False
1715

1816
@staticmethod
1917
def _url_by_parsing(url: str) -> dict | bool:
@@ -55,49 +53,56 @@ def this_url(cls, url: str, text_error: bool = False,
5553
enforce_https: bool = False) -> bool:
5654
parsed_url = cls._url_by_parsing(url)
5755
if not parsed_url:
58-
error_message = 'Invalid URL: Reason - URL is empty or does not contain a protocol'
59-
return error_message if text_error else False
60-
61-
# Validate protocol
62-
if parsed_url['protocol'] not in ['http', 'https']:
63-
error_message = 'Invalid URL: Reason - Protocol is not HTTP or HTTPS'
64-
return error_message if text_error else False
65-
elif enforce_https and parsed_url['protocol'] != 'https':
66-
error_message = 'Invalid URL: Reason - HTTPS protocol is enforced'
67-
return error_message if text_error else False
68-
69-
# Validate hostname
70-
hostname = parsed_url['hostname']
56+
return cls.__return_error('Invalid URL: Reason - URL is empty or does not contain a protocol', text_error)
57+
58+
if not cls.__validate_protocol(parsed_url['protocol'], enforce_https, text_error):
59+
return False
60+
61+
if not cls.__validate_hostname(parsed_url['hostname'], text_error):
62+
return False
63+
64+
if not cls.__validate_filename(parsed_url['filename'], text_error):
65+
return False
66+
67+
if not parsed_url['TLD']:
68+
return cls.__return_error('Invalid URL: Reason - TLD is not registered', text_error)
69+
70+
return True
71+
72+
@staticmethod
73+
def __return_error(message: str, text_error: bool) -> bool:
74+
return message if text_error else False
75+
76+
@classmethod
77+
def __validate_protocol(cls, protocol: str, enforce_https: bool, text_error: bool) -> bool:
78+
if protocol not in ['http', 'https']:
79+
return cls.__return_error('Invalid URL: Reason - Protocol is not HTTP or HTTPS', text_error)
80+
if enforce_https and protocol != 'https':
81+
return cls.__return_error('Invalid URL: Reason - HTTPS protocol is enforced', text_error)
82+
return True
83+
84+
@classmethod
85+
def __validate_hostname(cls, hostname: str, text_error: bool) -> bool:
7186
if len(hostname) > 253:
72-
error_message = 'Invalid URL: Reason - Hostname is too long'
73-
return error_message if text_error else False
87+
return cls.__return_error('Invalid URL: Reason - Hostname is too long', text_error)
7488
if hostname.count('.') < 1:
75-
error_message = 'Invalid URL: Reason - Hostname does not contain a domain'
76-
return error_message if text_error else False
89+
return cls.__return_error('Invalid URL: Reason - Hostname does not contain a domain', text_error)
7790
hostname_labels = hostname.split('.')
7891
for label in hostname_labels:
7992
if not all(c.isalnum() or c == '-' for c in label):
80-
error_message = 'Invalid URL: Reason - Invalid character in hostname'
81-
return error_message if text_error else False
93+
return cls.__return_error('Invalid URL: Reason - Invalid character in hostname', text_error)
8294
if label.startswith('-') or label.endswith('-'):
83-
error_message = 'Invalid URL: Reason - Label starts or ends with a hyphen'
84-
return error_message if text_error else False
95+
return cls.__return_error('Invalid URL: Reason - Label starts or ends with a hyphen', text_error)
8596
if label.isdigit():
86-
error_message = 'Invalid URL: Reason - Label is a number'
87-
return error_message if text_error else False
97+
return cls.__return_error('Invalid URL: Reason - Label is a number', text_error)
98+
return True
8899

89-
# Validate filename
90-
filename = parsed_url['filename']
100+
@classmethod
101+
def __validate_filename(cls, filename: str, text_error: bool) -> bool:
91102
if len(filename) > 256:
92-
error_message = 'Invalid URL: Reason - Filename is too long'
93-
return error_message if text_error else False
103+
return cls.__return_error('Invalid URL: Reason - Filename is too long', text_error)
94104
if ' ' in filename:
95-
error_message = 'Invalid URL: Reason - Filename contains spaces'
96-
return error_message if text_error else False
97-
98-
if not parsed_url['TLD']:
99-
error_message = 'Invalid URL: Reason - TLD is not registered'
100-
return error_message if text_error else False
105+
return cls.__return_error('Invalid URL: Reason - Filename contains spaces', text_error)
101106
return True
102107

103108
@staticmethod
@@ -196,8 +201,8 @@ def __init__(self):
196201
"""
197202
pass
198203

199-
@staticmethod
200-
def __luhn_algorithm(card_number: str) -> bool:
204+
@classmethod
205+
def __luhn_algorithm(cls, card_number: str) -> bool:
201206
"""
202207
Validates a card number using the Luhn algorithm.
203208
@@ -209,19 +214,27 @@ def __luhn_algorithm(card_number: str) -> bool:
209214
"""
210215
if len(card_number) < 13 or len(card_number) > 19:
211216
return False
217+
212218
num_list = [int(digit) for digit in card_number]
213219
num_list.reverse()
214-
total = 0
215-
for i, num in enumerate(num_list):
216-
if i % 2 == 1:
217-
doubled = num * 2
218-
if doubled > 9:
219-
doubled -= 9
220-
total += doubled
221-
else:
222-
total += num
220+
221+
total = sum(cls.__luhn_double(num) if i % 2 == 1 else num for i, num in enumerate(num_list))
223222
return total % 10 == 0
224223

224+
@staticmethod
225+
def __luhn_double(num: int) -> int:
226+
"""
227+
Doubles the number and subtracts 9 if the result is greater than 9.
228+
229+
Args:
230+
num (int): The number to be doubled.
231+
232+
Returns:
233+
int: The processed number.
234+
"""
235+
doubled = num * 2
236+
return doubled - 9 if doubled > 9 else doubled
237+
225238
@classmethod
226239
def american_express(cls, card_number: str) -> bool:
227240
"""

0 commit comments

Comments
 (0)