Package: table-format
table-formatFormat Python code (list of lists) as a fixed width table.Motivation and usageYou've got some tests or other code like this:deftest_the_table():assertgenerate_the_table()==[['Date','Description','Amount','Balance'],['2021-04-06','Account opened',0,0],['2021-04-07','Cash deposit at 123 Main St, Newtown',1000,1000],['2021-04-08','ACME anvils',-300,700],]Or maybe even worse like this:assertgenerate_the_table()==[["Date","Description","Amount","Balance",],["2021-04-06","Account opened",0,0,],["2021-04-07","Cash deposit at 123 Main St, Newtown",1000,1000,],["2021-04-08","ACME anvils",-300,700,],]Wouldn't it be nice to have readable tests with columns all lined up?Copy the whole list of lists to the clipboard, then pipe totable-format --guess-indent:on Linux you can usexselorxclipon MacOS you can usepbpasteon Windows Powershell you can useGet-ClipboardLinux example:$xsel|table-format--guess-indent[['Date','Description','Amount','Balance'],['2021-04-06','Account opened',0,0],['2021-04-07','Cash deposit at 123 Main St, Newtown',1000,1000],['2021-04-08','ACME anvils',-300,700],]The output should be ready to paste back into your editor.OptionsPass the--helpflag to show all options:$table-format--help⬇️ InstallationThe most recent release can be installed fromPyPIwith:$pipinstalltable-formatYou can also usepipxto install it more
conveniently in an isolated environment:$pipxinstalltable-formatSeeCHANGESfor changelog.The most recent code and data can be installed directly from GitHub with:$pipinstallgit+https://github.com/spookylukey/table-format.gitTo install in development mode, use the following:$gitclonegit+https://github.com/spookylukey/table-format.git
$cdtable-format
$pipinstall-e.Other tipsTuplesWe also support having lists of tuples as well as lists of lists, and this will be preserved.BlackBlack will revert code formatted like this. But you can use# fmt: offand# fmt: oncommands to stop black reformatting these blocks.To format quotes more similarly to Black style, use--quote-style=double(this won’t be exactly the same as Black, it just switches to double quotes
instead of single).Flake8Flake8 might complain about:E202 - whitespace before]E241 - multiple spaces after commaE501 - line too longThe simplest solution is to silence those flake8 errors globally in your config,
especially if you are using a formatter like Black which will enforce those same
rules for you automatically.Otherwise, you can silence the errors on a per-line basis usingnoqa:E202,E241,E501, which you can automatically add to the end of each line
like this:$table-format--add-noqa=E202,E241,E501Othernoqaitems and other end of line comments will be preserved.EmacsWith default keybindings, doingC-uM-|table-format --guess-indentENTERwill
replace the current region with the formatted version fromtable-format.You can wrap it up in a nice function like this:(defunalign-python-table()(interactive)(shell-command-on-region;; beginning and end of region(region-beginning)(region-end);; command and parameters"table-format --guess-indent";; output buffer(current-buffer);; replace?t;; name of the error buffer"*Table-Format Error Buffer*";; show error buffer?t))Other editorsContributions of instructions to make this easy to use in other editors are very
welcome!⚖️ LicenseThe code in this package is licensed under the MIT License.🙏 ContributingContributions, whether filing an issue, making a pull request, or forking, are appreciated. SeeCONTRIBUTING.rstfor more information on getting
involved.🛠️ DevelopmentThe final section of the README is you for if you want to get involved by making
a code contribution.❓ TestingAfter cloning the repository, install dev requirements:pipinstall-rrequirements-dev.txtAnd run pytest:pytestTo test across all environments:$toxAdditionally, these tests are automatically re-run with each commit in aGitHub
Action.🍪 Cookiecutter AcknowledgementThis package was created with@audreyfeldroy'scookiecutterpackage using@cthoyt'scookiecutter-snekpacktemplate.