crs4 / hl7apy

Python library to parse, create and handle HL7 v2 messages.
http://crs4.github.io/hl7apy/
MIT License
224 stars 88 forks source link

Zero padded ID in PID section is being unpadded #46

Closed Hezko closed 5 years ago

Hezko commented 5 years ago

Hi, I started to use hl7apy for creating hl7 messages, and encountered an issue: I wanted to send a message with the following pid section: "PID|001234567" (001234567 is an example id of course[please notice the zeros in it]) so I tried to create the hl7 message in the following way:

from hl7apy.parser import Message
message = Message("ADT_A01", version="2.5")
message.pid = f'PID|001234567'

the message is created successfully, but when I run: message.value I get the following output:

'MSH|^~\&|||||20190526130053|||||2.5\rPID|1234567'

As you can see, the id I inserted originally, was '001234567', but the actual id in the message was '1234567', which is invalid in my case. for now the workaround I found is putting a placeholder in the pid section and then running a str.replace to replace the placeholder with the actual zero padded output. I was curious if there is a specific reason for this behavior? can it be fixed? is there a better workaround for me to use?

svituz commented 5 years ago

Hi Hezko, thank you, I'll have a look at it. It is certainly e high priority issue.

svituz commented 5 years ago

Hi Hezko, I investigated the problem. The thing is that the PID_1 is a Field of type SI which is numerical data type. It means that hl7apy considers it as a number so it unpads the value, which I think is the correct behaviour. As a matter of fact, the HL7 definition of PID_1 is:

This field contains the number that identifies this transaction. For the first occurrence of the segment, the sequence number shall be one, for the second occurrence, the sequence number shall be two, etc.

Are you sure you are setting the right value for the PID_1 and that you don't want to assign that value to PID_3, which is the patient identifier? That said, I don't want to interfere with your integration scenario, maybe that's what you need. So, if you need to set a value like that to PID_1 in hl7apy, you need to change the PID_1 datatype. You must do it in TOLERANT validation mode, which is the default. Here is the correct snippet

from hl7apy.core import Message
message = Message("ADT_A01", version="2.5")
message.pid = 'PID||'  # notice I'm not assigning any value to PID_1 here. If you do, you cannot change the datatype
message.pid.pid_1.datatype = 'ST'  # ST is the datatype for alphanumeric values
message.pid.pid_1.value = '00012345'
print(message.to_er7())

In this way, you cannot validate the message since you are using a different structure than the official one. Hope that helps. Vittorio

Hezko commented 5 years ago

It is indeed very helpful! Thanks! I indeed wasn't able to use pid_3 field due to the specific integration scenario, so I will use your great code snippet and change pid_1 to type 'ST'. Thanks again!