Skip to main content

Pure python implementation of the BiDi layout algorithm

Project description

Bi-directional (BiDi) layout implementation in pure python

Package documentation


The algorithm starts with a single entry point bidi.algorithm.get_display.

Required arguments:

  • unicode_or_str: The orginal unicode or string (i.e: storage). If it’s a string use the optional argument encoding to specify it’s encoding.

Optional arguments:

  • encoding: If unicode_or_str is a string, specifies the encdoing. The algorithm uses unicodedata which requires unicode. This encoding will be used to decode and encode back to string before returning (default: “utf-8”).
  • upper_is_rtl: True to treat upper case chars as strong ‘R’ for debugging (default: False).
  • base_dir: ‘L’ or ‘R’, override the calculated base_level.
  • debug: True to display (using sys.stderr) the steps taken with the algorithm (default: False).

Returns the display layout, either as unicode or encoding encoded string (depending on the type of unicode_or_str').


>>> from bidi.algorithm import get_display
>>> get_display(u'car is THE CAR in arabic', upper_is_rtl=True)
u'car is RAC EHT in arabic'


pybidi is a command line utility (calling bidi.main) for running the bidi algorithm. the script can get a string as a parameter or read text from stdin. Usage:

$ pybidi -h
Usage: pybidi [options]

  -h, --help            show this help message and exit
  -e ENCODING, --encoding=ENCODING
                        Text encoding (default: utf-8)
  -u, --upper-is-rtl    treat upper case chars as strong 'R' for debugging
                        (default: False).
  -d, --debug           Output to stderr steps taken with the algorithm
  -b BASE_DIR, --base-dir=BASE_DIR
                        Override base direction [L|R]


$ pybidi -u 'car is THE CAR in arabic'
car is RAC EHT in arabic

$ cat ~/Documents/example.txt | pybidi


See docs/INSTALL.rst

Running tests

To run the tests:

python test

Some explicit tests are failing right now (see TODO)



  • Move to cookiecutter template
  • Python 3 support (py2.6, 2.7, 3.3, 3.4 and pypy)
  • Better docs
  • Travis integration
  • Tox tests
  • PEP8 cleanup


  • Remove extra newline in console script output


  • Implement overriding base paragraph direction
  • Allow overriding base direction in pybidi console script
  • Fix returning display in same encoding


  • Test for surrogate pairs
  • Fix indentation in documentations
  • Specify license in


  • Added missing description
  • docs/INSTALL.rst


  • Apply bidi mirroring
  • Move to back function based implementation


  • Move the algorithm to a class based implementation


  • Initial release

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
python_bidi-0.4.0-py2.py3-none-any.whl (17.8 kB) Copy SHA256 hash SHA256 Wheel 3.4
python-bidi-0.4.0.tar.gz (38.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page