Skip to content

Instantly share code, notes, and snippets.

@pybites
Created January 11, 2021 14:21
Show Gist options
  • Select an option

  • Save pybites/c964c34e46c84f5e9c9e335b098b3aaa to your computer and use it in GitHub Desktop.

Select an option

Save pybites/c964c34e46c84f5e9c9e335b098b3aaa to your computer and use it in GitHub Desktop.

Revisions

  1. pybites created this gist Jan 11, 2021.
    27 changes: 27 additions & 0 deletions objects.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    from importlib import import_module
    from keyword import kwlist
    import builtins
    from typing import Dict, List

    scores = {
    "builtin": 1,
    "keyword": 2,
    "module": 3,
    }


    def score_objects(objects: List[str],
    scores: Dict[str, int] = scores) -> int:
    total = 0
    for obj in objects:
    obj = str(obj)
    if obj in dir(builtins):
    total += scores['builtin']
    if obj in kwlist:
    total += scores['keyword']
    try:
    import_module(obj)
    total += scores['module']
    except ModuleNotFoundError:
    pass
    return total