Skip to main content

Map options/flags to methods using 32 or 64bit integers for optimized state management.

Project description

tiny_flags

A lightweight Python package for efficient settings management and manipulation. Easily handle language preferences, settings, and other flag-based configurations using minimal memory.

Features

  • 32-bit and 64-bit bitfield support (reserve one 32/64 bit integer)
  • Automatic bit position management
  • Boolean flags and multi-option settings
  • Ordered dictionary configuration for easy use
  • Memory efficient
  • Perfect for storing app settings where complex queries are not required

Installation

Install using pip:

pip install tiny_flags

How to use?

Requires OrderedDict

from collections import OrderedDict
from tiny_flags import TinyFlags

Define your settings

fields = OrderedDict([
    ('language', ['english', 'spanish', 'french']),  # Uses 2 bits, might make sense to add even number and mark the last as '_option'
    ('dark_mode', False),                            # Uses 1 bit, initial value
    ('notifications', True)                          # Uses 1 bit, initial value
])

You can easily create your settings structure by looking at the below example. One should think ahead before implementing the structure of the ordered dictionary and it might make sense to reserve some extra for the list options for later use. NOTE!: The order matters and changing it later can create some issues in migrating to a new structure. Think ahead in what you need, don't remove rather just add. If you don't need an option then perhaps leave it and rename with _ prefix, and just ignore it. Boolean flags are easy, it is just True or False, but the list options take up the same space whether odd or even number of items given. It might make sense for you to add the last option in such list and use _ prefix as for deprecated options mentioned above. Booleans use 1 bit position, and option list uses 1 bit position for every 2 items (even with 1 item 1 bit position is reserved, so be careful about odd numbers)

Initialize manager

manager = TinyFlags(fields)

Default value is 32bit, but give True for 64bit, TinyFlags(fields, True). Check that your environment supports 64bit integers before trying to use them.

Set values

manager.set_value('language', 'spanish')

For options lists just select the category first followed by the option you wish to set.

manager.set_value('dark_mode', True)

For boolean flags, just use the key and give True/False to enable/disable.

Get values

print(manager.get_value('language'))       # 'spanish'
print(manager.get_value('dark_mode'))      # True
print(manager.get_value('notifications'))  # True

Some details and thoughts on using tiny_flags

When using a database there are of course drawbacks on using a bitfield for settings for example if you need complex queries on the bits then put them in their own columns. If you need to do complex queries on the settings, for those kinds of settings it is not the best solution. Indexing will index the full integer not the individual bits. Changing settings later might be cumbersome, but if popularity picks up I can add a migration tool or if you have pioneering spirit please contribute.

Testing and development

Setup Development Environment & clone the repository

git clone https://github.com/FistOfTheNorthStar/tiny_flags
cd tiny_flags

Install pip install -e . Running Tests pytest tests/

There is also a dev_lint.sh script lints and installs the development version. Check it out if you are interested.

Contributions are welcome.

License

Distributed under the MIT License. See LICENSE for more information.

Todo for the future

Add more option mappings possibilities See if 32/64bit values should have a forced type Test with actual DB (MongoDB 32/64 bit ints)

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

tiny_flags-1.0.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

tiny_flags-1.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file tiny_flags-1.0.0.tar.gz.

File metadata

  • Download URL: tiny_flags-1.0.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for tiny_flags-1.0.0.tar.gz
Algorithm Hash digest
SHA256 abe02f53e414099b9afeab09acc42713cf933964d1d786e43ff7e2d47f6035a0
MD5 f8d230e440a38d7a1ba8707e862c7cda
BLAKE2b-256 fc4ddd01c21a47d1aed03c1d29c4e94081a426b0d76a161e5af511ab893b7c84

See more details on using hashes here.

File details

Details for the file tiny_flags-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tiny_flags-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for tiny_flags-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 deec67be1e5f85a284d019cd981a5b8f460b12a9a39847b9005f6b9c36b45137
MD5 8af0face10234ccf8e9d09f7e8689ccb
BLAKE2b-256 81c2d289395ba4a8e4bc9f9c3577ce392118802c8191df58b59442d0428aa8b1

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