Conditions | 24 |
Total Lines | 197 |
Code Lines | 157 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like tool.dev.entrypoints() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | #!/usr/bin/env python |
||
361 | @dev.command(short_help="List setuptools installed component information") |
||
362 | @click.option( |
||
363 | "--base", |
||
364 | "-b", |
||
365 | is_flag=True, |
||
366 | default=False, |
||
367 | help="Also list isomer-base (integrated) modules", |
||
368 | ) |
||
369 | @click.option( |
||
370 | "--sails", |
||
371 | "-s", |
||
372 | is_flag=True, |
||
373 | default=False, |
||
374 | help="Also list isomer-sails (integrated) modules", |
||
375 | ) |
||
376 | @click.option( |
||
377 | "--schemata", |
||
378 | is_flag=True, |
||
379 | default=False, |
||
380 | help="Also list registered schemata" |
||
381 | ) |
||
382 | @click.option( |
||
383 | "--management", |
||
384 | is_flag=True, |
||
385 | default=False, |
||
386 | help="Also list registered management commands" |
||
387 | ) |
||
388 | @click.option( |
||
389 | "--provisions", |
||
390 | is_flag=True, |
||
391 | default=False, |
||
392 | help="Also list registered provisions" |
||
393 | ) |
||
394 | @click.option( |
||
395 | "--frontend-only", |
||
396 | "-f", |
||
397 | is_flag=True, |
||
398 | default=False, |
||
399 | help="Only list modules with a frontend", |
||
400 | ) |
||
401 | @click.option( |
||
402 | "--frontend-list", |
||
403 | "-l", |
||
404 | is_flag=True, |
||
405 | default=False, |
||
406 | help="List files in frontend per module", |
||
407 | ) |
||
408 | @click.option( |
||
409 | "--directory", "-d", is_flag=True, default=False, help="Show directory of module" |
||
410 | ) |
||
411 | @click.option( |
||
412 | "--sort-key", |
||
413 | "-k", |
||
414 | default="package", |
||
415 | type=click.Choice( |
||
416 | ["name", "package", "classname", "location", "frontend", "group"]), |
||
417 | ) |
||
418 | @click.option( |
||
419 | "--list-all", |
||
420 | "--all", |
||
421 | "-a", |
||
422 | is_flag=True, |
||
423 | default=False, |
||
424 | help="List all registered entrypoints" |
||
425 | ) |
||
426 | @click.option( |
||
427 | "--filter-string", |
||
428 | "--filter", |
||
429 | "-f", |
||
430 | default="", |
||
431 | type=str, |
||
432 | help="Filter table by string" |
||
433 | ) |
||
434 | @click.option("--long", is_flag=True, default=False, help="Show full table") |
||
435 | def entrypoints(base, sails, schemata, management, provisions, frontend_only, |
||
436 | frontend_list, directory, sort_key, list_all, filter_string, long): |
||
437 | """Display list of entrypoints and diagnose module loading problems.""" |
||
438 | |||
439 | log("Showing entrypoints:") |
||
440 | |||
441 | full_component = namedtuple( |
||
442 | "Component", ["name", "package", "classname", "location", "frontend", "group"] |
||
443 | ) |
||
444 | component = namedtuple("Component", ["name", "package", "frontend", "group"]) |
||
445 | results = [] |
||
446 | |||
447 | from pkg_resources import iter_entry_points |
||
448 | |||
449 | entry_points = { |
||
450 | "components": iter_entry_points(group="isomer.components", name=None) |
||
451 | } |
||
452 | |||
453 | if list_all: |
||
454 | sails = base = schemata = management = provisions = True |
||
455 | |||
456 | if sails: |
||
457 | entry_points["sails"] = iter_entry_points(group="isomer.sails", name=None) |
||
458 | if base: |
||
459 | entry_points["base"] = iter_entry_points(group="isomer.base", name=None) |
||
460 | if schemata: |
||
461 | entry_points["schemata"] = iter_entry_points(group="isomer.schemata", name=None) |
||
462 | if management: |
||
463 | entry_points["management"] = iter_entry_points(group="isomer.management", |
||
464 | name=None) |
||
465 | if provisions: |
||
466 | entry_points["provisions"] = iter_entry_points(group="isomer.provisions", |
||
467 | name=None) |
||
468 | |||
469 | log("Entrypoints:", entry_points, pretty=True, lvl=verbose) |
||
470 | try: |
||
471 | for key, iterator in entry_points.items(): |
||
472 | for entry_point in iterator: |
||
473 | log("Entrypoint Group:", key, entry_point, pretty=True, lvl=debug) |
||
474 | |||
475 | try: |
||
476 | name = entry_point.name |
||
477 | package = entry_point.dist.project_name |
||
478 | log("Package:", package, pretty=True, lvl=debug) |
||
479 | location = entry_point.dist.location |
||
480 | try: |
||
481 | loaded = entry_point.load() |
||
482 | except pkg_resources.DistributionNotFound as e: |
||
483 | log( |
||
484 | "Required distribution not found:", |
||
485 | e, |
||
486 | pretty=True, |
||
487 | exc=True, |
||
488 | lvl=warn, |
||
489 | ) |
||
490 | continue |
||
491 | |||
492 | log( |
||
493 | "Entry point: ", |
||
494 | entry_point, |
||
495 | name, |
||
496 | entry_point.resolve(), |
||
497 | location, |
||
498 | lvl=debug, |
||
499 | ) |
||
500 | |||
501 | log("Loaded: ", loaded, lvl=debug) |
||
502 | |||
503 | try: |
||
504 | pkg = pkg_resources.Requirement.parse(package) |
||
505 | frontend = pkg_resources.resource_listdir(pkg, "frontend") |
||
506 | log("Frontend resources found:", frontend, lvl=debug) |
||
507 | except Exception as e: |
||
508 | log( |
||
509 | "Exception during frontend resource lookup:", |
||
510 | e, |
||
511 | lvl=debug, |
||
512 | exc=True, |
||
513 | ) |
||
514 | frontend = None |
||
515 | |||
516 | if frontend not in (None, []): |
||
517 | log("Contains frontend parts", lvl=debug) |
||
518 | if not frontend_list: |
||
519 | frontend = "[X]" |
||
520 | else: |
||
521 | frontend = "[ ]" |
||
522 | |||
523 | if filter_string != "": |
||
524 | if filter_string not in name and filter_string not in package and filter_string not in location: |
||
525 | continue |
||
526 | |||
527 | if long: |
||
528 | if key in ("schemata", "provisions"): |
||
529 | classname = "[SCHEMA]" |
||
530 | else: |
||
531 | classname = repr(loaded).lstrip("<class '").rstrip("'>") |
||
532 | |||
533 | result = full_component( |
||
534 | frontend=frontend, |
||
535 | name=name, |
||
536 | package=package, |
||
537 | classname=classname, |
||
538 | location=location if directory else "use -d", |
||
539 | group=key, |
||
540 | ) |
||
541 | else: |
||
542 | result = component( |
||
543 | frontend=frontend, name=name, package=package, group=key |
||
544 | ) |
||
545 | |||
546 | if not frontend_only or frontend: |
||
547 | results.append(result) |
||
548 | except ImportError as e: |
||
549 | log("Exception while iterating entrypoints:", e, type(e), lvl=warn, |
||
550 | exc=True) |
||
551 | except ModuleNotFoundError as e: |
||
|
|||
552 | log("Module could not be loaded:", e, exc=True) |
||
553 | |||
554 | results = sorted(results, key=attrgetter(sort_key)) |
||
555 | |||
556 | table = std_table(results) |
||
557 | log("\n%s" % table) |
||
558 | |||
583 |