2019-12-08 22:43:49 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
class ID:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_extid(extid: int) -> str:
|
|
|
|
"""
|
|
|
|
Take an ExtID as an integer, format it as a string.
|
|
|
|
|
|
|
|
If we had the ExtID 12345678, this would format as '1234-5678'
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
extid - The ID as an integer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A string suitable for display to the user.
|
|
|
|
"""
|
|
|
|
extid_str = str(extid)
|
|
|
|
while len(extid_str) < 8:
|
|
|
|
extid_str = '0' + extid_str
|
2020-01-07 22:29:07 +01:00
|
|
|
return f'{extid_str[0:4]}-{extid_str[4:8]}'
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_extid(extid: str) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Take an ExtID as a string, and return the integer.
|
|
|
|
|
|
|
|
If we had the ExtID '1234-5678', this would return 12345678.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
extid - The string ID as shown to a suer
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
An integer extid suitable for looking up in a DB.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if len(extid) == 9 and extid[4:5] == '-':
|
|
|
|
return int(extid[0:4] + extid[5:9])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_machine_id(machine_id: int) -> str:
|
|
|
|
"""
|
|
|
|
Take a machine ID as an integer, format it as a string.
|
|
|
|
"""
|
2020-01-07 22:29:07 +01:00
|
|
|
return f'US-{machine_id}'
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_machine_id(machine_id: str) -> Optional[int]:
|
|
|
|
"""
|
|
|
|
Take a formatted machine ID as a string, returning an int.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if machine_id[:3] == 'US-':
|
|
|
|
return int(machine_id[3:])
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|