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

Rename em_keyboard package to em and refactor #166

Merged
merged 9 commits into from
Jan 29, 2025
Merged

Rename em_keyboard package to em and refactor #166

merged 9 commits into from
Jan 29, 2025

Conversation

hugovk
Copy link
Owner

@hugovk hugovk commented Jan 23, 2025

The __main__ allows you to run python -m em_keyboard, making it easier to run/test with another interpreter.

Another reason is to mirror my other CLIs (for example, pypistats, norwegianblue, pepotron, linkotron and tinytext), making it easier to diff/compare and maintain.

Because em_keyboard doesn't have a public API, perhaps we could move most of __init__.py into cli.py as well?

@hugovk hugovk added the changelog: Changed For changes in existing functionality label Jan 23, 2025
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.85%. Comparing base (d8334bd) to head (daa0b6f).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #166      +/-   ##
==========================================
- Coverage   95.56%   92.85%   -2.71%     
==========================================
  Files           2        3       +1     
  Lines         203      126      -77     
==========================================
- Hits          194      117      -77     
  Misses          9        9              
Flag Coverage Δ
macos-latest 91.26% <100.00%> (-1.35%) ⬇️
ubuntu-latest 91.26% <97.36%> (-1.84%) ⬇️
windows-latest 91.26% <100.00%> (-1.35%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hugovk
Copy link
Owner Author

hugovk commented Jan 23, 2025

@hukkin What do you think of this?

@hukkin
Copy link
Contributor

hukkin commented Jan 23, 2025

I like this!

Something to also consider is making cli.main signature as follows

def main(args: Sequence[str]) -> int:
   ...

and then in __main__.py have:

def main() -> NoReturn:
    exit_code = em_keyboard.cli.main(sys.argv[1:])
    raise SystemExit(exit_code)


if __name__ == "__main__":
    main()

Oh and pyproject.toml needs to export __main__:main then.

The benefit of this is that it's very easy to test cli.main, as there's no mocking to be done to pass in arguments. Also the returned integer can be more convenient to check than a raised exception.

@hukkin
Copy link
Contributor

hukkin commented Jan 23, 2025

Another thing to consider: rename the import package as em.

Then one could do python -m em grinning_face. A lot nicer than with the _keyboard suffix.

@hugovk
Copy link
Owner Author

hugovk commented Jan 23, 2025

Yes, that reminds me of this pattern: https://pythontest.com/testing-argparse-apps/

I used something similar for python -m random and it made testing much easier: https://github.com/python/cpython/blob/225296cd5b505c180d3f45c355b43d7e1d99d3d5/Lib/test/test_random.py#L1401 and https://github.com/python/cpython/blob/225296cd5b505c180d3f45c355b43d7e1d99d3d5/Lib/random.py#L1003

The mocking is a pain, will be good to remove that.

And yes, em as the package makes sense.

@hugovk hugovk changed the title Refactor __init__ into cli and __main__ Rename em_keyboard package to em and refactor Jan 23, 2025
@hugovk
Copy link
Owner Author

hugovk commented Jan 23, 2025

OK, ready!

pyproject.toml Outdated
@@ -6,7 +6,7 @@ requires = [
]

[project]
name = "em-keyboard"
name = "em"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this we can't change as it'd try to change distribution name. em seems reserved on PyPI.

It seems though that the project is archived, probably abandoned, so it may be possible to request removal for use in this project.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, of course, reverted.

I'll email them asking if they would like to transfer the name :)

@hukkin
Copy link
Contributor

hukkin commented Jan 23, 2025

This is much nicer than before!

@hugovk hugovk merged commit e939829 into main Jan 29, 2025
54 checks passed
@hugovk hugovk deleted the refactor branch January 29, 2025 14:54
@hugovk
Copy link
Owner Author

hugovk commented Jan 29, 2025

Thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants