DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
timing
python
afc
toolbox.py
Go to the documentation of this file.
1
from
__future__
import
print_function
2
3
import
click
4
from
click
import
echo, style, secho
5
from
.click_texttable
import
Texttable
6
7
# ------------------------------------------------------------------------------
8
class
IntRange
(click.types.IntParamType):
9
"""A parameter that works similar to :data:`click.INT` but restricts
10
the value to fit into a range. The default behavior is to fail if the
11
value falls outside the range, but it can also be silently clamped
12
between the two edges.
13
14
See :ref:`ranges` for an example.
15
"""
16
name =
'integer range'
17
18
def
__init__
(self, min=None, max=None, clamp=False):
19
self.
min
= min
20
self.
max
= max
21
self.
clamp
= clamp
22
23
def
convert
(self, value, param, ctx):
24
25
if
type(value) == str:
26
if
value.startswith(
'0x'
):
27
base = 16
28
elif
value.startswith(
'0o'
):
29
bae = 8
30
elif
value.startswith(
'0b'
):
31
base = 2
32
else
:
33
base = 10
34
rv = int(value, base)
35
else
:
36
rv = int(value)
37
if
self.
clamp
:
38
if
self.
min
is
not
None
and
rv < self.
min
:
39
return
self.
min
40
if
self.
max
is
not
None
and
rv > self.
max
:
41
return
self.
max
42
if
self.
min
is
not
None
and
rv < self.
min
or
\
43
self.
max
is
not
None
and
rv > self.
max
:
44
if
self.
min
is
None
:
45
self.fail(
'%s is bigger than the maximum valid value '
46
'%s.'
% (rv, self.
max
), param, ctx)
47
elif
self.
max
is
None
:
48
self.fail(
'%s is smaller than the minimum valid value '
49
'%s.'
% (rv, self.
min
), param, ctx)
50
else
:
51
self.fail(
'%s is not in the valid range of %s to %s.'
52
% (rv, self.
min
, self.
max
), param, ctx)
53
return
rv
54
55
def
__repr__
(self):
56
return
'IntRange(%r, %r)'
% (self.
min
, self.
max
)
57
# ------------------------------------------------------------------------------
58
59
# ------------------------------------------------------------------------------
60
def
split
(ctx, param, value):
61
if
value
is
None
:
62
return
[]
63
64
return
value.split(
','
)
65
# ------------------------------------------------------------------------------
66
67
# ------------------------------------------------------------------------------
68
def
__str2int__
( value ):
69
if
value.startswith(
'0x'
):
70
base = 16
71
elif
value.startswith(
'0o'
):
72
bae = 8
73
elif
value.startswith(
'0b'
):
74
base = 2
75
else
:
76
base = 10
77
return
int(value, base)
78
79
def
split_ints
(ctx, param, value):
80
81
sep =
','
82
dash =
'-'
83
84
if
value
is
None
:
85
return
[]
86
87
numbers = []
88
for
item
in
value.split(sep):
89
nums = item.split(dash)
90
if
len(nums) == 1:
91
# single entry
92
numbers.append(
__str2int__
(item))
93
elif
len(nums) == 2:
94
# range
95
i, j =
__str2int__
(nums[0]),
__str2int__
(nums[1])
96
if
i > j:
97
click.ClickException(
'Invalid interval '
+item)
98
numbers.extend(list(range(i,j+1)))
99
else
:
100
click.ClickException(
'Malformed option (comma separated list expected): {}'
.format(value))
101
102
return
numbers
103
# ------------------------------------------------------------------------------
afc.toolbox.IntRange
Definition
toolbox.py:8
afc.toolbox.IntRange.min
min
Definition
toolbox.py:19
afc.toolbox.IntRange.clamp
clamp
Definition
toolbox.py:21
afc.toolbox.IntRange.__repr__
__repr__(self)
Definition
toolbox.py:55
afc.toolbox.IntRange.convert
convert(self, value, param, ctx)
Definition
toolbox.py:23
afc.toolbox.IntRange.__init__
__init__(self, min=None, max=None, clamp=False)
Definition
toolbox.py:18
afc.toolbox.IntRange.max
max
Definition
toolbox.py:20
afc.toolbox.__str2int__
__str2int__(value)
Definition
toolbox.py:68
afc.toolbox.split
split(ctx, param, value)
Definition
toolbox.py:60
afc.toolbox.split_ints
split_ints(ctx, param, value)
Definition
toolbox.py:79
Generated on
for DUNE-DAQ by
1.17.0