Package: python-walrus
walrusWriteassignment expressionsin Python 3.8 flavour, and letwalrusworry about back-port issues :beer:SincePEP 572, Python introducedassignment expressionssyntax in version3.8. For those who wish to useassignment expressionsin their code,walrusprovides an
intelligent, yet imperfect, solution of abackport compilerby replacingassignment expressionssyntax with
old-fashioned assignment-then-conditional syntax, which guarantees you to always writeassignment expressionsin
Python 3.8 flavour then compile for compatibility later.InstallationNote thatwalrusonly supports Python versionssince 3.3🐍For macOS users,walrusis now available throughHomebrew:brewtapjarryshaw/tap
brewinstallwalrus# or simply, a one-linerbrewinstalljarryshaw/tap/walrusSimply run the following to install the current version from PyPI:pipinstallpython-walrusOr install the latest version from the git repository:gitclonehttps://github.com/pybpc/walrus.gitcdwalrus
pipinstall-e.# and to update at any timegitpullBasic UsageCLIIt is fairly straightforward to usewalrus:context in${...}changes dynamically according to runtime environmentusage: 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)walruswill read then convert allassignment expressionssyntax in every Python
file under this path. In case there might be some problems with the conversion,walruswill
duplicate all original files it is to modify intoarchivedirectory ahead of the process,
if-nnot set.Besides, to keep consistency of API to users,walrusships with adecoratorfor
such functions to check assignment expressions at runtime, if-nlnot set.Developer ReferenceEnvironmentswalruscurrently 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)APIswalrus-- wrapper works for conversionwalrus(filename)Args:filename--str, file to be convertedEnvs: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 errorsconvert-- the main conversion processconvert(string,source='<unknown>')Args:string--str, context to be convertedsource--str, source of the contextEnvs: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 stringRaises:ConvertError-- when source code contains syntax errorsInternal exceptionsclassConvertError(SyntaxError):"""Parso syntax error."""classContextError(RuntimeError):"""Missing conversion context."""classEnvironError(EnvironmentError):"""Invalid environment."""TestSeetestsfolder.Known issuesSincewalrusis currently based onparsoproject,
it had encountered several compatibility and parsing issues.Parsing f-strings with format spec beginning with=produces incorrect SyntaxError (#89)This issue has been resolved sinceparsoversion0.5.2.Parsing invalid use cases of assignment expressions do not raise SyntaxError (#89)This issue has been resolved sinceparsoversion0.5.2.ContributionContributions are very welcome, especially fixing bugs and providing test cases.
Note that code must remain valid and reasonable.See Alsopybpc(formerly known aspython-babel)f2formatposeurvermin