diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-06-08 23:37:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-08 23:37:54 +0000 |
| commit | 601637bd48fcba826da01725430268f706181449 (patch) | |
| tree | 5b58723b931450d358d7e4387d87cc8e8b9166b2 /codegen/lib/utils.py | |
| parent | ea7249fb77a8e07d232600081c9c3df5f698d70f (diff) | |
| parent | fb1d419a3d4207a293a1ad6001253192f1b4d12f (diff) | |
| download | azalea-drasl-601637bd48fcba826da01725430268f706181449.tar.xz | |
Merge pull request #7 from mat-1/1.19
1.19
Diffstat (limited to 'codegen/lib/utils.py')
| -rw-r--r-- | codegen/lib/utils.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/codegen/lib/utils.py b/codegen/lib/utils.py new file mode 100644 index 00000000..c185c0e5 --- /dev/null +++ b/codegen/lib/utils.py @@ -0,0 +1,46 @@ +import re + +# utilities that could be used for things other than codegen + + +def to_snake_case(name: str): + s = re.sub('([A-Z])', r'_\1', name) + return s.lower().strip('_') + + +def to_camel_case(name: str): + s = re.sub('_([a-z])', lambda m: m.group(1).upper(), name) + return s[0].upper() + s[1:] + + +def padded_hex(n: int): + return f'0x{n:02x}' + + +class PacketIdentifier: + def __init__(self, packet_id: int, direction: str, state: str): + self.packet_id = packet_id + self.direction = direction + self.state = state + + def __eq__(self, other): + return self.packet_id == other.packet_id and self.direction == other.direction and self.state == other.state + + def __hash__(self): + return hash((self.packet_id, self.direction, self.state)) + + def __str__(self): + return f'{self.packet_id} {self.direction} {self.state}' + + def __repr__(self): + return f'PacketIdentifier({self.packet_id}, {self.direction}, {self.state})' + + +def group_packets(packets: list[PacketIdentifier]): + packet_groups: dict[tuple[str, str], list[int]] = {} + for packet in packets: + key = (packet.direction, packet.state) + if key not in packet_groups: + packet_groups[key] = [] + packet_groups[key].append(packet.packet_id) + return packet_groups |
