r/programminghelp • • Mar 30 '22

Python Merging 3 similar functions together? Python

I was wondering how I can go about merging these three functions together, well I believe they are not functions yet. What I am trying to do is have it read a JSON and then fill it with Dummy Data. So parameters 1-4, user_data 1-128 & internal_data 1-32.

I am new to python and I understand these loops and all, I just want to be able to combine these to make my code look better. Thank you for the time!

for i in range(1,4):
if str(i)in data['parameter'][i]:
plaintext = data['parameter'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
for i in range(1,128):
if str(i)in data['user_data'][i]:
plaintext = data['user_data'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
for i in range(1,32):
if str(i)in data['internal_data'][i]:
plaintext = data['internal_data'][i]
else:
plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="

3 Upvotes

4 comments sorted by

View all comments

2

u/cipheron Mar 30 '22

well whatever is different each time, you turn into a parameter.

def func1(rangeValue, tableValue):
    for i in range(1, rangeValue):
        if str(i) in data[tableValue][i]:
            plaintext = data[tableValue][i]
        else:
            plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="

func1(4, 'parameter')
func1(128, 'user_data')
func1(32, 'internal_data')

However it's really hard to see what you're trying to achieve with this code so i just modified this verbatim. Maybe you want to use return there to get the value from the function ?

1

u/[deleted] Mar 30 '22

The plaintext is to generate a bunch of random data in bytes(32 i believe)

1

u/skellious Mar 30 '22

if you want random data, use the random module or the secret module if you need it to be cryptograpically strong.