Skip to content

Move module-specific common codes to a separate file? #3357

Closed

Description

@seisman

Some modules may share the same or similar codes. For example,

Figure.plot has several lines of codes that check if data has a geometry of Point or MultiPoint, and then set the default style.

pygmt/pygmt/src/plot.py

Lines 246 to 255 in ecaab8f

if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
kwargs["S"] = "s0.2c"
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
try:
with Path(which(data)).open(encoding="utf-8") as file:
line = file.readline()
if "@GMULTIPOINT" in line or "@GPOINT" in line:
kwargs["S"] = "s0.2c"
except FileNotFoundError:
pass
.

Very similar codes are also used in

if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
.

The Figure.meca wrapper has functions like

def convention_code(convention, component="full"):

and
def convention_name(code):

They can be used directly in Figure.coupe (not wrapped yet).

So, instead of duplicating the same/similar codes in different files, we should try to reuse them as much as possible.

I think we have two options:

  1. Define small function in one file and import the function in another file. (e.g., define a private function like _determine_style in Figure.plot and use from pygmt.src.plot import _determine_style in Figure.plot3d.
  2. Define these functions in a common file and import the functions when necessary

I prefer option 2. Since these functions are module-specific, I think it makes more sense to put them in a file under pygmt/src (e.g, pygmt/src/_common.py). I also feel that these functions should all be private.

Thoughts @GenericMappingTools/pygmt-maintainers

Activity

  1. added
    discussions
    on Jul 25, 2024
  2. added this to the 0.14.0 milestone on Sep 27, 2024
  3. added
    maintenance
    on Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionsmaintenance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions