jupyter/faker.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import os\n",
"import random\n",
"import string\n",
"import uuid\n",
"from datetime import datetime, timedelta\n",
"\n",
"\n",
"class Faker:\n",
" class Misc:\n",
" @classmethod\n",
" def __init__(cls, extra_words: list = None):\n",
" \"\"\"\n",
" Initialize the Misc class with optional extra words for random text generation.\n",
"\n",
" :param extra_words: List of additional words to include in the random text generation.\n",
" \"\"\"\n",
" cls.words = [\n",
" \"Lorem\", \"ipsum\", \"dolor\", \"sit\", \"amet\", \"consectetur\", \"adipiscing\", \"elit\", \"sed\", \"do\", \"eiusmod\",\n",
" \"tempor\", \"incididunt\", \"ut\", \"labore\", \"et\", \"dolore\", \"magna\", \"aliqua\", \"Ut\", \"enim\", \"ad\", \"minim\",\n",
" \"veniam\", \"quis\", \"nostrud\", \"exercitation\", \"ullamco\", \"laboris\", \"nisi\", \"ut\", \"aliquip\", \"ex\", \"ea\",\n",
" \"commodo\", \"consequat\", \"Duis\", \"aute\", \"irure\", \"dolor\", \"in\", \"reprehenderit\", \"in\", \"voluptate\",\n",
" \"velit\", \"esse\", \"cillum\", \"dolore\", \"eu\", \"fugiat\", \"nulla\", \"pariatur\", \"Excepteur\", \"sint\",\n",
" \"occaecat\", \"cupidatat\", \"non\", \"proident\", \"sunt\", \"in\", \"culpa\", \"qui\", \"officia\", \"deserunt\",\n",
" \"mollit\", \"anim\", \"id\", \"est\", \"laborum\"\n",
" ]\n",
" if extra_words:\n",
" cls.words.extend(extra_words)\n",
"\n",
" @staticmethod\n",
" def generate_barcode(amount: int = 1, length: int = 12) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random barcodes.\n",
"\n",
" :param amount: Number of barcodes to generate.\n",
" :param length: Length of each barcode.\n",
" :return: List of generated barcodes.\n",
" \"\"\"\n",
" return [''.join(random.choices(string.digits, k=length)) for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def generate_uuid(amount: int = 1, version: int = 4, namespace: uuid.UUID = uuid.uuid1(),\n",
" name: str = os.name) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of UUIDs.\n",
"\n",
" :param amount: Number of UUIDs to generate.\n",
" :param version: Version of the UUID to generate (1, 3, 4, or 5).\n",
" :param namespace: Namespace for UUID versions 3 and 5.\n",
" :param name: Name for UUID versions 3 and 5.\n",
" :return: List of generated UUIDs.\n",
" :raises ValueError: If an invalid UUID version is provided or required arguments are missing.\n",
" \"\"\"\n",
" if version not in [1, 3, 4, 5]:\n",
" raise ValueError(\"Invalid UUID version. Use 1, 3, 4, or 5.\")\n",
" if version in [3, 5] and (namespace is None or name is None):\n",
" raise ValueError(f\"UUID version {version} requires 'namespace' and 'name' arguments\")\n",
" uuid_func = {\n",
" 1: uuid.uuid1,\n",
" 3: lambda: uuid.uuid3(namespace, name),\n",
" 4: uuid.uuid4,\n",
" 5: lambda: uuid.uuid5(namespace, name)\n",
" }.get(version)\n",
" if uuid_func is None:\n",
" raise ValueError(f\"Invalid UUID version: {version}\")\n",
" return [str(uuid_func()) for _ in range(amount)]\n",
"\n",
" @classmethod\n",
" def generate_random_text(cls, amount: int = 1, length: int = 100) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random text strings.\n",
"\n",
" :param amount: Number of text strings to generate.\n",
" :param length: Number of words in each text string.\n",
" :return: List of generated random text strings.\n",
" \"\"\"\n",
" if not hasattr(cls, 'words'):\n",
" cls.__init__()\n",
" return [' '.join(random.choices(cls.words, k=length)) for _ in range(amount)]\n",
"\n",
" class Financial:\n",
" @staticmethod\n",
" def __luhn_checksum(card_number: str) -> int:\n",
" \"\"\"\n",
" Calculate the Luhn checksum for a card number.\n",
"\n",
" :param card_number: Card number to calculate the checksum for.\n",
" :return: Luhn checksum.\n",
" \"\"\"\n",
" def digits_of(n):\n",
" return [int(d) for d in str(n)]\n",
"\n",
" digits = digits_of(card_number)\n",
" odd_digits = digits[-1::-2]\n",
" even_digits = digits[-2::-2]\n",
" checksum = sum(odd_digits)\n",
" for d in even_digits:\n",
" checksum += sum(digits_of(d * 2))\n",
" return checksum % 10\n",
"\n",
" @classmethod\n",
" def __generate_compliant_number(cls, length: int) -> str:\n",
" \"\"\"\n",
" Generate a Luhn-compliant card number of a specified length.\n",
"\n",
" :param length: Length of the card number to generate.\n",
" :return: Generated Luhn-compliant card number.\n",
" \"\"\"\n",
" number = ''.join(random.choices(string.digits, k=length - 1))\n",
" checksum = cls.__luhn_checksum(number + '0')\n",
" check_digit = (10 - checksum) % 10\n",
" return number + str(check_digit)\n",
"\n",
" @classmethod\n",
" def credit_card(cls, amount: int = 1) -> list[dict[str, str]]:\n",
" \"\"\"\n",
" Generate a list of random credit card details.\n",
"\n",
" :param amount: Number of credit card details to generate.\n",
" :return: List of generated credit card details.\n",
" \"\"\"\n",
" credit_cards = []\n",
" for _ in range(amount):\n",
" credit_card_number = cls.__generate_compliant_number(16)\n",
" cvv = ''.join(random.choices(string.digits, k=3))\n",
" expiration_date = f\"{random.randint(1, 12):02d}/{random.randint(22, 30):02d}\"\n",
" credit_cards.append({\n",
" \"card_number\": credit_card_number,\n",
" \"cvv\": cvv,\n",
" \"expiration_date\": expiration_date\n",
" })\n",
" return credit_cards\n",
"\n",
" @staticmethod\n",
" def bank_account(amount: int = 1) -> list[dict[str, str]]:\n",
" \"\"\"\n",
" Generate a list of random bank account details.\n",
"\n",
" :param amount: Number of bank account details to generate.\n",
" :return: List of generated bank account details.\n",
" \"\"\"\n",
" bank_accounts = []\n",
" for _ in range(amount):\n",
" bank_account_number = ''.join(random.choices(string.digits, k=12))\n",
" bank_routing_number = ''.join(random.choices(string.digits, k=8))\n",
" bank_routing_number += str(\n",
" (10 - sum(int(digit) for digit in bank_routing_number) % 10) % 10) # Check digit\n",
" bank_accounts.append({\n",
" \"account_number\": bank_account_number,\n",
" \"routing_number\": bank_routing_number\n",
" })\n",
" return bank_accounts\n",
"\n",
" class Personal:\n",
" @classmethod\n",
" def __init__(cls, extra_data: dict = None):\n",
" \"\"\"\n",
" Initialize the Personal class with optional extra data for name, address, and email generation.\n",
"\n",
" :param extra_data: Dictionary containing additional data for first names, last names, cities, countries, street names, and domains.\n",
" \"\"\"\n",
" cls.first_names = [\"John\", \"Jane\", \"Alice\", \"Bob\", \"Charlie\", \"David\", \"Eve\", \"Frank\", \"Grace\", \"Hank\"]\n",
" cls.last_names = [\"Doe\", \"Smith\", \"Johnson\", \"Williams\", \"Brown\", \"Jones\", \"Garcia\", \"Miller\", \"Davis\", \"Martinez\"]\n",
" cls.cities = [\"New York\", \"Los Angeles\", \"Chicago\", \"Houston\", \"Phoenix\", \"Philadelphia\", \"San Antonio\", \"San Diego\", \"Dallas\", \"San Jose\"]\n",
" cls.countries = [\"USA\", \"Canada\", \"Mexico\", \"UK\", \"Germany\", \"France\", \"Italy\", \"Spain\", \"Australia\", \"Japan\"]\n",
" cls.street_names = [\"Main\", \"Broadway\", \"Market\", \"Elm\", \"Maple\", \"Oak\", \"Pine\", \"Cedar\", \"Birch\", \"Walnut\"]\n",
" cls.domains = [\"example.com\", \"test.com\", \"demo.com\", \"fake.com\", \"sample.com\", \"mock.com\", \"dummy.com\", \"faux.com\", \"simulated.com\", \"placeholder.com\"]\n",
"\n",
" if extra_data:\n",
" cls.first_names.extend(extra_data.get(\"extra_first_names\", []))\n",
" cls.last_names.extend(extra_data.get(\"extra_last_names\", []))\n",
" cls.cities.extend(extra_data.get(\"extra_cities\", []))\n",
" cls.countries.extend(extra_data.get(\"extra_countries\", []))\n",
" cls.street_names.extend(extra_data.get(\"extra_street_names\", []))\n",
" cls.domains.extend(extra_data.get(\"extra_domains\", []))\n",
"\n",
" @classmethod\n",
" def name(cls, format: str = None, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random names.\n",
"\n",
" Format string can contain {first_name} and {last_name} placeholders.\n",
" Example format: \"{first_name} {last_name}\".\n",
"\n",
" :param format: Optional format string for the names, using {first_name} and {last_name} as placeholders.\n",
" :param amount: Number of names to generate.\n",
" :return: List of generated names.\n",
" \"\"\"\n",
" return [format.replace(\"{first_name}\", random.choice(cls.first_names)).replace(\"{last_name}\", random.choice(\n",
" cls.last_names)) if format else f\"{random.choice(cls.first_names)} {random.choice(cls.last_names)}\" for\n",
" _\n",
" in range(amount)]\n",
"\n",
" @classmethod\n",
" def address(cls, format: str = None, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random addresses.\n",
"\n",
" Format string can contain {street_address}, {city}, {country}, and {postal_code} placeholders.\n",
" Example format: \"{street_address}, {city}, {country} {postal_code}\".\n",
"\n",
" :param format: Optional format string for the addresses, using {street_address}, {city}, {country}, and {postal_code} as placeholders.\n",
" :param amount: Number of addresses to generate.\n",
" :return: List of generated addresses.\n",
" \"\"\"\n",
" if not hasattr(cls, 'street_names'):\n",
" cls.__init__()\n",
" addresses = []\n",
" for _ in range(amount):\n",
" address = cls.__helper_address()\n",
" if format:\n",
" formatted_address = format\n",
" for key, value in address.items():\n",
" formatted_address = formatted_address.replace(f\"{{{key}}}\", value)\n",
" addresses.append(formatted_address)\n",
" else:\n",
" addresses.append(address)\n",
" return addresses\n",
"\n",
" @classmethod\n",
" def __helper_address(cls):\n",
" address = {\n",
" \"street_address\": f\"{random.randint(1, 9999)} {random.choice(cls.street_names)} St\",\n",
" \"city\": random.choice(cls.cities),\n",
" \"country\": random.choice(cls.countries),\n",
" \"postal_code\": f\"{random.randint(10000, 99999)}\"\n",
" }\n",
" return address\n",
"\n",
" @staticmethod\n",
" def phone_number(format: str = None, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random phone numbers.\n",
"\n",
" Format string can contain {phone_number} as a placeholder.\n",
" Example format: \"John's phone number is {phone_number}\".\n",
"\n",
" :param format: Optional format string for the phone numbers, using {phone_number} as a placeholder.\n",
" :param amount: Number of phone numbers to generate.\n",
" :return: List of generated phone numbers.\n",
" \"\"\"\n",
" return [format.replace(\"{phone_number}\",\n",
" f\"{random.randint(100, 999)}-{random.randint(100, 999)}-{random.randint(1000, 9999)}\") if format else f\"{random.randint(100, 999)}-{random.randint(100, 999)}-{random.randint(1000, 9999)}\"\n",
" for _ in range(amount)]\n",
"\n",
" @classmethod\n",
" def email(cls, format: str = None, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random email addresses.\n",
"\n",
" Format string can contain {email} as a placeholder.\n",
" Example format: \"John's email is {email}\"\n",
"\n",
" :param format: Optional format string for the email addresses, using {email} as a placeholder.\n",
" :param amount: Number of email addresses to generate.\n",
" :return: List of generated email addresses.\n",
" \"\"\"\n",
" if not hasattr(cls, 'first_names') or not hasattr(cls, 'last_names') or not hasattr(cls, 'domains'):\n",
" cls.__init__()\n",
" return [format.replace(\"{email}\",\n",
" f\"{random.choice(cls.first_names).lower()}.{random.choice(cls.last_names).lower()}@{random.choice(cls.domains)}\") if format else f\"{random.choice(cls.first_names).lower()}.{random.choice(cls.last_names).lower()}@{random.choice(cls.domains)}\"\n",
" for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def date(format: str = None, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random dates.\n",
"\n",
" Format string should be in the strftime format.\n",
" Example format: \"%Y-%m-%d\".\n",
"\n",
" :param format: Optional format string for the dates.\n",
" :param amount: Number of dates to generate.\n",
" :return: List of generated dates.\n",
" \"\"\"\n",
" dates = []\n",
" for _ in range(amount):\n",
" start_date = datetime.now()\n",
" end_date = start_date + timedelta(days=365)\n",
" date = start_date + (end_date - start_date) * random.random()\n",
" dates.append(date.strftime(format) if format else date.strftime(\"%Y-%m-%d\"))\n",
" return dates\n",
"\n",
" class Business:\n",
" @classmethod\n",
" def __init__(cls, extra_company_names: list = None, extra_job_titles: list = None):\n",
" \"\"\"\n",
" Initialize the Business class with optional extra data for company names and job titles.\n",
"\n",
" :param extra_company_names: List of additional company names.\n",
" :param extra_job_titles: List of additional job titles.\n",
" \"\"\"\n",
" cls.company_names = [\"TechCorp\", \"InnovateX\", \"AlphaSolutions\", \"BetaWorks\", \"GammaEnterprises\"]\n",
" cls.job_titles = [\"Software Engineer\", \"Data Scientist\", \"Product Manager\", \"Designer\", \"QA Engineer\"]\n",
" if extra_company_names:\n",
" cls.company_names.extend(extra_company_names)\n",
" if extra_job_titles:\n",
" cls.job_titles.extend(extra_job_titles)\n",
"\n",
" @classmethod\n",
" def company_name(cls, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random company names.\n",
"\n",
" :param amount: Number of company names to generate.\n",
" :return: List of generated company names.\n",
" \"\"\"\n",
" if not hasattr(cls, 'company_names'):\n",
" cls.__init__()\n",
" return [str(random.choice(cls.company_names)) for _ in range(amount)]\n",
"\n",
" @classmethod\n",
" def job_title(cls, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random job titles.\n",
"\n",
" :param amount: Number of job titles to generate.\n",
" :return: List of generated job titles.\n",
" \"\"\"\n",
" return [str(random.choice(cls.job_titles)) for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def employee_id(amount: int = 1, characters_to_use: str = (string.ascii_uppercase + string.digits)) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random employee IDs.\n",
"\n",
" :param amount: Number of employee IDs to generate.\n",
" :param characters_to_use: Characters to use for generating the employee IDs.\n",
" :return: List of generated employee IDs.\n",
" \"\"\"\n",
" return [''.join(random.choices(characters_to_use, k=8)) for _ in range(amount)]\n",
"\n",
" class Product:\n",
" @classmethod\n",
" def __init__(cls, extra_product_names: list = None, extra_product_categories: list = None):\n",
" \"\"\"\n",
" Initialize the Product class with optional extra data for product names and categories.\n",
"\n",
" :param extra_product_names: List of additional product names.\n",
" :param extra_product_categories: List of additional product categories.\n",
" \"\"\"\n",
" cls.product_names = [\"Widget\", \"Gadget\", \"Thingamajig\", \"Doodad\", \"Gizmo\"]\n",
" cls.product_categories = [\"Electronics\", \"Home\", \"Toys\", \"Clothing\", \"Sports\"]\n",
" if extra_product_names:\n",
" cls.product_names.extend(extra_product_names)\n",
" if extra_product_categories:\n",
" cls.product_categories.extend(extra_product_categories)\n",
"\n",
" @classmethod\n",
" def generate_product_name(cls, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random product names.\n",
"\n",
" :param amount: Number of product names to generate.\n",
" :return: List of generated product names.\n",
" \"\"\"\n",
" if not hasattr(cls, 'product_names'):\n",
" cls.__init__()\n",
" return [str(random.choice(cls.product_names)) for _ in range(amount)]\n",
"\n",
" @classmethod\n",
" def generate_product_category(cls, amount: int = 1) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random product categories.\n",
"\n",
" :param amount: Number of product categories to generate.\n",
" :return: List of generated product categories.\n",
" \"\"\"\n",
" if not hasattr(cls, 'product_categories'):\n",
" cls.__init__()\n",
" return [str(random.choice(cls.product_categories)) for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def generate_price(amount: int = 1, min_price: float = 1.0, max_price: float = 100.0) -> list[float]:\n",
" \"\"\"\n",
" Generate a list of random prices within a specified range.\n",
"\n",
" :param amount: Number of prices to generate.\n",
" :param min_price: Minimum price value.\n",
" :param max_price: Maximum price value.\n",
" :return: List of generated prices.\n",
" \"\"\"\n",
" return [round(random.uniform(min_price, max_price), 2) for _ in range(amount)]\n",
"\n",
" class Internet:\n",
" \"\"\"\n",
" A class to generate various types of internet-related fake data.\n",
" \"\"\"\n",
"\n",
" @classmethod\n",
" def __init__(cls, extra_domains: list = None):\n",
" \"\"\"\n",
" Initialize the Internet class with optional extra domains.\n",
"\n",
" :param extra_domains: List of additional domains to include.\n",
" \"\"\"\n",
" cls.domains = [\"example.com\", \"test.com\", \"sample.org\", \"demo.net\", \"fake.com\",\n",
" \"mock.org\", \"example.net\", \"test.org\", \"sample.com\", \"demo.net\"]\n",
" if extra_domains:\n",
" cls.domains.extend(extra_domains)\n",
"\n",
" @staticmethod\n",
" def generate_username(amount: int = 1, size: int = 8,\n",
" charset: str = string.ascii_letters) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random usernames.\n",
"\n",
" :param amount: Number of usernames to generate.\n",
" :param size: Length of each username.\n",
" :param charset: Characters to use for generating the usernames.\n",
" :return: List of generated usernames.\n",
" \"\"\"\n",
" return [''.join(random.choices(charset, k=size)) for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def generate_password(amount: int = 1, size: int = 12,\n",
" charset: str = (string.ascii_letters + string.digits + string.punctuation)) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random passwords.\n",
"\n",
" :param amount: Number of passwords to generate.\n",
" :param size: Length of each password.\n",
" :param charset: Characters to use for generating the passwords.\n",
" :return: List of generated passwords.\n",
" \"\"\"\n",
" return [''.join(random.choices(charset, k=size)) for _ in range(amount)]\n",
"\n",
" @staticmethod\n",
" def generate_ip_address(amount: int = 1, version=4) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random IP addresses.\n",
"\n",
" :param amount: Number of IP addresses to generate.\n",
" :param version: IP version (4 or 6).\n",
" :return: List of generated IP addresses.\n",
" \"\"\"\n",
" ip_addresses = []\n",
" for _ in range(amount):\n",
" if version == 4:\n",
" ip_addresses.append('.'.join(str(random.randint(0, 255)) for _ in range(4)))\n",
" elif version == 6:\n",
" ip_addresses.append(':'.join(''.join(random.choices('0123456789abcdef', k=4)) for _ in range(8)))\n",
" else:\n",
" raise ValueError(\"Invalid IP version. Use 4 or 6.\")\n",
" return ip_addresses\n",
"\n",
" @classmethod\n",
" def generate_url(cls, amount: int = 1, format: str = None) -> list[str]:\n",
" \"\"\"\n",
" Generate a list of random URLs.\n",
"\n",
" Format string can contain {domain} and {path} placeholders.\n",
" Example format: \"https://{domain}/{path}\".\n",
"\n",
" :param amount: Number of URLs to generate.\n",
" :param format: Optional format string for the URLs, using {domain} and {path} as placeholders.\n",
" :return: List of generated URLs.\n",
" \"\"\"\n",
" if not hasattr(cls, 'domains'):\n",
" cls.__init__()\n",
" urls = []\n",
" for _ in range(amount):\n",
" domain = random.choice(cls.domains)\n",
" path = ''.join(random.choices(string.ascii_letters + string.digits, k=10))\n",
" url = f\"https://{domain}/{path}\"\n",
" if format:\n",
" url = format.replace(\"{domain}\", domain).replace(\"{path}\", path)\n",
" urls.append(url)\n",
" return urls\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}