ux_tools.py
A set of tools to conduct some UX (User eXperience) checks.
As of now, I just want to check for best UX and SEO practices such as checking for best practices in writing for the web.
The primary source of information we will begin to use are from Writing Compelling Digital Copy.
According to the article... * Be Succinct! (Writing for the Web) * "The 3 main guidelines for writing for the web are: - Be succinct: write no more than 50% of the text you would have used in a hardcopy publication - Write for scannability: don't require users to read long continuous blocks of text - Use hypertext to split up long information into multiple pages"
check_li_count(filename, list_type, max, min=3)
returns any warnings regarding li count
Warns if too many lis in a list, too few in a list (default is 3), or too much content in a list item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
name of the HTML document. |
required |
list_type
|
str
|
"ul" or "li" - we don't check definition lists. |
required |
max
|
int
|
maximum recommended list items per list. |
required |
min
|
minimum recommend list items per list (default is 3) |
3
|
Returns
Source code in webcode_tk/ux_tools.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
check_li_word_count(filepath, max=45)
sends warnings if too many words are in a list item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
str
|
relative path to the HTML document in question. |
required |
max
|
maximum number of words per li (default is 45) |
45
|
Returns:
Name | Type | Description |
---|---|---|
warnings |
list
|
any warnings about the li content having too many words. |
Source code in webcode_tk/ux_tools.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
extract_text(tag)
extracts only the text from a tag (no markup)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag
|
this is a Tag (bs4) |
required |
Returns: text: just the visible text from the element (with no nested markup).
Source code in webcode_tk/ux_tools.py
224 225 226 227 228 229 230 231 232 233 234 |
|
get_all_paragraphs(path)
returns a list of paragraph elements from a single file or project folder.
It checks the path to determine if it's a path to a single page or a project folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
a string path to either an html document or a project folder. |
required |
Returns:
Name | Type | Description |
---|---|---|
paragraphs |
list
|
a list of paragraph elements. |
Source code in webcode_tk/ux_tools.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
get_flesch_kincaid_grade_level(path)
returns the required education to be able to understand the text.
We're only looking at the paragraphs (not headers or list items) and ignoring break tags as being paragraph breaks (that could be up for debate in the future).
This function first checks to see if it's a project folder or just a single file and ascertains for all HTML documents if it's a project.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
the path to the page or project that we are measuring. |
required |
Returns:
Name | Type | Description |
---|---|---|
grade_level |
float
|
the US grade level equivalent. |
Source code in webcode_tk/ux_tools.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
get_paragraph_text(paragraphs)
Returns a string of all the contents of the list of paragraphs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paragraphs
|
list
|
a list of paragraph elements. |
required |
Returns:
Name | Type | Description |
---|---|---|
paragraph_text |
str
|
a string of all the content of the paragraph elements. |
Source code in webcode_tk/ux_tools.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
get_readability_stats(path, elements='p')
returns a dictionary of various readability stats.
It will collect all stats from the textastic libary as well as add a few of my own (words per sentence, sentences per paragraph).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
the path to the file or folder (only looks at HTML files) |
required |
elements
|
Union[str, Sequence]
|
by default we will only look at the text from paragraph tags. You could supply other elements in the form of a list or tuple (e.g. ['p', 'div', 'li', 'figcaption']). |
'p'
|
Returns:
Name | Type | Description |
---|---|---|
stats |
dict
|
a dictionary of stats on readability metrics. |
Source code in webcode_tk/ux_tools.py
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 |
|
get_text_from_elements(path, elements='p')
returns a list of text from elements (as strings) from a single file or project folder.
It checks the path to determine if it's a path to a single page or a project folder. It then uses the elements to determine from which HTML element it will grab paragraphs.
By default, it will only pull paragraphs from the <p>
tag, but you
can specify other elements (e.g. <div>
, <li>
, <figcaption>
, etc.).
NOTE: When selecting other tags, pass it a list or tuple of strings. Only
specify the element without angle brackets (e.g. ['p', 'li', 'div']
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
a string path to either an html document or a project folder. |
required |
elements
|
Union[str, Sequence]
|
the elements we want to pull our paragrphs from. |
'p'
|
Returns: paragraphs: a list of strings - text only without markup (for example if an anchor is nested in a pargraph, the markup will be extracted, and only the visible text on the page will be present.
Source code in webcode_tk/ux_tools.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
get_usability_report(project_dir, elements, goals=DEFAULT_GOALS, exceeds=DEFAULT_EXCEEDS_GOALS)
Generates a list of pass, fail, and warnings on usability.
If goals or exceeds not set, goals & exceeds should follow the example keys... goals avg_words_sentence_range, max_sentences_per_paragraph, min_word_count, min_num_single_sentence_paragraphs
exceeds max_avg_words_sentence, max_li_per_list, max_sentence_paragraph, max_words_per_li, max_words_per_paragraph, min_word_count
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_dir
|
str
|
path to project folder |
required |
goals
|
set of goals to test for. |
DEFAULT_GOALS
|
|
exceeds
|
set of metrics to determine if content goes above & beyond. |
DEFAULT_EXCEEDS_GOALS
|
|
elements
|
tuple
|
set of elements to capture |
required |
Returns:
Name | Type | Description |
---|---|---|
report |
list
|
a list of pass, fails, and warnings |
Source code in webcode_tk/ux_tools.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
get_word_count(txt)
returns word count of text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
txt
|
str
|
the raw text (may or may not contain line returns) |
required |
Returns:
Name | Type | Description |
---|---|---|
word_count |
int
|
number of words as measured by Textastic |
Source code in webcode_tk/ux_tools.py
248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
get_words_per_paragraph(path)
returns average number of words per paragraph
uses Textatistic stats
Source code in webcode_tk/ux_tools.py
237 238 239 240 241 242 243 244 245 |
|
remove_extensions(text)
removes extension from filenames.
Filenames with extensions end up causing textatistic to count filenames as two words instead of one, and we don't really need to count the extensions when calculating readability stats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
the text which may or may not have filenames in them. |
required |
Returns:
Name | Type | Description |
---|---|---|
newtext |
str
|
the text without file extensions. |
Source code in webcode_tk/ux_tools.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|