liara.util¶
Module Contents¶
Classes¶
Functions¶
For a list |
|
Add a suffix to a path. |
|
Given a number of words, estimate the time it would take to read them. |
|
Flatten a nested dictionary. This uses the separator to combine keys
together, so a dictionary access like |
|
Convert a plain string into a slug. |
|
Override “now” to allow previewing the page at a different point in time. |
|
Get the current date/time in the local time zone. |
|
Calculate a hash key for any kind of map, including chain maps. |
|
Back-compat implementation of Python’s 3.11 file_digest |
|
Recursively merge dictionary |
|
Calculate the thumbnail size from a given image size. |
API¶
- liara.util.pairwise(iterable)¶
For a list
s, return pairs for consecutive entries. For example, a lists0,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_suffixby adding a suffix without changing the extension, i.e. addingentofoo.bazwill producefoo.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_keysis 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, andignore_keysis{'a.b'}, thena.bwill not get flattened further, soa.bwill 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 worldto this function will returna-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 hastzinfoset to the local timezone.This can be overridden using
set_local_nowto 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_filespattern are ignored.The
dirnamespart 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
- liara.util.merge_dictionaries(a: dict, b: dict) dict¶
Recursively merge dictionary
bintoa.This looks a bit like a.update(b), but it actually will merge children, too. For example, if
a[foo]exists, andb[foo], the contents of both will be merged, instead of overwritinga[foo]withb[foo].
- class liara.util.CaseInsensitiveDictionary(d: Dict[str, Any])¶
Initialization
- get(key, default=None)¶
- keys()¶
- liara.util.get_thumbnail_size(image_size: tuple[int, int], thumbnail_size: Dict[str, int]) tuple[int, int] | None¶
Calculate the thumbnail size from a given image size.
- The thumbnail size must be a dictionary with either:
widthorheight(or both) set, which define the maximum thumbnail dimensionslongest_edgewhich defines the longest edge after scaling, maintaining the aspect ratio