Support for flags
| 1426 | |
| 1427 | |
| 1428 | class Flag(Enum, boundary=STRICT): |
| 1429 | """ |
| 1430 | Support for flags |
| 1431 | """ |
| 1432 | |
| 1433 | _numeric_repr_ = repr |
| 1434 | |
| 1435 | @staticmethod |
| 1436 | def _generate_next_value_(name, start, count, last_values): |
| 1437 | """ |
| 1438 | Generate the next value when not given. |
| 1439 | |
| 1440 | name: the name of the member |
| 1441 | start: the initial start value or None |
| 1442 | count: the number of existing members |
| 1443 | last_values: the last value assigned or None |
| 1444 | """ |
| 1445 | if not count: |
| 1446 | return start if start is not None else 1 |
| 1447 | last_value = max(last_values) |
| 1448 | try: |
| 1449 | high_bit = _high_bit(last_value) |
| 1450 | except Exception: |
| 1451 | raise TypeError('invalid flag value %r' % last_value) from None |
| 1452 | return 2 ** (high_bit+1) |
| 1453 | |
| 1454 | @classmethod |
| 1455 | def _iter_member_by_value_(cls, value): |
| 1456 | """ |
| 1457 | Extract all members from the value in definition (i.e. increasing value) order. |
| 1458 | """ |
| 1459 | for val in _iter_bits_lsb(value & cls._flag_mask_): |
| 1460 | yield cls._value2member_map_.get(val) |
| 1461 | |
| 1462 | _iter_member_ = _iter_member_by_value_ |
| 1463 | |
| 1464 | @classmethod |
| 1465 | def _iter_member_by_def_(cls, value): |
| 1466 | """ |
| 1467 | Extract all members from the value in definition order. |
| 1468 | """ |
| 1469 | yield from sorted( |
| 1470 | cls._iter_member_by_value_(value), |
| 1471 | key=lambda m: m._sort_order_, |
| 1472 | ) |
| 1473 | |
| 1474 | @classmethod |
| 1475 | def _missing_(cls, value): |
| 1476 | """ |
| 1477 | Create a composite member containing all canonical members present in `value`. |
| 1478 | |
| 1479 | If non-member values are present, result depends on `_boundary_` setting. |
| 1480 | """ |
| 1481 | if not isinstance(value, int): |
| 1482 | raise ValueError( |
| 1483 | "%r is not a valid %s" % (value, cls.__qualname__) |
| 1484 | ) |
| 1485 | # check boundaries |
no outgoing calls
searching dependent graphs…