latex2pydata – load data from LaTeX in Python literal format
The latex2pydata Python package is designed to load data in Python literal format that was saved to file by the latex2pydata LaTeX package. This allows data to be passed from LaTeX to Python.
Raw data is loaded with
ast.literal_eval(). This always yields either dict[str,str] or
list[dict[str,str]]. Then data is postprocessed to apply any schemas and to unpack key paths.
-
The LaTeX package allows schemas to be defined using Python type annotation syntax. Schemas are communicated within a special latex2pydata metadata comment at the very beginning of data files. When a schema exists, string values are evaluated with
ast.literal_eval(), and then the data type of each result is checked against the schema. -
All dict keys are required to match the regex
[A-Za-z_][0-9A-Za-z_]*. Periods in keys are interpreted as key paths and indicate sub-dicts. For example, the key pathmain.subrepresents a keymainin the main dict that maps to a sub-dict containing a keysub.
Schema support
The following scalar data types are supported in schemas: bool, bytes,
float, int, None, str, and tuple. The following collection types
are supported: dict, list, and set. Any is supported for scalars and
for collections (subscripting Any[...] is not supported for collections).
There is also a verbatim data type that is defined specifically for
latex2pydata. This keeps the string data received from LaTeX verbatim,
without any interpretation by ast.literal_eval().
The current parser for Python type annotation syntax is basic and limits the supported schema data types:
-
Nested collection data types are only supported 2 levels deep. So
list[list[int]]is fine, butlist[list[list[int]]]is not supported. -
Collection union types are not supported. For example,
set[int]|list[int]is not supported. (Scalar union types such asfloat|intare supported.)
It is possible to work around these limitations by setting
\pydatasetschemamissing{evalany} on the LaTeX side. This causes all values
without a schema definition to be evaluated with ast.literal_eval(), and
skips type checking for values that are not defined in the schema.
Currently, if a key path is used in defining a schema value or setting a data value, then the same key path must be used in both the schema and the data. That is, schema validation is currently performed before key path unpacking.
See the source code and the latex2pydata LaTeX package documentation for additional details about schema support.
Keys and key paths
Data is interpreted as Python literals. Thus, there is no checking for duplicate keys. If a key is defined multiple times, later values replace earlier values. Similarly, there is no checking for duplicate keys during key path unpacking.
Usage
The package provides two functions for loading data:
-
load(<filehandle or pathlib.Path>, encoding='utf-8-sig') -
loads(<string>)
Both of these functions takes optional arguments schema: dict[str, str] and
schema_missing: 'error' | 'verbatim' | 'evalany'. If these are provided,
they override any schema settings in the file/string metadata.
Tests
The latex2pydata Python package includes tests. Additional tests are part of the latex2pydata LaTeX package.
Release files for latex2pydata 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| latex2pydata-0.5.0.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| latex2pydata-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.9 kB
Release files / latex2pydata-0.5.0.tar.gz
| Download URL | latex2pydata-0.5.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bccad2083c3a6ed70492653d5d819ccd9360a3afc3c319dbecf496fba0574289
|
|
BLAKE2b-256 checksum How to use checksums |
1cd973eeb2c0f27ba3678d5ba84fb28c1edb89faca5a11818d4334458fe874b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|
Release files / latex2pydata-0.5.0-py3-none-any.whl
| Download URL | latex2pydata-0.5.0-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c4092562082985453f47f52db31e1f8c2edcf5d7f855f4c614cfba12808675b
|
|
BLAKE2b-256 checksum How to use checksums |
a3fd70f1ae3a49c44233ec1785f16ea4c261d12c200111e13d27e4f01de699bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.7
|