# BitField for [motorengine](https://github.com/heynemann/motorengine)
Custom field type for storing and retrieving bit-packed flags as enums.
Inspiration from [`enum.IntFlag` documentation](https://docs.python.org/3.6/library/enum.html#intflag).
## Installation
`motorengine-bitfield` requires a minimum Python version of 3.6.2.
Add the code directly to your project or install from pip with:
```shell
pip install motorengine-bitfield
```
## Examples
```python
from bitfield import BitField
# create an enum to use in our example
class UserFlags(enum.IntFlag):
IS_ACTIVE = enum.auto() # least-significant bit
IS_ADMIN = enum.auto()
HAS_LOGGED_IN = enum.auto()
HAS_SEEN_INTRO = enum.auto()
SUBSCRIBED_TO_NEWSLETTER = enum.auto() # most-significant bit
# use the enum in our Document mapping
class User(motorengine.Document):
name = StringField()
email = StringField()
flags = BitField(UserFlags)
# let's test one out!
user = User(
name='Bit Field',
email='example@bitfield.py',
flags=(UserFlags.IS_ACTIVE | UserFlags.SUBSCRIBED_TO_NEWSLETTER),
)
# enum instances are serialized into an integer
user.to_son()
# {'name': 'Bit Field', 'email': 'example@bitfield.py', 'flags': 17}
user = loop.run_until_complete(
user.save()
)
# and deserialized into the original enum
retrieved_user = loop.run_until_complete(
User.objects.get(user._id)
)
retrieved_user.flags
# <UserFlags.SUBSCRIBED_TO_NEWSLETTER|IS_ACTIVE: 17>
bool(user.flags & UserFlags.IS_ADMIN)
# False
# a BitField value also be created with plain integers
int_user = loop.run_until_complete(
User.objects.create(flags=(2**5 - 1))
)
loop.run_until_complete(User.objects.get(int_user._id)).flags
# <UserFlags.SUBSCRIBED_TO_NEWSLETTER|HAS_SEEN_INTRO|HAS_LOGGED_IN|IS_ADMIN|IS_ACTIVE: 31>
# or from strings of binary
str_user = loop.run_until_complete(
User.objects.create(flags='01101'))
)
loop.run_until_complete(User.objects.get(str_user._id)).flags
# <UserFlags.HAS_SEEN_INTRO|HAS_LOGGED_IN|IS_ACTIVE: 13>
```
## Development
Tests are written with pytest and can be run with `python setup.py test`.
Pull requests always welcome!
Custom field type for storing and retrieving bit-packed flags as enums.
Inspiration from [`enum.IntFlag` documentation](https://docs.python.org/3.6/library/enum.html#intflag).
## Installation
`motorengine-bitfield` requires a minimum Python version of 3.6.2.
Add the code directly to your project or install from pip with:
```shell
pip install motorengine-bitfield
```
## Examples
```python
from bitfield import BitField
# create an enum to use in our example
class UserFlags(enum.IntFlag):
IS_ACTIVE = enum.auto() # least-significant bit
IS_ADMIN = enum.auto()
HAS_LOGGED_IN = enum.auto()
HAS_SEEN_INTRO = enum.auto()
SUBSCRIBED_TO_NEWSLETTER = enum.auto() # most-significant bit
# use the enum in our Document mapping
class User(motorengine.Document):
name = StringField()
email = StringField()
flags = BitField(UserFlags)
# let's test one out!
user = User(
name='Bit Field',
email='example@bitfield.py',
flags=(UserFlags.IS_ACTIVE | UserFlags.SUBSCRIBED_TO_NEWSLETTER),
)
# enum instances are serialized into an integer
user.to_son()
# {'name': 'Bit Field', 'email': 'example@bitfield.py', 'flags': 17}
user = loop.run_until_complete(
user.save()
)
# and deserialized into the original enum
retrieved_user = loop.run_until_complete(
User.objects.get(user._id)
)
retrieved_user.flags
# <UserFlags.SUBSCRIBED_TO_NEWSLETTER|IS_ACTIVE: 17>
bool(user.flags & UserFlags.IS_ADMIN)
# False
# a BitField value also be created with plain integers
int_user = loop.run_until_complete(
User.objects.create(flags=(2**5 - 1))
)
loop.run_until_complete(User.objects.get(int_user._id)).flags
# <UserFlags.SUBSCRIBED_TO_NEWSLETTER|HAS_SEEN_INTRO|HAS_LOGGED_IN|IS_ADMIN|IS_ACTIVE: 31>
# or from strings of binary
str_user = loop.run_until_complete(
User.objects.create(flags='01101'))
)
loop.run_until_complete(User.objects.get(str_user._id)).flags
# <UserFlags.HAS_SEEN_INTRO|HAS_LOGGED_IN|IS_ACTIVE: 13>
```
## Development
Tests are written with pytest and can be run with `python setup.py test`.
Pull requests always welcome!
Release files for motorengine-bitfield 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| motorengine-bitfield-1.0.0.tar.gz | 16.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| motorengine_bitfield-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.4 kB
Release files / motorengine-bitfield-1.0.0.tar.gz
| Download URL | motorengine-bitfield-1.0.0.tar.gz |
|---|---|
| Size | 16.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79b47c01dcff8cc7803ade2181a62a9f208ab753466635c0f59d58637e5f4459
|
|
BLAKE2b-256 checksum How to use checksums |
ae09e6e65ec100e81b82faf53da36d24250d25531d94b0f9c3d50e1505dd548f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / motorengine_bitfield-1.0.0-py3-none-any.whl
| Download URL | motorengine_bitfield-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6cea85bb297ff9e95946b41644b67e6be3fd2d4fdaa1bddc9cf62d25717c8475
|
|
BLAKE2b-256 checksum How to use checksums |
b76363ae8051d491cafd913603928b377cd518063836c69b3b1ad0362f5ae0da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |