sasdata.dataloader.loader module¶
File handler to support different file extensions. Uses reflectometer registry utility. The default readers are found in the ‘readers’ sub-module and registered by default at initialization time. To add a new default reader, one must register it in the register_readers method found in readers/__init__.py. A utility method (find_plugins) is available to inspect a directory (for instance, a user plug-in directory) and look for new readers/writers.
- class sasdata.dataloader.loader.Loader¶
Bases:
objectUtility class to use the Registry as a singleton.
- __call__(file_path_list: List[str]) List[Data1D | Data2D]¶
Allow direct calls to the loader system for transient file loader systems. :param file_path_list: A list of string representations of file paths. Each item can either be a local file path
or a URI.
- Returns:
A list of loaded Data1D/2D objects.
- __dict__ = mappingproxy({'__module__': 'sasdata.dataloader.loader', '__doc__': '\n Utility class to use the Registry as a singleton.\n ', '_Loader__registry': <sasdata.dataloader.loader.Registry object>, 'associate_file_type': <function Loader.associate_file_type>, 'associate_file_reader': <function Loader.associate_file_reader>, 'load': <function Loader.load>, 'save': <function Loader.save>, '_get_registry_creation_time': <function Loader._get_registry_creation_time>, 'find_plugins': <function Loader.find_plugins>, 'get_wildcards': <function Loader.get_wildcards>, '__call__': <function Loader.__call__>, '__dict__': <attribute '__dict__' of 'Loader' objects>, '__weakref__': <attribute '__weakref__' of 'Loader' objects>, '__annotations__': {}})¶
- __doc__ = '\n Utility class to use the Registry as a singleton.\n '¶
- __module__ = 'sasdata.dataloader.loader'¶
- __registry = <sasdata.dataloader.loader.Registry object>¶
- __weakref__¶
list of weak references to the object
- _get_registry_creation_time() float¶
Internal method used to test the uniqueness of the registry object
- associate_file_reader(ext: str, loader) bool¶
Append a reader object to readers :param ext: file extension [string] :param module: reader object
- associate_file_type(ext: str, module: ModuleType) bool¶
Look into a module to find whether it contains a Reader class. If so, append it to readers and (potentially) to the list of writers for the given extension :param ext: file extension [string] :param module: module object
- find_plugins(directory: str) int¶
Find plugins in a given directory :param directory: directory to look into to find new readers/writers
- get_wildcards()¶
Return the list of wildcards
- load(file_path_list: List[str | Path] | str | Path, format: List[str] | str | None = None) List[Data1D | Data2D]¶
Load a file or series of files :param file_path_list: String representations of any number of file paths. This can either be a list or a string :param format: specified format to use (optional) :return: a list of DataInfo objects and/or loading exceptions.
- save(file: str, data, format: str) bool¶
Save a DataInfo object to file :param file: file name (path) :param data: DataInfo object :param format: format to write the data in
- class sasdata.dataloader.loader.Registry¶
Bases:
ExtensionRegistryRegistry class for file format extensions. Readers and writers are supported.
- __annotations__ = {}¶
- __doc__ = '\n Registry class for file format extensions.\n Readers and writers are supported.\n '¶
- __init__()¶
- __module__ = 'sasdata.dataloader.loader'¶
- _identify_plugin(module: ModuleType)¶
Look into a module to find whether it contains a Reader class. If so, add it to readers and (potentially) to the list of writers. :param module: module object :returns: True if successful
- associate_file_reader(file_extension, reader)¶
Append a reader object to readers :param file_extension: file extension [string] :param reader: reader object
- associate_file_type(ext: str, module: ModuleType) bool¶
Look into a module to find whether it contains a Reader class. If so, APPEND it to readers and (potentially) to the list of writers for the given extension :param ext: file extension [string] :param module: module object
- find_plugins(dir: str)¶
Find readers in a given directory. This method can be used to inspect user plug-in directories to find new readers/writers. :param dir: directory to search into :return: number of readers found
- load(file_path_list: List[str | Path] | str | Path, ext: List[str] | str | None = None, debug: bool | None = False, use_defaults: bool | None = True)¶
Call the loader for the file type of path.
- Parameters:
file_path_list – A list of pathlib.Path objects and/or string representations of file paths
ext – A list of explicit extensions, to force the use of a particular reader for a particular file. Usage If any ext is passed, the length of the ext list should be the same as the length of the file path list. A single extention, as a string or a list of length 1, will apply ext to all files in the file path list. Any other case will result in an error.
debug – when True, print the traceback for each loader that fails
use_defaults – Flag to use the default readers as a backup if the main reader fails or no reader exists
Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS readers if no reader was registered for the file’s extension.
- lookup_writers(path)¶
- Returns:
the loader associated with the file type of path.
- Raises ValueError:
if file type is not known.
- save(path: str, data, format: str | None = None)¶
Call the writer for the file type of path. Raises ValueError if no writer is available. Raises KeyError if format is not available. May raise a writer-defined exception if writer fails.