liara.util

Module Contents

Classes

Functions

pairwise

For a list s, return pairs for consecutive entries. For example, a list s0, s1, etc. will produce (s0,s1), (s1,s2), ... and so on.

add_suffix

Add a suffix to a path.

readtime

Given a number of words, estimate the time it would take to read them.

flatten_dictionary

Flatten a nested dictionary. This uses the separator to combine keys together, so a dictionary access like ['a']['b'] with a separator '.' turns into 'a.b'.

create_slug

Convert a plain string into a slug.

set_local_now

Override “now” to allow previewing the page at a different point in time.

local_now

Get the current date/time in the local time zone.

get_hash_key_for_map

Calculate a hash key for any kind of map, including chain maps.

file_digest

Back-compat implementation of Python’s 3.11 file_digest

API

liara.util.pairwise(iterable)

For a list s, return pairs for consecutive entries. For example, a list s0, s1, etc. will produce (s0,s1), (s1,s2), ... and so on.

See: https://docs.python.org/3/library/itertools.html#recipes.

liara.util.add_suffix(path: pathlib.PurePosixPath, suffix)

Add a suffix to a path.

This differs from with_suffix by adding a suffix without changing the extension, i.e. adding en to foo.baz will produce foo.en.baz.

liara.util.readtime(wordcount: int, words_per_minute=300)

Given a number of words, estimate the time it would take to read them.

Returns:

The time in minutes if it’s more than 1, otherwise 1.

liara.util.flatten_dictionary(d, sep='.', parent_key=None, *, ignore_keys: Optional[set] = None)

Flatten a nested dictionary. This uses the separator to combine keys together, so a dictionary access like ['a']['b'] with a separator '.' turns into 'a.b'.

If ignore_keys is set, it must be a list of fully flattened key names at which the flattening should stop. For instance, if a dictionary {'a': {'b': {'c': 1}}} is provided, and ignore_keys is {'a.b'}, then a.b will not get flattened further, so a.b will contain a dictionary with {'c': 1}.

liara.util.create_slug(s: str) str

Convert a plain string into a slug.

A slug is suitable for use as a URL. For instance, passing A new world to this function will return a-new-world.

liara.util.set_local_now(dt: datetime.datetime)

Override “now” to allow previewing the page at a different point in time.

liara.util.local_now() datetime.datetime

Get the current date/time in the local time zone.

This is equivalent to datetime.datetime.now(), except it returns a timestamp which has tzinfo set to the local timezone.

This can be overridden using set_local_now to build the page at a different point in time.

class liara.util.FilesystemWalker(ignore_files: Optional[List[str]] = None)

Initialization

walk(path: pathlib.Path)

Walk a directory recursively.

This is quite similar to os.walk, but with two major differences:

  • Files matching the ignore_files pattern are ignored.

  • The dirnames part of the tuple is omitted

liara.util.get_hash_key_for_map(m: collections.abc.Mapping) bytes

Calculate a hash key for any kind of map, including chain maps.

This method is fairly slow. Use with caution.

liara.util.file_digest(fp) bytes

Back-compat implementation of Python’s 3.11 file_digest