Skip to main content

Back-port compiler for Python 3.8 assignment expressions.

Project description

walrus

PyPI - Downloads PyPI - Version PyPI - Format PyPI - Python Version

Travis CI - Status Codecov - Coverage License LICENSE

Write assignment expressions in Python 3.8 flavour, and let walrus worry 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 walrus only 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/JarryShaw/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 --quiet option in CLI)
  • WALRUS_VERSION -- convert against Python version (same as --python option in CLI)
  • WALRUS_ENCODING -- encoding to open source files (same as --encoding option in CLI)
  • WALRUS_LINESEP -- line separator to process source files (same as --linesep option in CLI)
  • WALRUS_LINTING -- lint converted codes (same as --linting option in CLI)
  • WALRUS_TABSIZE -- indentation tab size (same as --tabsize option 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 --quiet option in CLI)
  • WALRUS_ENCODING -- encoding to open source files (same as --encoding option in CLI)
  • WALRUS_VERSION-- convert against Python version (same as --python option in CLI)
  • WALRUS_LINESEP -- line separator to process source files (same as --linesep option in CLI)
  • WALRUS_LINTING -- lint converted codes (same as --linting option in CLI)
  • WALRUS_TABSIZE -- indentation tab size (same as --tabsize option 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 converted
  • source -- str, source of the context

Envs:

  • WALRUS_VERSION -- convert against Python version (same as --python option in CLI)
  • WALRUS_LINESEP -- line separator to process source files (same as --linesep option in CLI)
  • WALRUS_LINTING -- lint converted codes (same as --linting option in CLI)
  • WALRUS_TABSIZE -- indentation tab size (same as --tabsize option 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 := formatter produces incorrect SyntaxError (#89)

  • Parsing invalid use cases of assignment expressions do not raise SyntaxError (#89)

Contribution

  Contributions are very welcome, especially fixing bugs and providing test cases. Note that code must remain valid and reasonable.

See Also

Project details


Download files

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

Source Distribution

python-walrus-0.1.3.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

python_walrus-0.1.3-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file python-walrus-0.1.3.tar.gz.

File metadata

  • Download URL: python-walrus-0.1.3.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5

File hashes

Hashes for python-walrus-0.1.3.tar.gz
Algorithm Hash digest
SHA256 00fdbabd77b52604980811fd968891f57558c1a9861c517ed0cdd39193e11e64
MD5 ca5daf04694537a7cf8899d56a8607f1
BLAKE2b-256 e650d4bd2003f0ac449eec56b36e2d7ca5b38575d58db566cda9bb053baf1734

See more details on using hashes here.

File details

Details for the file python_walrus-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: python_walrus-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.5

File hashes

Hashes for python_walrus-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 060f938358e5a4b3a2b5fac61f71363df09e672dc14417d515e906357d1981af
MD5 1f9b137e9f67faa7a5dd45ec3cf45143
BLAKE2b-256 b18a1e04c8ec310f132ae70d5cc4d622e4bd88b4cfa0a817e237fb7962aa8ffa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page