Randomizing outputs to file

59 views Asked by At

I am generating data for testing, using Python.

I have the whole process pretty much working as it should, however, I have this piece of code.

def get_lines():

    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information 1"
    line5 = "\nUser Information 1"
    line6 = "\nUser Information 1"
    line7 = "\nDocument Footer - Once per document"

    return line1 + line2 + line3 + line4 + line5 + line6 + line7

What i would like to be able to do is populate line4,5,6 with user information 2,3,4 something like this:

line1 = "Document Header - Once per document"
line2 = "\nDocument Information - Once per document"
line3 = "\nDocument Information 2 - Once per document"   
line4 = "\nUser information 1"
line5 = "\nUser Information 1"
line6 = "\nUser Information 1"  
line4 = "\nUser information 2"
line5 = "\nUser Information 2"
line6 = "\nUser Information 2"
line4 = "\nUser information 3"
line5 = "\nUser Information 3"
line6 = "\nUser Information 3"
line7 = "\nDocument Footer - Once per document"

But have it randomized, i.e say i want 10 files, some will contain one piece of user information some 2 some 3 etc etc...

I am struggling to find a consistent way to produce what i need.

Thank you.

EDIT: Added Sample Message: ORC OBR and OBX are all linked by UID's

MSH|^~\&||||||||201705301105||ORM^O01|4960855009|P|2.5||NE|AL||||
PID|1||^^^^HOSPITALNO~^^^^NHSNO||Hendry^John||190203130000|F|||||||||||||| 
PV1|1||G2D|||||||||||||||||||||||||||||||||||||||||||||||| 
ORC|NW|2017053019783377||19783377|||1^^^201705304500^^R||^^^201705
OBR|1|2017053019783377||1019|||2017053011045|201705301045||Test001||||||||||
OBX|1|ST|2017053019783377||2017053019783377|||||||||||||||
SPM|1|||||||||||||||||||||||||||||
2

There are 2 answers

1
Sanju On

Edit

I now see you have provided sample data , but I am not sure if that is the desired output you are expecting from the get_lines method or is that the input you are going to consume to produce the desired output from get_lines ?


Just pass a variable with the userid you want to print. you can also use random.choice to randomly select a value from the list or generate and pass random integers in a range with random.randint

def get_lines(userid):

    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information {}".format(userid)
    line5 = "\nUser Information {}".format(userid)
    line6 = "\nUser Information {}".format(userid)
    line7 = "\nDocument Footer - Once per document"

    return line1 + line2 + line3 + line4 + line5 + line6 + line7

userids = [1, 2, 3, ,4 ,5, 6, 7, 8, 9]

0
youDaily On

You can like this code.

def get_lines():
    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information 1"
    line5 = "\nUser Information 1"
    line6 = "\nUser Information 1"
    line7 = "\nDocument Footer - Once per document"

    return setLine(line1, 1) + setLine(line2, 1) + setLine(line3, 1) + setLine(line4, 3)+ setLine(line4, 3, "1", "2")


def setLine(content, iNum = 1, oldStr="", newStr=""):
    strStr = ""
    for ii in range(0, iNum):
    strStr += content.replace(oldStr, newStr)
    return strStr

print(get_lines())

The sample code output is:

Document Header - Once per document
Document Information - Once per document
Document Information 2 - Once per document
User information 1
User information 1
User information 1
User information 2
User information 2
User information 2