Skip to content

Commit

Permalink
Adding a get_values_list function for repetition.
Browse files Browse the repository at this point in the history
  • Loading branch information
minjitoscar committed Jun 27, 2016
1 parent a2894ff commit 3ed4711
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
56 changes: 52 additions & 4 deletions pyx12/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def format(self):
"""
return self.value

def format_list(self):
"""
@rtype: list(string)
"""
return [self.value]

def get_value(self):
"""
@rtype: string
Expand Down Expand Up @@ -243,15 +249,46 @@ def format(self, subele_term=None):
res = []
for itr, x in enumerate(self.elements[:i + 1]):
if isinstance(x, str):
if res[-1] == self.subele_term:
res.pop()
res.append(x)
else:
res.append(x.__repr__())
res.append(subele_term)
res.pop()
if itr + 1 < len(self.elements) and not isinstance(self.elements[itr+1], str):
res.append(subele_term)
if itr+1 < len(self.elements) and self.elements[itr+1].is_empty():
res.pop()
return ''.join(res)

def format_list(self, subele_term=None):
"""
Format a composite and return a tuple of repetitions
@return: list(string)
@raise EngineError: If terminator is None and no default
"""
if subele_term is None:
subele_term = self.subele_term
if subele_term is None:
raise EngineError('subele_term is None')
for i in range(len(self.elements) - 1, -1, -1):
if not self.elements[i].is_empty():
break
res = []
rep = []
for itr, x in enumerate(self.elements[:i + 1]):
if isinstance(x, str):
if itr+1 < len(self.elements) and self.elements[itr+1].is_empty():
rep.pop()
res.append(''.join(rep))
rep = []
else:
rep.append(x.__repr__())
if itr + 1 < len(self.elements) and not isinstance(self.elements[itr+1], str):
rep.append(subele_term)
if len(rep) > 0:
if itr+1 < len(self.elements) and self.elements[itr+1].is_empty():
rep.pop()
res.append(''.join(rep))
return res

def get_value(self):
"""
Get value of simple element
Expand Down Expand Up @@ -446,6 +483,17 @@ def get_value(self, ref_des):
else:
return comp1.format()

def get_values_list(self, ref_des):
"""
@param ref_des: X12 Reference Designator
@type ref_des: string
"""
comp1 = self.get(ref_des)
if comp1 is None:
return None
else:
return comp1.format_list()

def get_value_by_ref_des(self, ref_des):
"""
@param ref_des: X12 Reference Designator
Expand Down
20 changes: 20 additions & 0 deletions pyx12/test/test_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def test_repetitions(self):
self.assertEqual(self.seg.get('01').get_repetitions(), 1)
self.assertEqual(self.repeat_seg.get('01').get_repetitions(), 2)

def test_simple_indexing(self):
self.assertEqual(self.seg.get_value('TST01'), 'AA:1:Y')
self.assertEqual(self.seg.get_value('TST02'), 'BB:5')
self.assertEqual(self.seg.get_value('TST03'), 'ZZ')
self.assertEqual(self.seg.get_value('TST06'), None)

self.assertEqual(self.repeat_seg.get_value('TST01'), 'AA:1:Y^AA:2:Z')
self.assertEqual(self.repeat_seg.get_value('TST02'), 'BB:5')
self.assertEqual(self.repeat_seg.get_value('TST03'), 'Z')
self.assertEqual(self.repeat_seg.get_value('TST06'), None)

self.assertEqual(self.seg.get_values_list('TST01'), ['AA:1:Y'])
self.assertEqual(self.seg.get_values_list('TST02'), ['BB:5'])
self.assertEqual(self.seg.get_values_list('TST03'), ['ZZ'])
self.assertEqual(self.seg.get_values_list('TST06'), None)

self.assertEqual(self.repeat_seg.get_values_list('TST01'), ['AA:1:Y', 'AA:2:Z'])
self.assertEqual(self.repeat_seg.get_values_list('TST02'), ['BB:5'])
self.assertEqual(self.repeat_seg.get_values_list('TST03'), ['Z'])

def test_composite_indexing(self):
self.assertEqual(self.seg.get_value('TST01-1'), 'AA')
self.assertEqual(self.seg.get_value('TST01-3'), 'Y')
Expand Down

0 comments on commit 3ed4711

Please sign in to comment.