annotate python/base_encode.py @ 749:f7b939206baf

PS
author Jeff Hammel <k0scist@gmail.com>
date Tue, 30 Jun 2015 15:22:21 -0700
parents 9c2a18033128
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
729
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 #-*- coding:utf-8 -*-
730
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
3
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
4 """
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
5 arbitrary base encoding tools (convert number <=> string)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
6
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
7 modified from
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
8 https://gist.github.com/adyliu/4494223
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
9 """
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
10
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
11 import argparse
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
12 import string
729
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import sys
730
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
14
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
15 basedigits = string.digits + string.letters
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
16
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
17 def decode(s, basedigits=basedigits):
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
18
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
19 BASE = len(basedigits)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
20 ret,mult = 0,1
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
21 for c in reversed(s):
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
22 ret += mult*basedigits.index(c)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
23 mult *= BASE
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
24 return ret
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
25
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
26 def encode(num, basedigits=basedigits):
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
27 BASE = len(basedigits)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
28
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
29 if num < 0:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
30 raise Exception("positive number "+num)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
31 if num == 0:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
32 return '0'
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
33 ret=''
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
34 while num:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
35 ret = (basedigits[num%BASE])+ret
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
36 num = int(num/BASE)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
37 return ret
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
38
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
39
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
40 def main(args=sys.argv[1:]):
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
41
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
42 parser = argparse.ArgumentParser(description=__doc__)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
43 parser.add_argument('num', nargs='+',
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
44 help="num")
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
45 parser.add_argument('-b', '--base', dest='base',
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
46 type=int, default=32,
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
47 help="base to use [DEFAULT: %(default)s]")
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
48 parser.add_argument('-c', '--chars', '--base-digits', dest='basedigits',
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
49 default=globals()['basedigits'],
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
50 help="base digits [DEFAULT: %(default)s]")
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
51 options = parser.parse_args(args)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
52
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
53 # determine base digits
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
54 basedigits = options.basedigits[:options.base]
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
55 base = len(basedigits)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
56
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
57 for num in options.num:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
58
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
59 try:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
60 num = int(num)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
61 encoded = encode(num, basedigits)
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
62 print ('ENCODE base {}: {}->{} [{}]'.format(base, num, encoded, len(encoded)))
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
63 except ValueError:
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
64 print ('%*s %s %s' % ('DECODE', num, decode(num)))
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
65
729
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 if __name__ == '__main__':
730
9c2a18033128 flush out
Jeff Hammel <k0scist@gmail.com>
parents: 729
diff changeset
67 main()