Skip to main content

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

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.3.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.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bitaware-0.3.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.12

File hashes

Hashes for bitaware-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bfd92030204350d3e148635cfff325611026e87a701fba535fcff5fb530fd39b
MD5 4204794ff57734a88f09c678b57cedd7
BLAKE2b-256 a86052c2b7f67fdec8bd44fdf4559d726934bdcff0dbb53bbaa0f0835ea2b11a

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitaware-0.3.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: bitaware-0.3.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.12

File hashes

Hashes for bitaware-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5c121b907e55f07b185db29b9d73166ac2b1b4d7b3ea1aceadad02ad7d920cc
MD5 0436db08d5a77887f8856f0fd67e193e
BLAKE2b-256 7217a57dcb89926a934fa55113709ac88bbf3d5df193b735eefac75e00f4ad5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitaware-0.3.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 Sentry Error logging StatusPage Status page