Back-port compiler for Python 3.8 assignment expressions.
Project description
walrus
Write assignment expressions in Python 3.8 flavour, and let
walrusworry about back-port issues :beer:
Since PEP 572, Python introduced assignment expressions
syntax in version 3.8. For those who wish to use assignment expressions in their code, walrus provides an
intelligent, yet imperfect, solution of a backport compiler by replacing assignment expressions syntax with
old-fashioned assignment-then-conditional syntax, which guarantees you to always write assignment expressions in
Python 3.8 flavour then compile for compatibility later.
Installation
Note that
walrusonly supports Python versions since 3.3 🐍
For macOS users, walrus is now available through Homebrew:
brew tap jarryshaw/tap
brew install walrus
# or simply, a one-liner
brew install jarryshaw/tap/walrus
Simply run the following to install the current version from PyPI:
pip install python-walrus
Or install the latest version from the git repository:
git clone https://github.com/pybpc/walrus.git
cd walrus
pip install -e .
# and to update at any time
git pull
Basic Usage
CLI
It is fairly straightforward to use walrus:
context in
${...}changes dynamically according to runtime environment
usage: walrus [options] <python source files and folders...>
Back-port compiler for Python 3.8 assignment expressions.
positional arguments:
SOURCE python source files and folders to be converted (${CWD})
optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-q, --quiet run in quiet mode
archive options:
duplicate original files in case there's any issue
-na, --no-archive do not archive original files
-p PATH, --archive-path PATH
path to archive original files (${CWD}/archive)
convert options:
compatibility configuration for non-unicode files
-c CODING, --encoding CODING
encoding to open source files (${LOCALE_ENCODING})
-v VERSION, --python VERSION
convert against Python version (${LATEST_VERSION})
-s SEP, --linesep SEP
line separator to process source files (${OS_LINESEP})
-nl, --no-linting do not lint converted codes
-t INDENT, --tabsize INDENT
indentation tab size (4)
walrus will read then convert all assignment expressions syntax in every Python
file under this path. In case there might be some problems with the conversion, walrus will
duplicate all original files it is to modify into archive directory ahead of the process,
if -n not set.
Besides, to keep consistency of API to users, walrus ships with a decorator for
such functions to check assignment expressions at runtime, if -nl not set.
Developer Reference
Environments
walrus currently supports three environment arguments:
WALRUS_QUIET-- run in quiet mode (same as--quietoption in CLI)WALRUS_VERSION-- convert against Python version (same as--pythonoption in CLI)WALRUS_ENCODING-- encoding to open source files (same as--encodingoption in CLI)WALRUS_LINESEP-- line separator to process source files (same as--linesepoption in CLI)WALRUS_LINTING-- lint converted codes (same as--lintingoption in CLI)WALRUS_TABSIZE-- indentation tab size (same as--tabsizeoption in CLI)
APIs
walrus -- wrapper works for conversion
walrus(filename)
Args:
filename--str, file to be converted
Envs:
WALRUS_QUIET-- run in quiet mode (same as--quietoption in CLI)WALRUS_ENCODING-- encoding to open source files (same as--encodingoption in CLI)WALRUS_VERSION-- convert against Python version (same as--pythonoption in CLI)WALRUS_LINESEP-- line separator to process source files (same as--linesepoption in CLI)WALRUS_LINTING-- lint converted codes (same as--lintingoption in CLI)WALRUS_TABSIZE-- indentation tab size (same as--tabsizeoption in CLI)
Raises:
ConvertError-- when source code contains syntax errors
convert -- the main conversion process
convert(string, source='<unknown>')
Args:
string--str, context to be convertedsource--str, source of the context
Envs:
WALRUS_VERSION-- convert against Python version (same as--pythonoption in CLI)WALRUS_LINESEP-- line separator to process source files (same as--linesepoption in CLI)WALRUS_LINTING-- lint converted codes (same as--lintingoption in CLI)WALRUS_TABSIZE-- indentation tab size (same as--tabsizeoption in CLI)
Returns:
str-- converted string
Raises:
ConvertError-- when source code contains syntax errors
Internal exceptions
class ConvertError(SyntaxError):
"""Parso syntax error."""
class ContextError(RuntimeError):
"""Missing conversion context."""
class EnvironError(EnvironmentError):
"""Invalid environment."""
Test
See tests folder.
Known issues
Since walrus is currently based on parso project,
it had encountered several compatibility and parsing issues.
-
Parsing f-strings with format spec beginning withThis issue has been resolved since=produces incorrect SyntaxError (#89)parsoversion 0.5.2. -
Parsing invalid use cases of assignment expressions do not raise SyntaxError (#89)This issue has been resolved sinceparsoversion 0.5.2.
Contribution
Contributions are very welcome, especially fixing bugs and providing test cases. Note that code must remain valid and reasonable.
See Also
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-walrus-0.1.4.tar.gz.
File metadata
- Download URL: python-walrus-0.1.4.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff014752e4e5cfabb8b3b7727d3c31037ff0893fcab52fbe17b6135a5da07e0d
|
|
| MD5 |
d7de2e3ec6b573c01caa5850a1c20f6f
|
|
| BLAKE2b-256 |
14cdfc6e92adc09d541a0e46e793960b6b4497af7ea1a2e5b9f1a36d29ad10ab
|
File details
Details for the file python_walrus-0.1.4-py3-none-any.whl.
File metadata
- Download URL: python_walrus-0.1.4-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e21e43b1bddb0600b8effb6e00b095b05c66c2e8b7ebcec391cdbd20a1413faa
|
|
| MD5 |
c80ba76a903b35c7e6e04441ee1b74d9
|
|
| BLAKE2b-256 |
11a0794daa2262ac85b50c4f00656cb4eecad5a10802e0ec056c13a22d5ba06e
|