Mabasej_Team/.venv/lib/python3.9/site-packages/hyperframe/flags.py
Untriex Programming ed6afdb5c9 new
2021-03-17 08:57:57 +01:00

49 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
"""
hyperframe/flags
~~~~~~~~~~~~~~~~
Defines basic Flag and Flags data structures.
"""
import collections
from collections.abc import MutableSet
Flag = collections.namedtuple("Flag", ["name", "bit"])
class Flags(MutableSet):
"""
A simple MutableSet implementation that will only accept known flags as
elements.
Will behave like a regular set(), except that a ValueError will be thrown
when .add()ing unexpected flags.
"""
def __init__(self, defined_flags):
self._valid_flags = set(flag.name for flag in defined_flags)
self._flags = set()
def __repr__(self):
return repr(sorted(list(self._flags)))
def __contains__(self, x):
return self._flags.__contains__(x)
def __iter__(self):
return self._flags.__iter__()
def __len__(self):
return self._flags.__len__()
def discard(self, value):
return self._flags.discard(value)
def add(self, value):
if value not in self._valid_flags:
raise ValueError(
"Unexpected flag: {}. Valid flags are: {}".format(
value, self._valid_flags
)
)
return self._flags.add(value)