annotate python/base_encode.py @ 729:fc4749433229

https://gist.github.com/adyliu/4494223
author Jeff Hammel <k0scist@gmail.com>
date Wed, 10 Dec 2014 10:08:31 -0800
parents
children 9c2a18033128
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 -*-
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 # Base62 tools (convert number <=> string)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 # v1.0/20130109
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 # python 2.x/3.x supported
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 #
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 #author: Ady Liu(imxylz@gmail.com)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 #github: github.com/adyliu
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 basedigits='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 BASE=len(basedigits)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def decode(s):
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 ret,mult = 0,1
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 for c in reversed(s):
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 ret += mult*basedigits.index(c)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 mult *= BASE
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 return ret
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def encode(num):
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 if num <0: raise Exception("positive number "+num)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 if num ==0: return '0'
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 ret=''
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 while num != 0:
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 ret = (basedigits[num%BASE])+ret
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 num = int(num/BASE)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 return ret
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 if __name__ == '__main__':
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 if len(sys.argv) < 2:
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 print("Usage: base62.py <num>...")
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 sys.exit(1)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 width = max(len(x) for x in sys.argv[1:])
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for argv in sys.argv[1:]:
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 try:
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 num = int(argv)
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 print('%*s %s %s' % (width,argv,'ENCODE',encode(num)))
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 except ValueError:
fc4749433229 https://gist.github.com/adyliu/4494223
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 print('%*s %s %s' % (width,argv,'DECODE',decode(argv)))