File I/O¶
utils ¶
File I/O utilities for CSV/JSON serialization, path generation, and OS file opening.
flatten_dict ¶
Recursively flattens a nested dictionary using dot notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Dict[str, Any]
|
The dictionary to flatten. |
required |
parent_key
|
str
|
Prefix for keys at the current nesting level (used during recursion). |
''
|
sep
|
str
|
Separator between parent and child key segments. |
'.'
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A single-level dictionary with composite keys joined by |
Example
{"a": {"b": 1}} -> {"a.b": 1}
Source code in t3api_utils/file/utils.py
prioritized_fieldnames ¶
Orders CSV fieldnames by priority list, then appends remaining keys alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dicts
|
List[Dict[str, Any]]
|
List of dictionaries whose keys represent fieldnames. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
An ordered list of fieldnames with priority fields first, followed by |
List[str]
|
any remaining keys in alphabetical order. |
Source code in t3api_utils/file/utils.py
default_json_serializer ¶
Fallback serializer for non-JSON-native types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object that |
required |
Returns:
| Type | Description |
|---|---|
str
|
An ISO-format string if |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the object type is not supported for serialization. |
Source code in t3api_utils/file/utils.py
generate_output_path ¶
generate_output_path(*, model_name: str, license_number: str, output_dir: str, extension: str) -> Path
Constructs the output file path with consistent naming.
The filename format is <model_name>__<license_number>__<timestamp>.<ext>.
Parent directories are created automatically if they do not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Logical model name included in the filename. |
required |
license_number
|
str
|
License identifier included in the filename. |
required |
output_dir
|
str
|
Directory where the file will be saved. |
required |
extension
|
str
|
File extension without the leading dot (e.g., |
required |
Returns:
| Type | Description |
|---|---|
Path
|
A |
Source code in t3api_utils/file/utils.py
save_dicts_to_json ¶
save_dicts_to_json(*, dicts: List[Dict[str, Any]], model_name: str, license_number: str, output_dir: str = 'output') -> Path
Saves a list of dictionaries to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dicts
|
List[Dict[str, Any]]
|
Non-empty list of dictionaries to serialize. |
required |
model_name
|
str
|
Logical model name used in the output filename. |
required |
license_number
|
str
|
License identifier used in the output filename. |
required |
output_dir
|
str
|
Directory where the JSON file will be saved. |
'output'
|
Returns:
| Type | Description |
|---|---|
Path
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in t3api_utils/file/utils.py
save_dicts_to_csv ¶
save_dicts_to_csv(*, dicts: List[Dict[str, Any]], model_name: str, license_number: str, output_dir: str = 'output', strip_empty_columns: bool = False) -> Path
Saves a list of (possibly nested) dictionaries to a CSV file after flattening.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dicts
|
List[Dict[str, Any]]
|
Non-empty list of input dictionaries (may contain nested dicts). |
required |
model_name
|
str
|
Logical model name used in the output filename. |
required |
license_number
|
str
|
License identifier used in the output filename. |
required |
output_dir
|
str
|
Directory where the CSV file will be saved. |
'output'
|
strip_empty_columns
|
bool
|
If True, columns where every value is |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in t3api_utils/file/utils.py
open_file ¶
Opens a file using the default application for the current OS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Filesystem path to the file to open. |
required |