Skip to content

Commit 969ca54

Browse files
authored
Merge pull request #272 from bobleesj/docformatter
Apply `docformatter` in `pre-commit` for PEP 257 docstring auto format, no manual modifications made
2 parents de55560 + 32c0018 commit 969ca54

File tree

14 files changed

+89
-58
lines changed

14 files changed

+89
-58
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ repos:
5858
- id: prettier
5959
additional_dependencies:
6060
- "prettier@^3.2.4"
61+
# docformatter - formats docstrings using PEP 257
62+
- repo: https://github.com/s-weigand/docformatter
63+
rev: 5757c5190d95e5449f102ace83df92e7d3b06c6c
64+
hooks:
65+
- id: docformatter
66+
additional_dependencies: [tomli]
67+
args: [--in-place, --config, ./pyproject.toml]

news/docformatter.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* docforamtter in pre-commit for automatic formatting of docstrings to PEP 257
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# See LICENSE.rst for license information.
1717
#
1818
##############################################################################
19-
2019
"""diffpy - tools for structure analysis by diffraction.
2120
2221
Blank namespace package.

src/diffpy/utils/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See LICENSE.rst for license information.
1313
#
1414
##############################################################################
15-
16-
"""Shared utilities for diffpy packages"""
15+
"""Shared utilities for diffpy packages."""
1716

1817
# package version
1918
from diffpy.utils.version import __version__

src/diffpy/utils/parsers/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
##############################################################################
15-
16-
"""Various utilities related to data parsing and manipulation.
17-
"""
15+
"""Various utilities related to data parsing and manipulation."""

src/diffpy/utils/parsers/serialization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def serialize_data(
3333
show_path=True,
3434
serial_file=None,
3535
):
36-
"""Serialize file data into a dictionary. Can also save dictionary into a serial language file. Dictionary is
37-
formatted as {filename: data}.
36+
"""Serialize file data into a dictionary. Can also save dictionary into a
37+
serial language file. Dictionary is formatted as {filename: data}.
3838
3939
Requires hdata and data_table (can be generated by loadData).
4040

src/diffpy/utils/resampler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
##############################################################################
15-
1615
"""Various utilities related to data parsing and manipulation."""
1716

1817
import warnings
@@ -80,7 +79,8 @@ def wsinterp(x, xp, fp, left=None, right=None):
8079

8180

8281
def nsinterp(xp, fp, qmin=0, qmax=25, left=None, right=None):
83-
"""One-dimensional Whittaker-Shannon interpolation onto the Nyquist-Shannon grid.
82+
"""One-dimensional Whittaker-Shannon interpolation onto the Nyquist-Shannon
83+
grid.
8484
8585
Takes a band-limited function fp and original grid xp and resamples fp on the NS grid.
8686
Uses the minimum number of points N required by the Nyquist sampling theorem.

src/diffpy/utils/tools.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@
44
from pathlib import Path
55

66

7-
def _stringify(obj):
7+
def clean_dict(obj):
8+
"""Remove keys from the dictionary where the corresponding value is None.
9+
10+
Parameters
11+
----------
12+
obj: dict
13+
The dictionary to clean. If None, initialize as an empty dictionary.
14+
15+
Returns
16+
-------
17+
dict:
18+
The cleaned dictionary with keys removed where the value is None.
819
"""
9-
Convert None to an empty string.
20+
obj = obj if obj is not None else {}
21+
for key, value in copy(obj).items():
22+
if not value:
23+
del obj[key]
24+
return obj
25+
26+
27+
def _stringify(obj):
28+
"""Convert None to an empty string.
1029
1130
Parameters
1231
----------
@@ -22,8 +41,7 @@ def _stringify(obj):
2241

2342

2443
def _load_config(file_path):
25-
"""
26-
Load configuration from a .json file.
44+
"""Load configuration from a .json file.
2745
2846
Parameters
2947
----------
@@ -34,7 +52,6 @@ def _load_config(file_path):
3452
-------
3553
dict:
3654
The configuration dictionary or {} if the config file does not exist.
37-
3855
"""
3956
config_file = Path(file_path).resolve()
4057
if config_file.is_file():
@@ -46,8 +63,8 @@ def _load_config(file_path):
4663

4764

4865
def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
49-
"""
50-
Get name, email and orcid of the owner/user from various sources and return it as a metadata dictionary
66+
"""Get name, email and orcid of the owner/user from various sources and
67+
return it as a metadata dictionary.
5168
5269
The function looks for the information in json format configuration files with the name 'diffpyconfig.json'.
5370
These can be in the user's home directory and in the current working directory. The information in the
@@ -79,7 +96,6 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
7996
dict:
8097
The dictionary containing username, email and orcid of the user/owner, and any other information
8198
stored in the global or local config files.
82-
8399
"""
84100
runtime_info = {"owner_name": owner_name, "owner_email": owner_email, "owner_orcid": owner_orcid}
85101
for key, value in copy(runtime_info).items():
@@ -104,8 +120,7 @@ def get_user_info(owner_name=None, owner_email=None, owner_orcid=None):
104120

105121

106122
def get_package_info(package_names, metadata=None):
107-
"""
108-
Fetches package version and updates it into (given) metadata.
123+
"""Fetches package version and updates it into (given) metadata.
109124
110125
Package info stored in metadata as {'package_info': {'package_name': 'version_number'}}.
111126
@@ -119,7 +134,6 @@ def get_package_info(package_names, metadata=None):
119134
-------
120135
dict:
121136
The updated metadata dict with package info inserted.
122-
123137
"""
124138
if metadata is None:
125139
metadata = {}

src/diffpy/utils/transforms.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def _validate_inputs(q, wavelength):
2929

3030

3131
def q_to_tth(q, wavelength):
32-
r"""
33-
Helper function to convert q to two-theta.
32+
r"""Helper function to convert q to two-theta.
3433
3534
If wavelength is missing, returns x-values that are integer indexes
3635
@@ -72,9 +71,7 @@ def q_to_tth(q, wavelength):
7271

7372

7473
def tth_to_q(tth, wavelength):
75-
r"""
76-
77-
Helper function to convert two-theta to q on independent variable axis.
74+
r"""Helper function to convert two-theta to q on independent variable axis.
7875
7976
If wavelength is missing, returns independent variable axis as integer indexes.
8077
@@ -120,8 +117,8 @@ def tth_to_q(tth, wavelength):
120117

121118

122119
def q_to_d(q):
123-
r"""
124-
Helper function to convert q to d on independent variable axis, using :math:`d = \frac{2 \pi}{q}`.
120+
r"""Helper function to convert q to d on independent variable axis, using
121+
:math:`d = \frac{2 \pi}{q}`.
125122
126123
Parameters
127124
----------
@@ -140,8 +137,7 @@ def q_to_d(q):
140137

141138

142139
def tth_to_d(tth, wavelength):
143-
r"""
144-
Helper function to convert two-theta to d on independent variable axis.
140+
r"""Helper function to convert two-theta to d on independent variable axis.
145141
146142
The formula is .. math:: d = \frac{\lambda}{2 \sin\left(\frac{2\theta}{2}\right)}.
147143
@@ -174,8 +170,7 @@ def tth_to_d(tth, wavelength):
174170

175171

176172
def d_to_q(d):
177-
r"""
178-
Helper function to convert q to d using :math:`d = \frac{2 \pi}{q}`.
173+
r"""Helper function to convert q to d using :math:`d = \frac{2 \pi}{q}`.
179174
180175
Parameters
181176
----------
@@ -194,8 +189,7 @@ def d_to_q(d):
194189

195190

196191
def d_to_tth(d, wavelength):
197-
r"""
198-
Helper function to convert d to two-theta on independent variable axis.
192+
r"""Helper function to convert d to two-theta on independent variable axis.
199193
200194
The formula is .. math:: 2\theta = 2 \arcsin\left(\frac{\lambda}{2d}\right).
201195

src/diffpy/utils/version.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See LICENSE.rst for license information.
1313
#
1414
##############################################################################
15-
1615
"""Definition of __version__."""
1716

1817
# We do not use the other three variables, but can be added back if needed.

0 commit comments

Comments
 (0)