avatarJustin Davis

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1310

Abstract

mber">1</span>,<span class="hljs-number">1</span>) profile = { "name": fake.<span class="hljs-built_in">name</span>(), <span class="hljs-string">"birth_dt"</span> : fake.<span class="hljs-built_in">date_between</span>(start_date, end_date), <span class="hljs-string">"zip_cd"</span> : fake.<span class="hljs-built_in">zipcode</span>(), <span class="hljs-string">"city"</span> : fake.<span class="hljs-built_in">city</span>(), <span class="hljs-string">"state"</span> : fake.<span class="hljs-built_in">state</span>(), } return profile</pre></div><div id="f096"><pre><span class="hljs-attr">num_profiles</span> = <span class="hljs-number">1000</span> <span class="hljs-attr">fake</span> = Faker() <span class="hljs-attr">profiles</span> = [generate_profile(fake) for i in range(num_profiles)]</pre></div><div id="31a5"><pre><span class="hljs-function"><span class="hljs-title">print</span><span class="hljs-params">(profiles[<span class="hljs-number">0</span>])</span></span> {<span class="hljs-string">'name'</span>: <span class="hljs-string">'Allison Matthews'</span>, <span class="hljs-string">'birth_dt'</span>: datetime<span class="hljs-selector-class">.date</span>(<span class="hljs-number">2009</span>, <span class="hljs-number">3</span>, <span class="

Options

hljs-number">27</span>), <span class="hljs-string">'zip_cd'</span>: <span class="hljs-string">'73022'</span>, <span class="hljs-string">'city'</span>: <span class="hljs-string">'New Jeremyfurt'</span>, <span class="hljs-string">'state'</span>: <span class="hljs-string">'Washington'</span>}</pre></div><p id="1a2e">This approach to create profile is scalable, runs in a second, and is easy to modify. Say your project expectations change and you want to add occupation to the above dataset. There is built in functionality in faker to add a fake job:</p><div id="2339"><pre>fake.<span class="hljs-keyword">job</span>()</pre></div><p id="039c">If you are looking for additional options, they are available in the Faker documentation</p><p id="6f11"><a href="https://faker.readthedocs.io/en/master/">https://faker.readthedocs.io/en/master/</a></p><p id="e85f">Save the data</p><p id="eebb">Once you have generated the fake data, you can write it out as a json for future use:</p><div id="cc68"><pre><span class="hljs-keyword">import</span> json <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">'data/fake_data.json'</span>, <span class="hljs-string">'w'</span>) <span class="hljs-keyword">as</span> f: json.dump(profiles, f)</pre></div></article></body>

Fake it till you make it — Making fake data in Python with Faker

Don’t let missing data stop you from getting started on a project. Faker is python package that makes generating fake data eaasy as ever. The package is flexible, easy to use, requires minimal start up time, and has a wide range of options to choose from when it comes to creating fake data fields.

Say you are creating an app and need user information. The data you want is not yet available, so you want to generate fake profiles for over 1000 users. Generating this fake data is as easy as the following:

from faker import Faker
import datetime
def generate_profile(fake: Faker):
    start_date = datetime.date(2000, 1,1)
    end_date = datetime.date(2015, 1,1)
    profile = {
        "name": fake.name(),
        "birth_dt" : fake.date_between(start_date, end_date),
        "zip_cd" : fake.zipcode(),
        "city" : fake.city(),
        "state" : fake.state(),
    }
    return profile
num_profiles = 1000
fake = Faker()
profiles = [generate_profile(fake) for i in range(num_profiles)]
print(profiles[0])
{'name': 'Allison Matthews', 'birth_dt': datetime.date(2009, 3, 27), 'zip_cd': '73022', 'city': 'New Jeremyfurt', 'state': 'Washington'}

This approach to create profile is scalable, runs in a second, and is easy to modify. Say your project expectations change and you want to add occupation to the above dataset. There is built in functionality in faker to add a fake job:

fake.job()

If you are looking for additional options, they are available in the Faker documentation

https://faker.readthedocs.io/en/master/

Save the data

Once you have generated the fake data, you can write it out as a json for future use:

import json
with open('data/fake_data.json', 'w') as f:
    json.dump(profiles, f)
Python
Fake Data
Recommended from ReadMedium