mode.utils.text
Text and string manipulation utilities.
FuzzyMatch
Bases: NamedTuple
Fuzzy match result.
Source code in mode/utils/text.py
25 26 27 28 29 |
|
abbr(s, max, suffix='...', words=False)
Abbreviate word.
Source code in mode/utils/text.py
191 192 193 194 195 |
|
abbr_fqdn(origin, name, *, prefix='')
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.
Source code in mode/utils/text.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
didyoumean(haystack, needle, *, fmt_many='Did you mean one of {alt}?', fmt_one='Did you mean {alt}?', fmt_none='', min_ratio=0.6)
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:
Name | Type | Description | Default |
---|---|---|---|
haystack |
Iterable[str]
|
List of all available choices. |
required |
needle |
str
|
What the user provided. |
required |
fmt_many |
str
|
String format returned when there are more than one
alternative. Default is: |
'Did you mean one of {alt}?'
|
fmt_one |
str
|
String format returned when there's a single fuzzy match.
Default is: |
'Did you mean {alt}?'
|
fmt_none |
str
|
String format returned when there are no fuzzy matches.
Default is: |
''
|
min_ratio |
float
|
Minimum fuzzy ratio before word is considered a match. Default is 0.6. |
0.6
|
Source code in mode/utils/text.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
enumeration(items, *, start=1, sep='\n', template='{index}) {item}')
Enumerate list of strings.
Example:
>>> enumeration(['x', 'y', '...'])
"1) x\n2) y\n3) ..."
Source code in mode/utils/text.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
fuzzymatch_best(haystack, needle, *, min_ratio=0.6)
Fuzzy Match - Return best match only (single scalar value).
Source code in mode/utils/text.py
178 179 180 181 182 183 184 185 186 187 188 |
|
fuzzymatch_choices(haystack, needle, *, fmt_many='one of {alt}', fmt_one='{alt}', fmt_none='', min_ratio=0.6)
Fuzzy match reducing to error message suggesting an alternative.
Source code in mode/utils/text.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
fuzzymatch_iter(haystack, needle, *, min_ratio=0.6)
Fuzzy Match: Including actual ratio.
Yields:
Name | Type | Description |
---|---|---|
FuzzyMatch |
FuzzyMatch
|
tuples of |
Source code in mode/utils/text.py
164 165 166 167 168 169 170 171 172 173 174 175 |
|
isatty(fh)
Return True if fh has a controlling terminal.
Notes
Use with e.g. sys.stdin
.
Source code in mode/utils/text.py
46 47 48 49 50 51 52 53 54 55 |
|
maybecat(s, suffix='', *, prefix='')
Concatenate string only if existing string s' is defined.
Other Parameters:
Name | Type | Description |
---|---|---|
s |
Union[AnyStr, None]
|
Main string |
suffix |
str
|
add suffix if string s' is defined. |
prefix |
str
|
add prefix is string s' is defined. |
Source code in mode/utils/text.py
255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
pluralize(n, text, suffix='s')
Pluralize term when n is greater than one.
Source code in mode/utils/text.py
248 249 250 251 252 |
|
shorten_fqdn(s, max=32)
Shorten fully-qualified Python name (like "os.path.isdir").
Source code in mode/utils/text.py
238 239 240 241 242 243 244 245 |
|
title(s)
Capitalize sentence.
"foo bar" -> "Foo Bar"
"foo-bar" -> "Foo Bar"
Source code in mode/utils/text.py
58 59 60 61 62 63 64 65 66 67 |
|
want_bytes(s)
Convert string to bytes.
Source code in mode/utils/text.py
32 33 34 35 36 |
|
want_str(s)
Convert bytes to string.
Source code in mode/utils/text.py
39 40 41 42 43 |
|