wed.utils.generic_utils module¶
- wed.utils.generic_utils.handleKeyError(lst: List[Any], **kwargs)[source]¶
Check whether all
kwargs.values()
in thelst
.- Parameters
lst (List[Any]) – candidates.
kwargs (dict) –
key
is the varname that is easy to understand when an error occurs
Examples
>>> from wed.utils import handleKeyError >>> handleKeyError(lst=range(3), val=1) >>> handleKeyError(lst=range(3), val=100) KeyError: Please choose the argment val from ['0', '1', '2']. you chose 100 >>> handleKeyError(lst=range(3), val1=1, val2=2) >>> handleKeyError(lst=range(3), val1=1, val2=100) KeyError: Please choose the argment val2 from ['0', '1', '2']. you chose 100
- Raises
KeyError – If
kwargs.values()
not in thelst
- wed.utils.generic_utils.class2str(class_: object) → str[source]¶
Convert class to str.
- Parameters
class (object) – class object
- Returns
Class name.
- Return type
str
Examples
>>> from wed.utils import class2str >>> class2str(str) 'str' >>> class2str(tuple) 'tuple'
- wed.utils.generic_utils.handleTypeError(types: List = [typing.Any], **kwargs)[source]¶
Check whether all types of
kwargs.values()
match any oftypes
.- Parameters
types (List[Any]) – Candidate types.
kwargs (dict) –
key
is the varname that is easy to understand when an error occurs
Examples
>>> from wed.utils import handleTypeError >>> handleTypeError(types=[str], val="foo") >>> handleTypeError(types=[str, int], val=1) >>> handleTypeError(types=[str, int], val=1.) TypeError: val must be one of ['str', 'int'], not float >>> handleTypeError(types=[str], val1="foo", val2="bar") >>> handleTypeError(types=[str, int], val1="foo", val2=1.) TypeError: val2 must be one of ['str', 'int'], not float
- Raises
TypeError – If the types of
kwargs.values()
are none of thetypes
- wed.utils.generic_utils.str_strip(string: str) → str[source]¶
Convert all consecutive whitespace characters to ‘ ‘ (half-width whitespace), then return a copy of the string with leading and trailing whitespace removed.
- Parameters
string (str) – string
- Returns
A copy of the string with leading and trailing whitespace removed
- Return type
str
Example
>>> from wed.utils import str_strip >>> str_strip(" hoge ") 'hoge' >>> str_strip(" ho ge ") 'ho ge' >>> str_strip(" ho g e") 'ho g e'
- wed.utils.generic_utils.now_str(tz=None, fmt='%Y-%m-%d@%H.%M.%S')[source]¶
Returns new datetime string representing current time local to
tz
under the control of an explicit format string.- Parameters
tz (datetime.timezone) – Timezone object. If no
tz
is specified, uses local timezone.fmt (str) – format string. See Python Documentation
- Returns
A datetime string.
- Return type
str
Example
>>> from wed.utils import now_str >>> now_str() '2020-09-14@22.31.17' >>>now_str(fmt="%A, %d. %B %Y %I:%M%p") Monday, 14. September 2020 10:31PM' >>> now_str(tz=datetime.timezone.utc) '2020-09-14@13.31.17'
- wed.utils.generic_utils.flatten_dual(lst: List[List[Any]]) → List[Any][source]¶
Flatten double list.
- Parameters
lst (List[List[Any]]) – Dual list.
- Returns
Flattened single list.
- Return type
List[Any]
Example
>>> from pycharmers.utils import flatten_dual >>> flatten_dual([[1,2,3],[4,5,6]]) [1, 2, 3, 4, 5, 6] >>> flatten_dual([[[1,2,3]],[4,5,6]]) [[1, 2, 3], 4, 5, 6] >>> flatten_dual(flatten_dual([[[1,2,3]],[4,5,6]])) TypeError: 'int' object is not iterable
- Raises
TypeError – If list is not a dual list.
- wed.utils.generic_utils.verbose2print(prefix: str = '', verbose: bool = True)[source]¶
Create a simple print function based on verbose
- Parameters
prefix (str, optional) – Prefix Text for
verbose (bool, optional) – Whether to print or not. Defaults to
True
.
- Returns
Print function
- Return type
function
Examples
>>> from wed.utils import verbose2print >>> print_verbose = verbose2print(verbose=True) >>> print_non_verbose = verbose2print(verbose=False) >>> print_verbose("Hello, world.") Hello, world. >>> print_non_verbose = verbose2print("Hello, world.")
- wed.utils.generic_utils.pretty_3quote(*value, indent: int = 0)[source]¶
pretty 3 quote string.
- Parameters
indent (int, optional) – If indent is a non-negative integer, then multiple lines will be pretty-printed with that indent level. Defaults to
0
.
Examples
>>> from wed.utils import pretty_3quote >>> print(*pretty_3quote(""" ... When I was 17, I read a quote that went something like: ... “If you live each day as if it was your last, someday you’ll most certainly be right.” ... It made an impression on me, and since then, for the past 33 years, >>> """)) When I was 17, I read a quote that went something like: “If you live each day as if it was your last, someday you’ll most certainly be right.” It made an impression on me, and since then, for the past 33 years,