Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best method to get the most human-readable names for units? #1989

Open
dakotahorstman opened this issue May 15, 2024 · 0 comments

Comments

@dakotahorstman
Copy link

Like the title asks, I'm looking to generate a full list of the most human-readable names for every unit Pint supports.

I currently have this:

def snake_to_readable(snake_str: str) -> str:
    """Convert snake_case to Human Readable names"""
    return " ".join(word.capitalize() for word in snake_str.split("_"))


def get_unit_choices() -> List[Tuple[str, str]]:
    all_units = [unit for unit in dir(_ureg) if isinstance(getattr(_ureg, unit), Unit)]

    unit_choices = []
    for unit in all_units:
        if not unit.startswith("_"):
            unit_obj = _ureg[unit]
            unit_symbol = unit_obj.units
            human_readable_name = f"{snake_to_readable(unit)} ({unit_symbol})"
            unit_choices.append((unit, human_readable_name))

    return unit_choices

which outputs something like the following:
image

It's not bad, but also not the most readable sometimes, especially when you have single-letter acronyms.

Any suggestions to make this better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant