Outils pour utilisateurs

Outils du site


issue200:python

Ceci est une ancienne révision du document !


Greetings again, fellow Beings. First, I have to say how happy I am to be part of the 200th issue of Full Circle Magazine. I’ve been a part of the FCM family since issue 27 and I am still proud of it. Here’s to another 100 issues! How many times have you had a great idea for a program, but before you can really get it together, you found that you need some specialized data to actually get it properly programmed and tested? Data like user information, database records, sales data and so on? Sure, many of our programs and utilities can be tested with hand created data and that’s good enough. But sometimes you need a data table with hundreds or thousands of records that have 10 or more fields of realistic data that would take days to create by hand. Enter Faker, a library for Python that does most of the ‘heavy lifting; for you. Of course, you need to install Faker into Python and as usual, we can use pip to do this. $ pip3 install Faker Now that you’ve gotten it installed, let’s look at how it can help you.

First, we need to import the library and then create an instance of Faker. »> from faker import Faker »> fake = Faker() Now we can use the ‘fake’ instance to get some data. Let’s say we want to get a fake name for a fictitious user. »> fake.name() 'Ruth Duarte' How about an address for our new user Ruth? »> fake.address() '19373 Amanda Manors Suite 530\nWest William, MD 04391' Of course, Ruth will need a phone number and an email address, right? »> fake.phone_number() '+1-888-264-9990×8524' »> fake.ascii_email() 'kristin03@yahoo.com' And Ruth will need a credit card. »> fake.credit_card_full() 'VISA 16 digit\nKevin Smith\n4731880480844912 04/29\nCVC: 849\n'

Let’s not forget that Ruth will probably need to have a car, so we’ll need to give her a license plate. »> fake.license_plate() '780 VZD' That’s fantastic, right? So far, all of this information has been for the U.S. What if you need to have data for the UK? No problem. Just create an instance of Faker using the localization attribute. »> fakeUK=Faker('en_GB') »> fakeUK.phone_number() '+44121 4960479' »> fakeUK.license_plate() 'NL09VVR' What about people who need data for, let’s say, Norway? »> fakeNorway=Faker('no_NO') »> fakeNorway.address() 'Sørensenskrenten 3, 6580 Andersstrøm' »> fakeNorway.address() 'Evensengjerdet 76, 3451 Torillø' »> fakeNorway.phone_number() '94199271'

Remember, none of this data is meant to be any more realistic for anything outside of creating dummy data for testing your programs. However, if you are an author, who needs a number of new character names for your newest cozy mystery novel. Let’s say we need 15 people. »> for cntr in range(15): … personname=fake.name() … print(personname) Heidi Lucas Catherine Davis Andrea Williams Sierra Carpenter PhD Tammy Berg Diane Johnson Brandi Brown Jeffrey Flores Kevin Roy Jennifer Leonard Brian Mason Joshua Herrera Eric Mitchell Kelly Park Joseph Harris »> We can also have Faker generate enums, booleans, dictionaries, floats, ints, lists and more by using the python provider. For example, say we need some data that looks and acts like US Currency values. That’s really simple. »> fake.pyfloat(right_digits=2,min_value=200,max_value=10000) 722.58

The pyfloat method has the following syntax… pyfloat(left_digits=None, right_digits=None, positive=False, min_value=None, max_value=None) You can check the documentation for more detailed information on the python provider as well as all the “standard” providers as well as specialized providers provided by the Faker community. So far, everything we’ve done has been in the Python terminal. But reality is that we will need to write a program to do some heavier data faking. Let’s assume we have to create a user structure that has the following fields… User ID, User Name, User Email, User password, User phone number, Available balance, frozen balance and a status flag. In addition, we need to create, say 30 of these user data groups to test the program we are writing. Of course, we need to import Faker and a couple of things… from faker import Faker import pprint import random

Now, let’s create a function that will create our user data for us and return it as a dictionary (top right). Now that we have all the data we need for this simple exercise, we’ll create a dictionary (right) and return it. So, here (top right) is what the data looks like. I’ll only show a couple of the user dictionary data sets instead of the 30 that we created. You can find the Faker distribution homepage at https://github.com/joke2k/faker and the very nice documentation at https://faker.readthedocs.io/en/master/ That’s it for this month. I hope your December has been nice and happy and your New Year will bring happiness and prosperity. May the best day of your 2023 be the worst day of your 2024! Until next time, as always; stay safe, healthy, positive and creative!

issue200/python.1704008921.txt.gz · Dernière modification : 2023/12/31 08:48 de d52fr