mode.utils.text — mode-ng 0.4.0 documentation

# `mode.utils.text`

Text and string manipulation utilities.

class mode.utils.text.FuzzyMatch(ratio: float, value: str)

  Fuzzy match resut.

  ratio: float

    Alias for field number 0

  value: str

    Alias for field number 1

mode.utils.text.abbr(s: str, max: int, suffix: str = '...', words: bool = False) → str

  Abbreviate word.

mode.utils.text.abbr_fqdn(origin: str, name: str, *, prefix: str = '') → str

  Abbreviate fully-qualified Python name, by removing origin.

  `app.origin` is the package where the app is defined, so if this is `examples.simple`:

      >>> app.origin
      'examples.simple'
      >>> abbr_fqdn(app.origin, 'examples.simple.Withdrawal', prefix='[...]')
      '[...]Withdrawal'

      >>> abbr_fqdn(app.origin, 'examples.other.Foo', prefix='[...]')
      'examples.other.foo'

  `shorten_fqdn()` is similar, but will always shorten a too long name, abbr_fqdn will only remove the origin portion of the name.

mode.utils.text.didyoumean(haystack: Iterable[str], needle: str, *, fmt_many: str = 'Did you mean one of {alt}?', fmt_one: str = 'Did you mean {alt}?', fmt_none: str = '', min_ratio: float = 0.6) → str

  Generate message with helpful list of alternatives.

  Examples

      >>> raise Exception(f'Unknown mode: {mode}! {didyoumean(modes, mode)}')

      >>> didyoumean(['foo', 'bar', 'baz'], 'boo')
      'Did you mean foo?'

      >>> didyoumean(['foo', 'moo', 'bar'], 'boo')
      'Did you mean one of foo, moo?'

      >>> didyoumean(['foo', 'moo', 'bar'], 'xxx')
      ''

  Parameters:

    - haystack – List of all available choices.

    - needle – What the user provided.

    - fmt_many – String format returned when there are more than one alternative. Default is: `"Did you mean one of {alt}?"`.

    - fmt_one – String format returned when there’s a single fuzzy match. Default is: `"Did you mean {alt}?"`.

    - fmt_none – String format returned when there are no fuzzy matches. Default is: `""` (empty string, error message is usually printed before the alternatives so user has context).

    - min_ratio – Minimum fuzzy ratio before word is considered a match. Default is 0.6.

mode.utils.text.enumeration(items: Iterable[str], *, start: int = 1, sep: str = '\n', template: str = '{index}) {item}') → str

  Enumerate list of strings.

  Example

      >>> enumeration(['x', 'y', '...'])
      "1) x\n2) y\n3) ..."

mode.utils.text.fuzzymatch_best(haystack: Iterable[str], needle: str, *, min_ratio: float = 0.6) → Optional[str]

  Fuzzy Match - Return best match only (single scalar value).

mode.utils.text.fuzzymatch_choices(haystack: Iterable[str], needle: str, *, fmt_many: str = 'one of {alt}', fmt_one: str = '{alt}', fmt_none: str = '', min_ratio: float = 0.6) → str

  Fuzzy match reducing to error message suggesting an alternative.

mode.utils.text.fuzzymatch_iter(haystack: Iterable[str], needle: str, *, min_ratio: float = 0.6) → Iterator[FuzzyMatch]

  Fuzzy Match: Including actual ratio.

  Yields:

    FuzzyMatch – tuples of `(ratio, value)`.

mode.utils.text.isatty(fh: IO) → bool

  Return True if fh has a controlling terminal.

  Notes

  Use with e.g. `sys.stdin`.

mode.utils.text.maybecat(s: Optional[AnyStr], suffix: str = '', *, prefix: str = '') → Optional[str]

  Concatenate string only if existing string s’ is defined.

  Keyword Arguments:

    - suffix – add suffix if string s’ is defined.

    - prefix – add prefix is string s’ is defined.

mode.utils.text.pluralize(n: int, text: str, suffix: str = 's') → str

  Pluralize term when n is greater than one.

mode.utils.text.shorten_fqdn(s: str, max: int = 32) → str

  Shorten fully-qualified Python name (like “os.path.isdir”).

mode.utils.text.title(s: str) → str

  Capitalize sentence.

  `"foo bar" -> "Foo Bar"`

  `"foo-bar" -> "Foo Bar"`

mode.utils.text.want_bytes(s: AnyStr) → bytes

  Convert string to bytes.

mode.utils.text.want_str(s: AnyStr) → str

  Convert bytes to string.