Datatype factories

hl7apy.factories.date_factory(value, datatype_cls, validation_level=None)

Creates a DT object

The value in input must be a string parsable with datetime.strptime(). The date format is chosen according to the length of the value as stated in this table:

Length Format
4 %Y
6 %Y%m
8 %Y%m%d

Some examples that work are:

>>> from hl7apy.base_datatypes import DT
>>> date_factory("1974", DT) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DT object at 0x...>
>>> date_factory("198302", DT) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DT object at 0x...>
>>> date_factory("19880312", DT) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DT object at 0x...>

If the value does not match one of the valid format it raises ValueError

Parameters:
  • value (str) – the value to assign the date object
  • value – the DT class to use. It has to be one implementation of the different version modules
  • validation_level (int) – It must be a value from class validation_level VALIDATION_LEVEL hl7apy.consts.VALIDATION_LEVEL or None to use the default value
Return type:

hl7apy.base_datatypes.DT

hl7apy.factories.timestamp_factory(value, datatype_cls, validation_level=None)

Creates a TM object

The value in input must be a string parsable with datetime.strptime(). It can also have an offset part specified with the format +/-HHMM. The offset can be added with all the allowed format The date format is chosen according to the length of the value as stated in this table:

Length Format
2 %H
4 %H%M
6 %H%M%S
10-13 %H%M%S.%f

Some examples that work are:

>>> from hl7apy.base_datatypes import TM
>>> timestamp_factory("12", TM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.TM object at 0x...>
>>> timestamp_factory("12+0300", TM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.TM object at 0x...>
>>> timestamp_factory("1204", TM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.TM object at 0x...>
>>> timestamp_factory("120434", TM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.TM object at 0x...>
>>> timestamp_factory("120434-0400", TM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.TM object at 0x...>

If the value does not match one of the valid format it raises :exc:ValueError`

Parameters:
  • value (str) – the value to assign the date object
  • value – the TM class to use. It has to be one implementation of the different version modules
  • validation_level (int) – It must be a value from class validation_level VALIDATION_LEVEL hl7apy.consts.VALIDATION_LEVEL or None to use the default value
Return type:

TM

hl7apy.factories.datetime_factory(value, datatype_cls, validation_level=None)

Creates a hl7apy.base_datatypes.DTM object

The value in input must be a string parsable with datetime.strptime(). It can also have an offset part specified with the format +HHMM -HHMM. The offset can be added with all the allowed format. The date format is chosen according to the length of the value as stated in this table:

Length Format
4 %Y
6 %Y%m
8 %Y%m%d
10 %Y%m%d%H
12 %Y%m%d%H%M
14 %Y%m%d%H%M%S
18-21 %Y%m%d%H%M%S.%f

Some examples that work are:

>>> from hl7apy.base_datatypes import DTM
>>> datetime_factory("1924", DTM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DTM object at 0x...>
>>> datetime_factory("1924+0300", DTM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DTM object at 0x...>
>>> datetime_factory("19220430", DTM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DTM object at 0x...>
>>> datetime_factory("19220430-0400", DTM) #doctest: +ELLIPSIS
<hl7apy.base_datatypes.DTM object at 0x...>

If the value does not match one of the valid format it raises ValueError

Parameters:
  • value (str) – the value to assign the date object
  • value – the DTM class to use. It has to be one implementation of the different version modules
  • validation_level (int) – It must be a value from class validation_level VALIDATION_LEVEL hl7apy.consts.VALIDATION_LEVEL or None to use the default value
Return type:

DTM

hl7apy.factories.numeric_factory(value, datatype_cls, validation_level=None)

Creates a NM object

The value in input can be a string representing a decimal number or a float. (i.e. a string valid for decimal.Decimal()). If it’s not, a ValueError is raised Also an empty string or None are allowed

Parameters:
  • value (str or None) – the value to assign the numeric object
  • value – the NM class to use. It has to be one implementation of the different version modules
  • validation_level (int) – It must be a value from class VALIDATION_LEVEL hl7apy.consts.VALIDATION_LEVEL or None to use the default value
Return type:

NM

hl7apy.factories.sequence_id_factory(value, datatype_cls, validation_level=None)

Creates a SI object

The value in input can be a string representing an integer number or an int. (i.e. a string valid for int() ). If it’s not, a ValueError is raised Also an empty string or None are allowed

Parameters:
  • value (str or None) – the value to assign the date object
  • value – the SI class to use. It has to be loaded from one implementation of the different version modules
  • validation_level (int) – It must be a value from class VALIDATION_LEVEL hl7apy.consts.VALIDATION_LEVEL or None to use the default value
Return type:

SI