veditor.utils.generic_utils module¶
- veditor.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 veditor.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
- veditor.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 veditor.utils import class2str >>> class2str(str) 'str' >>> class2str(tuple) 'tuple'
- veditor.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 veditor.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
- veditor.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 veditor.utils import str_strip >>> str_strip(" hoge ") 'hoge' >>> str_strip(" ho ge ") 'ho ge' >>> str_strip(" ho g e") 'ho g e'
- veditor.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 veditor.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'
- veditor.utils.generic_utils.readable_bytes(byte: numbers.Number) → Tuple[numbers.Number, str][source]¶
Unit conversion for readability.
- Parameters
byte (Number) – File byte [B].
Examples
>>> from veditor.utils import readable_bytes >>> for i in range(1,30,3): ... byte = pow(10,i) ... size, unit = readable_bytes(pow(10,i)) ... print(f"{byte:.1g}[B] = {size:.2f}[{unit}]") 1e+01[B] = 10.00[B] 1e+04[B] = 9.77[KB] 1e+07[B] = 9.54[MB] 1e+10[B] = 9.31[GB] 1e+13[B] = 9.09[TB] 1e+16[B] = 8.88[PB] 1e+19[B] = 8.67[EB] 1e+22[B] = 8.47[ZB] 1e+25[B] = 8.27[YB] 1e+28[B] = 8271.81[YB]
- veditor.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.
- veditor.utils.generic_utils.assign_trbl(data: Dict[str, Any], name: str, default: Union[numbers.Number, List[numbers.Number]] = 0, ret_name: bool = False) → Union[Tuple[Tuple[numbers.Number, numbers.Number, numbers.Number, numbers.Number], Tuple[str, str, str, str]], Tuple[numbers.Number, numbers.Number, numbers.Number, numbers.Number]][source]¶
Return the
name
‘s values of [Top
,Right
,Bottom
,Left
] fromdata
. Determine the each position as well as css.- Parameters
data (Dict[str,Any]) – A dictionary which stores data.
name (str) – The name of the value you want to assign..
default (Union[Number,List[Number]], optional) – Default Value. Defaults to
0
.ret_name (bool, optional) – Whether to return names or not. Defaults to
False
.
- Returns
Values of
Top
,Right
,Bottom
,Left
. Ifret_name
isTrue
, add names.- Return type
Union[Tuple[Tuple[Number, Number, Number, Number], Tuple[str,str,str,str]], Tuple[Number, Number, Number, Number]]
Examples
>>> from veditor.utils import assign_trbl >>> assign_trbl(data={"margin": [1,2,3,4]}, name="margin") (1, 2, 3, 4) >>> assign_trbl(data={"margin": [1,2,3]}, name="margin") (1, 2, 3, 2) >>> assign_trbl(data={"margin": [1,2]}, name="margin") (1, 2, 1, 2) >>> assign_trbl(data={"margin": 1}, name="margin") (1, 1, 1, 1) >>> assign_trbl(data={"margin": 1}, name="padding", default=5) (5, 5, 5, 5)
- veditor.utils.generic_utils.openf(file_path: str, timeout: Optional[int] = None, shell: bool = True) → None[source]¶
Open a file in Finder.
- Parameters
file_path (str) – Path to the file to be opened.
timeout (Optional[int], optional) – [description]. Defaults to
None
.shell (bool, optional) – [description]. Defaults to
True
.