File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Base class for PyGMT common parameters.
3
+ """
4
+
5
+
6
+ class BaseParam :
7
+ """
8
+ Base class for PyGMT common parameters.
9
+
10
+ Examples
11
+ --------
12
+ >>> from typing import Any
13
+ >>> import dataclasses
14
+ >>> from pygmt.params.base import BaseParam
15
+ >>> from pygmt.alias import Alias
16
+ >>>
17
+ >>> @dataclasses.dataclass(repr=False)
18
+ ... class Test(BaseParam):
19
+ ... par1: Any = None
20
+ ... par2: Any = None
21
+ ... par3: Any = None
22
+ ...
23
+ ... _aliases = [
24
+ ... Alias("par1"),
25
+ ... Alias("par2", prefix="+a"),
26
+ ... Alias("par3", prefix="+b", separator="/"),
27
+ ... ]
28
+ >>> var = Test(par1="val1")
29
+ >>> str(var)
30
+ 'val1'
31
+ >>> repr(var)
32
+ "Test(par1='val1')"
33
+ """
34
+
35
+ def __str__ (self ):
36
+ """
37
+ String representation of the object that can be passed to GMT directly.
38
+ """
39
+ for alias in self ._aliases :
40
+ alias .value = getattr (self , alias .name )
41
+ return "" .join (
42
+ [alias .value for alias in self ._aliases if alias .value is not None ]
43
+ )
44
+
45
+ def __repr__ (self ):
46
+ """
47
+ String representation of the object.
48
+ """
49
+ string = []
50
+ for alias in self ._aliases :
51
+ value = getattr (self , alias .name )
52
+ if value is None or value is False :
53
+ continue
54
+ string .append (f"{ alias .name } ={ value !r} " )
55
+ return f"{ self .__class__ .__name__ } ({ ', ' .join (string )} )"
You can’t perform that action at this time.
0 commit comments