polymerist.genutils.textual.substrings

For identifying and concatenating substrings of other strings with unique properties

Functions

unique_string(→ str)

Accepts a string and returns another string containing

shortest_repeating_substring(→ str)

Return the shortest substring such that the passed string can be written as some number of repeats (including 1) of the substring

repeat_string_to_length(→ str)

Takes a string and repeats it cyclically to produce another string of a given length

Module Contents

polymerist.genutils.textual.substrings.unique_string(string: str, preserve_order: bool = True) str[source]

Accepts a string and returns another string containing only the UNIQUE characters in the origin string

Can specify whether order is important with the “preserve_order” keyword

Parameters:
  • string (str) – An arbitrary string on wants the unique characters from

  • preserve_order (bool, default True) – Whether or not to keep the unique characters in the order they are found For example:

    unique_string(“balaclava”, preserve_order=False) -> “bcavl” unique_string(“balaclava”, preserve_order=True) -> “balcv”

Returns:

uniquified_str – Another string containing only the unique characters in “string” Order depends on the value of the “preserve_order” parameter

Return type:

str

polymerist.genutils.textual.substrings.shortest_repeating_substring(string: str) str[source]

Return the shortest substring such that the passed string can be written as some number of repeats (including 1) of the substring Will return the original string if no simpler decomposition exists

polymerist.genutils.textual.substrings.repeat_string_to_length(string: str, target_length: int, joiner: str = '') str[source]

Takes a string and repeats it cyclically to produce another string of a given length The number of times the original string occurs in the new string may be fractional for example: >> repeat_string_to_length(“CAT”, 6) -> “CATCAT” >> repeat_string_to_length(“BACA”, 10) -> “BACABACABA”

Parameters:
  • string (str) – An arbitrary string to repeat

  • target_length (int) – The length of the final desired string This does NOT have to be an integer multiple of the length of “string”

    E.g. repeat_string_to_length(“BACA”, 10) -> “BACABACABA”

    Nor does it have to be greater than the length of “string”

    E.g. repeat_string_to_length(“BACA”, 3) -> “BAC”

Returns:

rep_string – A new string which has the desired target length and consists of cycles of the initial string

Return type:

str