Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tab completion in Jupyter by inserting aliases into the method signature #1282

Merged
merged 14 commits into from
Jun 16, 2021
Merged
2 changes: 2 additions & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
launch_external_viewer,
unique_name,
Expand Down Expand Up @@ -124,6 +125,7 @@ def region(self):
return wesn

@fmt_docstring
@insert_alias
@use_alias(
A="crop",
C="gs_option",
Expand Down
1 change: 1 addition & 0 deletions pygmt/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pygmt.helpers.decorators import (
deprecate_parameter,
fmt_docstring,
insert_alias,
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
kwargs_to_strings,
use_alias,
)
Expand Down
25 changes: 25 additions & 0 deletions pygmt/helpers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import functools
import textwrap
import warnings
from inspect import Parameter, signature

import numpy as np
from pygmt.exceptions import GMTInvalidInput
Expand Down Expand Up @@ -327,6 +328,30 @@ def new_module(*args, **kwargs):
return alias_decorator


def insert_alias(module_func):
Copy link
Member

@seisman seisman May 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should move the code into the use_alias decorator, because we only need to "insert aliases into the signature" when aliases are defined by @use_alias? It also means all functions support tab completion without any further actions (i.e., adding @insert_alias).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that combinging use_alias and insert_alias is the smart move, since I don't see how insert_alias would be used on its own.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of implementing this inside use_alias to simplify the diff, but would prefer to not combine by simply moving the code inside use_alias. As an alternate solution, my latest commit calls insert_alias from within use_alias. There are two reasons why I prefer this option to having all the code within use_alias. First, I think that it is simpler (i.e., easier to maintain) if individual functions do smaller, isolated tasks. Second, this implementation is compatible with the changes proposed in #1282. Even if that PR doesn't get merged, it seems to me that having insert_alias as a separate function could be more compatible with future updates to the decorators (reason 1).

"""
Decorator insertings aliases into the signature of a method.
"""

# Get current signature and parameters
sig = signature(module_func)
wrapped_params = list(sig.parameters.values())
kwargs_param = wrapped_params.pop(-1)
# Add new parameters from aliases
for alias in module_func.aliases.values():
if alias not in sig.parameters.keys():
new_param = Parameter(
alias, kind=Parameter.POSITIONAL_OR_KEYWORD, default=None
)
wrapped_params = wrapped_params + [new_param]
all_params = wrapped_params + [kwargs_param]
# Update method signature
sig_new = sig.replace(parameters=all_params)
module_func.__signature__ = sig_new

return module_func


def kwargs_to_strings(**conversions):
"""
Decorator to convert given keyword arguments to strings.
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
args_in_kwargs,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
3 changes: 3 additions & 0 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
data_kind,
dummy_context,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)
Expand Down Expand Up @@ -71,6 +72,7 @@ def _blockm(block_method, table, outfile, **kwargs):


@fmt_docstring
@insert_alias
@use_alias(
I="spacing",
R="region",
Expand Down Expand Up @@ -132,6 +134,7 @@ def blockmean(table, outfile=None, **kwargs):


@fmt_docstring
@insert_alias
@use_alias(
I="spacing",
R="region",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
args_in_kwargs,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
data_kind,
dummy_context,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="annotation",
B="frame",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/grd2cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="transparency",
C="cmap",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
G="outgrid",
R="region",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/grdcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
grdcontour - Plot a contour figure.
"""
from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="annotation",
B="frame",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
G="outgrid",
R="region",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="mode",
G="outgrid",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
D="distance",
F="filter",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
grdimage - Plot grids or images.
"""
from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="img_out",
B="frame",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
C="per_column",
D="tiles",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
build_arg_string,
data_kind,
fmt_docstring,
insert_alias,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(V="verbose", f="coltypes", n="interpolation")
def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
"""
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/grdview.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
build_arg_string,
data_kind,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
Histogram - Create a histogram
"""
from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
A="horizontal",
B="frame",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
image - Plot an image.
"""
from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
GMTTempFile,
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
C="per_column",
I="spacing",
Expand Down
9 changes: 8 additions & 1 deletion pygmt/src/inset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
import contextlib

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import (
build_arg_string,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@contextlib.contextmanager
@insert_alias
@use_alias(D="position", F="box", M="margin", N="no_clip", V="verbose")
@kwargs_to_strings(D="sequence", M="sequence")
def inset(self, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions pygmt/src/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
build_arg_string,
data_kind,
fmt_docstring,
insert_alias,
kwargs_to_strings,
use_alias,
)


@fmt_docstring
@insert_alias
@use_alias(
R="region",
J="projection",
Expand Down
Loading