Custom field type for storing and retrieving bit-packed flags as enums.
Project description
# 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!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file motorengine-bitfield-1.0.0.tar.gz
.
File metadata
- Download URL: motorengine-bitfield-1.0.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79b47c01dcff8cc7803ade2181a62a9f208ab753466635c0f59d58637e5f4459 |
|
MD5 | 79ecf1dff06b244a564b42b4d4d26f77 |
|
BLAKE2b-256 | ae09e6e65ec100e81b82faf53da36d24250d25531d94b0f9c3d50e1505dd548f |
File details
Details for the file motorengine_bitfield-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: motorengine_bitfield-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cea85bb297ff9e95946b41644b67e6be3fd2d4fdaa1bddc9cf62d25717c8475 |
|
MD5 | 4cc1400a92d33f737d3b32e37d764d78 |
|
BLAKE2b-256 | b76363ae8051d491cafd913603928b377cd518063836c69b3b1ad0362f5ae0da |