Collection of common python utils for machine learning and scientific computing workflows
—
Enhanced dataclass functionality with automatic type casting, context management, improved representation, and advanced field handling for robust data structures in Python applications.
Improved dataclass decorator with additional functionality.
def dataclass(
cls: type | None = None,
*,
init: bool = True,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: bool = False,
frozen: bool = False,
**kwargs
) -> type:
"""
Enhanced dataclass decorator with additional features.
Args:
cls: Class to decorate (when used without parentheses)
init: Generate __init__ method
repr: Generate __repr__ method
eq: Generate __eq__ method
order: Generate ordering methods
unsafe_hash: Generate __hash__ method
frozen: Make instances immutable
**kwargs: Additional dataclass options
Returns:
Enhanced dataclass with improved functionality
"""Enhanced field definition with additional options.
def field(
*,
default: Any = MISSING,
default_factory: Callable[[], Any] = MISSING,
init: bool = True,
repr: bool = True,
hash: bool | None = None,
compare: bool = True,
metadata: dict | None = None,
**kwargs
) -> Any:
"""
Enhanced field definition for dataclasses.
Args:
default: Default value for the field
default_factory: Function to generate default value
init: Include field in __init__
repr: Include field in __repr__
hash: Include field in __hash__
compare: Include field in comparison methods
metadata: Additional field metadata
**kwargs: Additional field options
Returns:
Field descriptor with enhanced functionality
"""Automatic type casting functionality for dataclass fields.
class AutoCast:
"""
Automatic type casting for dataclass fields.
Enables automatic conversion of field values to specified types
during dataclass instantiation.
"""
def __init__(self, cast_fn: Callable[[Any], Any]) -> None:
"""
Initialize AutoCast with casting function.
Args:
cast_fn: Function to cast values to desired type
"""
self.cast_fn = cast_fn
def __call__(self, value: Any) -> Any:
"""
Apply casting function to value.
Args:
value: Value to cast
Returns:
Casted value
"""
return self.cast_fn(value)Improved string representation for dataclasses.
def repr(obj: Any) -> str:
"""
Generate enhanced string representation for objects.
Args:
obj: Object to represent
Returns:
Enhanced string representation with better formatting
"""Context variable and stack management for dataclasses.
class ContextVar:
"""
Context variable implementation for managing state.
Provides thread-safe context-local storage for dataclass instances.
"""
def __init__(self, name: str, default: Any = None) -> None:
"""
Initialize context variable.
Args:
name: Name of the context variable
default: Default value when not set
"""
self.name = name
self.default = default
def get(self, default: Any = None) -> Any:
"""
Get current context value.
Args:
default: Default value if not set
Returns:
Current context value or default
"""
...
def set(self, value: Any) -> Any:
"""
Set context value.
Args:
value: Value to set in context
Returns:
Token for resetting context
"""
...
class ContextStack:
"""
Context stack for managing nested contexts.
Manages multiple context levels with automatic cleanup.
"""
def __init__(self) -> None:
"""Initialize empty context stack."""
...
def push(self, context: dict[str, Any]) -> None:
"""
Push new context level.
Args:
context: Dictionary of context variables
"""
...
def pop(self) -> dict[str, Any]:
"""
Pop current context level.
Returns:
Popped context dictionary
"""
...
def current(self) -> dict[str, Any]:
"""
Get current context level.
Returns:
Current context dictionary
"""
...from etils import edc
from typing import Optional
@edc.dataclass
class Person:
"""Example person dataclass with enhanced features."""
name: str
age: int
email: Optional[str] = edc.field(default=None, repr=False)
score: float = edc.field(default_factory=lambda: 0.0)
def __post_init__(self):
"""Validate data after initialization."""
if self.age < 0:
raise ValueError("Age cannot be negative")
# Usage
person = Person("Alice", 25, "alice@example.com")
print(edc.repr(person)) # Enhanced representationfrom etils import edc
from typing import List
# Define casting functions
string_to_int = edc.AutoCast(int)
string_to_float = edc.AutoCast(float)
any_to_string = edc.AutoCast(str)
@edc.dataclass
class DataRecord:
"""Data record with automatic type casting."""
id: int = edc.field(default_factory=lambda: string_to_int("0"))
value: float = edc.field(default_factory=lambda: string_to_float("0.0"))
label: str = edc.field(default_factory=lambda: any_to_string(""))
# Automatic casting during initialization
record = DataRecord()
record.id = string_to_int("42") # "42" -> 42
record.value = string_to_float("3.14") # "3.14" -> 3.14
record.label = any_to_string(123) # 123 -> "123"from etils import edc
from typing import List, Dict
import json
@edc.dataclass
class Configuration:
"""Configuration class with advanced field handling."""
# Required field with validation
name: str = edc.field(
metadata={"description": "Configuration name", "required": True}
)
# Optional field excluded from repr
secret_key: str = edc.field(
default="",
repr=False,
metadata={"sensitive": True}
)
# Field with custom default factory
settings: Dict[str, str] = edc.field(
default_factory=dict,
metadata={"description": "Configuration settings"}
)
# Field excluded from comparison
timestamp: float = edc.field(
default_factory=lambda: time.time(),
compare=False,
metadata={"auto_generated": True}
)
# List field with validation
tags: List[str] = edc.field(
default_factory=list,
metadata={"description": "Configuration tags"}
)
# Usage with field metadata
config = Configuration(name="prod_config")
config.settings["debug"] = "false"
config.tags.append("production")
# Access field metadata
field_info = Configuration.__dataclass_fields__
print(field_info['name'].metadata) # Field metadatafrom etils import edc
import threading
# Context variable for user information
current_user = edc.ContextVar('current_user', default="anonymous")
@edc.dataclass
class UserSession:
"""User session with context management."""
user_id: str
permissions: List[str] = edc.field(default_factory=list)
def __enter__(self):
"""Enter context and set current user."""
self.token = current_user.set(self.user_id)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context and restore previous user."""
current_user.set(self.token)
# Context stack for nested operations
context_stack = edc.ContextStack()
def with_user_context():
"""Function that uses user context."""
user = current_user.get()
print(f"Current user: {user}")
# Usage
with UserSession("alice", ["read", "write"]):
with_user_context() # Prints: Current user: alice
with UserSession("bob", ["read"]):
with_user_context() # Prints: Current user: bob
with_user_context() # Prints: Current user: alice
with_user_context() # Prints: Current user: anonymousfrom etils import edc
from typing import Optional, List
@edc.dataclass
class Product:
"""Product with enhanced representation."""
id: int
name: str
price: float
description: Optional[str] = None
tags: List[str] = edc.field(default_factory=list)
metadata: dict = edc.field(default_factory=dict, repr=False)
product = Product(
id=1,
name="Laptop",
price=999.99,
description="High-performance laptop",
tags=["electronics", "computers"]
)
# Enhanced representation
print(edc.repr(product))
# Produces well-formatted, readable output
# Standard dataclass repr would be less readable
print(repr(product))from etils import edc
from typing import List, Optional
from datetime import datetime
@edc.dataclass
class Address:
"""Address dataclass."""
street: str
city: str
country: str
postal_code: str = ""
@edc.dataclass
class Contact:
"""Contact information."""
email: str
phone: Optional[str] = None
address: Optional[Address] = None
@edc.dataclass
class User:
"""Complete user profile with nested dataclasses."""
id: int
username: str
contact: Contact
created_at: datetime = edc.field(default_factory=datetime.now)
is_active: bool = edc.field(default=True, compare=False)
permissions: List[str] = edc.field(default_factory=list, repr=False)
# Create nested structure
user = User(
id=1,
username="alice",
contact=Contact(
email="alice@example.com",
phone="+1-555-0123",
address=Address(
street="123 Main St",
city="New York",
country="USA",
postal_code="10001"
)
)
)
print(edc.repr(user)) # Enhanced nested representationfrom etils import edc
from typing import Union
import json
# Custom casting functions
def parse_json_string(value: Union[str, dict]) -> dict:
"""Parse JSON string or return dict as-is."""
if isinstance(value, str):
return json.loads(value)
return value
def ensure_list(value: Union[str, List]) -> List:
"""Ensure value is a list."""
if isinstance(value, str):
return [value]
return value
@edc.dataclass
class ApiRequest:
"""API request with automatic type casting."""
endpoint: str
params: dict = edc.field(
default_factory=dict,
metadata={"cast": parse_json_string}
)
headers: List[str] = edc.field(
default_factory=list,
metadata={"cast": ensure_list}
)
# Use casting during construction
request = ApiRequest(
endpoint="/api/users",
params='{"limit": 10, "offset": 0}', # JSON string -> dict
headers="Authorization: Bearer token" # string -> list
)
# Manual casting with AutoCast
json_caster = edc.AutoCast(parse_json_string)
list_caster = edc.AutoCast(ensure_list)
request.params = json_caster('{"filter": "active"}')
request.headers = list_caster("Content-Type: application/json")Install with Tessl CLI
npx tessl i tessl/pypi-etils