mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-27 14:01:09 +00:00
Added Random Names Generator
This commit is contained in:
parent
28f91e75f5
commit
a04df11006
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
35
Random_Names_Generator/README.md
Normal file
35
Random_Names_Generator/README.md
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
# Random Names Generator
|
||||||
|
|
||||||
|
## A simple python script to generate some random names.
|
||||||
|
|
||||||
|
### There are two csv files for 'First Name' and 'Last Name' in text 'CSV_Database_Of_First_And_Last_Names' taken from [here](http://www.quietaffiliate.com/free-first-name-and-last-name-databases-csv-and-sql/)
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
### Step 1
|
||||||
|
Fork this Repo and Clone it to your local machine.
|
||||||
|
```sh
|
||||||
|
$ git clone https://github.com/YOUR_USERNAME/Random_Names_Generator.git
|
||||||
|
$ cd Random_Names_Generator
|
||||||
|
```
|
||||||
|
### Step 2
|
||||||
|
To run the python3 program
|
||||||
|
```sh
|
||||||
|
$ python3 Random_Name_Generator.py
|
||||||
|
```
|
||||||
|
The above only returns only one name.
|
||||||
|
|
||||||
|
|
||||||
|
Add number of names if more than one name is required.
|
||||||
|
```sh
|
||||||
|
$ python3 Random_Name_Generator.py 5
|
||||||
|
```
|
||||||
|
---
|
||||||
|
Tip:-
|
||||||
|
In Linux Machines you can save the files as text using output redirection
|
||||||
|
```sh
|
||||||
|
$ python3 Random_Name_Generator.py 10 >> Random_Names_Generator.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
36
Random_Names_Generator/Random_Name_Generator.py
Normal file
36
Random_Names_Generator/Random_Name_Generator.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
from pathlib import Path
|
||||||
|
import argparse
|
||||||
|
import random
|
||||||
|
|
||||||
|
""" Generate a random line from a file without reading the entire file
|
||||||
|
Inspired from: https://stackoverflow.com/a/35579149 """
|
||||||
|
def random_line(file_path):
|
||||||
|
line_num = 0
|
||||||
|
selected_line = ''
|
||||||
|
with file_path.open(mode="r") as fp:
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line: break
|
||||||
|
line_num += 1
|
||||||
|
if random.uniform(0, line_num) < 1:
|
||||||
|
selected_line = line
|
||||||
|
return selected_line.strip()
|
||||||
|
|
||||||
|
""" Output a given number of random names """
|
||||||
|
def random_names(number_of_names = 1):
|
||||||
|
first_names_file = Path("CSV_Database_Of_First_And_Last_Names/CSV_Database_of_First_Names.csv")
|
||||||
|
last_names_file = Path("CSV_Database_Of_First_And_Last_Names/CSV_Database_of_Last_Names.csv")
|
||||||
|
if first_names_file.is_file() and last_names_file.is_file(): # Check if file exists
|
||||||
|
for i in range(number_of_names):
|
||||||
|
random_first_name = random_line(first_names_file)
|
||||||
|
random_last_name = random_line(last_names_file)
|
||||||
|
print(f'{random_first_name} {random_last_name}')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Accept command line argument for number of names required where default is 1
|
||||||
|
parser = argparse.ArgumentParser(description='Generate Random Names')
|
||||||
|
parser.add_argument('num', nargs='?', type=int, default=1)
|
||||||
|
args = parser.parse_args()
|
||||||
|
random_names(args.num)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user