Input Module¶
This section documents the input components of the Nextmv Python SDK.
input
¶
Module for handling input sources and data.
This module provides classes and functions for loading and handling input data in various formats for decision problems. It supports JSON and multi-file formats and can load data from standard input or files.
| CLASS | DESCRIPTION |
|---|---|
Input |
Container for input data with format specification and options. |
InputLoader |
Base class for loading inputs from various sources. |
LocalInputLoader |
Class for loading inputs from local files or stdin. |
| FUNCTION | DESCRIPTION |
|---|---|
load |
Load input data using a specified loader. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
INPUTS_KEY |
Key used for identifying inputs in the run.
TYPE:
|
DataFile
dataclass
¶
DataFile(
name: str,
loader: Callable[[str], Any],
loader_kwargs: dict[str, Any] | None = None,
loader_args: list[Any] | None = None,
input_data_key: str | None = None,
)
Represents data to be read from a file.
You can import the DataFile class directly from nextmv:
This class is used to define data that will be read from a file in the
filesystem. It includes the name of the file, and the reader function that
will handle the loading, and deserialization of the data from the file.
This DataFile class is typically used in the Input, when the
Input.input_format is set to ContentFormat.MULTI_FILE. Given that it is
difficul to handle every edge case of how data is deserialized, and read
from a file, this class exists so that the user can implement the reader
callable of their choice and provide it with any reader_args and
reader_kwargs they might need.
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the data (input) file. The file extension should be included in the name.
TYPE:
|
|
Callable that reads the data from the file. This should be a function
implemented by the user. There are convenience functions that you can
use as a reader as well. The
The The
TYPE:
|
-
nextmv
✏️ Modeling
Reference
Input Module
input
-
nextmv
✏️ Modeling
Reference
Input Module
input
input_data_key
class-attribute
instance-attribute
¶
Use this parameter to set a custom key to represent your file.
When using ContentFormat.MULTI_FILE as the input_format of the Input,
the data from the file is loaded to the .data parameter of the Input.
In that case, the type of .data is dict[str, Any], where each key
represents the file name (with extension) and the value is the data that is
actually loaded from the file using the loader function. You can set a
custom key to represent your file by using this attribute.
loader
instance-attribute
¶
Callable that reads (loads) the data from the file. This should be a function
implemented by the user. There are convenience functions that you can use
as a loader as well. The loader must receive, at the very minimum, the
following arguments:
file_path: astrargument which is the location where this data will be read from. This includes the dir and name of the file. As such, thenameparameter of this class is going to be passed to theloaderfunction, joined with the directory where the file will be read from.
The loader can also receive additional arguments, and keyword arguments.
The loader_args and loader_kwargs parameters of this class can be used
to provide those additional arguments.
The loader function should return the data that will be used in the model.
loader_args
class-attribute
instance-attribute
¶
Optional positional arguments to pass to the loader function. This can be used to customize the behavior of the loader.
loader_kwargs
class-attribute
instance-attribute
¶
Optional keyword arguments to pass to the loader function. This can be used to customize the behavior of the loader.
name
instance-attribute
¶
Name of the data (input) file. The file extension should be included in the name.
INPUTS_KEY
module-attribute
¶
Inputs key constant used for identifying inputs in the run.
Input
dataclass
¶
Input(
data: dict[str, Any]
| Any
| str
| list[dict[str, Any]]
| dict[str, list[dict[str, Any]]]
| dict[str, Any],
input_format: ContentFormat | None = JSON,
options: Options | None = None,
)
Input for a decision problem.
You can import the Input class directly from nextmv:
The data's type must match the input_format:
ContentFormat.JSON: the data isUnion[dict[str, Any], Any]. This just means that the data must be JSON-deserializable, which includes dicts and lists.ContentFormat.MULTI_FILE: the data isdict[str, Any], where for each item, the key is the file name (with the extension) and the actual data from the file is the value. When working with multi-file, data is loaded from one or more files in a specific directory. Given that each file can be of different types (JSON, CSV, Excel, etc...), the data captured from each might vary. To reflect this, the data is loaded as a dict of items. You can have a custom key for the data, that is not the file name, if you use theinput_data_keyparameter of theDataFileclass.
| PARAMETER | DESCRIPTION |
|---|---|
|
The actual data.
TYPE:
|
|
Format of the input data. Default is
TYPE:
|
|
Options that the input was created with.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the data type doesn't match the expected type for the given format. |
ValueError
|
If the |
-
nextmv
✏️ Modeling
Reference
Input Module
input
data
instance-attribute
¶
data: (
dict[str, Any]
| Any
| str
| list[dict[str, Any]]
| dict[str, list[dict[str, Any]]]
| dict[str, Any]
)
The actual data.
The data can be of various types, depending on the input format:
- For
ContentFormat.JSON:Union[dict[str, Any], Any] - For
ContentFormat.MULTI_FILE:dict[str, Any]
input_format
class-attribute
instance-attribute
¶
Format of the input data.
Default is ContentFormat.JSON.
options
class-attribute
instance-attribute
¶
Options that the Input was created with.
A copy of the options is made during initialization, ensuring the original options remain unchanged even if modified later.
to_dict
¶
Convert the input to a dictionary.
This method serializes the Input object to a dictionary format that can
be easily converted to JSON or other serialization formats. When the
input_type is set to ContentFormat.MULTI_FILE, it will not include
the data field, as it is uncertain how data is deserialized from the file.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Examples:
>>> from nextmv.input import Input, ContentFormat
>>> input_obj = Input(data={"key": "value"}, input_format=ContentFormat.JSON)
>>> input_dict = input_obj.to_dict()
>>> print(input_dict)
{'data': {'key': 'value'}, 'input_format': 'json', 'options': None}
Source code in nextmv/nextmv/input.py
InputLoader
¶
Base class for loading inputs.
You can import the InputLoader class directly from nextmv:
This abstract class defines the interface for input loaders. Subclasses must
implement the load method to provide concrete input loading functionality.
-
nextmv
✏️ Modeling
Reference
Input Module
inputLocalInputLoader
-
nextmv
✏️ Modeling
Reference
Input Module
inputload
load
¶
load(
input_format: ContentFormat = JSON,
options: Options | None = None,
*args,
**kwargs,
) -> Input
Read the input data. This method should be implemented by subclasses.
| PARAMETER | DESCRIPTION |
|---|---|
|
Format of the input data. Default is
TYPE:
|
|
Options for loading the input data.
TYPE:
|
|
Additional positional arguments.
DEFAULT:
|
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Input
|
The input data. |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the method is not implemented. |
Source code in nextmv/nextmv/input.py
LocalInputLoader
¶
Bases: InputLoader
Class for loading local inputs.
You can import the LocalInputLoader class directly from nextmv:
This class can load input data from the local filesystem, by using stdin, a file, or a directory, where applicable. It supports JSON and multi-file formats.
Call the load method to read the input data.
Examples:
>>> from nextmv.input import LocalInputLoader, ContentFormat
>>> loader = LocalInputLoader()
>>> # Load JSON from stdin or file
>>> input_obj = loader.load(input_format=ContentFormat.JSON, path="data.json")
FILE_READERS
class-attribute
instance-attribute
¶
Dictionary of functions to read from files.
Each key is a ContentFormat, and each value is a function that reads from a file in that format.
STDIN_READERS
class-attribute
instance-attribute
¶
Dictionary of functions to read from standard input.
Each key is a ContentFormat, and each value is a function that reads from standard input in that format.
load
¶
load(
input_format: ContentFormat | None = None,
options: Options | None = None,
path: str | None = None,
csv_configurations: dict[str, Any] | None = None,
data_files: list[DataFile] | None = None,
manifest: Manifest | None = None,
) -> Input
Load the input data. For ContentFormat.JSON, the data can be streamed
from stdin or read from a file. When the path argument is provided
(and valid), the input data is read from the file specified by path,
otherwise, it is streamed from stdin.
The Input that is returned contains the data attribute. This data
can be of different types, depending on the provided input_format:
ContentFormat.JSON: the data is adict[str, Any].ContentFormat.MULTI_FILE: the data is adict[str, Any], where each key is the file name (with extension) and the value is the data read from the file. The data can be of any type, depending on the file type and the reader function provided in theDataFileinstances.
| PARAMETER | DESCRIPTION |
|---|---|
|
Format of the input data. Default is
TYPE:
|
|
Options for loading the input data.
TYPE:
|
|
Path to the input data.
TYPE:
|
|
Configurations for loading CSV files. The default
TYPE:
|
|
List of
TYPE:
|
|
The manifest to use for loading the input data. If not provided,
the method will look for an
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Input
|
The input data. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the path is not a valid directory. |
Source code in nextmv/nextmv/input.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | |
csv_data_file
¶
csv_data_file(
name: str,
csv_configurations: dict[str, Any] | None = None,
input_data_key: str | None = None,
) -> DataFile
This is a convenience function to create a DataFile that reads CSV data.
You can import the csv_data_file function directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the data file. You don't need to include the
TYPE:
|
|
CSV-specific configurations for reading the data.
TYPE:
|
|
A custom key to represent the data from this file. When using
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFile
|
A |
Examples:
>>> from nextmv import csv_data_file
>>> data_file = csv_data_file("my_data")
>>> data = data_file.read()
>>> print(data)
[
{"column1": "value1", "column2": "value2"},
{"column1": "value3", "column2": "value4"}
]
Source code in nextmv/nextmv/input.py
json_data_file
¶
json_data_file(
name: str,
json_configurations: dict[str, Any] | None = None,
input_data_key: str | None = None,
) -> DataFile
This is a convenience function to create a DataFile that reads JSON data.
You can import the json_data_file function directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the data file. You don't need to include the
TYPE:
|
|
JSON-specific configurations for reading the data.
TYPE:
|
|
A custom key to represent the data from this file. When using
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFile
|
A |
Examples:
>>> from nextmv import json_data_file
>>> data_file = json_data_file("my_data")
>>> data = data_file.read()
>>> print(data)
{
"key": "value",
"another_key": [1, 2, 3]
}
Source code in nextmv/nextmv/input.py
load
¶
load(
input_format: ContentFormat | None = None,
options: Options | None = None,
path: str | None = None,
csv_configurations: dict[str, Any] | None = None,
loader: InputLoader | None = _LOCAL_INPUT_LOADER,
data_files: list[DataFile] | None = None,
manifest: Manifest | None = None,
) -> Input
Load input data using the specified loader.
You can import the load function directly from nextmv:
This is a convenience function for loading an Input object. By default,
it uses the LocalInputLoader to load data from local sources like files
or stdin. When working with the local filesystem, please consider the
following.
There are two main ways in which you can use this function:
- Having an
app.yaml(app manifest) present in the current working directory (recommended). In this case, you just use the function asload(), and it will automatically resolve theinput_format,path, andoptionsfrom the manifest. Alternatively, you can also provide the manifest explicitly as themanifestargument. - Providing the
input_format,path, andoptionsarguments directly to the function.
When both a manifest can be used (via the current working directory or the
manifest argument) and the input_format, path, or options arguments
are provided directly, the function will prioritize using the provided
arguments over the manifest values. This allows you to override the
manifest values when needed, while still having the convenience of using
the manifest for default configurations.
The input data can be in various formats and can be loaded from different sources depending on the loader:
ContentFormat.JSON: the data is adict[str, Any]ContentFormat.MULTI_FILE: the data is adict[str, Any], where each key is the file name (with extension) and the value is the data read from the file. This is used for loading multiple files in a single input, where each file can be of different types (JSON, CSV, Excel, etc.). The data is loaded as a dict of items, where each item corresponds to a file and its content.
When working with input_format as ContentFormat.MULTI_FILE, the
data_files argument must be provided. This argument is a list of
DataFile instances, each representing a file to be read. Each DataFile
instance should have a name (the file name with extension) and a loader
function that reads the data from the file. The loader function should
accept the file path as its first argument and return the data read from
the file. The loader can also accept additional positional and keyword
arguments, which can be provided through the loader_args and
loader_kwargs attributes of the DataFile instance.
There are convenience functions that can be used to create DataFile
classes, such as:
json_data_file: Creates aDataFilethat reads JSON data.csv_data_file: Creates aDataFilethat reads CSV data.text_data_file: Creates aDataFilethat reads utf-8 encoded text data.
When working with data in other formats, such as Excel files, you are
encouraged to create your own DataFile objects with your own
implementation of the loader function. This allows you to read data
from files in a way that suits your needs, while still adhering to the
DataFile interface.
| PARAMETER | DESCRIPTION |
|---|---|
|
Format of the input data. Default is
TYPE:
|
|
Options for loading the input data.
TYPE:
|
|
Path to the input data. For file-based loaders: - If provided, reads from the specified file or directory - If None, reads from stdin (for JSON) or uses a default directory (for multi-file)
TYPE:
|
|
Configurations for loading CSV files. Custom kwargs for
Python's
TYPE:
|
|
The loader to use for loading the input data.
Default is an instance of
TYPE:
|
|
List of There are convenience functions that can be used to create
TYPE:
|
|
The manifest to use for loading the input data. If not provided, the
function will look for an
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Input
|
The loaded input data in an Input object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If one of many validations fails. |
Examples:
Source code in nextmv/nextmv/input.py
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 | |
text_data_file
¶
text_data_file(
name: str, input_data_key: str | None = None
) -> DataFile
This is a convenience function to create a DataFile that reads utf-8
encoded text data.
You can import the text_data_file function directly from nextmv:
You must provide the extension as part of the name parameter.
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the data file. The file extension must be provided in the name.
TYPE:
|
|
A custom key to represent the data from this file. When using
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFile
|
A |
Examples:
>>> from nextmv import text_data_file
>>> data_file = text_data_file("my_data")
>>> data = data_file.read()
>>> print(data)
This is some text data.