tkmilan.util#

Utility functions.

Module Attributes

TK_VERSION

Get the tk version as an integer tuple.

Functions

lcm_multiple(*numbers)

Least Common Multiple: Multiple number

lcm_single(a, b)

Least Common Multiple: Single Pair

Classes

CCounter([default])

A monotonic counter, to be used by calling the instance.

ReorderableDict([initialdata])

A mix of dict and list, a mapping that allows for reordering the key list, but still allows for fast random access to the values.

class tkmilan.util.CCounter(default: int = 0)#

Bases: object

A monotonic counter, to be used by calling the instance.

This is to be used like this:

cnt = Counter()
print('Current:', cnt())  # Current: 1
print('Current:', cnt())  # Current: 2
print('Current:', cnt())  # Current: 3
assert cnt.count == 3
class tkmilan.util.ReorderableDict(initialdata: Mapping[RDkT, RDvT] | None = None)#

Bases: UserDict, Generic[RDkT, RDvT]

A mix of dict and list, a mapping that allows for reordering the key list, but still allows for fast random access to the values.

Stores a regular dict, plus a separate copy of the keys as a list. These are synchronized.

Should be used as a regular dict, and there are special functions for manipulating the key order.

See index and at, insert and move.

at(index: int) RDkT#

Return the key at index on the ordered key list.

Supports all upstream functionality, such as negative indexes to count from the end of the list. Slices are technically supported, but frowned upon.

Parameters:

index (int) – Index to locate the key

Returns:

If the index is out of bounds, raises IndexError (just like the upstream function). Otherwise, return the key located in that index.

The following invariant holds:

self.index(self.at(X)) == X

Return type:

RDkT

See also

See the upstream function list.__getitem__

clear() None#

Remove all items from the mapping.

See also

See the upstream function dict.clear.

index(key: RDkT, *args: Any) int | None#

Return the first index of the key in the ordered key list.

Parameters:
  • key (RDkT) – The key to search for.

  • args (Any) – Passed to the upstream function

Returns:

If the key is not present in the mapping, return None. Otherwise, return the first index on the ordered key list.

Return type:

int | None

See also

See the upstream function list.index

insert(index: int, key: RDkT, value: RDvT) None#

Insert a key-value pair before the given index.

To append a key-value pair, use self[key] = value. To move an existing key, use move.

Parameters:
  • index (int) – Index to insert the given key-value pair.

  • key (RDkT) – The key to insert

  • value (RDvT) – The value to insert

See also

See the upstream function list.insert.

items() Iterator[Tuple[RDkT, RDvT]]#

Return an iterator of key-value tuples, using the ordered key list.

See also

See the documentation on the upstream function dict.items.

Note

The return value is technically different from the upstream function, but it should not matter in practice.

keys() Iterable[RDkT]#

Return an iterator of keys, using the defined order.

See also

See the documentation on the upstream function dict.keys.

Note

The return value is technically different from the upstream function, but it should not matter in practice.

move(index: int, key: RDkT) None#

Move an existing key before the given index.

To append a key-value pair, use self[key] = value. To insert a new key-value pair, use insert.

Parameters:
  • index (int) – Index to move the existing key.

  • key (RDkT) – The key to move

tkmilan.util.lcm_multiple(*numbers)#

Least Common Multiple: Multiple number

Note

Python 3.9 has math.lcm.

tkmilan.util.lcm_single(a, b)#

Least Common Multiple: Single Pair

tkmilan.util.TK_VERSION = (8, 6)#

Get the tk version as an integer tuple.

Similar to sys.version_info.