varmeta
varmeta is a lightweight Python package for managing variable metadata in scientific, engineering, and data analysis workflows. It automatically unpacks multi-component variables and generates pandas DataFrames with rich multi-level column headers.
Purpose
Scientific and engineering data often has variables with multiple components (e.g., x, y, and z components of a force vector). varmeta makes it easy to:
- Define variables once with metadata (name, units, description, components)
- Automatically unpack component variables (e.g.,
force→force__x,force__y,force__z) - Generate pandas DataFrames with multi-index headers showing keys, names, and units
- Serialize and deserialize variable metadata to/from JSON
Quick-start tutorial
1. Define variables with metadata
import varmeta as vm
TEMP = "temp"
FORCE = "force"
# Scalar variable
temperature = vm.Var(
key=TEMP,
name="Temperature",
units="Celsius",
description="Ambient temperature",
)
# Vector variable (e.g., 3D force)
force = vm.Var(
key=FORCE,
name="Force",
units="N",
description="Force vector",
components=("x", "y", "z"),
component_axis=1,
)
2. Create a VarSet collection
# Create a VarSet from a list of Var instances
varset = vm.VarSet([temperature, force])
# Dict-like access by key
print(varset[TEMP]) # Temperature [Celsius]
print(varset[FORCE]) # Force [N]
3. Unpack and pack data with components
Component variables can be unpacked (expanded) and packed (reconstructed):
data = {TEMP: 25.0, FORCE: [10.0, 20.0, 30.0]}
# Unpack: expand components
unpacked_varset, unpacked_data = varset.unpack(data)
print(unpacked_data)
# {'temp': 25.0, 'force__x': 10.0, 'force__y': 20.0, 'force__z': 30.0}
# Pack: reconstruct original structure
packed_data = varset.pack(unpacked_data)
print(packed_data)
# {'temp': 25.0, 'force': [10.0, 20.0, 30.0]}
Component separator: Double underscore __ (e.g., force__x) minimizes collision risk with regular variable names.
Optional error handling:
unpack(data, raise_extra=True): Raise error if data contains extra keys not in VarSet (default: silently ignore)pack(data, raise_missing=True): Raise error if VarSet variables are missing from data (default: silently skip)
4. Create pandas DataFrames
It's easy to tabulate data into a DataFrame. Components are automatically unpacked, even from numpy arrays!
# Single dict → DataFrame
data = {TEMP: [30, 40], FORCE: [[200, 250, -30], [300, 350, -100]]}
df = varset.to_dataframe(data)
print(df)
# key temp force__x force__y force__z
# name Temperature Force - x Force - y Force - z
# units Celsius N N N
# 0 30 200 250 -30
# 1 40 300 350 -100
5. Create DataFrames from records
You can also pass a list of data dictionaries (records):
records = [
{TEMP: 30, FORCE: [200, 250, -30]},
{TEMP: 40, FORCE: [300, 350, -100]},
]
df = varset.to_dataframe(records)
print(df)
# key temp force__x force__y force__z
# name Temperature Force - x Force - y Force - z
# units Celsius N N N
# 0 30 200 250 -30
# 1 40 300 350 -100
Serialization
You can serialize VarSet metadata to JSON and reconstruct it later:
import json
# Serialize to JSON-compatible dict
var_data = varset.to_dict()
json_str = json.dumps(var_data)
# Deserialize from dict
varset_recreated = vm.VarSet.from_dict(json.loads(json_str))
# Verify equality
for key in varset:
print(f"{key}: {'matches!' if varset[key] == varset_recreated[key] else 'differs!'}")
# Output:
# temp: matches!
# force: matches!
Metadata
Release files for varmeta 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| varmeta-0.2.2.tar.gz | 35.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| varmeta-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 46.5 kB
Release files / varmeta-0.2.2.tar.gz
| Download URL | varmeta-0.2.2.tar.gz |
|---|---|
| Size | 35.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1fe72bad6dea840b07fc993665a821664ad694c88b39254d19fe5b162aae2287
|
|
BLAKE2b-256 checksum How to use checksums |
a3983cb162f274ed67b15240fdc7ea64d319e105daffe7d3ea8e6ed0769019cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / varmeta-0.2.2-py3-none-any.whl
| Download URL | varmeta-0.2.2-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8301cab66e47546671bff68135c25cbc974c98aa2dacf8cecb70d37b2155793e
|
|
BLAKE2b-256 checksum How to use checksums |
15e005c1c4685de046bd4b428f395c8435f10317cd6f8c1d8d273c25930f81d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|