Python BiDi
Bi-directional (BiDi) layout for Python providing 2 implementations:
V5 of the algorithm implemented with Python.
Wrapper the unicode-bidi Rust crate.
For the python implementation, and compatible with previous versions, use:
from bidi.algorithm import get_display
For the newer Rust based one, which seems to implement higher version of the algorithm (albeit with some missing see #25), use the top level import:
from bidi import get_display
API
The algorithm starts with a single entry point get_display (see above for selecting the implementaion).
Required arguments:
str_or_bytes: The string or bytes (i.e.: storage). If it’s bytes use the optional argument encoding to specify it’s encoding.
Optional arguments:
encoding: If unicode_or_str is a string, specifies the encoding. The algorithm uses unicodedata which requires unicode. This encoding will be used to decode and encode back to string before returning (default: “utf-8”).
base_dir: 'L' or 'R', override the calculated base_level.
debug: True to display the Unicode levels as seen by the algorithm (default: False).
The Python implementaion adds one more optional argument:
upper_is_rtl: True to treat upper case chars as strong ‘R’ for debugging (default: False).
It returns the display layout, either as str or encoding encoded bytes (depending on the type of str_or_bytes').
Example:
>>> from bidi import get_display >>> # keep as list with char per line to prevent browsers from changing display order >>> HELLO_HEB = "".join([ ... "ש", ... "ל", ... "ו", ... "ם" ... ]) >>> >>> HELLO_HEB_DISPLAY = "".join([ ... "ם", ... "ו", ... "ל", ... "ש", ... ]) >>> >>> get_display(HELLO_HEB) == HELLO_HEB_DISPLAY True
CLI
pybidi is a command line utility (calling bidi.main) for running the display algorithm. The script can get a string as a parameter or read text from stdin.
Usage:
$ pybidi -h
usage: pybidi [-h] [-e ENCODING] [-u] [-d] [-b {L,R}] [-r] [-v]
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), Ignored in Rust algo
-d, --debug Output to stderr steps taken with the algorithm
-b {L,R}, --base-dir {L,R}
Override base direction [L|R]
-r, --rust Use the Rust unicode-bidi implemention instead of the Python one
-v, --version show program's version number and exit
Examples:
$ pybidi -u 'Your string here' $ cat ~/Documents/example.txt | pybidi
Installation
From PyPI:
pip install python-bidi
Or with uv:
uv pip install python-bidi
Development / running tests
You need Rust (for maturin), uv, and a nox session backend of uv (nox reads this from noxfile.py).
From a clone, create the environment and run the test suite:
uv sync --extra dev uv run pytest
Or use nox (after uv sync --extra dev, or install nox with uv tool install nox / pipx install nox):
nox
Metadata
Release files for python-bidi-android 0.6.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_bidi_android-0.6.11.tar.gz | 55.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_bidi_android-0.6.11-cp313-cp313-android_24_arm64_v8a.whl | CPython 3.13 | CPython 3.13 | Android API level 24+ ARM64 v8a | Details |
Total release size: 327.2 kB
Release files / python_bidi_android-0.6.11.tar.gz
| Download URL | python_bidi_android-0.6.11.tar.gz |
|---|---|
| Size | 55.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24a47e1330297d886598851affab0152d0f220a09edc317deab7e070d6536360
|
|
BLAKE2b-256 checksum How to use checksums |
3b3b6b5f4ccc11d6df32bc8aec555ce9c7baa476abfdb9e39d241c34dd19876e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.15.0
|
Release files / python_bidi_android-0.6.11-cp313-cp313-android_24_arm64_v8a.whl
| Download URL | python_bidi_android-0.6.11-cp313-cp313-android_24_arm64_v8a.whl |
|---|---|
| Size | 271.8 kB |
| Tags | Android API level 24+ ARM64 v8a CPython 3.13 |
|
SHA-256 checksum How to use checksums |
d615bbf4ec02cdc1888086f60cc5de13c0a91c376d37916672104375d4bd52f9
|
|
BLAKE2b-256 checksum How to use checksums |
a096ba6d00fb8992d87897f2e6b779aaff82a3aba4c42814bf6ce559824c84ef
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.15.0
|