This should work most of the time, except with numbers starting with 091 - they should have two leading zero to be identified properly (0091), fix that corner case, and you should be good to go. And finally, we will insert the DataFrame into MongoDB by calling the insertmany() method. Interanational_IN_number = phonenumbers.format_number(num_obj, ) datadict data.todict('records') db client'company' collection db'employees' collection.insertmany(datadict) After logging into the MongoDB server, we get the database and the collection where the data will be stored. Num_obj = phonenumbers.parse(number, "IN") What is even more useful is that we can create a dataframe of 100 users from different countries. Fake data is often used for testing or filling databases with. We can quickly create a profile with: fake Faker () fake.profile () As we can see, most relevant information about a person is created with ease, even with mail, ssn, username, and website. If you want to ensure that every number has a +91, you need to tweak with factoryboy or wrap fake.phone_number with a parsing function.Ī neat solution could be using a library called phonenumbers like this: import phonenumbers Faker is a Python library that generates fake data. Phone = factory.Faker("phone_number", locale="hi_IN") You could even replace by +3 or more to reduce even more the possibility for an iteration, but then you would loose in performance at the chained replace calls.You can use faker's hindi Indian provider, it will generate most of the numbers with +91, but not all:Ĭode sample with faker and factoryboy.Faker: import factory Note that chaining replace turns out to be faster than sequntially calling it, as per this answer.Īlso, in the above, +1 is replaced by +2 when determining rand_lento reduce the number of iterations needed to achieve the requested length. Rand_len = 3 * (math.ceil((str_len - len(ret)) / 3) + 2) from faker import Factory import pandas as pd import random def createfakestuff(fake): df pd. If you put randomness above performance, you could do something like: def secure_random_alphanumeric(str_len: int) -> str: Since you need a "very quick way to generate an alphanumeric", this function sacrifices performance over security, since the frequencies of a and A (or whatever characters you choose to replace + and / with) will be increased compared to what urandom would give you otherwise. Return base64.b64encode(os.urandom(rand_len), altchars=b'aA').decode('ascii') Generate a random date of birth represented as a Date object, constrained by optional miminimumage and maximumage parameters. You can then read as much as you like from urandom and produce a random alphanummeric as follows: import mathĭef random_alphanumeric(str_len: int) -> str: dateofbirth(tzinfo: Optional datetime.tzinfo None, minimumage: int 0, maximumage: int 115) datetime.date. Today we learn how to professionally generate sample data in Python, using the faker library. Next, let’s create some functions to randomly generate our data for names, def randomnames(nametype, size) : ''' Generate n-length. import numpy as np import pandas as pd from import Provider. We are using NumPy and Faker to randomly generate fake data. It shouldn’t take more than a couple minutes. Depending on the version of Python you have installed, use the appropriate command to install the Faker package. To get started with the Faker library in Python, follow these steps: 1. pip install faker pip3 install faker python -m pip install faker python3 -m pip install faker. You can use this in your applications for testing, data generation, and more. Os.urandom, which reads from urandom, is considered secure (see the relevant answer in a question if urandom is secure). Generating Professional Sample Data with Faker in Python. First, let’s import pandas, NumPy, and some Faker providers. Faker is a useful Python library that helps you generate random fake data, such as names, addresses, and dates. I would prefer urandom over secrets.token_hex, as it samples from a richer character set and hence needs a smaller length to achieve the same entropy. 421 Vanessaside, VT 79393' 'PSC 4115, Box 7815 APO AA 41945' '778 Brown.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |