This release is a pre-release and may not be stable for production use.
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.
NB: walrus currently can only support assignment expressions used in if statements.
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/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
-n, --no-archive do not archive original files
-p PATH, --archive-path PATH
path to archive original files (${CWD}/archive)
convert options:
compatibility configuration for none-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
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)
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)
Raises:
ConvertError-- whenparso.ParserSyntaxErrorraised
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)
Returns:
str-- converted string
Raises:
ConvertError-- whenparso.ParserSyntaxErrorraised
ConvertError -- walrus internal exception
class ConvertError(SyntaxError):
pass
Test
See test.py.
Contribution
Contributions are very welcome, especially fixing bugs and providing test cases. Note that code must remain valid and reasonable.
See Also
Metadata
Release files for walrus-converter 0.0.0.dev0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| walrus-converter-0.0.0.dev0.tar.gz | 16.9 kB | Without provenance | Details for walrus-converter-0.0.0.dev0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| walrus_converter-0.0.0.dev0-py3-none-any.whl | Python 3 | none | any | Details for walrus_converter-0.0.0.dev0-py3-none-any.whl |
Total release size: 28.8 kB
Release files / walrus-converter-0.0.0.dev0.tar.gz
| Download URL | walrus-converter-0.0.0.dev0.tar.gz |
|---|---|
| Size | 16.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e3880f6f2c92204681d392a8c120c5c6e693b28a3c6c3793c3f17ee94e3ab31
|
|
BLAKE2b-256 checksum How to use checksums |
235f60b848850af34068d788875db7bbd5c68ae145636e1e3466c0667a0f93b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
|
Release files / walrus_converter-0.0.0.dev0-py3-none-any.whl
| Download URL | walrus_converter-0.0.0.dev0-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90c250f6cad66e29692847d7e845c1e5267b5d47562266d09173fd127c7134d6
|
|
BLAKE2b-256 checksum How to use checksums |
40f7c04491d320b4d7ab3505e32a7ced934a832e65ed5f494f81232f7ff64de4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
|
Security for walrus-converter 0.0.0.dev0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.