parsing: refine PHGR code and update tests
This commit is contained in:
parent
dc90983cb6
commit
2e9c4a9c56
|
|
@ -133,8 +133,10 @@ def parse_comment(body, parsed):
|
|||
|
||||
def parse_data_extentions(body):
|
||||
parsed = {}
|
||||
match = re.findall(r"^([0-9 .]{3})/([0-9 .]{3})", body)
|
||||
|
||||
# course speed bearing nrq
|
||||
# format: 111/222/333/444text
|
||||
match = re.findall(r"^([0-9 .]{3})/([0-9 .]{3})", body)
|
||||
if match:
|
||||
cse, spd = match[0]
|
||||
body = body[7:]
|
||||
|
|
@ -151,21 +153,23 @@ def parse_data_extentions(body):
|
|||
if nrq.isdigit():
|
||||
parsed.update({'nrq': int(nrq)})
|
||||
else:
|
||||
match = re.findall(r"^(PHG(\d[\x30-\x7e]\d\d[0-9A-Z]?\/))", body)
|
||||
# PHG format: PHGabcd....
|
||||
# RHGR format: RHGabcdr/....
|
||||
match = re.findall(r"^(PHG(\d[\x30-\x7e]\d\d)([0-9A-Z]\/)?)", body)
|
||||
if match:
|
||||
ext, phg = match[0]
|
||||
body = body[len(ext):]
|
||||
parsed.update({'phg': phg})
|
||||
else:
|
||||
match = re.findall(r"^(PHG(\d[\x30-\x7e]\d\d))", body)
|
||||
if match:
|
||||
ext, phg = match[0]
|
||||
body = body[len(ext):]
|
||||
parsed.update({'phg': phg})
|
||||
else:
|
||||
match = re.findall(r"^RNG(\d{4})", body)
|
||||
if match:
|
||||
rng = match[0]
|
||||
print(match)
|
||||
ext, phg, phgr = match[0]
|
||||
body = body[len(ext):]
|
||||
parsed.update({'phg': phg})
|
||||
|
||||
if phgr:
|
||||
# PHG rate per hour
|
||||
parsed['phg'] += phgr[0]
|
||||
parsed.update({'phg_rate': int(phgr[0], 16)}) # as decimal
|
||||
else:
|
||||
match = re.findall(r"^RNG(\d{4})", body)
|
||||
if match:
|
||||
rng = match[0]
|
||||
body = body[7:]
|
||||
parsed.update({'rng': int(rng) * 1.609344}) # miles to km
|
||||
|
||||
|
|
|
|||
|
|
@ -312,15 +312,36 @@ class DataExtentionsTC(unittest.TestCase):
|
|||
'nrq': 345,
|
||||
})
|
||||
|
||||
def test_PHG(self):
|
||||
body = "PHG1234Atext"
|
||||
def test_PHGR_1(self):
|
||||
body = "PHG12345/text"
|
||||
remaining, parsed = parse_data_extentions(body)
|
||||
|
||||
self.assertEqual(remaining, 'text')
|
||||
self.assertEqual(parsed, {
|
||||
'phg': '1234A',
|
||||
'phg': '12345',
|
||||
'phg_rate': 5,
|
||||
})
|
||||
|
||||
def test_PHGR_2(self):
|
||||
body = "PHG1234F/text"
|
||||
remaining, parsed = parse_data_extentions(body)
|
||||
|
||||
self.assertEqual(remaining, 'text')
|
||||
self.assertEqual(parsed, {
|
||||
'phg': '1234F',
|
||||
'phg_rate': 15,
|
||||
})
|
||||
|
||||
def test_PHG_1(self):
|
||||
body = "PHG1234Atext"
|
||||
remaining, parsed = parse_data_extentions(body)
|
||||
|
||||
self.assertEqual(remaining, 'Atext')
|
||||
self.assertEqual(parsed, {
|
||||
'phg': '1234',
|
||||
})
|
||||
|
||||
def test_PHG_2(self):
|
||||
body = "PHG1234text"
|
||||
remaining, parsed = parse_data_extentions(body)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue