-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.py
More file actions
executable file
·61 lines (53 loc) · 2.14 KB
/
rule.py
File metadata and controls
executable file
·61 lines (53 loc) · 2.14 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
from decimal import Decimal
HEADER = "BOTTOM - TOP +/-OFFSET |SPACING| [BANDWIDTH]"
FORMAT = "{:4.4f} MHz - {:4.4f} MHz {:+.1f} MHz |{:.1f} KHz| [{:f} KHz]"
class Rule:
def __init__(self, low, high, offset, spacing, bandwidth="25"):
self.low = Decimal(low)
self.high = Decimal(high)
self.offset = Decimal(offset)
self.spacing = Decimal(spacing)
self.bandwidth = Decimal(bandwidth)
def __hash__(self):
return hash((self.low, self.high, self.offset, self.spacing, self.bandwidth))
def __eq__(self, other):
return (
self.low == other.low
and self.high == other.high
and self.offset == other.offset
and self.spacing == other.spacing
and self.bandwidth == other.bandwidth
)
def __str__(self):
return FORMAT.format(
self.low, self.high, self.offset, self.spacing, self.bandwidth
)
def contains(self, channel, ignore_offset=False):
# Is the output in this rule's range?
if self.low <= channel.output <= self.high:
channel.rules[self] = set()
# Does it have the correct offset?
if ignore_offset or channel.offset == self.offset:
channel.rules[self].add("offset")
else:
return False
# Is it also aligned to this rule's spacing?
# `or 1` accounts for single-channel rules with 0 spacing
if (channel.output - self.low) % ((self.spacing / 1000) or 1) == 0:
channel.rules[self].add("spacing")
else:
return False
# And does it have a small enough bandwidth?
if (channel.bandwidth <= self.bandwidth) or (
# If the rule is ultra-narrow
# assume the channel is ultra-narrow
self.bandwidth
== Decimal("6.25")
):
channel.rules[self].add("bandwidth")
else:
return False
return True
return False
def __contains__(self, channel):
return self.contains(channel, ignore_offset=False)