Repository navigation
Expand file tree
/
Copy pathPythonProgramExample.py
More file actions
171 lines (136 loc) · 4.47 KB
/
Copy pathPythonProgramExample.py
File metadata and controls
171 lines (136 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# Copyright (C) 2012-2020 Euclid Science Ground Segment
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
""" Small example for a python based script
"""
import argparse
from ElementsKernel import Logging
from ElementsKernel.Program import str_to_bool
from ElementsExamples.PythonModuleExample import ClassExample
from ElementsKernel import Exit
def myLocalLogTestFunc():
""" example of logging from a function
"""
logger = Logging.getLogger()
logger.info("Test of Message")
logger2 = Logging.getLogger("TestLogger")
logger2.info("Test2 of Message")
logger3 = Logging.getLogger(__name__)
logger3.info("Test3 of Message")
def defineSpecificProgramOptions():
""" Allows to define the (command line and configuration file) options specific to
this program. See the ElementsProgram documentation for more details.
:return: An ArgumentParser.
"""
parser = argparse.ArgumentParser()
string_example_help = "A string example option"
int_example_help = "An int example option"
parser.add_argument(
'--string-option',
type=str,
help=string_example_help
)
parser.add_argument(
'--int-option',
type=int,
default=111,
help=int_example_help
)
parser.add_argument(
'--int-option-with-default-and-default-in-conf',
type=int,
default=222,
help=int_example_help
)
parser.add_argument(
'--int-option-with-default-no-default-in-conf',
type=int,
default=444,
help=int_example_help
)
parser.add_argument(
'--int-option-no-default-not-defined-in-conf',
type=int,
help=int_example_help
)
parser.add_argument(
'--int-option-with-no-defaults-anywhere',
type=int,
help=int_example_help
)
parser.add_argument(
'--float-option',
type=float,
default=99999.0,
help='A float option'
)
parser.add_argument(
'--int-list-option',
nargs='+',
type=int,
default=[-1, -2, 9],
help='A integer list option'
)
parser.add_argument(
'--overwrite',
nargs='?',
dest='overwrite',
default=False,
const=True,
type=str_to_bool,
help='Overwrite the output files'
)
parser.add_argument(
'--threshold', '-t',
type=float,
default=0.5,
help="signal threshold")
return parser
def mainMethod(args):
""" The "main" method.
This method is the entry point to the program. In this sense, it is similar to a main
(and it is why it is called mainMethod()). The code below contains the calls to the
different classes created for the first developer's workshop
See the ElementsProgram documentation for more details.
"""
logger = Logging.getLogger('ProgramExample')
logger.info('Entering ProgramExample mainMethod()')
#
# function with log messages
#
myLocalLogTestFunc()
#
# Log some of the arguments
#
string_from_configuration = args.string_option
logger.info('Example string : %s', string_from_configuration)
second_element = args.int_list_option[1]
logger.info('Second elements from the list : %s', str(second_element))
logger.info("the int_option value %d", args.int_option)
logger.info("the threshold value %.1f", args.threshold)
#
# Calling a static method
#
result = ClassExample.product(3, 4)
logger.info('Static method result : %s', str(result))
#
# Calling the constructor and a method member
#
example_object = ClassExample(args.int_list_option)
logger.info('Sum of the list elements : %s', str(example_object.sumListValues()))
logger.info('Exiting ProgramExample mainMethod()')
return Exit.Code["OK"]