forked from cta-observatory/ctapipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_example.py
More file actions
105 lines (83 loc) · 3.28 KB
/
Copy pathtool_example.py
File metadata and controls
105 lines (83 loc) · 3.28 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
"""A simple example of how to use traitlets.config.application.Application.
This should serve as a simple example that shows how the traitlets config
system works. The main classes are:
* traitlets.config.Configurable
* traitlets.config.SingletonConfigurable
* traitlets.config.Config
* traitlets.config.Application
To see the command line option help, run this program from the command line::
$ python test_tool.py --help
To make one of your classes configurable (from the command line and config
files) inherit from Configurable and declare class attributes as traits (see
classes Foo and Bar below). To make the traits configurable, you will need
to set the following options:
* ``config``: set to ``True`` to make the attribute configurable.
* ``shortname``: by default, configurable attributes are set using the syntax
"Classname.attributename". At the command line, this is a bit verbose, so
we allow "shortnames" to be declared. Setting a shortname is optional, but
when you do this, you can set the option at the command line using the
syntax: "shortname=value".
* ``help``: set the help string to display a help message when the ``-h``
option is given at the command line. The help string should be valid ReST.
When the config attribute of an Application is updated, it will fire all of
the trait's events for all of the config=True attributes.
"""
from traitlets import Bool, Unicode, Int, List, Dict
from ctapipe.core import Component, Tool
class AComponent(Component):
"""
A class that has configurable, typed attributes.
"""
i = Int(0, help="The integer i.").tag(config=True)
j = Int(1, help="The integer j.").tag(config=True)
name = Unicode("Brian", help="First name.").tag(config=True)
def __call__(self):
self.log.info("CALLED FOO")
class BComponent(Component):
""" Some Other Component """
enabled = Bool(True, help="Enable bar.").tag(config=True)
class MyTool(Tool):
""" My Tool """
name = Unicode("myapp")
running = Bool(False, help="Is the app running?").tag(config=True)
classes = [BComponent, AComponent]
aliases = Dict(
dict(
i="Foo.i",
j="Foo.j",
name="Foo.name",
running="MyApp.running",
enabled="Bar.enabled",
log_level="MyApp.log_level",
)
)
flags = Dict(
dict(
enable=({"Bar": {"enabled": True}}, "Enable Bar"),
disable=({"Bar": {"enabled": False}}, "Disable Bar"),
debug=({"MyApp": {"log_level": 10}}, "Set loglevel to DEBUG"),
)
)
def init_a_component(self):
""" setup the Foo component"""
self.log.info("INIT FOO")
self.a_component = AComponent(parent=self)
def init_b_component(self):
""" setup the Bar component"""
self.log.info("INIT BAR")
self.b_component = BComponent(parent=self)
def setup(self):
""" Setup all components and the tool"""
self.init_a_component()
self.init_b_component()
def start(self):
""" run the tool"""
self.log.info("app.config:")
self.log.info("THE CONFIGURATION: %s", self.get_current_config())
self.a_component()
def main():
""" run the app """
tool = MyTool()
tool.run()
if __name__ == "__main__":
main()