11"""
2- Alias system converting PyGMT parameters to GMT short-form options.
2+ PyGMT's alias system for converting PyGMT parameters to GMT short-form options.
33"""
44
55import dataclasses
66import inspect
77from collections import defaultdict
88from collections .abc import Mapping , Sequence
9- from typing import Any
9+ from typing import Any , Literal
1010
1111from pygmt .helpers .utils import is_nonstr_iter
1212
1313
1414def value_to_string (
1515 value : Any ,
1616 prefix : str = "" , # Default to an empty string to simplify the code logic.
17- separator : str | None = None ,
17+ separator : Literal [ "/" , "," ] | None = None ,
1818 mapping : bool | Mapping = False ,
1919) -> str | Sequence [str ] | None :
2020 """
@@ -28,14 +28,19 @@ def value_to_string(
2828 - Any other value will be converted to a string if possible.
2929
3030 If a mapping dictionary is provided, the value will be converted to the short-form
31- value that GMT accepts (e.g., mapping PyGMT long-form argument ``high`` to GMT's
32- short-form argument ``h ``). For values not in the mapping dictionary, the original
33- value will be returned. If ``mapping`` is set to ``True``, the first letter of the
34- long-form argument will be used as the short-form argument.
31+ string that GMT accepts (e.g., mapping PyGMT long-form argument ``" high" `` to GMT's
32+ short-form argument ``"h" ``). If the value is not in the mapping dictionary, the
33+ original value will be returned. If ``mapping`` is set to ``True``, the first letter
34+ of the long-form argument will be used as the short-form argument.
3535
3636 An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted
3737 string.
3838
39+ Need to note that this function doesn't check if the given parameters are valid, to
40+ avoid the overhead of checking. For example, if ``value`` is a sequence but
41+ ``separator`` is not specified, a sequence of strings will be returned. ``prefix``
42+ makes no sense here, but this function won't check it.
43+
3944 Parameters
4045 ----------
4146 value
@@ -92,11 +97,11 @@ def value_to_string(
9297 if is_nonstr_iter (value ): # Is a sequence.
9398 value = [str (item ) for item in value ] # Convert to a sequence of strings
9499 if separator is None :
95- # A sequence is given but separator is not specified. In this case, return
96- # a sequence of strings, which is used to support repeated GMT options like
97- # '-B'. 'prefix' makes no sense here, so ignored.
100+ # A sequence is given but separator is not specified. Return a sequence of
101+ # strings, to support repeated GMT options like '-B'. 'prefix' makes no
102+ # sense and is ignored.
98103 return value
99- value = separator .join (value ) # Join the sequence with the separator.
104+ value = separator .join (value ) # Join the sequence by the separator.
100105 elif mapping : # Mapping long-form arguments to short-form arguments.
101106 value = value [0 ] if mapping is True else mapping .get (value , value )
102107 # Return the final string with the optional prefix.
@@ -143,7 +148,7 @@ def __init__(
143148 self ,
144149 name : str ,
145150 prefix : str = "" ,
146- separator : str | None = None ,
151+ separator : Literal [ "/" , "," ] | None = None ,
147152 mapping : bool | Mapping = False ,
148153 value : Any = None ,
149154 ):
0 commit comments