Skip to main content

tiny_flags

A lightweight Python package for efficient settings management using bitfields. Handle language preferences, feature flags, and other configurations using a single 32 or 64-bit integer.

Features

  • 32-bit and 64-bit bitfield support
  • Automatic bit position management
  • Boolean flags and multi-option settings
  • Ordered dictionary configuration
  • Zero runtime dependencies
  • Type-checked with mypy strict mode

Installation

pip install tiny_flags

Quick Start

from collections import OrderedDict
from tiny_flags import TinyFlags

# Define your settings
fields = OrderedDict([
    ('language', ['english', 'spanish', 'french']),  # Uses 2 bits
    ('dark_mode', False),                            # Uses 1 bit
    ('notifications', True)                          # Uses 1 bit
])

# Initialize (default 32-bit, pass bits64=True for 64-bit)
manager = TinyFlags(fields)

# Set values
manager.set_value('language', 'spanish')
manager.set_value('dark_mode', True)

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

# Persist the entire state as a single integer
state = manager.bitfield.value       # e.g. 13

# Restore from that integer later
new_manager = TinyFlags(fields)
new_manager.bitfield.value = state
new_manager.get_value('language')    # 'spanish'

How Bit Widths Work

Field type Bits used Example
Boolean 1 ('dark_mode', False)
2 options 1 ('toggle', ['on', 'off'])
3-4 options 2 ('lang', ['en', 'es', 'fr'])
5-8 options 3 8 options fit in 3 bits
9-16 options 4 16 options fit in 4 bits

Option lists must have at least 2 items. Bit allocation is always based on powers of 2, so odd-count lists leave unused slots. Consider reserving those for future options.

Design Considerations

Order matters. The OrderedDict key order determines bit positions. Changing the order after data is persisted will corrupt stored values. When evolving your schema:

  • Add new fields at the end
  • Don't remove fields — rename unused ones with a _ prefix and ignore them

Database storage. The bitfield value is a single integer, making it efficient to store. However, you cannot query individual flags with database indexes — the index covers the whole integer. If you need complex queries on specific flags, store those as separate columns.

32 vs 64-bit. Default is 32-bit (up to 32 flags/option bits). Pass bits64=True for 64-bit support. Ensure your storage layer supports 64-bit integers before using this mode.

Development

git clone https://github.com/FistOfTheNorthStar/tiny_flags
cd tiny_flags
pip install -e ".[dev]"

Run checks

pytest --cov=tiny_flags           # tests + coverage
ruff check src/ tests/            # linting
mypy src/                         # type checking

Contributions are welcome.

License

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

Metadata

Release files for tiny-flags 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tiny-flags 2.0.0
File Size Uploaded
tiny_flags-2.0.0.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tiny-flags 2.0.0
File Interpreter ABI Platform
tiny_flags-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / tiny_flags-2.0.0.tar.gz

Download URL tiny_flags-2.0.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
95a8825c71a57f1c3abd46683e544233fb14b6a2e2cd04f15d10cceb34ae5965
BLAKE2b-256 checksum
How to use checksums
c64131b7c3177d8f3aac54c5d70a285405cecc2e4a3769e78e65db71784e7487
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / tiny_flags-2.0.0-py3-none-any.whl

Download URL tiny_flags-2.0.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93beae4d21855e1d45ecfac8750914dcbdad255f7c59385bd7cd89367d8841c4
BLAKE2b-256 checksum
How to use checksums
8084142d8c05acaa8c47bbb8f465bc04acbf289b6dc3a88ad5ca7782c1f46361
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page