|
7 | 7 | from collections.abc import Mapping
|
8 | 8 | from typing import Any, Literal
|
9 | 9 |
|
10 |
| -from pygmt.helpers.utils import is_nonstr_iter |
| 10 | +from pygmt.helpers.utils import is_nonstr_iter, sequence_join |
11 | 11 |
|
12 | 12 |
|
13 | 13 | def to_string(
|
14 | 14 | value: Any,
|
15 | 15 | prefix: str = "", # Default to an empty string to simplify the code logic.
|
16 |
| - separator: Literal["/", ","] | None = None, |
17 | 16 | mapping: bool | Mapping = False,
|
| 17 | + separator: Literal["/", ","] | None = None, |
18 | 18 | ) -> str | list[str] | None:
|
19 | 19 | """
|
20 | 20 | Convert any value to a string, a sequence of strings or None.
|
@@ -48,11 +48,11 @@ def to_string(
|
48 | 48 | The value to convert.
|
49 | 49 | prefix
|
50 | 50 | The string to add as a prefix to the returned value.
|
51 |
| - separator |
52 |
| - The separator to use if the value is a sequence. |
53 | 51 | mapping
|
54 | 52 | A mapping dictionary or ``True`` to map long-form arguments to GMT's short-form
|
55 | 53 | arguments. If ``True``, will use the first letter of the long-form arguments.
|
| 54 | + separator |
| 55 | + The separator to use if the value is a sequence. |
56 | 56 |
|
57 | 57 | Returns
|
58 | 58 | -------
|
@@ -97,12 +97,14 @@ def to_string(
|
97 | 97 | value = value[0] if mapping is True else mapping.get(value, value)
|
98 | 98 | return f"{prefix}{value}"
|
99 | 99 |
|
100 |
| - # Convert a sequence of values to a sequence of strings. |
101 |
| - # In some cases, "prefix" and "mapping" are ignored. We can enable them when needed. |
102 |
| - _values = [str(item) for item in value] |
103 |
| - # When separator is not specified, return a sequence of strings for repeatable GMT |
104 |
| - # options like '-B'. Otherwise, join the sequence of strings with the separator. |
105 |
| - return _values if separator is None else f"{prefix}{separator.join(_values)}" |
| 100 | + # Return the sequence if separator is not specified for options like '-B'. |
| 101 | + if separator is None: |
| 102 | + return [str(item) for item in value] |
| 103 | + |
| 104 | + # Join the sequence of values with the separator. |
| 105 | + # "prefix" and "mapping" are ignored. We can enable them when needed. |
| 106 | + _value = sequence_join(value, separator=separator) |
| 107 | + return f"{prefix}{_value}" |
106 | 108 |
|
107 | 109 |
|
108 | 110 | @dataclasses.dataclass
|
|
0 commit comments