1
2 __version__ = '$Revision: 2068 $'.split()[1]
3 __date__ = '$Date: 2006-05-02 08:17:59 -0400 (Tue, 02 May 2006) $'.split()[1]
4 __author__ = 'Kurt Schwehr'
5
6 __doc__='''
7 Codecs to handle encoding to and from BitVectors
8
9 @requires: U{BitVector<http://cheeseshop.python.org/pypi/BitVector>}
10 @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3
11
12 @bug: FIX: need some doctests
13 @bug: need some asserts
14 @todo: maybe decorators?
15
16 @author: '''+__author__+'''
17 @version: ''' + __version__ +'''
18 @copyright: 2006
19 @var __date__: Date of last svn commit
20 @undocumented: __version__ __author__ __doc__ myparser
21 '''
22
23
24 import sys
25 from decimal import Decimal
26
27
28 from BitVector import BitVector
29
30
31 import aisstring
32 import binary
33
34
35
36
37
38
39
40 encode={}
41 '''use this table to get the functions to go from usable values in python to bitvectors'''
42 decode={}
43 '''use this table to get the functions to go from bitvectors to usable values in python'''
44
45
46
47
53 '''@rtype: int'''
54 return int(bv)
55
56 encode['unsigned int']=unsigned_int_enc
57 decode['unsigned int']=unsigned_int_dec
58
59
66
67 encode['int']=int_enc
68 decode['int']=int_dec
69
70
72 '''@rtype: BitVector'''
73 assert(bitSize==1)
74 if val: return BitVector(bitString="0")
75 return BitVector(bitString="1")
77 '''@rtype: bool'''
78 assert(len(bv)==1)
79 if bv[0]==0: return False
80 return True
81
82 encode['bool']=bool_enc
83 decode['bool']=bool_dec
84
85
92 '''@rtype: Decimal'''
93 return Decimal(int(bv))
94
95 encode['unsigned decimal']=unsigned_decimal_enc
96 decode['unsigned decimal']=unsigned_decimal_dec
97
98
106
107 encode['decimal']=decimal_enc
108 decode['decimal']=decimal_dec
109
110
111
119
120 encode['float']=float_enc
121 decode['float']=float_dec
122
123
124 encode['string']=aisstring.encode
125 decode['string']=aisstring.decode
126
127
128 if __name__=='__main__':
129 from optparse import OptionParser
130 myparser = OptionParser(usage="%prog [options]",
131 version="%prog "+__version__)
132 myparser.add_option('--test','--doc-test',dest='doctest',default=False,action='store_true',
133 help='run the documentation tests')
134
135 (options,args) = myparser.parse_args()
136
137 success=True
138
139 if options.doctest:
140 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
141 sys.argv= [sys.argv[0]]
142
143 import doctest
144 numfail,numtests=doctest.testmod()
145 if numfail==0: print 'ok'
146 else:
147 print 'FAILED'
148 success=False
149
150 if not success:
151 sys.exit('Something Failed')
152