Skip to content

Commit

Permalink
Clean up whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpaulett committed Aug 30, 2011
1 parent 0b7ada8 commit b47ba7f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
42 changes: 22 additions & 20 deletions hl7/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def ishl7(line):

def parse(line):
"""Returns a instance of the :py:class:`hl7.Message` that allows
indexed access to the data elements.
indexed access to the data elements.
.. note::
Expand Down Expand Up @@ -55,12 +55,12 @@ def parse(line):

def _split(text, plan):
"""Recursive function to split the *text* into an n-deep list,
according to the :py:class:`hl7._ParsePlan`.
according to the :py:class:`hl7._ParsePlan`.
"""
## Base condition, if we have used up all the plans
if not plan:
return text

## Recurse so that the sub plans are used in order to split the data
## into the approriate type as defined by the current plan.
data = [_split(x, plan.next()) for x in text.split(plan.separator)]
Expand All @@ -75,8 +75,8 @@ def __init__(self, separator, sequence=[]):
## sequence. Since list([]) == [], using the default
## parameter will not cause any issues.
super(Container, self).__init__(sequence)
self.separator = separator
self.separator = separator

def __unicode__(self):
"""Join a the child containers into a single string, separated
by the self.separator. This method acts recursively, calling
Expand All @@ -86,15 +86,15 @@ def __unicode__(self):
>>> unicode(h) == message
True
"""
return self.separator.join((unicode(x) for x in self))

class Message(Container):
"""Representation of an HL7 message. It contains a list
of :py:class:`hl7.Segment` instances.
"""

def __getitem__(self, key):
"""Index or segment-based lookup.
Expand All @@ -118,30 +118,32 @@ def __getitem__(self, key):
return list.__getitem__(self, key)

def segment(self, segment_id):
"""Gets the first segment with the *segment_id* from the parsed *message*.
"""Gets the first segment with the *segment_id* from the parsed
*message*.
>>> h.segment('PID')
[[u'PID'], ...]
:rtype: :py:class:`hl7.Segment`
"""
## Get the list of all the segments and pull out the first one if possible
## Get the list of all the segments and pull out the first one,
## if possible
match = self.segments(segment_id)
## We should never get an IndexError, since segments will instead
## throw an KeyError
return match[0]

def segments(self, segment_id):
"""Returns the requested segments from the parsed *message* that are identified
by the *segment_id* (e.g. OBR, MSH, ORC, OBX).
"""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'], ...]]
:rtype: list of :py:class:`hl7.Segment`
"""
## Compare segment_id to the very first string in each segment, returning
## all segments that match
## Compare segment_id to the very first string in each segment,
## returning all segments that match
matches = [segment for segment in self if segment[0][0] == segment_id]
if len(matches) == 0:
raise KeyError('No %s segments' % segment_id)
Expand All @@ -158,7 +160,7 @@ class Field(Container):
"""Third level of an HL7 message, that traditionally is surrounded
by pipes and separated by carets. It contains a list of strings.
"""

def create_parse_plan(strmsg):
"""Creates a plan on how to parse the HL7 message according to
the details stored within the message.
Expand All @@ -172,7 +174,7 @@ def create_parse_plan(strmsg):
## The ordered list of containers to create
containers = [Message, Segment, Field]
return _ParsePlan(separators, containers)

class _ParsePlan(object):
"""Details on how to parse an HL7 message. Typically this object
should be created via :func:`hl7.create_parse_plan`
Expand All @@ -187,7 +189,7 @@ def __init__(self, separators, containers):
assert len(containers) == len(separators)
self.separators = separators
self.containers = containers

@property
def separator(self):
"""Return the current separator to use based on the plan."""
Expand All @@ -198,7 +200,7 @@ def container(self, data):
as specified by the current plan.
"""
return self.containers[0](self.separator, data)

def next(self):
"""Generate the next level of the plan (essentially generates
a copy of this plan with the level of the container and the
Expand Down
13 changes: 6 additions & 7 deletions tests/test_hl7.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_parse(self):
msg[3][3],
[u'1554-5', u'GLUCOSE', u'POST 12H CFST:MCNC:PT:SER/PLAS:QN']
)

def test_bytestring_converted_to_unicode(self):
msg = hl7.parse(str(sample_hl7))
self.assertEqual(len(msg), 5)
Expand All @@ -40,7 +40,7 @@ def test_non_ascii_bytestring(self):

def test_parsing_classes(self):
msg = hl7.parse(sample_hl7)

self.assertTrue(isinstance(msg, hl7.Message))
self.assertTrue(isinstance(msg[3], hl7.Segment))
self.assertTrue(isinstance(msg[3][0], hl7.Field))
Expand Down Expand Up @@ -82,9 +82,8 @@ def test_container_unicode(self):
c = hl7.Container('|')
c.extend(['1', 'b', 'data'])
self.assertEqual(unicode(c), '1|b|data')

class MessageTest(unittest.TestCase):

def test_segments(self):
msg = hl7.parse(sample_hl7)
s = msg.segments('OBX')
Expand All @@ -100,7 +99,7 @@ def test_segment(self):
msg = hl7.parse(sample_hl7)
s = msg.segment('OBX')
self.assertEqual(s[0:3], [['OBX'], ['1'], ['SN']])

def test_segment_does_not_exist(self):
msg = hl7.parse(sample_hl7)
self.assertRaises(KeyError, msg.segment, 'BAD')
Expand Down Expand Up @@ -134,13 +133,13 @@ def test_parse_plan_next(self):
n1 = plan.next()
self.assertEqual(n1.separators, ['|', '^'])
self.assertEqual(n1.containers, [Segment, Field])

n2 = n1.next()
self.assertEqual(n2.separators, ['^'])
self.assertEqual(n2.containers, [Field])

n3 = n2.next()
self.assertTrue(n3 is None)

if __name__ == '__main__':
unittest.main()

0 comments on commit b47ba7f

Please sign in to comment.