mcfonts.strings

Utilities for strings.

Module Contents

divide_strings(strings: collections.abc.Iterable[str], width: int = 16) collections.abc.Generator[str]

Divide a list of strings into a list of strings with equal widths.

>>> list(divide_strings(["thisisareallylongcharlist"]))
['thisisareallylon', 'gcharlist']
>>> list(divide_strings(["thisisareallylongcharlist"], 5))
['thisi', 'sarea', 'llylo', 'ngcha', 'rlist']
Parameters:
strings : collections.abc.Iterable[str]

The string list.

width : int

The width to divide each string on.

Returns:

Generator that yields strings.

Return type:

collections.abc.Generator[str]

is_string_padding(string: str, /) bool

Return if string contains only padding characters.

>>> is_string_padding("\0\0\0\x20\x20\x20\0\0")
True
>>> is_string_padding("         ")
True
>>> is_string_padding("      xxx")
False
Parameters:
string : str

The string.

Returns:

If all padding characters.

Return type:

bool

pad_strings(strings: collections.abc.Sequence[str], pad_amount: int = 0, padding_character: str = ' ', pad_from: 'left' | 'right' = 'right') collections.abc.Generator[str]

Pad so that all strings have the same length.

>>> list(pad_strings(["abc", "de"]))
['abc', 'de ']
>>> list(pad_strings(["abc", "defg"]))
['abc ', 'defg']
Parameters:
strings : collections.abc.Sequence[str]

String list to pad.

pad_amount : int

What to pad the characters to; the ideal width. If this is 0, this will be determined from the largest length of strings in strings.

padding_character : str

The character to pad with, default is a space.

pad_from : Literal['left', 'right']

Direction to pad the string from, left => "___Text", right => "Text___".

Returns:

The padded charlist list.

Return type:

collections.abc.Generator[str]

string_lengths_equal(strings: collections.abc.Sequence[str], /) tuple[bool, int]

Ensure that every string inside strings has the same length as the first string.

>>> string_lengths_equal(["abc", "de"])
(False, 3)
>>> string_lengths_equal(["abc", "def"])
(True, 3)
>>> string_lengths_equal([])
(True, 0)
>>> string_lengths_equal([""])
(True, 0)
Parameters:
strings : collections.abc.Sequence[str]

The list of strings to check lengths of.

Returns:

A tuple of (if lengths are equal, the expected length).

Return type:

tuple[bool, int]

string_to_chars(string_list: collections.abc.Iterable[str], /) collections.abc.Generator[str]

Yield all the characters that are in the string list.

>>> chars = ['agtg', 'b', '5', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8']
>>> list(string_to_chars(chars))
['a', 'g', 't', 'g', 'b', '5', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8']
Parameters:
string_list : collections.abc.Iterable[str]

The charlist.

Returns:

A yield of characters.

Return type:

collections.abc.Generator[str]

unicode_range_table(start: str, end: str, width: int = 16) list[str]

Return a character list that contains characters from start to end in order.

Given a starting character start, and an ending character end, return a charlist that contains these characters in order, and whose width is equal to width.

>>> unicode_range_table("\u2600", "\u26ff")
[
    '☀☁☂☃☄★☆☇☈☉☊☋☌☍☎☏',
    '☐☑☒☓☔☕☖☗☘☙☚☛☜☝☞☟',
    '☠☡☢☣☤☥☦☧☨☩☪☫☬☭☮☯',
    '☰☱☲☳☴☵☶☷☸☹☺☻☼☽☾☿',
    '♀♁♂♃♄♅♆♇♈♉♊♋♌♍♎♏',
    '♐♑♒♓♔♕♖♗♘♙♚♛♜♝♞♟',
    '♠♡♢♣♤♥♦♧♨♩♪♫♬♭♮♯',
    '♰♱♲♳♴♵♶♷♸♹♺♻♼♽♾♿',
    '⚀⚁⚂⚃⚄⚅⚆⚇⚈⚉⚊⚋⚌⚍⚎⚏',
    '⚐⚑⚒⚓⚔⚕⚖⚗⚘⚙⚚⚛⚜⚝⚞⚟',
    '⚠⚡⚢⚣⚤⚥⚦⚧⚨⚩⚪⚫⚬⚭⚮⚯',
    '⚰⚱⚲⚳⚴⚵⚶⚷⚸⚹⚺⚻⚼⚽⚾⚿',
    '⛀⛁⛂⛃⛄⛅⛆⛇⛈⛉⛊⛋⛌⛍⛎⛏',
    '⛐⛑⛒⛓⛔⛕⛖⛗⛘⛙⛚⛛⛜⛝⛞⛟',
    '⛠⛡⛢⛣⛤⛥⛦⛧⛨⛩⛪⛫⛬⛭⛮⛯',
    '⛰⛱⛲⛳⛴⛵⛶⛷⛸⛹⛺⛻⛼⛽⛾⛿'
]
>>> unicode_range_table(" ", "\xff", 8)
[
    ' !"#$%&'',
    '()*+,-./',
    '01234567',
    '89:;<=>?',
    '@ABCDEFG',
    'HIJKLMNO',
    'PQRSTUVW',
    'XYZ[\\]^_',
    '`abcdefg',
    'hijklmno',
    'pqrstuvw',
    'xyz{|}~\x7f',
    '\x80\x81\x82\x83\x84\x85\x86\x87',
    '\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f',
    '\x90\x91\x92\x93\x94\x95\x96\x97',
    '\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f',
    '\xa0¡¢£¤¥¦§',
    '¨©ª«¬\xad®¯',
    '°±²³´µ¶·',
    '¸¹º»¼½¾¿',
    'ÀÁÂÃÄÅÆÇ',
    'ÈÉÊËÌÍÎÏ',
    'ÐÑÒÓÔÕÖ×',
    'ØÙÚÛÜÝÞß',
    'àáâãäåæç',
    'èéêëìíîï',
    'ðñòóôõö÷',
    'øùúûüýþÿ'
]
Parameters:
start : str

The starting single character.

end : str

The ending single character. Codepoint must be greater than the codepoint of start.

width : int

The number of characters to put in one row of the returned charlist.

Returns:

A charlist, with each string's length equal to width.

Return type:

list[str]

PADDING_CHARS : Final[set[str]]