cheran-senthil / PyRival

⚡ Competitive Programming Library
https://pyrival.readthedocs.io/
Apache License 2.0
1.17k stars 312 forks source link

Simplify imports #39

Closed bjorn-martinsson closed 4 years ago

bjorn-martinsson commented 4 years ago

Currently PyRival uses a somewhat complicated import system

  1. When adding/modifying a python file, you have to update the corresponding __init__.py in a kind of complicated way.
  2. Every file in PyRival is imported on doing any kind of import in PyRival. This means that we can't do costly calculations on imports of a certain file, since that would slow down imports of other files.
  3. The PyRival namespace is currently mixed with a lot of functions. I feel we should try to keep all of the files more separate.

I propose fixing this by just having a single __init__.py file in the pyrival package containing a modification to __path__, allowing all .py files in pyrival/... to be reachable by doing import pyrival.insert_filename_here. This greatly simplifies PyRival, while also fixing all of the 3 issues above.

With these changes it is possible to do things like

@Mukundan314 and @cheran-senthil what do you think about this? I think something like this would definitely be an improvement.

bjorn-martinsson commented 4 years ago

For some reason the automatic tests seem to break from this, but I can run things locally without any import errors. Really not sure what the issue is.

Mukundan314 commented 4 years ago

The ordersort tests are still using the old import format.

Edit: Fixed it

bjorn-martinsson commented 4 years ago

The test are failing because your import syntax is wrong for the gcd test (fixed it)

I'm seeing errors like this under "tox run"

ImportError while importing test module '/home/runner/work/PyRival/PyRival/tests/algebra/test_chinese_remainder.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/algebra/test_chinese_remainder.py:3: in <module>
    from pyrival.chinese_remainder import *
E   ModuleNotFoundError: No module named 'pyrival.chinese_remainder'

I'm really not sure why I'm getting these on tox. Locally running the imports does not cause any errors.