Socon utils

This document covers all modules in socon.utils. Most of the modules in socon.utils are designed for internal use but we believe that it can be useful as well for your framework.

socon.utils.func

get_object_attr()[source]

Return a value from any object attribute. This function allows you to pass a default value if not found or raise a ValueError.

socon.utils.reshape

class TemplateEngine[source]

Base class to modify the content of a file.

Read-only attributes

TemplateEngine.content

The content of the file given at the instance creation.

Methods

TemplateEngine.__init__(filepath: Union[str, PathLike])[source]

When you create TemplateEngine instance, you need to give the path to the file you want to work with. The file is then read and the content is saved in TemplateEngine.content.

TemplateEngine.revert_modif()[source]

Revert the modification applied to the content of the file. Really useful when you have modified the content of a file for a moment but you want to get back to the original quickly after.

TemplateEngine.write(content: str = None, dest: Union[str, Path] = None, **kwargs)[source]

Write content to the current file or to a another file using the dest parameter. Also, this method will allow to pass any parameters to the open function. If you don’t pass any content to the method, TemplateEngine will use the TemplateEngine.content of the current instance.

TemplateEngine.replace(pattern: str, replace: str, **kwargs)[source]

Replace any string that match the pattern in the file. The method use re.sub for replacing the string. You can pass any argument to this function using the kwargs argument.

TemplateEngine.read_file()[source]

Read the content of the file.

TemplateEngine.render(context: dict)[source]

Render a specific template with tags like {{ name }}. This works like jinja. You can pass a dictionary of name and value to render a template that contain {{ name }} tags.

socon.utils.module_loading

Functions for working with Python modules.

import_string(dotted_path: str)[source]

Imports a dotted module path and returns the attribute/class designated by the last name in the path. Raises ImportError if the import failed. For example:

from socon.utils.module_loading import import_string
ImproperlyConfigured = import_string('socon.core.exceptions.ImproperlyConfigured')

is equivalent to:

from socon.core.exceptions import ImproperlyConfigured