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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 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 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
from __future__ import annotations
import abc
import warnings
from collections import ChainMap
from collections.abc import Callable, Mapping, MutableMapping, Sequence
from dataclasses import dataclass
from fractions import Fraction
from types import MappingProxyType, SimpleNamespace
from typing import Any, Final, NamedTuple, Union, cast
import numpy as np
import pandas as pd
import xarray
from typing_extensions import override
from .. import parser
ConversionFunction = Callable[[Union[parser.RData, parser.RObject]], Any]
class RLanguage(NamedTuple):
"""R language construct."""
elements: list[Any]
attributes: Mapping[str, Any]
class RExpression(NamedTuple):
"""R expression."""
elements: list[RLanguage]
@dataclass
class RBuiltin:
"""R builtin."""
name: str
@dataclass
class RFunction:
"""R function."""
environment: Mapping[str, Any]
formals: Mapping[str, Any] | None
body: RLanguage
attributes: Mapping[str, Any]
@property
def source(self) -> str:
return "\n".join(self.attributes["srcref"].srcfile.lines)
@dataclass
class RExternalPointer:
"""R bytecode."""
protected: Any
tag: Any
@dataclass
class RBytecode:
"""R bytecode."""
code: xarray.DataArray
constants: Sequence[Any]
attributes: Mapping[str, Any]
class REnvironment(ChainMap[str, Any]):
"""R environment."""
def __init__(
self,
*maps: MutableMapping[str, Any],
frame: Mapping[str, Any] | None = None,
) -> None:
super().__init__(*maps)
self.frame = frame
def convert_list(
r_list: parser.RObject,
conversion_function: ConversionFunction,
) -> Mapping[str, Any] | list[Any]:
"""
Expand a tagged R pairlist to a Python dictionary.
Args:
r_list: Pairlist R object, with tags.
conversion_function: Conversion function to apply to the elements of
the list. By default is the identity function.
Returns:
A dictionary with the tags of the pairwise list as keys and their
corresponding values as values.
See Also:
convert_vector
"""
if r_list.info.type is parser.RObjectType.NILVALUE:
return {}
if r_list.info.type not in {
parser.RObjectType.LIST,
parser.RObjectType.LANG,
}:
msg = "Must receive a LIST, LANG or NILVALUE object"
raise TypeError(msg)
tag = None if r_list.tag is None else conversion_function(r_list.tag)
cdr = conversion_function(r_list.value[1])
if tag is not None:
if cdr is None:
cdr = {}
return {tag: conversion_function(r_list.value[0]), **cdr}
if cdr is None:
cdr = []
return [conversion_function(r_list.value[0]), *cdr]
def convert_env(
r_env: parser.RObject,
conversion_function: ConversionFunction,
) -> REnvironment:
"""Convert environment objects."""
if r_env.info.type is not parser.RObjectType.ENV:
msg = "Must receive a ENV object"
raise TypeError(msg)
frame = conversion_function(r_env.value.frame)
enclosure = conversion_function(r_env.value.enclosure)
hash_table = conversion_function(r_env.value.hash_table)
dictionary = {}
if hash_table is not None:
for d in hash_table:
if d is not None:
dictionary.update(d)
return REnvironment(dictionary, enclosure, frame=frame)
def convert_attrs(
r_obj: parser.RObject,
conversion_function: ConversionFunction,
) -> Mapping[str, Any]:
"""
Return the attributes of an object as a Python dictionary.
Args:
r_obj: R object.
conversion_function: Conversion function to apply to the elements of
the attribute list. By default is the identity function.
Returns:
A dictionary with the names of the attributes as keys and their
corresponding values as values.
See Also:
convert_list
"""
if r_obj.attributes:
attrs = cast(
Mapping[str, Any],
conversion_function(r_obj.attributes),
)
else:
attrs = {}
return attrs
def convert_vector(
r_vec: parser.RObject,
conversion_function: ConversionFunction,
attrs: Mapping[str, Any] | None = None,
) -> list[Any] | Mapping[str, Any]:
"""
Convert a R vector to a Python list or dictionary.
If the vector has a ``names`` attribute, the result is a dictionary with
the names as keys. Otherwise, the result is a Python list.
Args:
r_vec: R vector.
conversion_function: Conversion function to apply to the elements of
the vector. By default is the identity function.
attrs: Attributes of the vector.
Returns:
A dictionary with the ``names`` of the vector as keys and their
corresponding values as values. If the vector does not have an
argument ``names``, then a normal Python list is returned.
See Also:
convert_list
"""
if attrs is None:
attrs = {}
if r_vec.info.type not in {
parser.RObjectType.VEC,
parser.RObjectType.EXPR,
}:
msg = "Must receive a VEC or EXPR object"
raise TypeError(msg)
value: list[Any] | Mapping[str, Any] = [
conversion_function(o) for o in r_vec.value
]
# If it has the name attribute, use a dict instead
field_names = attrs.get("names")
if field_names is not None:
value = dict(zip(field_names, value))
return value
def safe_decode(byte_str: bytes, encoding: str) -> str | bytes:
"""Decode a (possibly malformed) string."""
try:
return byte_str.decode(encoding)
except UnicodeDecodeError as e:
warnings.warn( # noqa: B028
f"Exception while decoding {byte_str!r}: {e}",
)
return byte_str
def convert_char(
r_char: parser.RObject,
*,
default_encoding: str | None = None,
force_default_encoding: bool = False,
) -> str | bytes | None:
"""
Decode a R character array to a Python string or bytes.
The bits that signal the encoding are in the general pointer. The
string can be encoded in UTF8, LATIN1 or ASCII, or can be a sequence
of bytes.
Args:
r_char: R character array.
default_encoding: Default encoding to apply when encoding info
is not available.
force_default_encoding: Always use the default encoding.
Returns:
Decoded string.
See Also:
convert_symbol
"""
if r_char.info.type is not parser.RObjectType.CHAR:
msg = "Must receive a CHAR object"
raise TypeError(msg)
if r_char.value is None:
return None
assert isinstance(r_char.value, bytes)
encoding = None
if not force_default_encoding:
if r_char.info.gp & parser.CharFlags.UTF8:
encoding = "utf_8"
elif r_char.info.gp & parser.CharFlags.LATIN1:
encoding = "latin_1"
elif r_char.info.gp & parser.CharFlags.ASCII:
encoding = "ascii"
elif r_char.info.gp & parser.CharFlags.BYTES:
encoding = "bytes"
if encoding is None:
if default_encoding:
encoding = default_encoding
else:
# Assume ASCII if no encoding is marked
warnings.warn("Unknown encoding. Assumed ASCII.") # noqa: B028
encoding = "ascii"
return (
r_char.value
if encoding == "bytes"
else safe_decode(r_char.value, encoding)
)
def convert_symbol(
r_symbol: parser.RObject,
conversion_function: ConversionFunction,
) -> str | bytes:
"""
Decode a R symbol to a Python string or bytes.
Args:
r_symbol: R symbol.
conversion_function: Conversion function to apply to the char element
of the symbol. By default is the identity function.
Returns:
Decoded string.
See Also:
convert_char
"""
if r_symbol.info.type is parser.RObjectType.SYM:
symbol = conversion_function(r_symbol.value)
assert isinstance(symbol, str)
return symbol
msg = "Must receive a SYM object"
raise TypeError(msg)
def convert_array(
r_array: parser.RObject,
attrs: Mapping[str, Any] | None = None,
) -> np.ndarray[Any, Any] | xarray.DataArray:
"""
Convert a R array to a Numpy ndarray or a Xarray DataArray.
If the array has attribute ``dimnames`` the output will be a
Xarray DataArray, preserving the dimension names.
Args:
r_array: R array.
attrs: Attributes of the array.
Returns:
Array.
See Also:
convert_vector
"""
if attrs is None:
attrs = {}
if r_array.info.type not in {
parser.RObjectType.LGL,
parser.RObjectType.INT,
parser.RObjectType.REAL,
parser.RObjectType.CPLX,
}:
msg = "Must receive an array object"
raise TypeError(msg)
value = r_array.value
shape = attrs.get("dim")
if shape is not None:
# R matrix order is like FORTRAN
value = np.reshape(value, shape, order="F")
dimension_names = None
coords = None
dimnames = attrs.get("dimnames")
if dimnames:
if isinstance(dimnames, Mapping):
dimension_names = list(dimnames.keys())
coords = dimnames
else:
dimension_names = [f"dim_{i}" for i, _ in enumerate(dimnames)]
coords = {
dimension_names[i]: d
for i, d in enumerate(dimnames)
if d is not None
}
value = xarray.DataArray(
value,
dims=dimension_names,
coords=coords,
)
return value # type: ignore [no-any-return]
R_INT_MIN = -2**31
def _dataframe_column_transform(source: Any) -> Any: # noqa: ANN401
if isinstance(source, np.ndarray):
if np.issubdtype(source.dtype, np.integer):
return pd.Series(source, dtype=pd.Int32Dtype()).array
if np.issubdtype(source.dtype, np.bool_):
return pd.Series(source, dtype=pd.BooleanDtype()).array
if np.issubdtype(source.dtype, np.str_):
return pd.Series(source, dtype=pd.StringDtype()).array
return source
def dataframe_constructor(
obj: Mapping[str, Any],
attrs: Mapping[str, Any],
) -> pd.DataFrame:
row_names = attrs["row.names"]
obj = {key: _dataframe_column_transform(val) for key, val in obj.items()}
# Default row names are stored as [R_INT_NA, -len]
default_row_names_len = 2
index: pd.RangeIndex | tuple[str, ...] = (
pd.RangeIndex(1, abs(row_names[1]) + 1)
if (
len(row_names) == default_row_names_len
and isinstance(row_names, np.ma.MaskedArray)
and row_names.mask[0]
)
else tuple(row_names)
)
return pd.DataFrame(obj, columns=obj, index=index)
def _factor_constructor_internal(
obj: np.ndarray[Any, np.dtype[np.integer[Any]]],
attrs: Mapping[str, Any],
*,
ordered: bool,
) -> pd.Categorical:
values = [attrs["levels"][i - 1] if i >= 0 else None for i in obj]
return pd.Categorical(values, attrs["levels"], ordered=ordered)
def factor_constructor(
obj: np.ndarray[Any, np.dtype[np.integer[Any]]],
attrs: Mapping[str, Any],
) -> pd.Categorical:
"""Construct a factor objects."""
return _factor_constructor_internal(obj, attrs, ordered=False)
def ordered_constructor(
obj: np.ndarray[Any, np.dtype[np.integer[Any]]],
attrs: Mapping[str, Any],
) -> pd.Categorical:
"""Contruct an ordered factor."""
return _factor_constructor_internal(obj, attrs, ordered=True)
def ts_constructor(
obj: np.ndarray[Any, Any],
attrs: Mapping[str, Any],
) -> pd.Series[Any]:
"""Construct a time series object."""
start, end, frequency = attrs["tsp"]
frequency = int(frequency)
real_start = Fraction(int(round(start * frequency)), frequency)
real_end = Fraction(int(round(end * frequency)), frequency)
index: np.ndarray[Any, Any] = np.arange(
real_start,
real_end + Fraction(1, frequency),
Fraction(1, frequency),
)
if frequency == 1:
index = index.astype(int)
return pd.Series(obj, index=index)
@dataclass
class SrcRef:
"""Reference to a source file location."""
first_line: int
first_byte: int
last_line: int
last_byte: int
first_column: int
last_column: int
first_parsed: int
last_parsed: int
srcfile: SrcFile
def srcref_constructor(
obj: tuple[int, int, int, int, int, int, int, int],
attrs: Mapping[str, Any],
) -> SrcRef:
return SrcRef(*obj, srcfile=attrs["srcfile"])
@dataclass
class SrcFile:
"""Source file."""
filename: str
file_encoding: str | None
string_encoding: str | None
def srcfile_constructor(
obj: REnvironment,
attrs: Mapping[str, Any], # noqa: ARG001
) -> SrcFile:
frame = obj.frame
assert frame is not None
filename = frame["filename"][0]
file_encoding = frame.get("encoding")
string_encoding = frame.get("Enc")
return SrcFile(
filename=filename,
file_encoding=file_encoding,
string_encoding=string_encoding,
)
@dataclass
class SrcFileCopy(SrcFile):
"""Source file with a copy of its lines."""
lines: Sequence[str]
def srcfilecopy_constructor(
obj: REnvironment,
attrs: Mapping[str, Any], # noqa: ARG001
) -> SrcFileCopy:
frame = obj.frame
assert frame is not None
filename = frame["filename"][0]
file_encoding = frame.get("encoding", (None,))[0]
string_encoding = frame.get("Enc", (None,))[0]
lines = frame["lines"]
return SrcFileCopy(
filename=filename,
file_encoding=file_encoding,
string_encoding=string_encoding,
lines=lines,
)
Constructor = Callable[[Any, Mapping[str, Any]], Any]
ConstructorDict = Mapping[
Union[str, bytes],
Constructor,
]
default_class_map_dict: Final[ConstructorDict] = {
"data.frame": dataframe_constructor,
"factor": factor_constructor,
"ordered": ordered_constructor,
"ts": ts_constructor,
"srcref": srcref_constructor,
"srcfile": srcfile_constructor,
"srcfilecopy": srcfilecopy_constructor,
}
#: Default mapping of constructor functions.
DEFAULT_CLASS_MAP: Final = MappingProxyType(default_class_map_dict)
class Converter(abc.ABC):
"""Interface of a class converting R objects in Python objects."""
@abc.abstractmethod
def convert(self, data: parser.RData | parser.RObject) -> Any: # noqa: ANN401
"""Convert a R object to a Python one."""
@dataclass
class UnresolvedReference:
references: MutableMapping[int, Any]
index: int
class SimpleConverter(Converter):
"""
Class converting R objects to Python objects.
Args:
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.
"""
def __init__(
self,
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,
) -> None:
self.constructor_dict = constructor_dict
self.default_encoding = default_encoding
self.force_default_encoding = force_default_encoding
self.global_environment = REnvironment(
{} if global_environment is None
else global_environment,
)
self.base_environment = REnvironment(
{} if base_environment is None
else base_environment,
)
self.empty_environment: Mapping[str, Any] = REnvironment({})
self._reset()
def _reset(self) -> None:
self.references: MutableMapping[int, Any] = {}
self.default_encoding_used = self.default_encoding
@override
def convert(
self,
data: parser.RData | parser.RObject,
) -> Any:
self._reset()
return self._convert_next(data)
def _convert_next( # noqa: C901, PLR0912, PLR0915
self,
data: parser.RData | parser.RObject,
) -> Any: # noqa: ANN401
"""Convert a R object to a Python one."""
obj: parser.RObject
if isinstance(data, parser.RData):
obj = data.object
if self.default_encoding is None:
self.default_encoding_used = data.extra.encoding
else:
obj = data
attrs = convert_attrs(obj, self._convert_next)
reference_id = id(obj)
# Return the value if previously referenced
value: Any = self.references.get(id(obj))
if value is not None:
pass
if obj.info.type == parser.RObjectType.SYM:
# Return the internal string
value = convert_symbol(obj, self._convert_next)
elif obj.info.type == parser.RObjectType.LIST:
# Expand the list and process the elements
value = convert_list(obj, self._convert_next)
elif obj.info.type == parser.RObjectType.CLO:
assert obj.tag is not None
assert obj.attributes is not None
environment = self._convert_next(obj.tag)
formals = self._convert_next(obj.value[0])
body = self._convert_next(obj.value[1])
attributes = self._convert_next(obj.attributes)
value = RFunction(
environment=environment,
formals=formals,
body=body,
attributes=attributes,
)
elif obj.info.type == parser.RObjectType.ENV:
# Return a ChainMap of the environments
value = convert_env(obj, self._convert_next)
elif obj.info.type == parser.RObjectType.LANG:
# Expand the list and process the elements, returning a
# special object
rlanguage_list = convert_list(obj, self._convert_next)
assert isinstance(rlanguage_list, list)
attributes = self._convert_next(
obj.attributes,
) if obj.attributes else {}
value = RLanguage(rlanguage_list, attributes)
elif obj.info.type in {
parser.RObjectType.SPECIAL,
parser.RObjectType.BUILTIN,
}:
value = RBuiltin(name=obj.value.decode("ascii"))
elif obj.info.type == parser.RObjectType.CHAR:
# Return the internal string
value = convert_char(
obj,
default_encoding=self.default_encoding_used,
force_default_encoding=self.force_default_encoding,
)
elif obj.info.type in {
parser.RObjectType.LGL,
parser.RObjectType.INT,
parser.RObjectType.REAL,
parser.RObjectType.CPLX,
}:
# Return the internal array
value = convert_array(obj, attrs=attrs)
elif obj.info.type == parser.RObjectType.STR:
# Convert the internal strings
value = np.array([self._convert_next(o) for o in obj.value])
elif obj.info.type == parser.RObjectType.VEC:
# Convert the internal objects
value = convert_vector(obj, self._convert_next, attrs=attrs)
elif obj.info.type == parser.RObjectType.EXPR:
rexpression_list = convert_vector(
obj,
self._convert_next,
attrs=attrs,
)
assert isinstance(rexpression_list, list)
# Convert the internal objects returning a special object
value = RExpression(rexpression_list)
elif obj.info.type == parser.RObjectType.BCODE:
value = RBytecode(
code=self._convert_next(obj.value[0]),
constants=[self._convert_next(c) for c in obj.value[1]],
attributes=attrs,
)
elif obj.info.type == parser.RObjectType.EXTPTR:
value = RExternalPointer(
protected=self._convert_next(obj.value[0]),
tag=self._convert_next(obj.value[1]),
)
elif obj.info.type == parser.RObjectType.S4:
value = SimpleNamespace(**attrs)
elif obj.info.type == parser.RObjectType.BASEENV:
value = self.base_environment
elif obj.info.type == parser.RObjectType.EMPTYENV:
value = self.empty_environment
elif obj.info.type == parser.RObjectType.MISSINGARG:
value = NotImplemented
elif obj.info.type == parser.RObjectType.GLOBALENV:
value = self.global_environment
elif obj.info.type == parser.RObjectType.REF:
# Return the referenced value
value = self.references.get(id(obj.referenced_object))
if value is None:
reference_id = id(obj.referenced_object)
assert obj.referenced_object is not None
self.references[reference_id] = UnresolvedReference(
self.references,
reference_id,
)
value = self._convert_next(obj.referenced_object)
elif obj.info.type == parser.RObjectType.NILVALUE:
value = None
else:
msg = f"Type {obj.info.type} not implemented"
raise NotImplementedError(msg)
if obj.info.object and attrs is not None:
classname = attrs.get("class", ())
for i, c in enumerate(classname):
constructor = self.constructor_dict.get(c, None)
new_value = (
constructor(value, attrs)
if constructor
else NotImplemented
)
if new_value is NotImplemented:
missing_msg = (
f"Missing constructor for R class \"{c}\". "
)
if len(classname) > (i + 1):
solution_msg = (
f"The constructor for class "
f"\"{classname[i+1]}\" will be "
f"used instead."
)
else:
solution_msg = (
"The underlying R object is "
"returned instead."
)
warnings.warn(
missing_msg + solution_msg,
stacklevel=1,
)
else:
value = new_value
break
self.references[reference_id] = value
return value
def convert(
data: parser.RData | parser.RObject,
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
"""
Use the default converter (:func:`SimpleConverter`) to convert the data.
Args:
data: Parsed data.
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.
Examples:
Parse one of the included examples, containing a vector
>>> import rdata
>>>
>>> parsed = rdata.parser.parse_file(
... rdata.TESTDATA_PATH / "test_vector.rda")
>>> converted = rdata.conversion.convert(parsed)
>>> converted
{'test_vector': array([1., 2., 3.])}
Parse another example, containing a dataframe
>>> import rdata
>>>
>>> parsed = rdata.parser.parse_file(
... rdata.TESTDATA_PATH / "test_dataframe.rda")
>>> converted = rdata.conversion.convert(parsed)
>>> converted
{'test_dataframe': class value
1 a 1
2 b 2
3 b 3}
"""
return SimpleConverter(
constructor_dict=constructor_dict,
default_encoding=default_encoding,
force_default_encoding=force_default_encoding,
global_environment=global_environment,
base_environment=base_environment,
).convert(data)
|