Skip to main content

Bitwise support for configuration

Project description

BitAware

Bitwise Toolbox

python ruff pytest markdown License: GPLv3

Usage

Define

from bitaware import BitFlag, BitAware

class PermissionTypes(BitFlag):
    READ = 1
    CREATE = 2
    EDIT = 4
    ADMIN = 8


class UserPermission(BitAware[PermissionTypes]):
    def __init__(self, value: int):
        super().__init__(value, PermissionTypes)

    pass

    # "User Roles" Named sets can be used interchangeably
    READER = PermissionTypes.READ
    CREATOR = PermissionTypes.CREATE | PermissionTypes.READ
    EDITOR = PermissionTypes.EDIT | PermissionTypes.READ

Simple Use

Standalone:

permission = UserPermission(PermissionTypes.ADMIN | PermissionTypes.READ)

print(permission.has(PermissionTypes.ADMIN))  # True
print(permission.has(PermissionTypes.CREATE))  # False

Pydantic

As Pydantic field:

class User(BaseModel):
    name: str
    permissions: UserPermission

Usage

user = User(name="test_user", permissions=UserPermission.READER)
admin = User(name="admin_user", permissions=PermissionTypes.READ | PermissionTypes.ADMIN)

print(user.model_dump_json(indent=2))

Above code will print:

{
  "name": "test_user",
  "permissions": 1
}

Misc

x = UserPermission(12)

print(str(x)) # EDIT|ADMIN [EDIT, ADMIN]
print(repr(x)) # UserPermission(PermissionTypes.EDIT | PermissionTypes.ADMIN)
print(list(x)) # [<PermissionTypes.EDIT: 4>, <PermissionTypes.ADMIN: 8>]

For predefined named flag set:

x = UserPermission(UserPermission.CREATOR)

print(str(x)) # CREATOR [READ, CREATE]
print(repr(x)) # UserPermission(PermissionTypes.READ | PermissionTypes.CREATE)
print(list(x)) # [<PermissionTypes.READ: 1>, <PermissionTypes.CREATE: 2>]

Predefined named flag sets:

print(UserPermission.properties()) # {<PermissionTypes.READ|CREATE: 3>: 'CREATOR', <PermissionTypes.READ|EDIT: 5>: 'EDITOR', <PermissionTypes.READ: 1>: 'READER'}

Test

uv run pytest

Security

If you discover any security-related issues, please email email instead of using the issue tracker.


Copyright (c) 2025 Wojciech Nowicki

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitaware-0.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bitaware-0.2.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file bitaware-0.2.0.tar.gz.

File metadata

  • Download URL: bitaware-0.2.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitaware-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4e0bbf281e05348b03b49cdd0b642fec32f624c9e2149206fe14e63bd2c94d2e
MD5 dc8e61614d3f62c6943dc336634cd0b1
BLAKE2b-256 03c3b410f30c126bee71b0c4fdb2a04f436b046061c8fe1f699386d3b5282094

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitaware-0.2.0.tar.gz:

Publisher: publish.yml on wnowicki/bitaware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitaware-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: bitaware-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitaware-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 784af35c5991d1ece890117aedf8c43a969b845ff9e020f7b91917718c4cd681
MD5 5bd3b1c053fc5079c2777e124df715c7
BLAKE2b-256 7deea1ff2f79af98e108d81328170369bf9826f55d03f80e9b9da51efa49d9f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitaware-0.2.0-py3-none-any.whl:

Publisher: publish.yml on wnowicki/bitaware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page