Skip to content

Commit

Permalink
clib.Session: Refactor the __getitem__ special method for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed May 19, 2024
1 parent 861f454 commit 11d9a13
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
np.datetime64: "GMT_DATETIME",
np.timedelta64: "GMT_LONG",
}
GMT_ENUMS = {}

# Load the GMT library outside the Session class to avoid repeated loading.
_libgmt = load_libgmt()
Expand Down Expand Up @@ -239,23 +240,41 @@ def __exit__(self, exc_type, exc_value, traceback):
"""
self.destroy()

def __getitem__(self, name):
def __getitem__(self, name: str) -> int:
"""
Get the value of a GMT constant.
Parameters
----------
name
The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``).
Returns
-------
value
Integer value of the constant. Do not rely on this value because it might
change.
"""
if name not in GMT_ENUMS:
GMT_ENUMS[name] = self.get_enum(name)
return GMT_ENUMS[name]

def get_enum(self, name: str) -> int:
"""
Get the value of a GMT constant (C enum) from gmt_resources.h.
Used to set configuration values for other API calls. Wraps
``GMT_Get_Enum``.
Used to set configuration values for other API calls. Wraps ``GMT_Get_Enum``.
Parameters
----------
name : str
The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``)
name
The name of the constant (e.g., ``"GMT_SESSION_EXTERNAL"``).
Returns
-------
constant : int
Integer value of the constant. Do not rely on this value because it
might change.
value
Integer value of the constant. Do not rely on this value because it might
change.
Raises
------
Expand All @@ -266,18 +285,15 @@ def __getitem__(self, name):
"GMT_Get_Enum", argtypes=[ctp.c_void_p, ctp.c_char_p], restype=ctp.c_int
)

# The C lib introduced the void API pointer to GMT_Get_Enum so that
# it's consistent with other functions. It doesn't use the pointer so
# we can pass in None (NULL pointer). We can't give it the actual
# pointer because we need to call GMT_Get_Enum when creating a new API
# session pointer (chicken-and-egg type of thing).
# The C lib introduced the void API pointer to GMT_Get_Enum so that it's
# consistent with other functions. It doesn't use the pointer so we can pass
# in None (NULL pointer). We can't give it the actual pointer because we need
# to call GMT_Get_Enum when creating a new API session pointer (chicken-and-egg
# type of thing).
session = None

value = c_get_enum(session, name.encode())

if value is None or value == -99999:
raise GMTCLibError(f"Constant '{name}' doesn't exist in libgmt.")

return value

def get_libgmt_func(self, name, argtypes=None, restype=None):
Expand Down

0 comments on commit 11d9a13

Please sign in to comment.