Wrapper around ast.literal_eval with new {foo='bar', key=None} dict syntax.
Project description
litereval
litereval
is wrapper around ast.literal_eval
with new additional {foo='bar', key=None}
dict
syntax.
Plus some helper tools to deep merge dictionaries, parse ast.literal_eval
python data to *args
and **kwargs
.
Can be used to create wrapper command line interfaces. See pyppdf.
Install
Needs python 3.6+
conda install -c defaults -c conda-forge litereval
or
pip install litereval
API
litereval
def litereval(string: str):
"""
Small extension of ``ast.literal_eval`` that also
accepts dict in a form of ``{key=100, foo='bar'}``
Returns
-------
ret :
ast.literal_eval(preprocess(string))
"""
merge
def merge(source: dict, destination: dict,
copy: bool = False) -> dict:
"""
Deep merge two dictionaries.
Overwrites in case of conflicts.
From https://stackoverflow.com/a/20666342
"""
args_kwargs
def args_kwargs(args: Any) -> Tuple[
Union[tuple, None], Union[dict, None]
]:
"""
Parses ``args`` object to ``(*args, **kwargs)`` tuple.
Special case when ``args`` is ``None``: returns ``(None, None)``.
Otherwise tries to put not iterable object to tuple:
``args`` to ``(args,)``. Examples:
* ``(1, 2)`` to ``(1, 2), {}``
* ``"foo"`` to ``("foo",), {}``
* ``{(): ('a', 0), 'foo': None} to
``('a', 0), {'foo': None}``
Returns
-------
ret :
tuple: *args, **kwargs
"""
get_args
def get_args(name: str, args, default=None) -> Args:
"""
Gets ``*args`` and ``**kwargs`` for a ``name`` function
from an ``args`` dict. Wrapper around ``args_kwargs`` function.
Returns ``NamedTuple`` ``Args``: ``(args: tuple, kwargs: dict)``
"""
get
def get(key: str, dic, default=None):
"""Gets key even from not a dictionary."""
tuple_
def tuple_(obj: Any) -> tuple:
"""Converts any object to tuple. ``string`` to ``(string,)``."""
validated
def validated(args: tuple, kwargs: dict) -> Tuple[tuple, dict]:
"""Validates inputs and returns ``*args, **kwargs``."""
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
litereval-0.0.11.tar.gz
(20.8 kB
view details)
File details
Details for the file litereval-0.0.11.tar.gz
.
File metadata
- Download URL: litereval-0.0.11.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2df538916be7aca13c2ebe6afa98ce7b0bbd683b28425189bfa1f24582f512d |
|
MD5 | 085e94db92577346b1b722591e85c35f |
|
BLAKE2b-256 | ee46e4f867cdf72dfec08ae0c854ae163bf6643161120be27d7797f24015e24c |