Skip to main content

Type-preserving key-value store for Tango database

Project description

Tango Free Properties with Type Preservation

A Python module for managing key-value pairs in Tango database's free property section while preserving Python types.

Properties go into the TangoKeystore namespace by default.

Jive free properties

Problem

Tango database stores all properties as strings. When you store a float like 1.2, it gets stored as the string '1.2'. This module solves this by storing type metadata alongside the value, enabling proper type restoration upon retrieval.

Features

  • ✅ Preserves original Python types (int, float, str, bool, list, dict, None)
  • ✅ Automatic serialization/deserialization using JSON
  • ✅ Simple, intuitive API
  • ✅ Bulk operations (get_all, clear)
  • ✅ Key existence checking
  • ✅ No default values, keys must exist before being accessed
  • ✅ Keys must be explicitly created before being accessed
    • either with must_exist=False or by using the create method.
  • ✅ Key name validation: r"^[a-zA-Z_][a-zA-Z0-9_]*$"
    • this means keys are also valid python identifiers

The command line tool takes input in the JSON format whereas the python module uses Python types.

Value Type Parsing

The CLI automatically detects and converts value types:

Input Parsed Type Example
42 int count=42
-10 int offset=-10
3.14 float pi=3.14
-2.5 float delta=-2.5
true, false bool enabled=true
none, null NoneType optional=none
[1,2,3] list values=[1,2,3]
{"a":1} dict config={"timeout":30}
hello str name=hello

Installation

tango-keystore is on PyPI and conda-forge. Install it with your preferred package manager: pip, uv, conda or pixi.

pip install tango-keystore

Usage

Basic Example

from tango_keystore import TangoKeystore

# Initialize
keystore = TangoKeystore()

# Store values with automatic type preservation
keystore.put("omega_reference", 1.2, must_exist=False)
keystore.put("iteration_count", 42, must_exist=False)
keystore.put("is_enabled", True, must_exist=False)

# Retrieve with correct types
omega = keystore.get("omega_reference")  # Returns float 1.2
count = keystore.get("iteration_count")  # Returns int 42
enabled = keystore.get("is_enabled")    # Returns bool True

print(type(omega))  # <class 'float'>

Complex Types

# Store lists and dictionaries
keystore.put("coordinates", [10.5, 20.3, 30.1])
keystore.put("config", {"timeout": 30, "retry": 3})

# Retrieve with proper types
coords = keystore.get("coordinates")  # Returns list
config = keystore.get("config")       # Returns dict

utilities

# get history for a Key (timestamp is epoch)
history = keystore.get_history("omega_reference")
[{'key': 'omega_reference',
  'value': 1.2,
  'type': 'float',
  'timestamp': 1763123518.0},
 {'key': 'omega_reference',
  'value': 4.2,
  'type': 'float',
  'timestamp': 1763123535.0},
 {'key': 'omega_reference',
  'value': 4.2,
  'type': 'float',
  'timestamp': 1763123740.0}]

# feature flags
keystore.put("room_temperature", "on", must_exist=False)
keystore.is_active("room_temperature")
True
keystore.is_on("room_temperature")
True
keystore.is_active("room_temperature")
True
keystore.is_inactive("room_temperature")
False
keystore.is_off("room_temperature")
False

Additional Operations

# Check if key exists
if keystore.exists("omega_reference"):
    print("Key exists!")

# get with metadata
keystore.get_with_metadata("temperature")
{'value': 35.0, 'type': 'float'}

# Get all keys
all_keys = keystore.get_all_key_names()

# Get all properties as dictionary
all_props = keystore.get_all()

# Delete a property
keystore.delete("omega_reference")

# Clear all properties, requires confirmation code.
# Freja+ coming soon.
keystore.clear()
Type confirmation code to continue => 18398  : 18398

Using Custom Database Connection

import tango

# Use specific database
db = tango.Database("host", "port")
keystore = TangoKeystore(db=db)

API Reference

TangoKeystore(namespace="FunkyKeystore)

Initialize the manager with optional namespace.

put(key, value)

Store a value with type preservation.

  • key: Property name (str)
  • value: Value to store (must be JSON-serializable)

get(key)

Retrieve a value with type restoration.

  • key: Property name (str)
  • Returns: Original value with proper type

delete(key)

Delete a property.

exists(key)

Check if a property exists.

  • Returns: bool

get_all_keys()

Get all property keys.

  • Returns: list of keys

get_all()

Get all properties as a dictionary.

  • Returns: dict mapping keys to typed values

clear()

Delete all properties (use with caution!).

How It Works

The module wraps each value in a JSON object containing both the value and its type:

# When you call: keystore.put("omega_reference", 1.2)
# It stores: '{"value": 1.2, "type": "float"}'

# When you call: keystore.get("omega_reference")
# It retrieves the JSON, extracts the value and type, and returns: 1.2 (as float)

Supported Types

  • int
  • float
  • str
  • bool
  • list
  • dict
  • NoneType (None)

Error Handling

  • Raises TypeError for unsupported types
  • Raises ValueError for corrupted or invalid data
  • Returns default value if key doesn't exist (instead of raising exception)

License

See LICENSE file.

Disclaimer

This package was initially produced with AI tools from Claude by Anthropic.

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

tango_keystore-0.5.0.tar.gz (148.1 kB view details)

Uploaded Source

Built Distribution

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

tango_keystore-0.5.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file tango_keystore-0.5.0.tar.gz.

File metadata

  • Download URL: tango_keystore-0.5.0.tar.gz
  • Upload date:
  • Size: 148.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tango_keystore-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d139601e07bde4c80792c6c54e6efb89fa3c5478439d4ffa8209dc3027763166
MD5 19b1ea06c8ca2d1a0d1b1421bc8cc94c
BLAKE2b-256 2c228e38975cf083156a50cd98c7f6f971453c242bb1f210223b175a2fbe4142

See more details on using hashes here.

File details

Details for the file tango_keystore-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: tango_keystore-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for tango_keystore-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 917e9152a60f818f0f1680fea766074185754e97c2664fe194dc3c1f57a23cb7
MD5 b211d42a3d0416a854f90e12a5fdd262
BLAKE2b-256 c41b132c80300332129c5f32fd2dd65098422e6509b6aba95a88f3afc8798a6c

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