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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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