【发布时间】:2010-12-04 06:37:57
【问题描述】:
import numbers
class base62(numbers.Number):
digits='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def __init__(self,value):
if isinstance(value,int):
self.value=value
if value==0:
self.string='0'
else:
self.string='' if value>0 else '-'
while value!=0:
value,d=divmod(value,62)
self.string=self.digits[d]+self.string
elif isinstance(value,(str,bytes)):
assert(value.isalnum())
self.string=str(value)
self.value=0
for d in value:
self.value=self.value*62+self.digits.index(d)
def __int__(self):
return self.value
def __str__(self):
return self.string
def __repr__(self):
return self.string
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import base62
>>> b = base62(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>>
【问题讨论】:
-
这个函数有一些bug,所以我重写了:stackoverflow.com/questions/4351467/…