-
Notifications
You must be signed in to change notification settings - Fork 221
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
New alias system towards a more Pythonic interface #3239
Comments
Ping @GenericMappingTools/pygmt-maintainers for comments and thoughts. |
Thanks @seisman for opening up this for discussion. The I'll need more time to look into your implementation at #3238. My initial impression is that the implementation of |
Yes.
It looks worth a try. |
GMT's single-letter options (e.g.
-B
) are difficult to read/understand, so they are not recommended for use in PyGMT. Instead, PyGMT uses long-form parameters and the PyGMT alias system is responsible for translating PyGMT long-form parameters into the corresponding short-form GMT options. The alias system was originally implemented by @leouieda in aad12e0 (seven years ago!) and hasn't changed much since then. The alias system has some limitations and flaws that prevent us from achieving the project goal: "Build a Pythonic API for GMT". Now it's time to design a new alias system. This issue reviews the current alias system and proposes a new alias system. The initial implementation of the proposed new alias system is available for review in #3238.The current alias system
Currently, the alias system looks like this:
The current alias system works in this way:
kwargs_to_string
decorator converts an argument to a string. The argument can be a string, a numeric value, or a sequence (e.g., convertingregion=[10, 20, 30, 40]
toregion="10/20/30/40"
).use_alias
decorator maps long-form PyGMT parameters (e.g,region
) to short-form GMT options (e.g.,R
). The short-form options are then stored inkwargs
(i.e., convertingregion="10/20/30/40" to kwargs["R"]="10/20/30/40"
.build_arg_list
(previouslybuild_arg_string
) converts the dictionarykwargs
to a list/string that GMT API can take.The current alias system has some known limitations and flaws:
Long arguments are difficult to read/write.
Since each GMT option usually has many modifiers, some arguments are very long and no tab autocompletion is possible.
Here is an example from What's the most Pythonic way for long GMT arguments? #1082:
The parameter names
position
andbox
are good, but their arguments are difficult to write/read. In What's the most Pythonic way for long GMT arguments? #1082, some candidate solutions (dict, class or function) were proposed. Please refer to What's the most Pythonic way for long GMT arguments? #1082 for detailed discussions.Short arguments are easy to write but difficult to read
For some options, GMT uses single-letter arguments. Here are two examples:
Figure.coast
,resolution="f"
is not readable.resolution="full"
is more Pythonicpygmt.binstats
,statistic="z"
is not readable.statstic="sum"
is more Pythonic.To support Pythonic long-form arguments, we can use a dictionary which maps long-form arguments to short-form arguments. In the current alias system, it means a lot of coding effort, see POC: pygmt.binstats: Make the 'statistic' parameter more Pythonic #3012 and Figure.coast/pygmt.select/pygmt.grdlandmask: Use long names ("crude"/"low"/"intermediate"/"high"/"full") for the 'resolution' parameter #3013.
Abuse of the
kwargs
parameter.Short-form GMT options are stored in the keyword argument
kwargs
, so it must be the last parameter for all wrappers that use the alias system.Can't access the original argument by the long-form parameter name inside the wrappers
The alias system is implemented as decorators, so all conversions/mappings are done outside of the wrappers. It means we can't access the original argument by the long-form parameter name in the wrappers.
For example, in
Figure.plot
,S
is aliased tostyle
. To access the argument ofstyle
, we have to usekwargs.get("S")
.Another example is,
region=[10, 20, 30, 40]
is converted tokwargs["R"]="10/20/30/40"
. If we want to get the region bounds in the wrapper, we have to do the inversed conversion:w, e, s, n = kwargs["R"].split("/")
.Difficult to implement Pythonic high-level wrappers
Due to the design of the GMT modules, each GMT module usually does too man things. For example,
basemap
/coast
provide exactly the same option for adding scale bar, direction rose, and magnetic rose. In Higher-level plotting methods for scale bar, map direction rose and magnetic rose? #2831, we proposed to provide high-level wrappers that do a single job. These high-level wrappers should have a Pythonic interface with many long-form parameters (see Higher-level plotting methods for scale bar, map direction rose and magnetic rose? #2831 for the proposed API) but it's unclear how to translate so many parameters into GMT short-form options (we can but it usually means a lot of if-else tests, e.g., Add Pythonic argument options for colorbar frame parameters #2130).Another related issue is Higher-level plotting methods for Figure.plot and Figure.plot3d #2797 for high-level wrappers of
plot
andplot3d
.The new alias system
Here, I propose a new alias system after half a year of design and coding (design takes more time than coding!). The new alias system is implemented in
pygmt/alias.py
of PR #3238.The
Alias
classThe
Alias
class defines how to convert the argument of a long-form parameter name to a string (or a sequence of strings) that can be passed to GMT API.In the example below, we define a parameter
offset
. Its value can be a number, a string, or a sequence, or any object that the string representation (__str__
) makes sense to GMT. If a sequence is given, the sequence will be joined into a string by the separator '/'. The prefix+o
will also be added at the beginning of the string.The
Alias
class has thevalue
property, which is implemented using the setter method. So the argument is converted whenAlias.value
is assigned.Here are more examples:
The
AliasSystem
classThe
AliasSystem
class is similar to the olduse_alias
decorator, which aliases GMT single-letter options to aAlias
object or a list ofAlias
objectsn.Here is an example:
In this example,
A
is mapped to a list ofAlias
objesct. So, arguments ofpar1
/par2
/par3
will be used to build the-A
option (e.g.,par1="mytext", par3=(12, 12)
is converted tokwdict["A"]="mytext+o12/12"
). It means now we can break any complicated GMT option into multiple long-form parameters.The
AliasSystem
class provides the propertykwdict
which is a dictionary with single-letter options as keys and string/sequence as values. It can be passed directly to thebuild_arg_list
function. Thekwdict
dictionary is dynamically calculated from the current values of long-form parameters. In this way, we can always access the original values of parameters by long-form parameter names and even make changes to them before accessingalias.kwdict
property.The
BaseParam
class for common parametersAs discussed in #1082, for some options, it makes more sense to define a class to avoid having too many (potentially conflicting) parameter names.
With the help of the
Alias
system, theBaseParam
implementation is easy. Users won't use theBaseParam
class but we developers can use it to create new classes in a few lines without much coding effort (So adding new classes can be marked as "good-first-issue"!).The
Box
classIn
pygmt/params/box.py
, I've implemented theBox
class as an example. Thebox
parameter is commonly used for plotting scale bar, color bar, gmt logo, images, inset, and more. So it makes sense to have aBox
class.Below is the definition of the
Box
class. To define a class for a parameter, we just need to define some fields (e.g.,clearance
/fill
), and the special field_aliases
, which is a list ofAlias
object.Here is an example. Please refer to the docstrings for more examples.
It's important to know that the Box class supports autocompletion!
The
Frame
/Axes
/Axis
classesThe
-B
option is one of the most complicated GMT options. It can repeat multiple times in GMT CLI, making it more complicated to support in Python.In
pygmt/params/frame.py
, theFrame
/Axes
/Axis
classes are implemented to address one of our oldest issues #249.The technical details don't matter much. Here is an example use:
Check out PR #3238 and try it yourself! Enjoy autocompletion!
Pros/Cons of the new alias system
Pros:
kwargs
anymorekwargs.get("S")
(Maybe faster)Box
/Frame
Cons:
{aliases}
in docstrings is not supported in the new alias system. [The list of aliases are not needed if we write good documentation.]This is another big refactor towards a Pythonic interface! Ping @GenericMappingTools/pygmt-maintainers for comments.
The text was updated successfully, but these errors were encountered: