murphyqm / pytesimal

Model the conductive cooling of small planetary bodies with temperature-dependent material properties
MIT License
1 stars 0 forks source link

Restructure for packaging #35

Closed andreww closed 3 years ago

andreww commented 3 years ago

This issue can collect together the various bits and bobs that need thinking about to turn the current repository into something that is a python package. We can link to more detailed issues as needed. Anyway, I there are the following basic tasks:

  1. Restructure the repository. https://docs.python-guide.org/writing/structure/ starts off sensibly but gets a bit bogged down towards the end (in that page, the package name is intended to be "sample"). Basically most of the python files should be in a directory called pytesimal, with the tests in a separate directory called tests (and the notebooks probably in a third directory called notebooks, or in with the documents).
  2. Allow imports like pytesimal.conductive_cooling or pytesimal.modular_cond_cooling.conductive_cooling. This will need a new file (called __init__.py) in the pytesimal directory. This turns the directory into a python package. The file is often empty, but anything in that file can be accessed by doing import pytesimal. What is quite common is to import the most commonly used functions into that file. Probably worth thinking about what we want the externally facing API to look like.
  3. The first two steps will need some modification to notebooks and tests so that the code can be found (and imported) without installing the package. There is a nice example of how to do this (search for "tests/context.py" in the web page above.
  4. Make it possible to install the package. This means adding a file called setup.py. What this will do (roughly) is copy the stuff in the pytesimal directory into somewhere on the users system that exists in the python path.

I've probably forgotten something else too.

andreww commented 3 years ago

An example of how to deal with temporary directories:https://github.com/andreww/lema/blob/master/tests/hc_tests.py (But this uses unittest, maybe pytest does something similar?)

andreww commented 3 years ago

@andreww to look at context module in tests - how should this work?

murphyqm commented 3 years ago

additional to-do:

  1. If Python version is being fixed above 3.6, can make full use of pathlib instead of inspect, os etc. Instead of trying to figure out relative paths from inside modules (possibly different working directories? - check with some thoughtfully placed print(pathlib.Path.cwd()) statements), figure out file paths at start (in set up or testing, depending on use) and pass these in as strings into different functions and objects. This will simplify testing filepaths too - if filepaths are set up all together at the start, won't have the issues of duplicating the output_runs\ directory within both the tests\ and pytesimal\ directories.

Also, the tempfile module used in above example is independent of unittest; looking at pytest alternatives to setup and teardown methods - fixtures? Useful for testing (#32 ) and reusing data from time-consuming timestepping runs. Still need something like this before and after testing:

def setUp(self):
        # Create a temporary directory for each test
        self.test_dir = tempfile.mkdtemp()
        self.starting_dir = os.getcwd()
        os.chdir(self.test_dir)

def tearDown(self):
        # Remove the directory after each test
        os.chdir(self.starting_dir)
        shutil.rmtree(self.test_dir)

Not sure outside of the unittest TestCase framework how to force the order of these - eg if tests are in a variety of test_something.py files, how can you get the setup function to run before anything, and the teardown function to run afterwards - even if something fails? Todo - look this up. I guess alternative is just to have these in each module with tests - maybe this is best practise as it allows tests to be run independently.

andreww commented 3 years ago

Actual packaging probably needs to be in an issue by itself. But for the record documentation is at https://packaging.python.org/

murphyqm commented 3 years ago

File restructuring done, migrating to packaging issue https://github.com/murphyqm/pytesimal/issues/39