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
Next Next commit
Try injecting one parameter into method signature
  • Loading branch information
maxrjones committed May 21, 2021
commit 8d21c1bbb494c50dc03bce4d14362dd57afa62d2
1 change: 1 addition & 0 deletions pygmt/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
deprecate_parameter,
fmt_docstring,
kwargs_to_strings,
tab_complete_alias,
use_alias,
)
from pygmt.helpers.tempfile import GMTTempFile, tempfile_from_geojson, unique_name
Expand Down
24 changes: 24 additions & 0 deletions pygmt/helpers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import warnings

import numpy as np
from inspect import signature, Parameter
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers.utils import is_nonstr_iter

Expand Down Expand Up @@ -327,6 +328,29 @@ def new_module(*args, **kwargs):
return alias_decorator



def tab_complete_alias(module_func):
"""
Decorator injecting aliases of a method as attributes
"""
@functools.wraps(module_func)
def wrapper(*args, **kwargs):
"""
New module instance that includes aliases in the signature.
"""
sig = signature(module_func)
param = Parameter("verbose",kind=Parameter.POSITIONAL_OR_KEYWORD,default=None)
wrapped_params = [param for param in sig.parameters.values()]
kwargs_param = wrapped_params.pop(-1)
all_params = wrapped_params + [param] + [kwargs_param]
sig = sig.replace(parameters=all_params)
wrapper.__signature__ = sig

return module_func(*args,**kwargs)

return wrapper


def kwargs_to_strings(**conversions):
"""
Decorator to convert given keyword arguments to strings.
Expand Down
4 changes: 4 additions & 0 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
dummy_context,
fmt_docstring,
kwargs_to_strings,
tab_complete_alias,
use_alias,
)
from pygmt.helpers.decorators import tab_complete_alias


def _blockm(block_method, table, outfile, **kwargs):
Expand Down Expand Up @@ -71,6 +73,7 @@ def _blockm(block_method, table, outfile, **kwargs):


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


@fmt_docstring
@tab_complete_alias
@use_alias(
I="spacing",
R="region",
Expand Down