Python LevelDB Bindings (Unmaintained)

0.201 · abandoned · verified Wed Apr 15

This package provides Python bindings for the LevelDB key-value store. It allows Python applications to interact with LevelDB databases for embedded, persistent key-value storage. Note: This specific PyPI package (`leveldb`) is largely unmaintained (last release 2017). Version 0.201. Releases are infrequent to non-existent.

Warnings

Install

Imports

Quickstart

This example demonstrates opening a LevelDB database, putting and getting key-value pairs (which must be bytes), iterating over all entries, and handling missing keys.

import leveldb
import os
import shutil

# Define a temporary database path
db_path = './my_temp_leveldb'

# Ensure clean slate for demonstration
if os.path.exists(db_path):
    shutil.rmtree(db_path)

try:
    # Open the database, creating it if it doesn't exist
    # Note: create_if_missing is implicitly True by default
    db = leveldb.LevelDB(db_path)
    print(f"Database opened at {db_path}")

    # Put a key-value pair (keys and values must be bytes)
    db.Put(b'name', b'Alice')
    db.Put(b'age', b'30')
    print("Put: name -> Alice, age -> 30")

    # Get a value
    name = db.Get(b'name')
    print(f"Get 'name': {name.decode('utf-8')}")

    # Iterate through keys
    print("All keys in DB:")
    for key, value in db.RangeIter():
        print(f"  {key.decode('utf-8')}: {value.decode('utf-8')}")

    # Try to get a non-existent key (raises KeyError)
    try:
        db.Get(b'city')
    except KeyError:
        print("Successfully caught KeyError for non-existent key 'city'")

finally:
    # Clean up the database directory
    if os.path.exists(db_path):
        shutil.rmtree(db_path)
        print(f"Cleaned up database at {db_path}")

view raw JSON →