Skip to content

Commit

Permalink
Add constructor_dict to helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
trossi committed Oct 2, 2024
1 parent 59f4269 commit 540a59a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
15 changes: 12 additions & 3 deletions rdata/_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

from typing import TYPE_CHECKING

from .conversion import convert_python_to_r_data
from .conversion.to_r import DEFAULT_FORMAT_VERSION
from .conversion import (
DEFAULT_CONSTRUCTOR_DICT,
DEFAULT_FORMAT_VERSION,
convert_python_to_r_data,
)
from .unparser import unparse_file

if TYPE_CHECKING:
import os
from typing import Any

from .conversion.to_r import Encoding
from .conversion.to_r import ConstructorDict, Encoding
from .unparser import Compression, FileFormat


Expand All @@ -23,6 +26,7 @@ def write_rds(
compression: Compression = "gzip",
encoding: Encoding = "utf-8",
format_version: int = DEFAULT_FORMAT_VERSION,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
) -> None:
"""
Write an RDS file.
Expand All @@ -37,6 +41,7 @@ def write_rds(
compression: Compression.
encoding: Encoding to be used for strings within data.
format_version: File format version.
constructor_dict: Dictionary mapping Python types to R classes.
See Also:
:func:`write_rda`: Similar function that writes an RDA or RDATA file.
Expand All @@ -53,6 +58,7 @@ def write_rds(
data,
encoding=encoding,
format_version=format_version,
constructor_dict=constructor_dict,
)

unparse_file(
Expand All @@ -72,6 +78,7 @@ def write_rda(
compression: Compression = "gzip",
encoding: Encoding = "utf-8",
format_version: int = DEFAULT_FORMAT_VERSION,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
) -> None:
"""
Write an RDA or RDATA file.
Expand All @@ -86,6 +93,7 @@ def write_rda(
compression: Compression.
encoding: Encoding to be used for strings within data.
format_version: File format version.
constructor_dict: Dictionary mapping Python types to R classes.
See Also:
:func:`write_rds`: Similar function that writes an RDS file.
Expand All @@ -102,6 +110,7 @@ def write_rda(
data,
encoding=encoding,
format_version=format_version,
constructor_dict=constructor_dict,
file_type="rda",
)

Expand Down
2 changes: 2 additions & 0 deletions rdata/conversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
ts_constructor as ts_constructor,
)
from .to_r import (
DEFAULT_CONSTRUCTOR_DICT as DEFAULT_CONSTRUCTOR_DICT,
DEFAULT_FORMAT_VERSION as DEFAULT_FORMAT_VERSION,
ConverterFromPythonToR as ConverterFromPythonToR,
convert_python_to_r_data as convert_python_to_r_data,
convert_python_to_r_object as convert_python_to_r_object,
Expand Down
17 changes: 11 additions & 6 deletions rdata/conversion/to_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,31 +346,30 @@ class ConverterFromPythonToR:
Class converting Python objects to R objects.
Attributes:
constructor_dict: Dictionary mapping Python types to R classes.
encoding: Encoding to be used for strings within data.
format_version: File format version.
r_version_serialized: R version written as the creator of the object.
constructor_dict: Dictionary mapping Python types to R classes.
"""
def __init__(self,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
*,
def __init__(self, *,
encoding: Encoding = "utf-8",
format_version: int = DEFAULT_FORMAT_VERSION,
r_version_serialized: int = DEFAULT_R_VERSION_SERIALIZED,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
) -> None:
"""
Init class.
Args:
constructor_dict: Dictionary mapping Python types to R classes.
encoding: Encoding to be used for strings within data.
format_version: File format version.
r_version_serialized: R version written as the creator of the object.
constructor_dict: Dictionary mapping Python types to R classes.
"""
self.constructor_dict = constructor_dict
self.encoding = encoding
self.format_version = format_version
self.r_version_serialized = r_version_serialized
self.constructor_dict = constructor_dict
self._references: dict[str | None, tuple[int, RObject | None]] \
= {None: (0, None)}

Expand Down Expand Up @@ -631,6 +630,7 @@ def convert_python_to_r_data(
encoding: Encoding = "utf-8",
format_version: int = DEFAULT_FORMAT_VERSION,
r_version_serialized: int = DEFAULT_R_VERSION_SERIALIZED,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
file_type: FileType = "rds",
) -> RData:
"""
Expand All @@ -641,6 +641,7 @@ def convert_python_to_r_data(
encoding: Encoding to be used for strings within data.
format_version: File format version.
r_version_serialized: R version written as the creator of the object.
constructor_dict: Dictionary mapping Python types to R classes.
file_type: File type.
Returns:
Expand All @@ -653,6 +654,7 @@ def convert_python_to_r_data(
encoding=encoding,
format_version=format_version,
r_version_serialized=r_version_serialized,
constructor_dict=constructor_dict,
).convert_to_r_data(data, file_type=file_type)


Expand All @@ -662,6 +664,7 @@ def convert_python_to_r_object(
encoding: Encoding = "utf-8",
format_version: int = DEFAULT_FORMAT_VERSION,
r_version_serialized: int = DEFAULT_R_VERSION_SERIALIZED,
constructor_dict: ConstructorDict = DEFAULT_CONSTRUCTOR_DICT,
) -> RObject:
"""
Convert Python data to R object.
Expand All @@ -671,6 +674,7 @@ def convert_python_to_r_object(
encoding: Encoding to be used for strings within data.
format_version: File format version.
r_version_serialized: R version written as the creator of the object.
constructor_dict: Dictionary mapping Python types to R classes.
Returns:
Corresponding RObject object.
Expand All @@ -682,4 +686,5 @@ def convert_python_to_r_object(
encoding=encoding,
format_version=format_version,
r_version_serialized=r_version_serialized,
constructor_dict=constructor_dict,
).convert_to_r_object(data)

0 comments on commit 540a59a

Please sign in to comment.