Import Statement Guide¶
We follow PEP 8 when importing modules. In short, the following rules must be followed:
Format¶
Imports should be on separate lines:
import numpy
import pandas
import pytest
However, when importing modules from the same package or functions/classes/variables from the same module, it is ok to write:
from powersimdata.input import grid, change_table
from postreise.analyze.time import change_time_zone, resample_time_series
where grid
and change_table
are modules whereas change_time_zone
and
resample_time_series
are functions defined in the time
module.
Also, import statements are always put at the top of the file. We require that imports are alphabetically sorted, and automatically separated into sections and by type. If you run:
tox -e checkformatting
in the directory enclosing the tox.ini file, you will be notified of any irregularity. It is the isort package that accomplishes this task.
Path¶
Use absolute import statements. For example, if you need to create a Grid
object
in a module located at the same level as the grid module, use:
from powersimdata.input.grid import Grid
grid = Grid("Western")
and not:
from grid import Grid
grid = Grid("Western")
Be Specific¶
Don’t use wildcards import, e.g.:
from powersimdata.network.usa_tamu.constants.zones import *
since it will be unclear which names are present in the namespace, confusing both
readers and many automated tools. Another good reason to avoid wildcards is to prevent
collisions. Finally, even if we don’t use wild cards, we list the modules by setting the
__all__
variable in an __init__.py file.