BTrees

6.3 · active · verified Wed Apr 15

BTrees is a Python package that provides a set of scalable, persistent object containers built around a modified BTree data structure. It is heavily optimized for use within ZODB's "optimistic concurrency" paradigm, offering efficient storage and retrieval of large mappings by only loading relevant nodes into memory. The current version is 6.3, released on November 16, 2025, with an active release cadence, often aligning with Python version support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of an OOBTree (Object-Object BTree), including creation, insertion, access, iteration, existence checks, and deletion. BTrees behave largely like standard Python dictionaries but are optimized for persistence and large datasets.

from BTrees.OOBTree import OOBTree

# Create an in-memory Object-Object BTree
my_btree = OOBTree()

# Insert key-value pairs (like a dictionary)
my_btree['apple'] = 1
my_btree['banana'] = 2
my_btree['cherry'] = 3
my_btree['date'] = 4

print(f"BTree after insertions: {list(my_btree.items())}")

# Access values by key
print(f"Value for 'banana': {my_btree['banana']}")

# Iterate over sorted keys
print("Keys in sorted order:")
for key in my_btree.keys():
    print(key)

# Check for key existence
print(f"'apple' in btree: {'apple' in my_btree}")
print(f"'grape' in btree: {'grape' in my_btree}")

# Delete a key
del my_btree['cherry']
print(f"BTree after deleting 'cherry': {list(my_btree.items())}")

# Example of getting a value with a default
value_or_default = my_btree.get('fig', 'default_value')
print(f"Value for 'fig' (with default): {value_or_default}")

view raw JSON →