-
Notifications
You must be signed in to change notification settings - Fork 229
New Alias System: Add the private _to_string function #3986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7019d0
Add the _to_string function
seisman 8f5c52b
Use GMTValueError
seisman c499aab
Remove the support of mapping=True
seisman 232ff52
Add more docstrings
seisman 2dc37c3
Improve docstrings
seisman f75a7ce
Merge branch 'main' into AliasSystem/to_string
seisman 1b7ab63
Merge branch 'main' into AliasSystem/to_string
seisman b76ee57
Fix one doctest
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
""" | ||
The PyGMT alias system to convert PyGMT's long-form arguments to GMT's short-form. | ||
""" | ||
|
||
from collections.abc import Mapping, Sequence | ||
from typing import Any, Literal | ||
|
||
from pygmt.exceptions import GMTValueError | ||
from pygmt.helpers.utils import is_nonstr_iter, sequence_join | ||
|
||
|
||
def _to_string( | ||
value: Any, | ||
prefix: str = "", # Default to an empty string to simplify the code logic. | ||
mapping: Mapping | None = None, | ||
separator: Literal["/", ","] | None = None, | ||
size: int | Sequence[int] | None = None, | ||
ndim: int = 1, | ||
name: str | None = None, | ||
) -> str | list[str] | None: | ||
""" | ||
Convert any value to a string, a sequence of strings or None. | ||
|
||
The general rules are: | ||
|
||
- ``None``/``False`` will be converted to ``None``. | ||
- ``True`` will be converted to an empty string. | ||
- A sequence will be joined by the separator if a separator is provided. Otherwise, | ||
each item in the sequence will be converted to a string and a sequence of strings | ||
will be returned. It's also possible to validate the size and dimension of the | ||
sequence. | ||
- Any other type of values will be converted to a string if possible. | ||
|
||
If a mapping dictionary is provided, the value will be converted to the short-form | ||
string that GMT accepts (e.g., mapping PyGMT's long-form argument ``"high"`` to | ||
GMT's short-form argument ``"h"``). | ||
|
||
An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted | ||
string. | ||
|
||
To avoid extra overhead, this function does not validate parameter combinations. For | ||
example, if ``value`` is a sequence but ``separator`` is not specified, the function | ||
will return a sequence of strings. In this case, ``prefix`` has no effect, but the | ||
function does not check for such inconsistencies. The maintaner should ensure that | ||
the parameter combinations are valid. | ||
|
||
Parameters | ||
---------- | ||
value | ||
The value to convert. | ||
prefix | ||
The string to add as a prefix to the returned value. | ||
mapping | ||
A mapping dictionary to map PyGMT's long-form arguments to GMT's short-form. | ||
separator | ||
The separator to use if the value is a sequence. | ||
size | ||
Expected size of the 1-D sequence. It can be either an integer or a sequence of | ||
integers. If an integer, it is the expected size of the 1-D sequence. If it is a | ||
sequence, it is the allowed sizes of the 1-D sequence. | ||
ndim | ||
The expected maximum number of dimensions of the sequence. | ||
name | ||
The name of the parameter to be used in the error message. | ||
|
||
Returns | ||
------- | ||
ret | ||
The converted value. | ||
|
||
Examples | ||
-------- | ||
>>> _to_string("text") | ||
'text' | ||
>>> _to_string(12) | ||
'12' | ||
>>> _to_string(True) | ||
'' | ||
>>> _to_string(False) | ||
>>> _to_string(None) | ||
|
||
>>> _to_string("text", prefix="+a") | ||
'+atext' | ||
>>> _to_string(12, prefix="+a") | ||
'+a12' | ||
>>> _to_string(True, prefix="+a") | ||
'+a' | ||
>>> _to_string(False, prefix="+a") | ||
>>> _to_string(None, prefix="+a") | ||
|
||
>>> _to_string("mean", mapping={"mean": "a", "mad": "d", "full": "g"}) | ||
'a' | ||
>>> _to_string("invalid", mapping={"mean": "a", "mad": "d", "full": "g"}) | ||
Traceback (most recent call last): | ||
... | ||
pygmt...GMTValueError: Invalid value: 'invalid'. Expected one of: 'mean', ... | ||
|
||
>>> _to_string((12, 34), separator="/") | ||
'12/34' | ||
>>> _to_string(("12p", "34p"), separator=",") | ||
'12p,34p' | ||
>>> _to_string(("12p", "34p"), prefix="+o", separator="/") | ||
'+o12p/34p' | ||
|
||
>>> _to_string(["xaf", "yaf", "WSen"]) | ||
['xaf', 'yaf', 'WSen'] | ||
""" | ||
# None and False are converted to None. | ||
if value is None or value is False: | ||
return None | ||
# True is converted to an empty string with the optional prefix. | ||
if value is True: | ||
return f"{prefix}" | ||
# Any non-sequence value is converted to a string. | ||
if not is_nonstr_iter(value): | ||
if mapping: | ||
if value not in mapping and value not in mapping.values(): | ||
raise GMTValueError( | ||
value, | ||
description="value for parameter {name!r}" if name else "value", | ||
choices=mapping.keys(), | ||
) | ||
value = mapping.get(value, value) | ||
return f"{prefix}{value}" | ||
|
||
# Return the sequence if separator is not specified for options like '-B'. | ||
# True in a sequence will be converted to an empty string. | ||
if separator is None: | ||
return [str(item) if item is not True else "" for item in value] | ||
# Join the sequence of values with the separator. | ||
# "prefix" and "mapping" are ignored. We can enable them when needed. | ||
_value = sequence_join(value, separator=separator, size=size, ndim=ndim, name=name) | ||
return f"{prefix}{_value}" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.