`
san_yun
  • 浏览: 2639495 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

python Short URL Generator

 
阅读更多

python提供的短链接服务

http://code.activestate.com/recipes/576918/

 

## {{{ http://code.activestate.com/recipes/576918/ (r3)
'''
Short URL Generator
===================

Python implementation for generating Tiny URL- and bit.ly-like URLs.

A bit-shuffling approach is used to avoid generating consecutive, predictable 
URLs.  However, the algorithm is deterministic and will guarantee that no 
collisions will occur.

The URL alphabet is fully customizable and may contain any number of 
characters.  By default, digits and lower-case letters are used, with 
some removed to avoid confusion between characters like o, O and 0.  The 
default alphabet is shuffled and has a prime number of characters to further 
improve the results of the algorithm.

The block size specifies how many bits will be shuffled.  The lower BLOCK_SIZE 
bits are reversed.  Any bits higher than BLOCK_SIZE will remain as is.
BLOCK_SIZE of 0 will leave all bits unaffected and the algorithm will simply 
be converting your integer to a different base.

The intended use is that incrementing, consecutive integers will be used as 
keys to generate the short URLs.  For example, when creating a new URL, the 
unique integer ID assigned by a database could be used to generate the URL 
by using this module.  Or a simple counter may be used.  As long as the same 
integer is not used twice, the same short URL will not be generated twice.

The module supports both encoding and decoding of URLs. The min_length 
parameter allows you to pad the URL if you want it to be a specific length.

Sample Usage:

>>> import short_url
>>> url = short_url.encode_url(12)
>>> print url
LhKA
>>> key = short_url.decode_url(url)
>>> print key
12

Use the functions in the top-level of the module to use the default encoder. 
Otherwise, you may create your own UrlEncoder object and use its encode_url 
and decode_url methods.

Author: Michael Fogleman
License: MIT
Link: http://code.activestate.com/recipes/576918/
'''

DEFAULT_ALPHABET = 'mn6j2c4rv8bpygw95z7hsdaetxuk3fq'
DEFAULT_BLOCK_SIZE = 24
MIN_LENGTH = 5

class UrlEncoder(object):
    def __init__(self, alphabet=DEFAULT_ALPHABET, block_size=DEFAULT_BLOCK_SIZE):
        self.alphabet = alphabet
        self.block_size = block_size
        self.mask = (1 << block_size) - 1
        self.mapping = range(block_size)
        self.mapping.reverse()
    def encode_url(self, n, min_length=MIN_LENGTH):
        return self.enbase(self.encode(n), min_length)
    def decode_url(self, n):
        return self.decode(self.debase(n))
    def encode(self, n):
        return (n & ~self.mask) | self._encode(n & self.mask)
    def _encode(self, n):
        result = 0
        for i, b in enumerate(self.mapping):
            if n & (1 << i):
                result |= (1 << b)
        return result
    def decode(self, n):
        return (n & ~self.mask) | self._decode(n & self.mask)
    def _decode(self, n):
        result = 0
        for i, b in enumerate(self.mapping):
            if n & (1 << b):
                result |= (1 << i)
        return result
    def enbase(self, x, min_length=MIN_LENGTH):
        result = self._enbase(x)
        padding = self.alphabet[0] * (min_length - len(result))
        return '%s%s' % (padding, result)
    def _enbase(self, x):
        n = len(self.alphabet)
        if x < n:
            return self.alphabet[x]
        return self._enbase(x / n) + self.alphabet[x % n]
    def debase(self, x):
        n = len(self.alphabet)
        result = 0
        for i, c in enumerate(reversed(x)):
            result += self.alphabet.index(c) * (n ** i)
        return result

DEFAULT_ENCODER = UrlEncoder()

def encode(n):
    return DEFAULT_ENCODER.encode(n)

def decode(n):
    return DEFAULT_ENCODER.decode(n)

def enbase(n, min_length=MIN_LENGTH):
    return DEFAULT_ENCODER.enbase(n, min_length)

def debase(n):
    return DEFAULT_ENCODER.debase(n)

def encode_url(n, min_length=MIN_LENGTH):
    return DEFAULT_ENCODER.encode_url(n, min_length)

def decode_url(n):
    return DEFAULT_ENCODER.decode_url(n)

if __name__ == '__main__':
    for a in range(0, 200000, 37):
        b = encode(a)
        c = enbase(b)
        d = debase(c)
        e = decode(d)
        assert a == e
        assert b == d
        c = (' ' * (7 - len(c))) + c
        print '%6d %12d %s %12d %6d' % (a, b, c, d, e)
## end of http://code.activestate.com/recipes/576918/ }}}
 
分享到:
评论

相关推荐

    Python Generator

    标题中提到了Python Generator,这是指Python中的生成器,一种特殊的迭代器,能够让用户在循环中逐步产生一个序列中的值,而不是一次性返回整个序列。在描述中提到的是David Beazley所作的关于Python生成器的系统...

    generator_sound_frequency_python_generator_

    标题中的"generator_sound_frequency_python_generator_"暗示我们讨论的是一个使用Python编写的频率发生器,它能够生成.wav音频文件,并在该文件中实现频率的扫频。这个工具可能用于测试音频设备,声学研究,或者...

    URLGenerator(URL生成器)v1.00官方英文绿色版

    URL Generator(URL生成器)是来自国外的一款URL生产工具,URL Generator界面整洁、功能强大,能够一键生产URL,快捷方便、小巧又实用,URL Generator将根据您输入的字段值创建一个URL列表,然后可以从程序中保存URL...

    Python库 | generator3-0.0.7-py3-none-any.whl

    在Python的世界里,`generator3-0.0.7-py3-none-any.whl` 是一个特定版本的Python库包,主要用于处理生成器(generator)相关的功能。这个压缩包文件是一个wheel格式的软件包,它是Python的一种二进制分发格式,旨在...

    Python库 | datamodel_code_generator-0.10.3-py3-none-any.whl

    资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:datamodel_code_generator-0.10.3-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python yield和Generator函数用法详解

    Python中的`yield`关键字和Generator函数是Python编程中一种高效且节省内存的机制,它们用于创建迭代器。本文将深入探讨`yield`的工作原理以及如何使用Generator函数。 首先,让我们理解`yield`的关键作用。在普通...

    Python生成器generator原理及用法解析

    Python生成器generator简介 iteration, iterable, iterator 迭代(iteration):在python中迭代通常是通过for…in…来实现的.而且只要是可迭代对象iterable,都能进行迭代. 可迭代对象(iterable):Python中的任意的对象...

    Python库 | rosinstall_generator-0.1.22.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:rosinstall_generator-0.1.22.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Python库 | generator_tools-0.3.6.win32-py2.5.exe

    python库。 资源全名:generator_tools-0.3.6.win32-py2.5.exe

    Python库 | htg_url_generator-0.1.9.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:htg_url_generator-0.1.9.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Super shorturl generator-crx插件

    语言:English 此扩展名使您可以从Couleur Citron的新URL缩短器创建shorturl。 只需单击缩短器图标即可生成一个随机的缩短的URL。 要自定义您的网址,请访问http://coulc.it/并选择自定义网址。

    generator_sweep_frequency_python_generator_audio_

    标题 "generator_sweep_frequency_python_generator_audio_" 暗示我们关注的是一个使用Python生成音频文件的项目,其中涉及了频率扫描或扫频技术。扫频是电子和通信领域中的常见技术,用于测试设备对不同频率的响应...

    Python生成器generator用法示例

    本文实例分析了Python生成器generator用法。分享给大家供大家参考,具体如下: 生成器generator本质是一个函数,它记住上一次在函数体中的位置,在生成器函数下一次调用,会自动找到该位置,局部变量都保持不变 l =...

    Python库 | dungeon-generator-0.0.2.tar.gz

    《Python库:dungeon-generator-0.0.2——打造迷宫生成的魔法世界》 在编程领域,Python因其简洁易读的语法和丰富的库支持而备受青睐。今天我们要探讨的是一个名为“dungeon-generator”的Python库,版本为0.0.2,...

    Python库 | sitemap_generator-0.5.2-py2.7.egg

    《Python库sitema_generator-0.5.2-py2.7.egg:构建网站地图的利器》 在IT行业中,Python作为一种强大的开发语言,因其简洁、易读的语法和丰富的库支持而广受开发者喜爱。在搜索引擎优化(SEO)领域,一个重要的...

    超短流发电机「Super shorturl generator」-crx插件

    这个扩展允许你从Couleur Citron的新网址缩短器中创建shorturl。 只需点击缩短图标即可生成随机缩短的网址。 要自定义您的网址,请转到http://coulc.it/并选择自定义网址。 支持语言:English

    Python库 | datamodel-code-generator-0.6.17.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:datamodel-code-generator-0.6.17.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    generator_sweep_frequency_python_WiT_generator_

    Frequency generator in python. Generates .wav file wit sweeping frequency. No external library needed.

Global site tag (gtag.js) - Google Analytics