added docstring to helper function import_string

This commit is contained in:
Jotagê Sales 2018-12-27 14:35:04 -02:00
parent 375ebd39f0
commit bf029c1b9d
2 changed files with 18 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import types
from sanic.exceptions import PyFileError
from sanic.helpers import import_string
SANIC_PREFIX = "SANIC_"
@ -104,6 +105,9 @@ class Config(dict):
from yourapplication import default_config
app.config.from_object(default_config)
or also:
app.config.from_object('myproject.config.MyConfigClass')
You should not use this function to load the actual configuration but
rather configuration defaults. The actual config should be loaded
with :meth:`from_pyfile` and ideally from a location not within the

View File

@ -1,6 +1,7 @@
"""Defines basics of HTTP standard."""
from importlib import import_module
STATUS_CODES = {
100: b"Continue",
101: b"Switching Protocols",
@ -135,6 +136,17 @@ def remove_entity_headers(headers, allowed=("content-location", "expires")):
def import_string(module_name):
module, obj = module_name.rsplit('.', 1)
"""
import a module or class by string path.
:module_name: str with path of module or path to import and
instanciate a class
:returns: a module object or one instance from class if
module_name is a valid path to class
"""
module, klass = module_name.rsplit(".", 1)
module = import_module(module)
return getattr(module, obj)()
if hasattr(module, klass):
return getattr(module, klass)()
return module