this code send generates a random json file of user ids provided and btw range is also given..
so this code outputs 50 jsons for each user.
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint
ids= ('5cda','6cda')
fake = Faker('en_US')
for ind in ids:
cont = []
#Overall dictionary with first and user_ids
dct = {}
for idx in range(50):
sms = {
"id":"AB-asfgw",
"body": fake.text(),
"mime": fake.ean(),
"hashed": fake.ean(),
"pid": fake.ean(),
"user_id": ind,
"text": fake.sentence()
}
cont.append(sms)
dct['messages'] = cont
dct['user_id'] = ind
#print(dct)
f_name = '{}.json'.format(ind)
with open(f_name, 'w') as fp:
#Save the dictionary
json.dump(dct, fp, indent=4)
print('saved {}'.format(f_name))
auth = "authorization: token 1324"
file = "5cda.json"
fd=open("5cda.json")
json_content = fd.read()
fd.close()
subprocess.run(["grpcurl", "-plaintext","-H", auth,"-d",json_content,"-format","json","100.20.20.1:5000","api.Service/Method"])
this loop.py code loops the first code 20 times
from datetime import datetime
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint
import subprocess
import sys
for i in range(20):
subprocess.call(['python','grploop1.py'])
i need to save the output of loop.py code for each loop. and store that json. example : we are looping the first code for 20 times in loop.py so ineed the output stored should be like 5cda1.json ........ 5cda20.json and 6cda1.json..... 6cda20.json
here we are giving two user ids ids= ('5cda','6cda')
so output would be total 40 json files.
You would want to bring the file saving logic inside the for ind in ids:
loops, and use the index in the overall loop to save the file with different names.
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint
ids= ('5cda','7f36')
fake = Faker('en_US')
msg_list = []
#Overall loop to run inner loop 20 times
for i in range(20):
#Loop to generate messages
for ind in ids:
cont = []
#Overall dictionary with first and user_ids
dct = {}
for idx in range(20):
sms = {
"id":"AB-Iasd",
"body": fake.sentence(),
"reae": fake.ean(),
"ashe": fake.ean(),
"id2": fake.ean(),
"user_id": ind,
"pid": fake.sentence()
}
cont.append(sms)
#Use a dictionary to save cont list to first key, and ind to user_ids key
dct['messages'] = cont
dct['user_id'] = ind
msg_list.append(dct)
#Append the index to the file here, and save the file
f_name = '{}{}.json'.format(ind,i+1)
with open(f_name, 'w') as fp:
# Save the dictionary
json.dump(dct, fp, indent=4)
print('saved {}'.format(f_name))
You will get 40 files as per your requirements, with unique content in all files.
You can save the output of called python file to a file if you write the J-son into STDOUT and in loop.py redirect the STDOUT to a file. In you "generator" file: (Of course, you should remove other prints if you don't want to get invalid J-son file.)
...
dct['messages'] = cont
dct['user_id'] = ind
print(dct)
...
I your loop.py file:
subprocess.call(["ardbuf", "-oL", "python","grploop1.py", ">", test_folder/tmp_file.json])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.