Skip to content

Commit 544dbdc

Browse files
loganthomasZeroto521ericmjl
authored
[DOC] MkDocs Utils Part 1 (#912)
* [INF] simplify a bit linting, use pre-commit as CI linting checker (#892) * [INF] simplify a bit linting There is two similar linting CIs (pre-commit and code-checks) but also have tiny differences. We should use one of them as the standard, not both. * Update CHANGELOG.md * should be `--config` not `-c` `-c` = `--code TEXT`, Format the code passed in as a string. * remove nbstripout keep the same with the old code-checks.yml * lint via pre-commit * Update CHANGELOG.md Co-authored-by: Eric Ma <[email protected]> * [DOC] mkdocs updates to check() in utils * [DOC] mkdocs updates to _strip_underscores() in utils * [DOC] mkdocs updates to import_message() in utils * [DOC] mkdocs updates to idempotent() in utils * [DOC] mkdocs updates to deprecated_alias() in utils Co-authored-by: Zero <[email protected]> Co-authored-by: Eric Ma <[email protected]>
1 parent 536f741 commit 544dbdc

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

janitor/utils.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ def check(varname: str, value, expected_types: list):
4343
One-liner syntactic sugar for checking types.
4444
It can also check callables.
4545
46-
Should be used like this::
46+
Example usage:
4747
48-
check('x', x, [int, float])
48+
```python
49+
check('x', x, [int, float])
50+
```
4951
5052
:param varname: The name of the variable (for diagnostic error message).
51-
:param value: The value of the varname.
52-
:param expected_types: The types we expect the item to be.
53+
:param value: The value of the `varname`.
54+
:param expected_types: The type(s) the item is expected to be.
5355
:raises TypeError: if data is not the expected type.
5456
"""
5557
is_expected_type: bool = False
@@ -146,15 +148,17 @@ def _strip_underscores(
146148
147149
Underscores can be stripped from the beginning, end or both.
148150
149-
.. code-block:: python
151+
Example usage:
150152
151-
df = _strip_underscores(df, strip_underscores='left')
153+
```
154+
df = _strip_underscores(df, strip_underscores='left')
155+
```
152156
153157
:param df: The pandas DataFrame object.
154158
:param strip_underscores: (optional) Removes the outer underscores from all
155-
column names. Default None keeps outer underscores. Values can be
156-
either 'left', 'right' or 'both' or the respective shorthand 'l', 'r'
157-
and True.
159+
column names. Default `None` keeps outer underscores. Values can be
160+
either `'left'`, `'right'` or `'both'` or the respective shorthand
161+
`'l'`, `'r'` and `True`.
158162
:returns: A pandas DataFrame with underscores removed.
159163
"""
160164
df = df.rename(
@@ -195,11 +199,11 @@ def import_message(
195199
optional module / package that is not currently installed. Includes
196200
installation instructions. Used in `chemistry.py` and `biology.py`.
197201
198-
:param submodule: pyjanitor submodule that needs an external dependency.
202+
:param submodule: `pyjanitor` submodule that needs an external dependency.
199203
:param package: External package this submodule relies on.
200-
:param conda_channel: Conda channel package can be installed from,
204+
:param conda_channel: `conda` channel package can be installed from,
201205
if at all.
202-
:param pip_install: Whether package can be installed via pip.
206+
:param pip_install: Whether package can be installed via `pip`.
203207
"""
204208
is_conda = os.path.exists(os.path.join(sys.prefix, "conda-meta"))
205209
installable = True
@@ -231,43 +235,41 @@ def import_message(
231235

232236
def idempotent(func: Callable, df: pd.DataFrame, *args, **kwargs):
233237
"""
234-
Raises error if a function operating on a `DataFrame` is not idempotent,
235-
that is, `func(func(df)) = func(df)` is not true for all `df`.
238+
Raises an error if a function operating on a DataFrame is not idempotent.
239+
That is, `func(func(df)) = func(df)` is not `True` for all `df`.
236240
237-
:param func: A python method.
241+
:param func: A Python method.
238242
:param df: A pandas `DataFrame`.
239243
:param args: Positional arguments supplied to the method.
240244
:param kwargs: Keyword arguments supplied to the method.
241245
:raises ValueError: If `func` is found to not be idempotent for the given
242-
`DataFrame` `df`.
246+
DataFrame (`df`).
243247
"""
244248
if not func(df, *args, **kwargs) == func(
245249
func(df, *args, **kwargs), *args, **kwargs
246250
):
247251
raise ValueError(
248-
"Supplied function is not idempotent for the given " "DataFrame."
252+
"Supplied function is not idempotent for the given DataFrame."
249253
)
250254

251255

252256
def deprecated_alias(**aliases) -> Callable:
253257
"""
254258
Used as a decorator when deprecating old function argument names, while
255-
keeping backwards compatibility.
256-
257-
Implementation is inspired from `StackOverflow`_.
259+
keeping backwards compatibility. Implementation is inspired from [`StackOverflow`][stack_link].
258260
259-
.. _StackOverflow: https://stackoverflow.com/questions/49802412/how-to-implement-deprecation-in-python-with-argument-alias
261+
[stack_link]: https://stackoverflow.com/questions/49802412/how-to-implement-deprecation-in-python-with-argument-alias
260262
261263
Functional usage example:
262264
263-
.. code-block:: python
264-
265-
@deprecated_alias(a='alpha', b='beta')
266-
def simple_sum(alpha, beta):
267-
return alpha + beta
265+
```python
266+
@deprecated_alias(a='alpha', b='beta')
267+
def simple_sum(alpha, beta):
268+
return alpha + beta
269+
```
268270
269271
:param aliases: Dictionary of aliases for a function's arguments.
270-
:return: Your original function wrapped with the kwarg redirection
272+
:return: Your original function wrapped with the `kwarg` redirection
271273
function.
272274
""" # noqa: E501
273275

0 commit comments

Comments
 (0)