使用新的{foo='bar',key=none}dict语法包装ast.literal的eval。
litereval的Python项目详细描述
文学评论
litereval
是ast.literal_eval
的包装,有新的附加{foo='bar', key=None}
dict
语法。
再加上一些帮助工具来深入合并字典,将python数据解析为*args
和**kwargs
。
可用于创建包装器命令行接口见pyppdf。
安装
需要Python3.6+
conda install -c defaults -c conda-forge litereval
或
pip install litereval
API文学评论
deflitereval(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)) """
合并
defmerge(source:dict,destination:dict,copy:bool=False)->dict:""" Deep merge two dictionaries. Overwrites in case of conflicts. From https://stackoverflow.com/a/20666342 """
阿格斯夸格斯
defargs_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 """
获取参数
defget_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)`` """
获取
defget(key:str,dic,default=None):"""Gets key even from not a dictionary."""
元组
deftuple_(obj:Any)->tuple:"""Converts any object to tuple. ``string`` to ``(string,)``."""
已验证
defvalidated(args:tuple,kwargs:dict)->Tuple[tuple,dict]:"""Validates inputs and returns ``*args, **kwargs``."""
deftuple_(obj:Any)->tuple:"""Converts any object to tuple. ``string`` to ``(string,)``."""
已验证
defvalidated(args:tuple,kwargs:dict)->Tuple[tuple,dict]:"""Validates inputs and returns ``*args, **kwargs``."""