Load data from LaTeX in Python literal format
Project description
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. 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.sub
represents a keymain
in the main dict that maps to a sub-dict containing a keysub
.
Schema support
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|int
are 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' | 'rawstr' | '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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file latex2pydata-0.4.0.tar.gz
.
File metadata
- Download URL: latex2pydata-0.4.0.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1206b570748fd7cee78f27a56f4ca209da64d3c419c8e6de9516b64bbf401351 |
|
MD5 | e694ed652b3787598097e7bd56ff73de |
|
BLAKE2b-256 | b2f477eedb8549f1c3a0fcb233ad9c98aecd7b63f4d9c5442912da0f60f8441d |
File details
Details for the file latex2pydata-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: latex2pydata-0.4.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 304012522f2243dcc384ce36015e62416918209a3198e2edccc3005ec3543379 |
|
MD5 | 521a5542f1493727f79ece6c79ba5027 |
|
BLAKE2b-256 | a1b758a2f82beecdba21a0af6c1da62227d2a1cf54ba1cfd433a9b77c7fb708a |