- censusname 0.2.2
- Basic use
- Methods and Objects
- generate
- Censusname
- Censusname.generate
- Advanced
- Example: Middle Names
- Formatters
- names 0.3.0
- Usage
- Command Line Usage
- Python Package Usage
- License
- Changes
- 0.3.0 (2013-05-14)
- 0.2 (2013-02-17)
- Contributing
- Hacking and Pull Requests
- Running the tests
- How to generate random names (first and last names) with python ?
- Install the module names
- Install the module names with anaconda
- Create random names with python
- Create random male names
- Create random female names
- Create random male first names
- Create random female first names
- Create random last names
- References
- Benjamin
- Random name generator in Python
- It’s really simple
- Examples
- Write Pythonic code
- Favorite Posts
censusname 0.2.2
Generate random names based on US Census data, or files that you provide.
Basic use
The simplest way to use censusname is with the generate method. It generates a names based on last and first name distributions in the 2000 Census. It has a 50/50 chance of providing a first name from either the female or male lists.
Or, on the command line, run:
The simplest way to customize Censusname is with the name_format argument It takes a string with two formatting keys: ‘given’ and ‘surname’ (The format should look familiar from Python’s str.format builtin).
Methods and Objects
generate
Generates random names. See below for details on valid arguments.
Censusname
The generate method is called on a default instance of the Censusname object. Censusname is the meat of the module, and instances a can be created with custom formatting and custom lists of names.
Keyword arguments: nameformat , namefiles , max_frequencies , formatters , capitalize .
Censusname.generate
Each part of the name is also a keyword argument. The default data set includes given name files broken up into male and female names. The module can be told to always use a certain file:
Since the argument to nameformat is passed to Str.format, one can use any string formatting options, like padding:
The default dataset in censusname gives all names totally capitalized, and censusname changes them to title case. This can be turned off with a the capitalize argument, which works for both Censusname and Censusname.generate :
Yes, it’s a bit strange for capitalize=False to result in uppercase names. The false omits str.capitalize, so the default capitalization from the raw data shines through, which happens to be all uppercase. You can customize the module arbitrary reformatting methods. Read on!
Advanced
You can pass your own names file to Censusname to generate names with arbitary formatting. For each section of a name, a different sets of files can be used. This could be useful if you have name data broken down by time, geography, or any other variable. By default, male and female first name data from 1990 are combined with last name data from 2000.
Files must have two fields: name and cumulative_frequency . By default, the package expects comma-delimited files, buy you can pass in csv.DictReader arguments with the paramenter csv_args .
The cumulative_frequency field should be calculated based on ascending frequency, and should be a number between from 0 to and some maximum — see the discussion of max_frequencies below.
By default, the name generator looks at separate lists of male and female names. You can specify lists for arbitrary groupings of names. Let’s say you have name distribution data for two provinces in Spain. In Spain, two surnames are used: the paternal and maternal, so you have four files total for surnames, as well as general files for male and female first names.
The US Census names files don’t contain every name, only those that cover about 90% of the population. With that in mind, random_name can take a max_frequencies argument to give these maximums. We specify these maximum with a dictionary whose keys are the file names. If you give custom files but no max_frequencies , 100 will be used. (The max frequencies are hard coded for the default files.)
Also, we want to use a standard conjuction in the name:
Generating names with these examples:
Example: Middle Names
Use the built-in data to fake middle names by randomly picking either a first or last name:
Formatters
You can specify arbitary reformatting methods that are run on each part of the name before they are returned. By default, the package includes a surname formatter that tries to intelligently format raw names like OHALLORAN (to O’Halloran).
You can specify formatters with a dict that targets each part of a name. The formatters should be a list of methods.
Additional formatters can be added to Censusname.generate , they will be run in addition to any formatters included in the object.
Note that passing a formatters argument to censusname will exclude the default surname formatter. It’s easy enough to keep it, though:
names 0.3.0
The script is available on PyPI. To install with pip:
Usage
Names can be used as a command line utility or imported as a Python package.
Command Line Usage
To use the script from the command line:
Python Package Usage
Here are examples of all current features:
License
This project is released under an MIT License.
Data in the following files are public domain (derived from 1990 Census data):
Changes
0.3.0 (2013-05-14)
0.2 (2013-02-17)
Contributing
Please file bugs to the Github issue tracker. Pull requests are welcome.
Hacking and Pull Requests
Please try to conform to PEP8 for code contributions and ensure that the tests continue to function.
Please include new tests with your pull requests when appropriate.
Running the tests
You will need tox and coverage installed to run the tests on your code:
To run the tests and generate a coverage report:
The coverage output should look similar to this:
_____________________ summary _____________________ py27: commands succeeded py32: commands succeeded py33: commands succeeded pypy: commands succeeded flake8: commands succeeded congratulations :) Name Stmts Miss Branch BrMiss Cover -------------------------------------------------- names/__init__ 25 0 8 0 100% names/main 4 0 0 0 100% -------------------------------------------------- TOTAL 29 0 8 0 100%
How to generate random names (first and last names) with python ?
In python, I used to enter random names manually (like John Doe Or Jana Doe) to create for example fake database and for testing. However to generate random names a better solution is to use for example the module «names» created by Trey Hunner:
Install the module names
To install names with pip
Install the module names with anaconda
If you work with anaconda just do:
conda create -n my_env
source activate my_env
conda install pip
pip install names
Create random names with python
Then to create random names just do:
import names
for i in range(10):
print(names.get_full_name())
April Reiter
Emory Miller
David Ballin
Alice Trotter
Virginia Rios
Thomas Wheeler
James Harrell
Nicolas White
Mary Flanagan
Velda Grubb
Create random male names
for i in range(10):
rand_name = names.get_full_name(gender='male')
print(rand_name)
Arthur Manning
Victor Ishee
Roland Chambless
Fred Shawler
Nicholas North
James Michaud
Mitchell Dorsey
Willie Porras
Antonio Green
Joe Sherman
Create random female names
for i in range(10):
rand_name = names.get_full_name(gender='female')
print(rand_name)
Lorri Boles
Carin Hodge
Judith Mcdaniel
Elaine Jones
Terri Tanguay
Caroline Crowley
Edith Jones
Katlyn Bellamy
Jeannie Mayberry
Marge Swaim
Create random male first names
for i in range(10):
rand_name = names.get_first_name(gender='male')
print(rand_name)
George
Shawn
Robert
Steven
William
James
Christopher
James
Michael
Donovan
Create random female first names
for i in range(10):
rand_name = names.get_first_name(gender='female')
print(rand_name)
Jessica
Stephanie
Ann
Emma
Heather
Anna
Kelli
Pauline
Tanya
Kathy
Create random last names
for i in range(10):
rand_name = names.get_last_name()
print(rand_name)
Sanders
Meyer
Ingram
Straight
Caldwell
Cox
Hudson
Basso
Millhouse
Rivett
References
Benjamin
Greetings, I am Ben! I completed my PhD in Atmospheric Science from the University of Lille, France. Subsequently, for 12 years I was employed at NASA as a Research Scientist focusing on Earth remote sensing. Presently, I work with NOAA concentrating on satellite-based Active Fire detection. Python, Machine Learning and Open Science are special areas of interest to me.
Skills
Random name generator in Python
I’ve used multiple websites to generate random names for my test data when running manual or automated QA tests.
Since discovering DuckDuckGo’s random word generation, I’ve hoped that someone would make a DuckDuckGo plugin to generate random names also.
I didn’t want to write my own DuckDuckGo plugin yet so I made a Python-powered command line tool instead using 1990 Census data.
The program is called names and can be found on Github and PyPI.
It’s really simple
It’s basically just one file currently that’s about 40 lines long. There is only one feature available from the command line currently: generate a single random full name. There’s a few more features if importing as a Python package: generate random last name or generate random first name (with or without specifying gender), generate random full name (also without or without gender).
The random name picker relies on the cumulative frequencies listed in the included Census data files. Here’s the steps that are taken: 1. A random floating point number is chosen between 0.0 and 90.0 2. Name file lines are iterated through until a cumulative frequency is found that is less than the randomly generated number 3. The name on that line is chosen and returned (or printed out)
Examples
Here’s how you use it from the command line:
Here’s how you use it as a Python package:
>>> import names >>> names.get_full_name() u'Patricia Halford' >>> names.get_full_name(gender='male') u'Patrick Keating' >>> names.get_first_name() 'Bernard' >>> names.get_first_name(gender='female') 'Christina' >>> names.get_last_name() 'Szczepanek'
Posted by Trey Hunner Feb 17 th , 2013 12:00 am python
Hi! My name is Trey Hunner.
I help Python teams write better Python code through Python team training.
I also help individuals level-up their Python skills with weekly Python skill-building.
Write Pythonic code
The best way to improve your skills is to write more code, but it’s time consuming to figure out what code to write. I’ve made a Python skill-building service to help solve this problem.
Each week you’ll get an exercise that’ll help you dive deeper into Python and carefully reflect on your own coding style. The first 3 exercises are free.
Sign up below for three free exercises!
Favorite Posts
Copyright © 2023 — Trey Hunner — Powered by Octopress
Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.
You’re nearly signed up. You just need to check your email and click the link there to set your password.
Right after you’ve set your password you’ll receive your first Python Morsels exercise.