Package ais :: Module aisstring
[hide private]
[frames] | no frames]

Module aisstring

source code

Handle encoding and decoding AIS strings.


Bugs:

Author: Kurt Schwehr

Version: 2068

Copyright: 2006

Date: 2006-05-02

Functions [hide private]
str
decode(bits, dropAfterFirstAt=False)
Decode bits as a string.
source code
BitVector
encode(string, bitSize=None)
Returns: enocded bits for the string
source code
str
unpad(string, removeBlanks=True)
Remove AIS string padding
source code
str
pad(string, length)
pad a string out to the proper length with the @ character as required by the ais spec
source code
Variables [hide private]
list characterLUT = ['@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '...
lookup table for decode to fetch characters faster
  characterDict = {' ': 32, '!': 33, '"': 34, '#': 35, '$': 36, ...
Fast lookup for the AIS int code for a character
dict characterBits = {' ': <ais.BitVector.BitVector object at 0x148...
lookup table for going from a single character to a 6 bit BitVector
Function Details [hide private]

decode(bits, dropAfterFirstAt=False)

source code 
Decode bits as a string. Does not remove the end space or @@@@. Must be an multiple of 6 bits.
Parameters:
  • bits (BitVector) - n*6 bits that represent a string.
Returns: str
string with pad spaces or @@@@

encode(string, bitSize=None)

source code 
Parameters:
  • string (str) - python ascii string to encode.
  • bitSize (int) - how many bits should this take. must be a multiple of 6
Returns: BitVector
enocded bits for the string
Bugs:
  • force to upper case
  • building this in reverse may be faster
  • check that bitSize is a multple of 6
  • pad with "@" to reach requested bitSize

unpad(string, removeBlanks=True)

source code 
Remove AIS string padding
>>> unpad('@')
''
>>> unpad('A@')
'A'
>>> unpad('ABCDEF1234@@@@@')
'ABCDEF1234'
FIX: is this the correct response?
>>> unpad('A@B')
'A@B'
This is non standard behavior, but some AIS systems space pad the right
>>> unpad(' ')
''
>>> unpad('MY SHIP NAME    ')
'MY SHIP NAME'
The standard implies this behavior with is less fun
>>> unpad('MY SHIP NAME    ',removeBlanks=False)
'MY SHIP NAME    '
Parameters:
  • string (str) - string to cleanup
  • removeBlanks (bool) - set to true to strip spaces on the right
Returns: str
cleaned up string

Bug: use a faster algorithm for truncating the string

pad(string, length)

source code 
pad a string out to the proper length with the @ character as required by the ais spec
>>> pad('',0)
''
>>> pad('',1)
'@'
>>> pad('A',1)
'A'
>>> pad('A',2)
'A@'
>>> pad('MY SHIP NAME',20)
'MY SHIP NAME@@@@@@@@'
Parameters:
  • string (str) - string to pad out
  • length (int) - number of characters that the string must be
Returns: str
str of len length

Bug: Use a list and join to make the string building faster


Variables Details [hide private]

characterLUT

lookup table for decode to fetch characters faster
Type:
list
Value:
['@',
 'A',
 'B',
 'C',
 'D',
 'E',
 'F',
 'G',
...

characterDict

Fast lookup for the AIS int code for a character
Value:
{' ': 32,
 '!': 33,
 '"': 34,
 '#': 35,
 '$': 36,
 '%': 37,
 '&': 38,
 '(': 40,
...

characterBits

lookup table for going from a single character to a 6 bit BitVector
Type:
dict
Value:
{}