Extension of Builtin Functions¶
Accessor¶
- extepy.arggetter(*keys)[source]¶
Return a callable object that fetches the argument(s) from its operand.
- Parameters:
*keys (int | str) – If it is an
int
(can be negative), it is the index of the positional argument to be fetched. If it is astr
, it is the name of keyword argument to be fetched.- Return type:
callable
- Raises:
KeyError | IndexError – Raises when the argument is not found.
Examples
Get the first positional argument.
>>> getter = arggetter(0) >>> getter("a", "b", "c", key="value") 'a'
Get the positional arguments and keyword arguments.
>>> getter = arggetter(-1, 0, 1, "key") >>> getter("a", "b", "c", key="value") ('c', 'a', 'b', 'value')
Raise an error if the index is out of range.
>>> getter = arggetter(0, 1, 2) >>> getter("A", key="value") Traceback (most recent call last): ... IndexError: positional argument index is out of range
Checker¶
- extepy.argchecker(*keys)[source]¶
Return a callable object to check the existences of argument(s).
- Parameters:
*keys (int | str) – If it is an
int
(can be negative), it is the index of the positional argument to be fetched. If it is astr
, it is the name of keyword argument to be fetched.- Return type:
bool | tuple[bool]
Examples
>>> getter = argchecker(-1, 0, 1, 5, "key", "other") >>> getter("a", "b", "c", key="value") (True, True, True, False, True, False)
- extepy.attrchecker(*attrs)[source]¶
Return a callable object to check the existence of the given attribute(s).
- Parameters:
*attrs (str) – Attribute names.
Examples
>>> c = attrchecker("mro", "type") >>> c(type) (True, False)
>>> import os >>> c = attrchecker("path", "path.sep", "sep.path") >>> c(os) (True, True, False)
- extepy.itemchecker(*items)[source]¶
Return a callable object to check the existence of the given item(s).
Examples
>>> data = {"name": {"first": "Zhiqing", "last": "Xiao"}, "city": "Beijing"} >>> c = itemchecker(["name", "first"], ["name", "middle"], ["name", "last"], "city", "sex") >>> c(data) (True, False, True, True, False)
>>> itemchecker(-3, 0, 3)([1, 2, 3]) (True, True, False)
Factory¶
- extepy.constantcreator(value, copy=False)[source]¶
Callable that returns the same constant when it is called.
- Parameters:
value – Constant value to be returned.
copy – If
True
, return a new copy of the constant value.
- Returns:
Callable object that returns
value
, ignoring its parameters.- Return type:
callable
Examples
Always return the string
"value"
.>>> creator = constantcreator("value") >>> creator() 'value'
Create a pd.DataFrame whose elements are all empty lists.
>>> import pandas as pd >>> df = pd.DataFrame(index=range(3), columns=["A"]) >>> (df.map if hasattr(df, "map") else df.applymap)(constantcreator([])) A 0 [] 1 [] 2 []
Return a new copy when
copy=True
.>>> import pandas as pd >>> df = pd.DataFrame(index=range(3), columns=["A"]) >>> constantcreator(df, copy=True)() is df False