harshraj22 / rl_lab

Contains Solutions of Lab assignments of Reinforcement Learning lab
0 stars 0 forks source link

Structuring Python Projects #18

Open harshraj22 opened 2 years ago

harshraj22 commented 2 years ago

Add a wiki page on structuring Python Projects. Adding details like limiting number of .py files in the top directory, separating concerns across directories, preferring to change python search path to import from sibling directories etc

references: [1], [2]

harshraj22 commented 2 years ago

Imports

https://github.com/harshraj22/rl_lab/blob/20527d8734c02e2a1059cf9aa7cfab6d6ceab860/lab4/models/q_learning.py#L5

Instead of using sys.path.append or modifying PYTHONPATH to include files from other directory, one should prefer using setup.py: python3 setup.py develop

References: 1 2 3 4

To uninstall: python3 setup.py develop --uninstall1


Use:

from setuptools import setup, find_packages

setup(package_dir={'': 'src'})

in case of directory structure like:

.
├── setup.py
├── src
│   └── package
│       └── utils.py
│       └── __init__.py
└── tests
    └── test_module.py

and the test_module.py can contain imports like:

from package.utils import utility_func
harshraj22 commented 2 years ago

Configs and Command Line Args

Tools like Hydra and Omegaconf make it clean and easy to manage configurations compared to Argparse (which makes code hard to read)

Here's some details on how to enforce type checking with Omegaconf

harshraj-wadhwani commented 2 years ago

Another good coding standards guide

the hitchhiker

harshraj-wadhwani commented 2 years ago

Logging

Use a logging configuration to set a system wide logging instead of repeating logger's code in each file

reference, 1

A good answer to using multiple loggers across files here

pycon talk about logging

harshraj22 commented 2 years ago

Testing and OOPs principle

Linkedin Post

harshraj-wadhwani commented 2 years ago

Meta Classes

Could have used meta class to enforce the existance of mode property in the subclasses, rather than having a default on in the base class. The current used approach violates design principle.

harshraj22 commented 2 years ago

Pre-commit hooks

Medium Article