Code Monkey home page Code Monkey logo

base62's Introduction

base62

Build Status Coveralls PyPI

A Python module for base62 encoding. Ported from PHP code that I wrote in mid-2000, which can be found here.

Rationale

When writing a web application, often times we would like to keep the URLs short.

http://localhost/post/V1Biicwt

This certainly gives a more concise look than the following.

http://localhost/post/109237591284123

This was the original motivation to write this module, but there shall be much more broader potential use cases of this module. The main advantage of base62 is that it is URL-safe (as opposed to base64) due to the lack of special characters such as '/' or '='. Another key aspect is that the alphabetical orders of the original (unencoded) data is preserved when encoded. In other words, encoded data can be sorted without being decoded at all.

Installation

base62 can be installed via pypi. Unfortunately, the package name base62 on pypi had already been occupied by someone else, so we had to go by pybase62.

pip install pybase62

Alternatively, you may clone the code to manually install it.

git clone https://github.com/suminb/base62
cd base62 && python setup.py install

Usage

The following section describes a basic usage of base62.

>>> import base62

>>> base62.encode(34441886726)
'base62'

>>> base62.decode('base62')
34441886726

From version 0.2.0, base62 supports bytes array encoding as well.

>>> base62.encodebytes(b'\0')
0

>>> base62.encodebytes(b'\xff\xff')
H31

>>> base62.decodebytes('0')
b''

>>> base62.decodebytes('1')
b'\x01'

Tests

You may run some test cases to ensure all functionalities are operational.

py.test -v

If pytest is not installed, you may want to run the following commands:

pip install -r tests/requirements.txt

Deployment

Deploy a source package (to pypi) as follows:

python setup.py sdist upload

base62's People

Contributors

dhimmel avatar suminb avatar yeonghoey avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.