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, plain text, CSV, and CSV archive formats and can load data from standard input or files.
CLASS | DESCRIPTION |
---|---|
InputFormat |
Enum defining supported input data formats (JSON, TEXT, CSV, CSV_ARCHIVE). |
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. |
DataFile
dataclass
¶
DataFile(
name: str,
loader: Callable[[str], Any],
loader_kwargs: Optional[dict[str, Any]] = None,
loader_args: Optional[list[Any]] = None,
input_data_key: Optional[str] = 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 InputFormat.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:
|
input_data_key
class-attribute
instance-attribute
¶
Use this parameter to set a custom key to represent your file.
When using InputFormat.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
: astr
argument which is the location where this data will be read from. This includes the dir and name of the file. As such, thename
parameter of this class is going to be passed to theloader
function, 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.
Input
dataclass
¶
Input(
data: Union[
Union[dict[str, Any], Any],
str,
list[dict[str, Any]],
dict[str, list[dict[str, Any]]],
dict[str, Any],
],
input_format: Optional[InputFormat] = JSON,
options: Optional[Options] = None,
)
Input for a decision problem.
You can import the Input
class directly from nextmv
:
The data
's type must match the input_format
:
InputFormat.JSON
: the data isUnion[dict[str, Any], Any]
. This just means that the data must be JSON-deserializable, which includes dicts and lists.InputFormat.TEXT
: the data isstr
, and it must be utf-8 encoded.InputFormat.CSV
: the data islist[dict[str, Any]]
, where each dict represents a row in the CSV.InputFormat.CSV_ARCHIVE
: the data isdict[str, list[dict[str, Any]]]
, where each key is the name of a CSV file and the value is a list of dicts representing the rows in that CSV file.InputFormat.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_key
parameter of theDataFile
class.
PARAMETER | DESCRIPTION |
---|---|
|
TYPE:
|
|
The actual data.
|
|
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 |
data
instance-attribute
¶
data: Union[
Union[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
JSON
:Union[dict[str, Any], Any]
- For
TEXT
:str
- For
CSV
:list[dict[str, Any]]
- For
CSV_ARCHIVE
:dict[str, list[dict[str, Any]]]
- For
MULTI_FILE
:dict[str, Any]
input_format
class-attribute
instance-attribute
¶
input_format: Optional[InputFormat] = JSON
Format of the input data.
Default is InputFormat.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 InputFormat.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, InputFormat
>>> input_obj = Input(data={"key": "value"}, input_format=InputFormat.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
InputFormat
¶
Bases: str
, Enum
Format of an Input
.
You can import the InputFormat
class directly from nextmv
:
This enum specifies the supported formats for input data.
ATTRIBUTE | DESCRIPTION |
---|---|
JSON |
JSON format, utf-8 encoded.
TYPE:
|
TEXT |
Text format, utf-8 encoded.
TYPE:
|
CSV |
CSV format, utf-8 encoded.
TYPE:
|
CSV_ARCHIVE |
CSV archive format: multiple CSV files.
TYPE:
|
MULTI_FILE |
Multi-file format, used for loading multiple files in a single input.
TYPE:
|
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.
load
¶
load(
input_format: InputFormat = JSON,
options: Optional[Options] = 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 various input formats like JSON, TEXT, CSV, and CSV archive.
Call the load
method to read the input data.
Examples:
>>> from nextmv.input import LocalInputLoader, InputFormat
>>> loader = LocalInputLoader()
>>> # Load JSON from stdin or file
>>> input_obj = loader.load(input_format=InputFormat.JSON, path="data.json")
>>> # Load CSV from a file
>>> input_obj = loader.load(input_format=InputFormat.CSV, path="data.csv")
FILE_READERS
class-attribute
instance-attribute
¶
Dictionary of functions to read from files.
Each key is an InputFormat, and each value is a function that reads from a file in that format.
STDIN_READERS
class-attribute
instance-attribute
¶
STDIN_READERS = {
JSON: lambda _: load(stdin),
TEXT: lambda _: rstrip("\n"),
CSV: lambda csv_configurations: list(
DictReader(stdin, **csv_configurations)
),
}
Dictionary of functions to read from standard input.
Each key is an InputFormat, and each value is a function that reads from standard input in that format.
load
¶
load(
input_format: Optional[InputFormat] = JSON,
options: Optional[Options] = None,
path: Optional[str] = None,
csv_configurations: Optional[dict[str, Any]] = None,
data_files: Optional[list[DataFile]] = None,
) -> Input
Load the input data. The input data can be in various formats. For
InputFormat.JSON
, InputFormat.TEXT
, and InputFormat.CSV
, 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. For
InputFormat.CSV_ARCHIVE
, the input data is read from the directory
specified by path
. If the path
is not provided, the default
location input
is used. The directory should contain one or more
files, where each file in the directory is a CSV file.
The Input
that is returned contains the data
attribute. This data
can be of different types, depending on the provided input_format
:
InputFormat.JSON
: the data is adict[str, Any]
.InputFormat.TEXT
: the data is astr
.InputFormat.CSV
: the data is alist[dict[str, Any]]
.InputFormat.CSV_ARCHIVE
: the data is adict[str, list[dict[str, Any]]]
. Each key is the name of the CSV file, minus the.csv
extension.InputFormat.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 theDataFile
instances.
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:
|
RETURNS | DESCRIPTION |
---|---|
Input
|
The input data. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the path is not a directory when working with CSV_ARCHIVE. |
Source code in nextmv/nextmv/input.py
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 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
|
csv_data_file
¶
csv_data_file(
name: str,
csv_configurations: Optional[dict[str, Any]] = None,
input_data_key: Optional[str] = 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: Optional[dict[str, Any]] = None,
input_data_key: Optional[str] = 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: Optional[InputFormat] = JSON,
options: Optional[Options] = None,
path: Optional[str] = None,
csv_configurations: Optional[dict[str, Any]] = None,
loader: Optional[InputLoader] = _LOCAL_INPUT_LOADER,
data_files: Optional[list[DataFile]] = 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.
The input data can be in various formats and can be loaded from different sources depending on the loader:
InputFormat.JSON
: the data is adict[str, Any]
InputFormat.TEXT
: the data is astr
InputFormat.CSV
: the data is alist[dict[str, Any]]
InputFormat.CSV_ARCHIVE
: the data is adict[str, list[dict[str, Any]]]
Each key is the name of the CSV file, minus the.csv
extension.InputFormat.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 specifying input_format
as InputFormat.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 aDataFile
that reads JSON data.csv_data_file
: Creates aDataFile
that reads CSV data.text_data_file
: Creates aDataFile
that reads utf-8 encoded text data.
When workiing 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, typically reads from stdin (for JSON, TEXT, CSV) or uses a default directory (for CSV_ARCHIVE)
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:
|
RETURNS | DESCRIPTION |
---|---|
Input
|
The loaded input data in an Input object. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the path is invalid or data format is incorrect. |
Examples:
>>> from nextmv.input import load, InputFormat
>>> # Load JSON from stdin
>>> input_obj = load(input_format=InputFormat.JSON)
>>> # Load CSV from a file
>>> input_obj = load(input_format=InputFormat.CSV, path="data.csv")
>>> # Load CSV archive from a directory
>>> input_obj = load(input_format=InputFormat.CSV_ARCHIVE, path="input_dir")
Source code in nextmv/nextmv/input.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 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 |
|
load_local
¶
load_local(
input_format: Optional[InputFormat] = JSON,
options: Optional[Options] = None,
path: Optional[str] = None,
csv_configurations: Optional[dict[str, Any]] = None,
) -> Input
Warning
load_local
is deprecated, use load
instead.
Load input data from local sources.
This is a convenience function for instantiating a LocalInputLoader
and calling its load
method.
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. Custom kwargs for
Python's
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Input
|
The loaded input data in an Input object. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the path is invalid or data format is incorrect. |
See Also
load : The recommended function to use instead.
Source code in nextmv/nextmv/input.py
text_data_file
¶
text_data_file(
name: str, input_data_key: Optional[str] = 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.