René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:672e9f79-ba6f-672f-d0a5-2167cede5f7f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBE02:7F150:1731BBB:1EA34AC:6B0019D8
html-safe-nonceef2574d6088c7ba2b2fad4b94183a2554528cadddb0e58a4b522883f76f08e7d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTAyOjdGMTUwOjE3MzFCQkI6MUVBMzRBQzo2QjAwMTlEOCIsInZpc2l0b3JfaWQiOiI5MDI2OTAxNjkzMTU3MDg3NzA0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac11ca07b62916a8ff07498e763a2e77e6519f31d55e131888949066ac957484f2
hovercard-subject-tagissue:939189637
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/google/python-fire/344/issue_layout
twitter:imagehttps://opengraph.githubassets.com/30c2e09cf38a45176f6284c7739068c163480306c1706409b4ca93e7a03c7a00/google/python-fire/issues/344
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/30c2e09cf38a45176f6284c7739068c163480306c1706409b4ca93e7a03c7a00/google/python-fire/issues/344
og:image:altFire'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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamebeasteers
hostnamegithub.com
expected-hostnamegithub.com
Nonea2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4
turbo-cache-controlno-preview
go-importgithub.com/google/python-fire git https://github.com/google/python-fire.git
octolytics-dimension-user_id1342004
octolytics-dimension-user_logingoogle
octolytics-dimension-repository_id82729529
octolytics-dimension-repository_nwogoogle/python-fire
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id82729529
octolytics-dimension-repository_network_root_nwogoogle/python-fire
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release70121d4e855ee0bb7b75cf15d061d91f8863c0f8
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/google/python-fire/issues/344#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogle%2Fpython-fire%2Fissues%2F344
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogle%2Fpython-fire%2Fissues%2F344
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=google%2Fpython-fire
Reloadhttps://github.com/google/python-fire/issues/344
Reloadhttps://github.com/google/python-fire/issues/344
Reloadhttps://github.com/google/python-fire/issues/344
googlehttps://github.com/google
python-firehttps://github.com/google/python-fire
Codehttps://github.com/google/python-fire
Issues146 (146)https://github.com/google/python-fire/issues
Pull requests85 (85)https://github.com/google/python-fire/pulls
Actionshttps://github.com/google/python-fire/actions
Projectshttps://github.com/google/python-fire/projects
Security and qualityhttps://github.com/google/python-fire/security
Insightshttps://github.com/google/python-fire/insights
https://github.com/google/python-fire
New issuehttps://github.com/login?return_to=https://github.com/google/python-fire/issues/344
New issuehttps://github.com/login?return_to=https://github.com/google/python-fire/issues/344
#345https://github.com/google/python-fire/pull/345
Override Result Formatting for pretty and non-intrusive CLIs 🌈✨https://github.com/google/python-fire/issues/344#top
#345https://github.com/google/python-fire/pull/345
https://github.com/beasteers
beasteershttps://github.com/beasteers
on Jul 7, 2021https://github.com/google/python-fire/issues/344#issue-939189637
beasteershttps://github.com/beasteers
on Aug 18, 2021https://github.com/google/python-fire/issues/344#event-5177623819
dbieberhttps://github.com/dbieber
completedhttps://github.com/google/python-fire/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#345https://github.com/google/python-fire/pull/345
on Apr 16, 2022https://github.com/google/python-fire/issues/344#event-6443836807
dbieberhttps://github.com/dbieber
on Apr 16, 2022https://github.com/google/python-fire/issues/344#event-6443836829
Add custom formatter for Fire result (#345)https://github.com/google/python-fire/commit/8bddeec6bd9c78b9b5ad20e9a58625cbd3096fe9
#345https://github.com/google/python-fire/pull/345
https://docs.github.com/articles/closing-issues-via-commit-messages
8bddeechttps://github.com/google/python-fire/commit/8bddeec6bd9c78b9b5ad20e9a58625cbd3096fe9
https://github.com/dbieber
dbieberhttps://github.com/dbieber
on Apr 16, 2022https://github.com/google/python-fire/issues/344#issuecomment-1100756780
Sign up for freehttps://github.com/signup?return_to=https://github.com/google/python-fire/issues/344
Sign in to commenthttps://github.com/login?return_to=https://github.com/google/python-fire/issues/344
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.