X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=string_utils.py;h=78e72cca5a36e672fdc8931cf9a9b9b946ac148e;hb=791755ededfc0de84a81b4c7313830efbd27bf8c;hp=0829846bb26741bfb0c8bdd0226d56511fc31bbd;hpb=6f132c0342ab7aa438ed88d7c5f987cb52d8ca05;p=python_utils.git diff --git a/string_utils.py b/string_utils.py index 0829846..78e72cc 100644 --- a/string_utils.py +++ b/string_utils.py @@ -10,10 +10,12 @@ import numbers import random import re import string -from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple +from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple import unicodedata from uuid import uuid4 +import list_utils + logger = logging.getLogger(__name__) NUMBER_RE = re.compile(r"^([+\-]?)((\d+)(\.\d+)?([e|E]\d+)?|\.\d+)$") @@ -1059,18 +1061,26 @@ def to_bool(in_str: str) -> bool: >>> to_bool('True') True + >>> to_bool('1') True + >>> to_bool('yes') True + >>> to_bool('no') False + >>> to_bool('huh?') False + + >>> to_bool('on') + True + """ if not is_string(in_str): raise ValueError(in_str) - return in_str.lower() in ("true", "1", "yes", "y", "t") + return in_str.lower() in ("true", "1", "yes", "y", "t", "on") def to_date(in_str: str) -> Optional[datetime.date]: @@ -1274,12 +1284,15 @@ def ngrams(txt: str, n: int): """ words = txt.split() - return ngrams_presplit(words, n) + for ngram in ngrams_presplit(words, n): + ret = '' + for word in ngram: + ret += f'{word} ' + yield ret.strip() -def ngrams_presplit(words: Iterable[str], n: int): - for ngram in zip(*[words[i:] for i in range(n)]): - yield(' '.join(ngram)) +def ngrams_presplit(words: Sequence[str], n: int): + return list_utils.ngrams(words, n) def bigrams(txt: str):