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 |
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_suffix
by adding a suffix without changing the extension, i.e. addingen
tofoo.baz
will 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_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, andignore_keys
is{'a.b'}
, thena.b
will not get flattened further, soa.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 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 hastzinfo
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