scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
401 stars 67 forks source link

Dtype could be callable #304

Closed scott-griffiths closed 5 months ago

scott-griffiths commented 7 months ago

We could add something like this to the Dtype class:

    def __call__(self, value) -> Bits:
        b = Bits()  # TODO: Inefficient to create then set
        self.set_fn(b, value)
        return b

This would let you call a Dtype to create an object of that type:

>>> a = Dtype('uint12')
>>> a(4095)
Bits('0xfff')
scott-griffiths commented 5 months ago

This has ended up being the parse method, which should be in 4.2.