gummy.utils.generic_utils module¶
Utility programs that can be used in general.
-
gummy.utils.generic_utils.
handleKeyError
(lst, **kwargs)[source]¶ Check whether all
kwargs.values()
in thelst
.- Parameters
lst (list) – candidates.
kwargs –
key
is the varname that is easy to understand when an error occurs
Examples
>>> from pycharmers.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
-
gummy.utils.generic_utils.
class2str
(class_)[source]¶ Convert class to str.
- Parameters
class (class) – class object
Examples
>>> from pycharmers.utils import class2str >>> class2str(str) 'str' >>> class2str(tuple) 'tuple'
-
gummy.utils.generic_utils.
handleTypeError
(types, **kwargs)[source]¶ Check whether all types of
kwargs.values()
match any oftypes
.- Parameters
lst (list) – candidate types.
kwargs –
key
is the varname that is easy to understand when an error occurs
Examples
>>> from pycharmers.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
-
gummy.utils.generic_utils.
str_strip
(string)[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
Example
>>> from pycharmers.utils import str_strip >>> str_strip(" hoge ") 'hoge' >>> str_strip(" ho ge ") 'ho ge' >>> str_strip(" ho g e") 'ho g e'
-
gummy.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
Example
>>> from pycharmers.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'
-
gummy.utils.generic_utils.
mk_class_get
(all_classes={}, gummy_abst_class=[], genre='')[source]¶ Create a get function.
- Parameters
all_classes (dict) – Dictionary of
identifier
-> instancegummy_abst_class (list) – The list of GummyAbstClass names.
genre (str) – Genre of the class.
-
gummy.utils.generic_utils.
recreate_dir
(path, exist_ok=True)[source]¶ Super-mkdir. Create a leaf directory and all intermediate ones.
- Parameters
path (str) – Path to the target directory.
exist_ok (bool) – If the target directory already exists, raise an FileExistsError if
exist_ok
isFalse
.
-
gummy.utils.generic_utils.
readable_bytes
(size)[source]¶ Unit conversion for readability. :param size: File size expressed in bytes :type size: int
- Returns
(size, unit)
- Return type
tuple (int, str)
Examples
>>> from pycharmers.utils import readable_bytes >>> size, unit = readable_bytes(1e2) >>> print(f"{size:.2f}[{unit}]") 100.00[KB] >>> size, unit = readable_bytes(1e5) >>> print(f"{size:.2f}[{unit}]") 97.66[MB] >>> size, unit = readable_bytes(1e10) >>> print(f"{size:.2f}[{unit}]") 9.31[GB]
-
gummy.utils.generic_utils.
splitted_query_generator
(query, maxsize=5000)[source]¶ Use Natural Language Toolkit to split text wisely.
NOTE: If
word_tokenize(sentence) >> maxsize
, Get stuck in an infinite loop- Parameters
query (str) – English texts.
maxsize (int) – Number of English characters that this generator can yield at one time.
Examples
>>> from gummy.utils import splitted_query_generator >>> gen = splitted_query_generator(query="I have a pen. I have an apple. Apple pen! I have a pen. I have a pineapple. Pineapple pen! Applepen… pineapplepen… Pen-Pineapple-Apple-Pen! Pen-Pineapple-Apple-Pen!", maxsize=25) >>> for i,text in enumerate(gen): ... print(i, text) 0 I have a pen. 1 I have an apple. 2 Apple pen! I have a pen. 3 I have a pineapple. 4 Pineapple pen! Applepen… 5 pineapplepen… 6 Pen-Pineapple-Apple-Pen ! 7 Pen-Pineapple-Apple-Pen!
-
gummy.utils.generic_utils.
get_latest_filename
(dirname='.', ext=None)[source]¶ Returns the most recently edited or added file path.
- Parameters
dirname (str) – Where the extracted file will be stored.
ext (str) – Extract only files with this extension from compressed files. If
None
, all files will be extracted.
Examples
>>> from gummy.utils import UTILS_DIR, get_latest_filename >>> get_latest_filename(UTILS_DIR) '/Users/iwasakishuto/Github/portfolio/Translation-Gummy/gummy/utils/__pycache__' >>> get_latest_filename(UTILS_DIR, ext=".py") '/Users/iwasakishuto/Github/portfolio/Translation-Gummy/gummy/utils/generic_utils.py'
-
class
gummy.utils.generic_utils.
DictParamProcessor
(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]¶ Bases:
argparse.Action
Receive an argument as a dictionary.
- Raises
ValueError – You must give one argument for each one keyword.
Examples
>>> import argparse >>> from gummy.utils import DictParamProcessor >>> parser = argparse.ArgumentParser() >>> parser.add_argument("--dict_params", action=DictParamProcessor) >>> args = parser.parse_args(args=["--dict_params", "foo = [a, b, c]", "--dict_params", "bar=d"]) >>> args.dict_params {'foo': ['a', 'b', 'c'], 'bar': 'd'} >>> args = parser.parse_args(args=["--dict_params", "foo=a, bar=b"]) ValueError: too many values to unpack (expected 2)
Note
If you run from the command line, execute as follows:
$ python app.py --dict_params "foo = [a, b, c]" --dict_params bar=c
-
gummy.utils.generic_utils.
ListParamProcessorCreate
(type=<class 'str'>)[source]¶ Create a ListParamProcessor
- Parameters
type (type) – type of each element in list.
- Returns
Processor which receives list arguments.
- Return type
ListParamProcessor (argparse.Action)
Examples
>>> import argparse >>> from pycharmers.utils import ListParamProcessorCreate >>> parser = argparse.ArgumentParser() >>> parser.add_argument("--list_params", action=ListParamProcessorCreate()) >>> args = parser.parse_args(args=["--list_params", "[あ, い, う]"]) >>> args.list_params ['あ', 'い', 'う']
-
gummy.utils.generic_utils.
try_wrapper
(func, *args, ret_=None, msg_='', verbose_=True, **kwargs)[source]¶ Wrap
func(*args, **kwargs)
withtry-
andexcept
blocks.- Parameters
func (functions) – functions.
args (tuple) –
*args
forfunc
.kwargs (kwargs) –
*kwargs
forfunc
.ret (any) – default ret val.
msg (str) – message to print.
verbose (bool) – Whether to print message or not. (default=
True
)
Examples
>>> from gummy.utils import try_wrapper >>> ret = try_wrapper(lambda x,y: x/y, 1, 2, msg_="divide") * Succeeded to divide >>> ret 0.5 >>> ret = try_wrapper(lambda x,y: x/y, 1, 0, msg_="divide") * Failed to divide (ZeroDivisionError: division by zero) >>> ret is None True >>> ret = try_wrapper(lambda x,y: x/y, 1, 0, ret_=1, msg_="divide") * Failed to divide (ZeroDivisionError: division by zero) >>> ret is None False >>> ret 1