How to use the check50.internal.import_file function in check50

To help you get started, we’ve selected a few check50 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cs50 / check50 / check50 / flask.py View on Github external
def __init__(self, path="application.py", app_name="app"):

        path = pathlib.Path(path).resolve()

        # Add directory of flask app to sys.path so we can import it properly
        prevpath = sys.path[0]
        try:
            sys.path[0] = str(path.parent)
            mod = internal.import_file(path.stem, path.name)
        except FileNotFoundError:
            raise Failure(_("could not find {}").format(path.name))
        finally:
            # Restore sys.path
            sys.path[0] = prevpath

        try:
            app = getattr(mod, app_name)
        except AttributeError:
            raise Failure(_("{} does not contain an app").format(file))

        app.testing = True
        # Initialize flask client
        self._client = app.test_client()

        self.response = None
github cs50 / check50 / check50 / py.py View on Github external
def import_(path):
    """Import a Python program given a raw file path

    :param path: path to python file to be imported
    :type path: str
    :raises check50.Failure: if ``path`` doesn't exist, or if the Python file at ``path`` throws an exception when imported.
    """
    exists(path)
    log(_("importing {}...").format(path))
    name = Path(path).stem
    try:
        return internal.import_file(name, path)
    except Exception as e:
        raise Failure(str(e))
github cs50 / check50 / check50 / _api.py View on Github external
This function is particularly useful when a set of checks logically extends
    another, as is often the case in CS50's own problems that have a "less comfy"
    and "more comfy" version. The "more comfy" version can include all of the
    "less comfy" checks like so::

        less = check50.import_checks("../less")
        from less import *

    .. note::
        the ``__name__`` of the imported module is given by the basename
        of the specified path (``less`` in the above example).

    """
    dir = internal.check_dir / path
    file = internal.load_config(dir)["checks"]
    mod = internal.import_file(dir.name, (dir / file).resolve())
    sys.modules[dir.name] = mod
    return mod