Codecs¶
- hl7types.codecs.er7.encoder.encode_er7_segment(seg, enc=EncodingChars(field='|', component='^', repetition='~', escape='\\', subcomponent='&', truncation=''))¶
Encode a single segment model to an ER7 segment string.
- Parameters:
seg (BaseModel) – The segment model instance to encode.
enc (EncodingChars, optional) – Delimiter characters to use. Defaults to the standard HL7 encoding characters (
|,^,~,\,&).
- Returns:
A single ER7 segment string with no trailing separator, e.g.
"MSA|AA|MSG001".- Return type:
str
Examples
>>> from hl7types.hl7.v2_5_1.segments import MSA >>> from hl7types.codecs.er7.encoder import encode_er7_segment >>> encode_er7_segment(MSA(msa_1="AA", msa_2="MSG001")) 'MSA|AA|MSG001'
- hl7types.codecs.er7.encoder.encode_er7(model, segment_separator='\r')¶
Encode a message or segment model to an ER7 wire string.
Encoding characters are read from the delimiter-definition segment (
MSH,FHS, orBHS) in the model if present, falling back to the standard HL7 defaults.- Parameters:
model (BaseModel) – The message, group, or segment model instance to encode.
segment_separator (str, optional) – Character used to join segments. The HL7 v2 specification mandates a carriage return (
\r, ASCII 0x0D). Defaults to"\r".
- Returns:
ER7-encoded wire string with segments joined by
segment_separator. Returns an empty string if the model contains no encodable segments.- Return type:
str
Examples
>>> from hl7types.hl7.v2_5_1.segments import MSA >>> from hl7types.codecs.er7.encoder import encode_er7 >>> encode_er7(MSA(msa_1="AA", msa_2="MSG001")) 'MSA|AA|MSG001'
- hl7types.codecs.er7.decoder.decode_er7_segment(seg_str, seg_cls, enc=EncodingChars(field='|', component='^', repetition='~', escape='\\', subcomponent='&', truncation=''), *, strict=False)¶
Decode a single ER7 segment string into a typed segment model.
- Parameters:
seg_str (str) – A single ER7 segment string, e.g.
"MSA|AA|MSG001".seg_cls (type[BaseModel]) – The segment model class to decode into.
enc (EncodingChars, optional) – Delimiter characters to use for decoding. For delimiter-definition segments (
MSH,FHS,BHS), the encoding characters are read from the segment string itself and override this value. Defaults to the standard HL7 encoding characters.strict (bool, optional) – If
True, raisespydantic.ValidationErrorwhen required fields are absent. IfFalse, missing required fields are filled with empty placeholder values and aUserWarningis emitted. Defaults toFalse.
- Returns:
A validated instance of
seg_cls.- Return type:
BaseModel
- Raises:
pydantic.ValidationError – If
strict=Trueand required fields are missing, or if any field value fails format validation.
Examples
>>> from hl7types.hl7.v2_5_1.segments import MSA >>> from hl7types.codecs.er7.decoder import decode_er7_segment >>> seg = decode_er7_segment("MSA|AA|MSG001", MSA) >>> seg.msa_1 'AA'
- hl7types.codecs.er7.decoder.decode_er7(wire, msg_cls=None, segment_separator='\r', *, strict=True, registry=None)¶
Decode an ER7 wire string into a typed message model.
When
msg_clsis not provided, the message class is resolved automatically fromMSH.9(message type) andMSH.12(version) in the wire string. Encoding characters are read from the delimiter-definition segment in the wire and applied consistently throughout decoding.- Parameters:
wire (str) – A complete ER7-encoded message string with segments separated by
segment_separator.msg_cls (type[BaseModel], optional) – The message model class to decode into. If
None, the class is resolved dynamically fromMSH.9andMSH.12.segment_separator (str, optional) – Character used to split segments. Defaults to
"\r".strict (bool, optional) – If
True, raisespydantic.ValidationErrorwhen required fields or segments are absent. IfFalse, missing required fields are filled with empty placeholder values and aUserWarningis emitted. Defaults toTrue.registry (HL7Registry, optional) – Registry of custom segment and message classes. Consulted when the decoder encounters a segment or message type not present in the generated specification models.
- Returns:
A validated instance of the resolved or provided message class.
- Return type:
BaseModel
- Raises:
ValueError – If the wire string is empty, no MSH segment is found, or the message type or version cannot be resolved to a known model class.
pydantic.ValidationError – If
strict=Trueand required fields or segments are missing, or if any field value fails format validation.
Examples
>>> from hl7types import decode_er7 >>> wire = "MSH|^~\\&|SEND||RECV||20260101||ACK|001|P|2.5.1\rMSA|AA|001" >>> msg = decode_er7(wire) >>> msg.MSA.msa_1 'AA'