Install & Compatibility
Where this runs
tested against v0.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.517s · 505.2MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 30.6s · import 0.481s · 506MB
500MB installed
● package 500MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
struct
✓ from tinsel import struct
✗ from tinsel.schema import struct
The `struct` decorator is directly available from the top-level `tinsel` package.
transform
✓ from tinsel import transform
✗ import tinsel.transform
The `transform` function is a direct import from the `tinsel` package.
This quickstart demonstrates how to define a PySpark schema using Tinsel with Python's `dataclasses` and `NamedTuple`. It then converts this definition into a `StructType` compatible with PySpark and creates a DataFrame with sample data.
from dataclasses import dataclass
from typing import NamedTuple, Optional, Dict, List
from tinsel import struct, transform
from pyspark.sql import SparkSession
# Define nested schema using dataclass
@struct
@dataclass
class UserInfo:
hobby: List[str]
last_seen: Optional[int]
pet_ages: Dict[str, int]
# Define root schema using NamedTuple
@struct
class User(NamedTuple):
login: str
age: int
active: bool
info: Optional[UserInfo]
# Transform the Python class into a PySpark schema
spark_schema = transform(User)
# Prepare sample data matching the defined structure
data = [
User(
login="Ben",
age=18,
active=False,
info=None
),
User(
login="Tom",
age=32,
active=True,
info=UserInfo(
hobby=["pets", "flowers"],
last_seen=16,
pet_ages={
"Jack": 2,
"Sunshine": 6
}
)
)
]
# Initialize SparkSession
spark = SparkSession.builder.master('local').appName("TinselQuickstart").getOrCreate()
# Create DataFrame using the generated schema and data
df = spark.createDataFrame(data=data, schema=spark_schema)
df.printSchema()
df.show(truncate=False)
spark.stop()
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tinsel'
The `tinsel` library is not installed in the current Python environment.
fixRun `pip install tinsel` to install the library.
TypeError: 'StructType' object is not callable
Attempting to call the result of `transform(YourClass)` as if it were a function, or misusing the generated schema object.
fixEnsure the output of `transform()` is assigned to a variable (e.g., `schema = transform(YourClass)`) and then passed to PySpark's `createDataFrame` using the `schema=` keyword argument (e.g., `spark.createDataFrame(data, schema=schema)`).
AttributeError: 'module' object has no attribute 'struct' or 'transform'
This usually means `struct` or `transform` was imported incorrectly, or the `tinsel` package itself is not properly installed or is shadowed by another module.
fixVerify that `from tinsel import struct, transform` is used. Check your Python environment for any conflicting packages named `tinsel` or issues with the installation.
Upgrade
Version history
0.3.0latest on PyPI · released Sep 1, 2018
Audit
Dependencies
pysparkrequiredCore functionality relies on PySpark for DataFrame operations and schema generation.
dataclassesoptionalUsed for schema definition; built-in in Python 3.7+, requires backport for Python 3.6.