DEV Community

Cover image for You Faker
AnneMiriam
AnneMiriam

Posted on

You Faker

Here we are at the end of week 12 at Flatiron School. In this phase, p4, my cohort mates and I were introduced to Flask. We're finally working with back-end and tying it into our front-end(React). It has been really exciting, but also very nerve-racking. We are so close to the finish line, only 3 weeks to go, but we also now have to go back and remember that stuff we possibly haven't touched in 5 weeks, because we were so focused on learning Python.

And did I mention Deployment? 🤯 Trying to weed my way through that whole process has begun to add a whole new level of stress. But that's another story.

I have been feeling the imposter syndrome a lot this last week. Getting back into React things looked familiar, but I felt like I didn't know what I was doing. I kinda felt like I was faking it! Which happens to be something Python can do, with the Faker library.

What is Faker?

Faker is a Python library that allows developers to create fake data, such as names, addresses, sentences, and email addresses. It gives you a simple way to produce random data that looks like real information.

Why Would I Want to Use Faker?

It is good for testing or simulating interactions. We all likely encountered it in our phase 4 projects, as we had to create databases that shared connections with other databases in one-to-many and many-to-many relationships. It would have taken a lot of time to manually fill in those databases with information that we would have had to make up anyway. So, in comes Faker.

Let's look at what Faker can do!

Faker can create names:

  • name() = a random full name
  • first_name() = a random first name only
  • last_name() = a random last name only
  • prefix() = a random prefix(Mr., Dr., Ms., etc)

Faker can create locations and addresses:

  • email() = a random email address
  • address() = a random street address
  • city() = a random city name
  • state() = a random state name or abbreviation
  • country() = a random country name
  • url() = a random URL

Faker can create dates and times:

  • date_of_birth() = a random date of birth
  • date() = a random date
  • time() = a random time
  • date_time() = a random date and time

Faker can create writing:

  • text() = a random paragraph(non-sensical)
  • sentence() = a random sentence(non-sensical)
  • word() = a random word

Those are just a sample. Faker can also create phone numbers, credit card information, numbers, and colors, to name a few more.

Lets see how it looks in our code 👇🏻

Faker code snippet

Output from Faker snippet

Those all look reasonably real, if you don't look too close.

Faker's limitations

Faker can randomize locations, but it is limited to predefined locations. So, if you are trying to generate data for a specific region, it may not work.

Also, if I try to look up any of the output from above, like South Cynthia, in Google Maps, the first match for a destination is S. Cynthia St., McAllen, TX. It is not a town in Oklahoma. When I look up the zip code, I found that it belongs New Orleans, LA. Another run of the same code gave me the postal code, 26368, for a city called Kupinik, in Serbia. Not South Adam, MO.
If we look at the phone number, the country code(+1) is the USA, but the area code does not currently exist. 375 is the country code(+375) for Belarus.
*Fun Fact - on a little further digging, according to KMPH Fox 26, the Central Valley will be getting a 375 area code once they run out of 559 numbers.

Where does Faker really struggle?

Like I said above, if you don't look to close, the output looks good. But is there a situation where it's obviously not real?
Yes!
I give you: sentence()!

Faker sentence

confused toddler
That sentence makes as much sense as the look on my face and that outfit(and my sister's outfit, behind me, given that it was Christmas. Ah, the 80's!).

Conclusion

Faker has some good uses and just like anything fake, not so good uses. If your using Faker to just auto-populate some databases that aren't really going to be seen, fake away! But if that data might actually be seen or needs to make sense, your better off manually entering it.

Happy Faking!


Resources:
Tellez, M. 'New area code coming to Central Valley'. Published 21 December 2023. Retrieved on 26 January 2024, from
https://kmph.com/news/local/new-area-code-coming-to-central-valley-357-overlay-geographic-fresno-county-san-juaquin-madera-mariposa-merced-tulare-kings-counties-clovis-porterville-reedley-selma-visalia-coalinga

Das, M. 'Faker: Python is Just a Fake Away!'. Published 9 August 2023. Retrieved on 19 January 2024 from https://medium.com/@HeCanThink/faker-python-is-just-a-fake-away-ef626a0dcf8d

Top comments (0)