|
| 1 | +"""Functions to perform conversion and unparsing in one step.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from .conversion import build_r_data, convert_to_r_object, convert_to_r_object_for_rda |
| 7 | +from .conversion.to_r import DEFAULT_FORMAT_VERSION |
| 8 | +from .unparser import unparse_file |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + import os |
| 12 | + from typing import Any |
| 13 | + |
| 14 | + from .conversion.to_r import Encoding |
| 15 | + from .unparser import Compression, FileFormat |
| 16 | + |
| 17 | + |
| 18 | +def write_rds( |
| 19 | + path: os.PathLike[Any] | str, |
| 20 | + data: Any, # noqa: ANN401 |
| 21 | + *, |
| 22 | + file_format: FileFormat = "xdr", |
| 23 | + compression: Compression = "gzip", |
| 24 | + encoding: Encoding = "utf-8", |
| 25 | + format_version: int = DEFAULT_FORMAT_VERSION, |
| 26 | +) -> None: |
| 27 | + """ |
| 28 | + Write an RDS file. |
| 29 | +
|
| 30 | + This is a convenience function that wraps |
| 31 | + :func:`rdata.conversion.convert_to_r_object`, |
| 32 | + :func:`rdata.conversion.build_r_data`, |
| 33 | + and :func:`rdata.unparser.unparse_file`, |
| 34 | + as it is the common use case. |
| 35 | +
|
| 36 | + Args: |
| 37 | + path: File path to be written. |
| 38 | + data: Python data object. |
| 39 | + file_format: File format. |
| 40 | + compression: Compression. |
| 41 | + encoding: Encoding to be used for strings within data. |
| 42 | + format_version: File format version. |
| 43 | +
|
| 44 | + See Also: |
| 45 | + :func:`write_rda`: Similar function that writes an RDA or RDATA file. |
| 46 | +
|
| 47 | + Examples: |
| 48 | + Write a Python object to an RDS file. |
| 49 | +
|
| 50 | + >>> import rdata |
| 51 | + >>> |
| 52 | + >>> data = ["hello", 1, 2.2, 3.3+4.4j] |
| 53 | + >>> rdata.write_rds("test.rds", data) |
| 54 | + """ |
| 55 | + r_object = convert_to_r_object( |
| 56 | + data, |
| 57 | + encoding=encoding, |
| 58 | + ) |
| 59 | + r_data = build_r_data( |
| 60 | + r_object, |
| 61 | + encoding=encoding, |
| 62 | + format_version=format_version, |
| 63 | + ) |
| 64 | + unparse_file( |
| 65 | + path, |
| 66 | + r_data, |
| 67 | + file_type="rds", |
| 68 | + file_format=file_format, |
| 69 | + compression=compression, |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def write_rda( |
| 74 | + path: os.PathLike[Any] | str, |
| 75 | + data: dict[str, Any], |
| 76 | + *, |
| 77 | + file_format: FileFormat = "xdr", |
| 78 | + compression: Compression = "gzip", |
| 79 | + encoding: Encoding = "utf-8", |
| 80 | + format_version: int = DEFAULT_FORMAT_VERSION, |
| 81 | +) -> None: |
| 82 | + """ |
| 83 | + Write an RDA or RDATA file. |
| 84 | +
|
| 85 | + This is a convenience function that wraps |
| 86 | + :func:`rdata.conversion.convert_to_r_object_for_rda`, |
| 87 | + :func:`rdata.conversion.build_r_data`, |
| 88 | + and :func:`rdata.unparser.unparse_file`, |
| 89 | + as it is the common use case. |
| 90 | +
|
| 91 | + Args: |
| 92 | + path: File path to be written. |
| 93 | + data: Python dictionary with data and variable names. |
| 94 | + file_format: File format. |
| 95 | + compression: Compression. |
| 96 | + encoding: Encoding to be used for strings within data. |
| 97 | + format_version: File format version. |
| 98 | +
|
| 99 | + See Also: |
| 100 | + :func:`write_rds`: Similar function that writes an RDS file. |
| 101 | +
|
| 102 | + Examples: |
| 103 | + Write a Python dictionary to an RDA file. |
| 104 | +
|
| 105 | + >>> import rdata |
| 106 | + >>> |
| 107 | + >>> data = {"name": "hello", "values": [1, 2.2, 3.3+4.4j]} |
| 108 | + >>> rdata.write_rda("test.rda", data) |
| 109 | + """ |
| 110 | + r_object = convert_to_r_object_for_rda( |
| 111 | + data, |
| 112 | + encoding=encoding, |
| 113 | + ) |
| 114 | + r_data = build_r_data( |
| 115 | + r_object, |
| 116 | + encoding=encoding, |
| 117 | + format_version=format_version, |
| 118 | + ) |
| 119 | + unparse_file( |
| 120 | + path, |
| 121 | + r_data, |
| 122 | + file_type="rda", |
| 123 | + file_format=file_format, |
| 124 | + compression=compression, |
| 125 | + ) |
0 commit comments