1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
"""Functions to perform parsing and conversion in one step."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from .conversion._conversion import DEFAULT_CLASS_MAP, ConstructorDict, convert
from .parser._parser import (
DEFAULT_ALTREP_MAP,
AcceptableFile,
AltRepConstructorMap,
Traversable,
parse_file,
)
if TYPE_CHECKING:
import os
from collections.abc import MutableMapping
def read_rdata( # noqa: PLR0913
file_or_path: AcceptableFile | os.PathLike[Any] | Traversable | str,
*,
expand_altrep: bool = True,
altrep_constructor_dict: AltRepConstructorMap = DEFAULT_ALTREP_MAP,
extension: str | None = None,
constructor_dict: ConstructorDict = DEFAULT_CLASS_MAP,
default_encoding: str | None = None,
force_default_encoding: bool = False,
global_environment: MutableMapping[str, Any] | None = None,
base_environment: MutableMapping[str, Any] | None = None,
) -> Any: # noqa: ANN401
parsed = parse_file(
file_or_path=file_or_path,
expand_altrep=expand_altrep,
altrep_constructor_dict=altrep_constructor_dict,
extension=extension,
)
return convert(
parsed,
constructor_dict=constructor_dict,
default_encoding=default_encoding,
force_default_encoding=force_default_encoding,
global_environment=global_environment,
base_environment=base_environment,
)
def read_rds( # noqa: PLR0913
file_or_path: AcceptableFile | os.PathLike[Any] | Traversable | str,
*,
expand_altrep: bool = True,
altrep_constructor_dict: AltRepConstructorMap = DEFAULT_ALTREP_MAP,
constructor_dict: ConstructorDict = DEFAULT_CLASS_MAP,
default_encoding: str | None = None,
force_default_encoding: bool = False,
global_environment: MutableMapping[str, Any] | None = None,
base_environment: MutableMapping[str, Any] | None = None,
) -> Any: # noqa: ANN401
"""
Read an RDS file, containing an R object.
This is a convenience function that wraps :func:`rdata.parser.parse_file`
and :func:`rdata.parser.convert`, as it is the common use case.
Args:
file_or_path: File in the RDS format.
expand_altrep: Whether to translate ALTREPs to normal objects.
altrep_constructor_dict: Dictionary mapping each ALTREP to
its constructor.
constructor_dict: Dictionary mapping names of R classes to constructor
functions with the following prototype:
.. code-block :: python
def constructor(obj, attrs):
...
This dictionary can be used to support custom R classes. By
default, the dictionary used is
:data:`~rdata.conversion._conversion.DEFAULT_CLASS_MAP`
which has support for several common classes.
default_encoding: Default encoding used for strings with unknown
encoding. If `None`, the one stored in the file will be used, or
ASCII as a fallback.
force_default_encoding:
Use the default encoding even if the strings specify other
encoding.
global_environment: Global environment to use. By default is an empty
environment.
base_environment: Base environment to use. By default is an empty
environment.
Returns:
Contents of the file converted to a Python object.
See Also:
:func:`read_rda`: Similar function that parses a RDA or RDATA file.
Examples:
Parse one of the included examples, containing a dataframe
>>> import rdata
>>>
>>> data = rdata.read_rds(
... rdata.TESTDATA_PATH / "test_dataframe.rds"
... )
>>> data
class value
1 a 1
2 b 2
3 b 3
"""
return read_rdata(
file_or_path=file_or_path,
expand_altrep=expand_altrep,
altrep_constructor_dict=altrep_constructor_dict,
extension=".rds",
constructor_dict=constructor_dict,
default_encoding=default_encoding,
force_default_encoding=force_default_encoding,
global_environment=global_environment,
base_environment=base_environment,
)
def read_rda( # noqa: PLR0913
file_or_path: AcceptableFile | os.PathLike[Any] | Traversable | str,
*,
expand_altrep: bool = True,
altrep_constructor_dict: AltRepConstructorMap = DEFAULT_ALTREP_MAP,
constructor_dict: ConstructorDict = DEFAULT_CLASS_MAP,
default_encoding: str | None = None,
force_default_encoding: bool = False,
global_environment: MutableMapping[str, Any] | None = None,
base_environment: MutableMapping[str, Any] | None = None,
) -> dict[str, Any]:
"""
Read an RDA or RDATA file, containing an R object.
This is a convenience function that wraps :func:`rdata.parser.parse_file`
and :func:`rdata.parser.convert`, as it is the common use case.
Args:
file_or_path: File in the RDA format.
expand_altrep: Whether to translate ALTREPs to normal objects.
altrep_constructor_dict: Dictionary mapping each ALTREP to
its constructor.
constructor_dict: Dictionary mapping names of R classes to constructor
functions with the following prototype:
.. code-block :: python
def constructor(obj, attrs):
...
This dictionary can be used to support custom R classes. By
default, the dictionary used is
:data:`~rdata.conversion._conversion.DEFAULT_CLASS_MAP`
which has support for several common classes.
default_encoding: Default encoding used for strings with unknown
encoding. If `None`, the one stored in the file will be used, or
ASCII as a fallback.
force_default_encoding:
Use the default encoding even if the strings specify other
encoding.
global_environment: Global environment to use. By default is an empty
environment.
base_environment: Base environment to use. By default is an empty
environment.
Returns:
Contents of the file converted to a Python object.
See Also:
:func:`read_rds`: Similar function that parses a RDS file.
Examples:
Parse one of the included examples, containing a dataframe
>>> import rdata
>>>
>>> data = rdata.read_rda(
... rdata.TESTDATA_PATH / "test_dataframe.rda"
... )
>>> data
{'test_dataframe': class value
1 a 1
2 b 2
3 b 3}
"""
return read_rdata( # type: ignore[no-any-return]
file_or_path=file_or_path,
expand_altrep=expand_altrep,
altrep_constructor_dict=altrep_constructor_dict,
extension=".rda",
constructor_dict=constructor_dict,
default_encoding=default_encoding,
force_default_encoding=force_default_encoding,
global_environment=global_environment,
base_environment=base_environment,
)
|