Skip to main content

Python alternative (binary) flags

Project description

alt-flags

altflags allows you to easily map, parse and manipulate binary flags

Why?

  1. The built in Python Flags and IntFlags didn't fit my needs
  2. Simple usage to handle binary flag mapping, parsing and manipulation
  3. Needs to run super efficiently and quick (same thing?)
  4. This is my first public package, it's small and easy to maintain

Quick Start

1. Install with pip from PyPi

python -m pip install altflags

2. Create altflags, Flags class

from altflags import Flags, flag

class Permissions(Flags):
    create_message = flag(0)
    delete_message = flag(1)
    edit_message = flag(2)

user_permissions = Permissions()

3. Edit your flags

# Set create_message and edit_message flags to true
user_permissions.create_message = True
user_permissions.edit_message = True

# print flags as binary
print("{:0b}".format(user_permissions.flags))
# >>> 101
# all flags are False (0) from initialization

# print flags as integer
print({:0n}.format(user_permissions.flags))
# >>> 5

4. Compare flags

user2_permissions = Permission()
user2_permissions.create_message = True
user2_permissions.edit_message = True

print(user_permissions == user2_permissions)
# >>> True

user2_permissions.create_message = False
print(user_permissions == user2_permissions)
# >>> False

5. Extend altflags with class methods that return pre-formatted flag objects

class Permissions(Flags):
    create_message = flag(0)
    delete_message = flag(1)
    edit_message = flag(2)

    @classmethod
    def all(cls):
        new_cls = cls()
        new_cls.create_message = True
        new_cls.delete_message = True
        new_cls.edit_message = True
        return new_cls

user_permissions = Permissions.all()

print({:0b}.format(user_permissions))
# >>> 111

print({:0n}.format(user_permissions))
# >>> 7

Notes

  • flags(n: int) n argument specifies the bit position of your flag (Warning: These can be overwritten).

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

altflags-0.0.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

altflags-0.0.3-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file altflags-0.0.3.tar.gz.

File metadata

  • Download URL: altflags-0.0.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.2

File hashes

Hashes for altflags-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3832943ec68faa6c270f07acbb7150e0d0d02164a568ae1a1f9af3eef6f0b521
MD5 8bf8457c45a97cbde2b2c6e59017300e
BLAKE2b-256 91e2e4e4ec7fadcb53e235a9aeec8b0b85915e82feb0107ec089e91ef28fd077

See more details on using hashes here.

File details

Details for the file altflags-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: altflags-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.2

File hashes

Hashes for altflags-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ad6c8d7a29bc96372c971de4a6629440dd5cccdedc14e6c49f5f35ab7ad7e84e
MD5 b9b7794821e8287fc013b493ab99d4d3
BLAKE2b-256 149ccd3c3e8ebbfa591511dc32e811737ec58caee1c1ed74cd95c22b3b6bf8ab

See more details on using hashes here.

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