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

Commit 748e1f0

Browse files
Documenting - faker.py
Also fixing minor bugs
1 parent 9c50d60 commit 748e1f0

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

algopy/faker.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def generate_uuid(amount: int = 1, version: int = 4, namespace: uuid.UUID = uuid
5050
:return: List of generated UUIDs.
5151
:raises ValueError: If an invalid UUID version is provided or required arguments are missing.
5252
"""
53+
if version not in [1, 3, 4, 5]:
54+
raise ValueError("Invalid UUID version. Use 1, 3, 4, or 5.")
5355
if version in [3, 5] and (namespace is None or name is None):
5456
raise ValueError(f"UUID version {version} requires 'namespace' and 'name' arguments")
5557
uuid_func = {
@@ -96,7 +98,7 @@ def digits_of(n):
9698
return checksum % 10
9799

98100
@classmethod
99-
def __generate_luhn_compliant_number(cls, length: int) -> str:
101+
def __generate_compliant_number(cls, length: int) -> str:
100102
"""
101103
Generate a Luhn-compliant card number of a specified length.
102104
@@ -118,7 +120,7 @@ def credit_card(cls, amount: int = 1) -> list[dict[str, str]]:
118120
"""
119121
credit_cards = []
120122
for _ in range(amount):
121-
credit_card_number = cls.__generate_luhn_compliant_number(16)
123+
credit_card_number = cls.__generate_compliant_number(16)
122124
cvv = ''.join(random.choices(string.digits, k=3))
123125
expiration_date = f"{random.randint(1, 12):02d}/{random.randint(22, 30):02d}"
124126
credit_cards.append({
@@ -176,6 +178,9 @@ def name(cls, format: str = None, amount: int = 1) -> list[str]:
176178
"""
177179
Generate a list of random names.
178180
181+
Format string can contain {first_name} and {last_name} placeholders.
182+
Example format: "{first_name} {last_name}".
183+
179184
:param format: Optional format string for the names, using {first_name} and {last_name} as placeholders.
180185
:param amount: Number of names to generate.
181186
:return: List of generated names.
@@ -190,6 +195,9 @@ def address(cls, format: str = None, amount: int = 1) -> list[str]:
190195
"""
191196
Generate a list of random addresses.
192197
198+
Format string can contain {street_address}, {city}, {country}, and {postal_code} placeholders.
199+
Example format: "{street_address}, {city}, {country} {postal_code}".
200+
193201
:param format: Optional format string for the addresses, using {street_address}, {city}, {country}, and {postal_code} as placeholders.
194202
:param amount: Number of addresses to generate.
195203
:return: List of generated addresses.
@@ -223,6 +231,9 @@ def phone_number(format: str = None, amount: int = 1) -> list[str]:
223231
"""
224232
Generate a list of random phone numbers.
225233
234+
Format string can contain {phone_number} as a placeholder.
235+
Example format: "John's phone number is {phone_number}".
236+
226237
:param format: Optional format string for the phone numbers, using {phone_number} as a placeholder.
227238
:param amount: Number of phone numbers to generate.
228239
:return: List of generated phone numbers.
@@ -236,6 +247,9 @@ def email(cls, format: str = None, amount: int = 1) -> list[str]:
236247
"""
237248
Generate a list of random email addresses.
238249
250+
Format string can contain {email} as a placeholder.
251+
Example format: "John's email is {email}"
252+
239253
:param format: Optional format string for the email addresses, using {email} as a placeholder.
240254
:param amount: Number of email addresses to generate.
241255
:return: List of generated email addresses.
@@ -251,6 +265,9 @@ def date(format: str = None, amount: int = 1) -> list[str]:
251265
"""
252266
Generate a list of random dates.
253267
268+
Format string should be in the strftime format.
269+
Example format: "%Y-%m-%d".
270+
254271
:param format: Optional format string for the dates.
255272
:param amount: Number of dates to generate.
256273
:return: List of generated dates.
@@ -302,7 +319,7 @@ def job_title(cls, amount: int = 1) -> list[str]:
302319
return [str(random.choice(cls.job_titles)) for _ in range(amount)]
303320

304321
@staticmethod
305-
def employee_id(amount: int = 1, characters_to_use: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> list[str]:
322+
def employee_id(amount: int = 1, characters_to_use: str = (string.ascii_uppercase + string.digits)) -> list[str]:
306323
"""
307324
Generate a list of random employee IDs.
308325
@@ -376,13 +393,14 @@ def __init__(cls, extra_domains: list = None):
376393
377394
:param extra_domains: List of additional domains to include.
378395
"""
379-
cls.domains = ["example.com", "test.com", "sample.org", "demo.net"]
396+
cls.domains = ["example.com", "test.com", "sample.org", "demo.net", "fake.com",
397+
"mock.org", "example.net", "test.org", "sample.com", "demo.net"]
380398
if extra_domains:
381399
cls.domains.extend(extra_domains)
382400

383401
@staticmethod
384402
def generate_username(amount: int = 1, size: int = 8,
385-
charset: str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-<>?!@#$%^&*()[]{};=-_") -> list[str]:
403+
charset: str = string.ascii_letters) -> list[str]:
386404
"""
387405
Generate a list of random usernames.
388406
@@ -395,7 +413,7 @@ def generate_username(amount: int = 1, size: int = 8,
395413

396414
@staticmethod
397415
def generate_password(amount: int = 1, size: int = 12,
398-
charset: str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-<>?!@#$%^&*()[]{};=-_") -> list[str]:
416+
charset: str = (string.ascii_letters + string.digits + string.punctuation)) -> list[str]:
399417
"""
400418
Generate a list of random passwords.
401419
@@ -421,13 +439,18 @@ def generate_ip_address(amount: int = 1, version=4) -> list[str]:
421439
ip_addresses.append('.'.join(str(random.randint(0, 255)) for _ in range(4)))
422440
elif version == 6:
423441
ip_addresses.append(':'.join(''.join(random.choices('0123456789abcdef', k=4)) for _ in range(8)))
442+
else:
443+
raise ValueError("Invalid IP version. Use 4 or 6.")
424444
return ip_addresses
425445

426446
@classmethod
427447
def generate_url(cls, amount: int = 1, format: str = None) -> list[str]:
428448
"""
429449
Generate a list of random URLs.
430450
451+
Format string can contain {domain} and {path} placeholders.
452+
Example format: "https://{domain}/{path}".
453+
431454
:param amount: Number of URLs to generate.
432455
:param format: Optional format string for the URLs, using {domain} and {path} as placeholders.
433456
:return: List of generated URLs.

0 commit comments

Comments
 (0)