he package

he.contexts module

Helpers that can be used for context management.

he.contexts.working_directory(temporary_path: Union[pathlib.Path, str], initial_path: Union[pathlib.Path, str] = PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/he/checkouts/latest/docs/source')) → Iterator[None][source]

Change working directory, and return to initial_path on exit.

It’s needed for PRAW for example, because it looks for praw.ini in Path.cwd(), but that file could be kept in a different directory.

initial_path can be used for example to change working directory relative to the script path, or to end up in a different directory than Path.cwd() of the calling script.

Inspiration: https://stackoverflow.com/questions/41742317/how-can-i-change-directory-with-python-pathlib

he.decorators module

Helpers that can be used to decorate functions.

class he.decorators.CountCalls(func: F)[source]

Bases: object

Log to DEBUG how many times a function gets called, save the result in a newly created attribute num_calls.

he.decorators.count_calls(func: F) → F[source]

Log to DEBUG how many times a function gets called, save the result in a newly created attribute num_calls.

he.decorators.debug(func: F) → F[source]

Output to logging.DEBUG the function arguments and return value.

he.decorators.repeat(_func: Optional[F] = None, *, num_times: int = 3) → Union[F, Callable[[F], F]][source]

Repeat a function 3 times when decorated without arguments. Otherwise num_times.

he.decorators.singleton(cls: C) → C[source]

Transform a class into a Singleton (only one instance can exist).

he.decorators.throttle(_func: Optional[F] = None, *, rate: float = 1) → Union[F, Callable[[F], F]][source]

Throttle a function call, so that at minimum it can be called every rate seconds.

Usage:

# this will enforce the default minimum time of 1 second between function calls
@throttle
def ...

or:

# this will enforce a custom minimum time of 2.5 seconds between function calls
@throttle(rate=2.5)
def ...

This will raise an error, because rate= needs to be specified:

@throttle(5)
def ...
he.decorators.timer(func: F) → F[source]

Calculate the runtime of a function, and output it to logging.DEBUG.

he.file_tools module

Helpers related to local files.

he.handlers module

Helpers that can be used for callback.

class he.handlers.InterruptHandler[source]

Bases: object

Intercept force-quiting of the app.

To use:

from he.handlers import InterruptHandler
INTERRUPT_HANDLER = InterruptHandler()
signal.signal(signal.SIGINT, INTERRUPT_HANDLER.signal_handler)
signal.signal(signal.SIGTERM, INTERRUPT_HANDLER.signal_handler)

Elsewhere:

while True:
    # task
    if INTERRUPT_HANDLER.interrupted:
        break

Inspiration: https://stackoverflow.com/a/43787607/4257312

signal_handler(signal: int) → None[source]

he.http_tools module

Helpers related to HTTP/HTTPS.

he.http_tools.get_json_parsed_from(url: Union[str, bytes]) → Dict[Any, Any][source]

Get a JSON file and return it parsed, or return an empty dict if any error occurred.

he.http_tools.random_headers() → Dict[str, str][source]

Return headers dict for use when making requests to web servers.

Useful to simulate a browser more realistically. The user agent is random.

he.other module

Helpers that don’t fit specifically in any of the other modules.

he.other.run(shell_command: Union[bytes, str, Sequence[Union[bytes, str, os.PathLike]]], **subprocess_run_kwargs) → int[source]

Run one or more commands in the local shell.

WARNING: Make sure that user input cannot get in any way as an argument to this function!