Table Of Contents

Previous topic

python-hl7 - Easy HL7 v2.x Parsing

Next topic

mllp_send - MLLP network client

This Page

python-hl7 API

hl7.parse(line)

Returns a instance of the hl7.Message that allows indexed access to the data elements.

Note

HL7 usually contains only ASCII, but can use other character sets (HL7 Standards Document, Section 1.7.1). Therefore, python-hl7 works on Python unicode strings. hl7.parse() will accept ASCII-only strings and automatically convert them into unicode. However, if the message contains non-ASCII characters, it is the responsibility of the caller of hl7.parse() to properly convert the message string to unicode first.

>>> h = hl7.parse(message)
Return type:hl7.Message
hl7.ishl7(line)

Determines whether a line looks like an HL7 message. This method only does a cursory check and does not fully validate the message.

Return type:bool

Data Types

class hl7.Container(separator, sequence=[])

Abstract root class for the parts of the HL7 message.

__unicode__()

Join a the child containers into a single string, separated by the self.separator. This method acts recursively, calling the children’s __unicode__ method. Thus unicode() is the approriate method for turning the python-hl7 representation of HL7 into a standard string.

>>> unicode(h) == message
True
class hl7.Message(separator, sequence=[])

Representation of an HL7 message. It contains a list of hl7.Segment instances.

__getitem__(key)

Index or segment-based lookup.

If key is an integer, __getitem__ acts list a list, returning the hl7.Segment held at that index:

>>> h[1]
[[u'PID'], ...]

If the key is a string, __getitem__ acts like a dictionary, returning all segments whose segment_id is key (alias of hl7.Message.segments()).

>>> h['OBX']
[[[u'OBX'], [u'1'], ...]]
Return type:hl7.Segment or list of hl7.Segment
segment(segment_id)

Gets the first segment with the segment_id from the parsed message.

>>> h.segment('PID')
[[u'PID'], ...]
Return type:hl7.Segment
segments(segment_id)

Returns the requested segments from the parsed message that are identified by the segment_id (e.g. OBR, MSH, ORC, OBX).

>>> h.segments('OBX')
[[[u'OBX'], [u'1'], ...]]
Return type:list of hl7.Segment
class hl7.Segment(separator, sequence=[])

Second level of an HL7 message, which represents an HL7 Segment. Traditionally this is a line of a message that ends with a carriage return and is separated by pipes. It contains a list of hl7.Field instances.

class hl7.Field(separator, sequence=[])

Third level of an HL7 message, that traditionally is surrounded by pipes and separated by carets. It contains a list of strings.

MLLP Network Client

class hl7.client.MLLPClient(host, port)

A basic, blocking, HL7 MLLP client based upon socket.

MLLPClient implements two methods for sending data to the server.

  • MLLPClient.send() for raw data that already is wrapped in the appropriate MLLP container (e.g. <SB>message<EB><CR>).
  • MLLPClient.send_message() will wrap the message in the MLLP container

Can be used by the with statement to ensure MLLPClient.close() is called:

with MLLPClient(host, port) as client:
    client.send_message('MSH|...')
close()

Release the socket connection

send(data)

Low-level, direct access to the socket.send (data must be already wrapped in an MLLP container). Blocks until the server returns.

send_message(message)

Wraps a str, unicode, or :py:cls:`hl7.Message` in a MLLP container and send the message to the server