This Python code generates random Malaysian names and attributes, creates random NRIC numbers based on the date of birth and gender, and writes the generated data to a file. It also reads configuration options from a text file.
Here are the steps to use the script:
-
Install the required Python libraries - requests, BeautifulSoup, faker pip install requests beautifulsoup4 Faker
-
Download the script and create a configuration file named "config.txt" in the same directory as the script.
-
Define the configuration options in the "config.txt" file. The options include the number of users to generate, the format for the user IDs, whether to replace spaces in names with a specified character, and the character to replace spaces with. The configuration file has a specific format described in the function parse_config().
-
Call the script by running it from the command line: python script.py
-
The script will generate fake personal details including name, gender, and NRIC number for the specified number of users. The script will output the results to the console.
The following Python libraries are used in this code:
- requests
- BeautifulSoup
- Faker
generate_fake_nric This function generates a random fake Malaysian NRIC number based on the date of birth and gender.
Parameters
- dob: date of birth string
- dob_format: format of date of birth string (default: '%Y-%m-%d')
- gender: gender of the person (default: 'M')
Return value
- string representation of the generated NRIC number
parse_config This function reads a configuration file and returns a dictionary of configuration options.
Parameters
- file_path: The path to the configuration file.
Return value
- A dictionary containing the configuration options.
The main code generates random Malaysian names and attributes using a website that generates random names. It then generates random NRIC numbers using the generate_fake_nric function and writes the generated data to a file. It reads configuration options from a text file using the parse_config function.
Configuration file
The configuration file should contain configuration options in the following format:
key: value
If a line starts with "#" or is empty, it will be ignored.
If a line starts with "default_", it will be treated as a default value. Default values are stored in a separate dictionary and are used when a key is not found in the main dictionary.
Configuration options
- num_users: the number of users to generate
- replace_spaces: whether to replace spaces in names with a specific character (default: False)
- space_char: the character to use when replacing spaces in names (default: '-')
- id_format: the format to use for the user IDs (default: 'USER{:03d}')
This code was tested with Python version 3.9.7. The script uses web scraping techniques to generate fake personal details, so it may be affected by changes to the website structure. The script should not be used for any illegal or unethical purposes.