Example #1
0
    def __new__(metacls, cls, bases, classdict):
        if type(classdict) is dict:
            original_dict = classdict
            classdict = enum._EnumDict()
            for k, v in original_dict.items():
                classdict[k] = v

        if '__getitem__'  not in classdict:
            classdict['__getitem__'] = lambda self, idx: [self.value, self.display][idx]
        if '__len__' not in classdict:
            classdict['__len__'] = lambda self: 2

        return super(ChoicesEnumMeta, metacls).__new__(metacls, cls, bases, classdict)
Example #2
0
 def __new__(metacls, cls, bases, classdict):
     original_dict = classdict
     classdict = enum._EnumDict()
     for k, v in original_dict.items():
         classdict[k] = v
     temp = type(classdict)()
     names = set(classdict._member_names)
     i = 0
     for k in classdict._member_names:
         v = classdict[k]
         if v == ():
             v = i
         else:
             i = v
         i += 1
         temp[k] = v
     for k, v in classdict.items():
         if k not in names:
             temp[k] = v
     return super(auto_enum, metacls).__new__(
             metacls, cls, bases, temp)
Example #3
0
File: utils.py Project: Ecpy/i3py
def create_register_flag(register_name: str,
                         names: Union[tuple, dict],
                         length: int) -> Type[IntFlag]:
    """Create a IntFlag subclass for a bit field.

    Parameters
    ----------
    register_name : str
        Name of the subclass to create.

    names : dict | list
        Mapping between the field names and their index or list of names.
        In the case of a list its length must match length.

    length : int
        Number of fields in the bit field.

    Returns
    -------
    register_flag : IntFlag
        IntFlag subclass whose fields match the register fields. If some of the
        names are not provided, the associated fields are named 'BIT_n' with n
        the bit number.

    """
    register_names = _EnumDict()
    register_names.update({'BIT_%d' % i: 2**i for i in range(length)})
    if isinstance(names, dict):
        for n, i in names.items():
            register_names[n] = 2**i

    elif names:
        if len(names) != length:
            raise ValueError('Register necessitates %d names' % length)

        for i, n in enumerate(names[:]):
            if n:
                register_names[n] = 2**i

    return type(register_name, (IntFlag,), register_names)