Title: Override Result Formatting for pretty and non-intrusive CLIs 🌈✨ · Issue #344 · google/python-fire · GitHub
Open Graph Title: Override Result Formatting for pretty and non-intrusive CLIs 🌈✨ · Issue #344 · google/python-fire
X Title: Override Result Formatting for pretty and non-intrusive CLIs 🌈✨ · Issue #344 · google/python-fire
Description: Fire's main benefit is that it can be dropped in and turn anything into a CLI! But sometimes there are certain types that either don't play nice with fire's formatters and some that just don't print out in a pretty way. What I'm proposin...
Open Graph Description: Fire's main benefit is that it can be dropped in and turn anything into a CLI! But sometimes there are certain types that either don't play nice with fire's formatters and some that just don't prin...
X Description: Fire's main benefit is that it can be dropped in and turn anything into a CLI! But sometimes there are certain types that either don't play nice with fire's formatters and some that jus...
Opengraph URL: https://github.com/google/python-fire/issues/344
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Override Result Formatting for pretty and non-intrusive CLIs 🌈✨","articleBody":"Fire's main benefit is that it can be dropped in and turn anything into a CLI! But sometimes there are certain types that either don't play nice with fire's formatters and some that just don't print out in a pretty way.\r\n\r\n**What I'm proposing:** Allow users to provide a function that can override result formatting. Simply, it takes the output of the fire call and passes it through the user formatter, and then the result that call is treated as the result of the original fire call. \r\n\r\n### Outputting tabular data:\r\n```python\r\nimport random\r\ndef data():\r\n return [\r\n {\r\n 'is_up': random.choice([False, True]), \r\n 'value_A': random.random(), \r\n 'value_B': random.random() + 100,\r\n 'id': random.randint(1000, 5000)} \r\n for i in range(8)\r\n ]\r\n\r\nimport fire\r\nfire.Fire(data)\r\n```\r\nOutputs this:\r\n```\r\n{\"is_up\": true, \"value_A\": 0.6004291859538904, \"value_B\": 100.77910907893889, \"id\": 474}\r\n{\"is_up\": false, \"value_A\": 0.1406617230697117, \"value_B\": 100.35721554966845, \"id\": 740}\r\n{\"is_up\": true, \"value_A\": 0.3612392830626744, \"value_B\": 100.60814663568802, \"id\": 509}\r\n{\"is_up\": false, \"value_A\": 0.11247653550250092, \"value_B\": 100.2673181440675, \"id\": 305}\r\n{\"is_up\": false, \"value_A\": 0.9505598630828166, \"value_B\": 100.84615141986525, \"id\": 85}\r\n{\"is_up\": true, \"value_A\": 0.17544933002396768, \"value_B\": 100.66062056951291, \"id\": 385}\r\n{\"is_up\": false, \"value_A\": 0.25245927587860695, \"value_B\": 100.75492369068093, \"id\": 923}\r\n{\"is_up\": true, \"value_A\": 0.9237200249249168, \"value_B\": 100.94228120845642, \"id\": 702}\r\n```\r\nBut if we can define a formatter.\r\n```python\r\nimport tabulate\r\n\r\ndef fancy_table(result):\r\n if not result: # show a message instead of an empty response\r\n return 'nada. sorry.'\r\n \r\n # display a list of dicts as a table\r\n if isinstance(result, (list, tuple)) and all(isinstance(x, dict) for x in result):\r\n return tabulate.tabulate([\r\n {col: cell_format(value) for col, value in row.items()}\r\n for row in result\r\n ], headers=\"keys\")\r\n\r\n return result # otherwise, let fire handle it\r\n\r\ndef cell_format(value, decimals=3, bool=('🌹', '🥀')):\r\n if value is True:\r\n return bool[0]\r\n if value is False:\r\n return bool[1]\r\n if isinstance(value, float):\r\n return '{:.{}f}'.format(value, decimals)\r\n return value\r\n\r\nfire.Fire(data, formatter=fancy_table)\r\n```\r\nOutputs this:\r\n```\r\nis_up value_A value_B id\r\n------- --------- --------- ----\r\n🌹 0.115 100.013 1821\r\n🌹 0.439 100.167 4242\r\n🥀 0.68 100.345 2937\r\n🥀 0.074 100.119 4675\r\n🌹 0.189 100.462 4571\r\n🌹 0.221 100.342 1522\r\n🌹 0.02 100.452 2363\r\n🥀 0.023 100.812 2433\r\n```\r\n\r\n**And to address the potential comment that you can do this already by wrapping the function:** yes that's true for simple cases, but if you start wrapping classes or a family of functions (`fire.Fire()` can call any defined function) it becomes less realistic and then `fire` needs wrappers for all of those functions which makes it much more cumbersome (I'm literally doing this in a project and it's such a pain). And that doesn't get into how much more difficult it'd be to wrap object methods that return other objects with their own methods, etc. And on top of that, wrappers mess with function signature inspection which is used by fire for command line help strings so it's problematic in many ways.\r\n\r\nThis is a very simple addition to fire that can supercharge the look of your CLI with very little effort and intervention in existing code!\r\n\r\nReal Use Cases that I've personally encountered:\r\n - Rest API class (A class that wraps a collection of API methods): provide API result data in pretty ways (e.g. like the table above!!). Right now I'm doing it manually by subclassing it and wrapping every single method (UGH!)\r\n - Keras models: they do not print out nicely when returned from Fire and I have a file full of functions that return them. It would be a big hassle to wrap them all when I just want to test a couple out quickly","author":{"url":"https://github.com/beasteers","@type":"Person","name":"beasteers"},"datePublished":"2021-07-07T19:20:18Z","url":"https://github.com/google/python-fire/issues/344"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:672e9f79-ba6f-672f-d0a5-2167cede5f7f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE02:7F150:1731BBB:1EA34AC:6B0019D8 |
| html-safe-nonce | ef2574d6088c7ba2b2fad4b94183a2554528cadddb0e58a4b522883f76f08e7d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTAyOjdGMTUwOjE3MzFCQkI6MUVBMzRBQzo2QjAwMTlEOCIsInZpc2l0b3JfaWQiOiI5MDI2OTAxNjkzMTU3MDg3NzA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 11ca07b62916a8ff07498e763a2e77e6519f31d55e131888949066ac957484f2 |
| hovercard-subject-tag | issue:939189637 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/google/python-fire/344/issue_layout |
| twitter:image | https://opengraph.githubassets.com/30c2e09cf38a45176f6284c7739068c163480306c1706409b4ca93e7a03c7a00/google/python-fire/issues/344 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/30c2e09cf38a45176f6284c7739068c163480306c1706409b4ca93e7a03c7a00/google/python-fire/issues/344 |
| og:image:alt | Fire's main benefit is that it can be dropped in and turn anything into a CLI! But sometimes there are certain types that either don't play nice with fire's formatters and some that just don't prin... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | beasteers |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/google/python-fire git https://github.com/google/python-fire.git |
| octolytics-dimension-user_id | 1342004 |
| octolytics-dimension-user_login | |
| octolytics-dimension-repository_id | 82729529 |
| octolytics-dimension-repository_nwo | google/python-fire |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 82729529 |
| octolytics-dimension-repository_network_root_nwo | google/python-fire |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width