finesse.script.util module

Kat script utility functions.

finesse.script.util.duplicates(items, key=None)[source]

Get duplicate keys and values in the 1D sequence items.

Parameters

itemssequence

The sequence to find duplicates in.

keycallable, optional

The key function to use for comparisons. If not specified, defaults to the identity function.

Returns

list

The duplicates in items. This is a sequence of tuples containing the result of the key function for each entry of items, where at least two such keys exist, and the original items that matched that key.

Examples

>>> [k for k, _ in duplicates("AAAABBCDAAB")]
["A", "B", "C", "D"]
>>> [list(g) for _, g in duplicates("AAAABBCDAAB")]
[["A", "A", "A", "A", "A", "A"], ["B", "B", "B"], ["C"], ["D"]]
finesse.script.util.index_ranges(intlist)[source]

Create a sequence of range strings from a sequence of integers.

Based on https://stackoverflow.com/a/9471386/2251982.

Parameters

intlistsequence

Integers to convert to range strings.

Yields

str

The next range string.

Examples

>>> list(index_ranges([1, 2, 4, 5, 6, 7, 9]))
["1-2", "4-7", "9"]
finesse.script.util.merge_attributes(attr1, attr2)[source]

Perform a deep attribute dictionary merge.

This merges list and tuples in attr1 and attr2 into one dict. Values that are not lists or tuples result in an error. Items in attr1 appear before those in attr2 where they share the same key.

Parameters

attr1, attr2dict

The attribute dictionaries to merge.

Returns

dict

The merged attribute dictionary.

Raises

ValueError

If a dict value in attr1 or attr2 is not a list or tuple.

finesse.script.util.scriptsorted(items, reverse=False)[source]

Sort container items by their script position.

Parameters

itemssequence of TokenContainer

The container items to sort.

reversebool, optional

Request the result in descending instead of ascending order.

Returns

list of TokenContainer

Container items sorted by line number then column.