Skip to content

Commit a13182f

Browse files
committed
Improve the docstrings of the value_to_string function
1 parent 0a14135 commit a13182f

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

pygmt/alias.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Alias system that converts PyGMT parameters to GMT short-form options.
2+
Alias system converting PyGMT parameters to GMT short-form options.
33
"""
44

55
import dataclasses
@@ -20,14 +20,18 @@ def value_to_string(
2020
"""
2121
Convert any value to a string, a sequence of strings or None.
2222
23-
``None`` or ``False`` will be converted to ``None``.
23+
- ``None`` or ``False`` will be converted to ``None``.
24+
- ``True`` will be converted to an empty string.
25+
- A sequence will be joined by the separator if a separator is provided. Otherwise,
26+
each item in the sequence will be converted to a string and a sequence of strings
27+
will be returned.
28+
- Any other value will be converted to a string if possible.
2429
25-
``True`` will be converted to an empty string. If the value is a sequence and a
26-
separator is provided, the sequence will be joined by the separator. Otherwise, each
27-
item in the sequence will be converted to a string and a sequence of strings will be
28-
returned. Any other value will be converted to a string if possible. It also tried
29-
to convert PyGMT's long-form arguments into GMT's short-form arguments by using a
30-
mapping dictionary or simply using the first letter of the long-form arguments.
30+
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.
3135
3236
An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted
3337
string.
@@ -37,12 +41,17 @@ def value_to_string(
3741
value
3842
The value to convert.
3943
prefix
40-
The string to add as a prefix to the value.
44+
The string to add as a prefix to the returned value.
4145
separator
4246
The separator to use if the value is a sequence.
4347
mapping
44-
Map long-form arguments to GMT's short-form arguments. If ``True``, will use the
45-
first letter of the long-form arguments.
48+
A mapping dictionary or ``True`` to map long-form arguments to GMT's short-form
49+
arguments. If ``True``, will use the first letter of the long-form arguments.
50+
51+
Returns
52+
-------
53+
ret
54+
The converted value.
4655
4756
Examples
4857
--------
@@ -66,8 +75,6 @@ def value_to_string(
6675
['xaf', 'yaf', 'WSen']
6776
>>> value_to_string("high", mapping=True)
6877
'h'
69-
>>> value_to_string("low", mapping=True)
70-
'l'
7178
>>> value_to_string("mean", mapping={"mean": "a", "mad": "d", "full": "g"})
7279
'a'
7380
>>> value_to_string("invalid", mapping={"mean": "a", "mad": "d", "full": "g"})
@@ -77,20 +84,20 @@ def value_to_string(
7784
if value is None or value is False:
7885
return None
7986
# True means the parameter is specified, returns an empty string with the optional
80-
# prefix ('prefix' defaults to an empty string!).
87+
# prefix. We don't have to check 'prefix' since it defaults to an empty string!
8188
if value is True:
8289
return f"{prefix}"
8390

84-
# Convert any value to a string or a sequence of strings
91+
# Convert any value to a string or a sequence of strings.
8592
if is_nonstr_iter(value): # Is a sequence
8693
value = [str(item) for item in value] # Convert to a sequence of strings
8794
if separator is None:
8895
# A sequence is given but separator is not specified. In this case, return
8996
# a sequence of strings, which is used to support repeated GMT options like
9097
# '-B'. 'prefix' makes no sense here, so ignored.
9198
return value
92-
value = separator.join(value) # Join the sequence by the specified separator.
93-
if mapping: # Mapping long-form arguments to short-form arguments
99+
value = separator.join(value) # Join the sequence with the separator.
100+
elif mapping: # Mapping long-form arguments to short-form arguments.
94101
value = value[0] if mapping is True else mapping.get(value, value)
95102
return f"{prefix}{value}"
96103

0 commit comments

Comments
 (0)