If I understand correctly, the problem is that you want to generate different data every time. To do that, you need to:
Currently the
LoginDataattributes are generated when the class is defined. If you want them to be different for each instance, make them instance attributes and set them in the__init__()method.You need to create a new
LoginDatainstance in every iteration of the for loop ininput_data().
Code:
from faker import Faker
import random
class LoginData:
def __init__(self):
fake = Faker()
self.password = "password@123"
self.email = fake.email()
self.username = fake.first_name()
self.first_name = fake.first_name()
self.last_name = fake.last_name()
self.phone = random.randint(9000000000, 9999999999)
self.city = fake.city()
self.about = "This is a sample text : about"
def get_json(self):
p = {
'password': self.password,
'email': self.email,
'username': self.first_name,
'first_name': self.first_name,
'last_name': self.last_name,
'phone': self.phone,
'city': self.city,
'about': self.about
}
return p
def input_data(x):
for i in range(0, x):
logindata = LoginData()
print(logindata.get_json())
def main():
no_of_input = 5
input_data(no_of_input)
main()
Note that get_json() still returns a Python dict, not JSON. For JSON, you can use the json module in the standard library:
import json
and in get_json()
return json.dumps(p)
Answer from Maximouse on Stack Overflow
» pip install jsondatafaker
Automatic fake JSON data creation from schema
python - generating millions of json data - Stack Overflow
Newest 'json-schema-faker' Questions - Stack Overflow
Generate realistic dummy data in CSV and JSON format
Videos
https://github.com/ghandic/jsf
Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.
Main Features
Provides out of the box data generation from any JSON schema 📦
Extendable custom data providers using any lambda functions 🔗
Multi level state for dependant data (eg multiple objects sharing value, such as children with same surname) 🤓
Inbuilt validation of fake JSON produced ✅
In memory conversion from JSON Schema to Pydantic Models with generated examples 🤯
Seamless integration with FastAPI 🚀
Installation
$ pip install jsf ---> 100%
Usage
Basic 😊
from jsf import JSF
faker = JSF(
{
"type": "object",
"properties": {
"name": {"type": "string", "$provider": "faker.name"},
"email": {"type": "string", "$provider": "faker.email"},
},
"required": ["name", "email"],
}
)
fake_json = faker.generate()Results in ...
{
'name': 'Jesse Phillips',
'email': 'xroberson@hotmail.com'
}From JSON file 📁
from jsf import JSF
faker = JSF.from_json("demo-schema.json")
fake_json = faker.generate()<details markdown="1"> <summary>Or run stright from the <code>commandline</code>...</summary>
Native install
jsf --schema src/tests/data/custom.json --instance wow.json
Docker
docker run -v $PWD:/data challisa/jsf jsf --schema /data/custom.json --instance /data/example.json
</details>
FastAPI Integration 🚀
Create a file main.py with:
from jsf import JSF
from fastapi import FastAPI
app = FastAPI(docs_url="/")
generator = JSF.from_json("custom.json")
@app.get("/generate", response_model=generator.pydantic())
def read_root():
return generator.generate()Run the server with:
<div class="termy">
$ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [28720] INFO: Started server process [28722] INFO: Waiting for application startup. INFO: Application startup complete.
Navigate to http://127.0.0.1:8000 and check out your endpoint. Notice the following are all automatically created:
Schema with descriptions and examples
Example response
Data generation by clicking "try it out"
» pip install faker-cli
You do not need to use OrderedDict: JSON format may not (and will not) save order of items. Even if order will be saved in the file - it will breaks when another project will parse that file.
You just need to use dict. Also it will be much more faster.
To save the order of items you should explicitly preserve the index of an each element. Like this:
from json import dumps
from faker import Faker
import collections
import json
def fake_person_generator(length, fake):
for x in range(length): # xrange in Python 2.7
yield {'last_name': fake.last_name(),
'first_name': fake.first_name(),
'street_address': fake.street_address(),
'email': fake.email(),
'index': x}
database = []
filename = '1M'
length = 1000000
fake = Faker() # <--- Forgot this
fpg = fake_person_generator(length, fake)
with open('%s.json' % filename, 'w') as output:
output.write('[') # to made json file valid according to JSON format
for person in fpg:
json.dump(person, output)
output.write(']') # to made json file valid according to JSON format
print "Done."
You go out-of-memory because you first generate the whole database first, and then dump the database. A more memory-friendy way would be to generate the dict entries on the fly. A better way would be to use a generator which makes the entries on the fly.
def fake_person_generator(length):
for x in range(length): # xrange in Python 2.7
yield OrderedDict([
('last_name', 'lastname_%i' % x),
('first_name', 'firstname_%i' % x),
('street_address', 'adress_%i' % x),
('email', 'email_%i' % x)])
Combined with Alex Hall's answer this should reduce the memory need dramatically.
I don't know the json-module so well, but the writing would be something like:
length = 1000000
fpg = fake_person_generator(length)
with open('%s.json' % filename, 'w') as output:
for person in fpg:
json.dump(person, output)
print "Done."