String¶
String manipulation operations.
- disseminate.utils.string.convert_camelcase(string, sep='_')¶
Convert a camel-case string to a string to a lower-case string with a seperator character.
- Parameters
- stringstr
The camel-case string to convert
- sepOptional[str]
The separator character.
- Returns
- processed_stringstr
The converted string
Examples
>>> convert_camelcase('ProcessContext') 'process_context' >>> convert_camelcase('processcontext') 'processcontext' >>> convert_camelcase('ProcessContext', '.') 'process.context'
- disseminate.utils.string.find_basestring(strings)¶
Evaluate the common base string amongst a list of strings.
- Parameters
- stringsList[str]
A list of strings
- Returns
- base_stringstr
The common base string.
Examples
>>> find_basestring(['tester', 'test', 'testest']) 'test' >>> find_basestring(['tester', 'test', 'testest', 'smile']) ''
- disseminate.utils.string.group_strings(lst)¶
Group adjacent strings in a list and remove empty strings.
- Parameters
- lstUnion[list, str or
Tag
] An AST comprising a strings, tags or lists of both.
- lstUnion[list, str or
- Returns
- processed_list
The list with the strings grouped and cleaned up
Examples
>>> group_strings(lst=['a', 'b', '', 3, '', 4, 5, 'f']) ['ab', 3, 4, 5, 'f'] >>> group_strings(lst=['a', 'b', 'c', 3, 'd', 'e', 4, 5, 'f']) ['abc', 3, 'de', 4, 5, 'f']
- class disseminate.utils.string.groupby(iterable, key=None) make an iterator that returns consecutive ¶
Bases:
object
keys and groups from the iterable. If the key function is not specified or is None, the element itself is used for grouping.
- disseminate.utils.string.hashtxt(text, truncate=10)¶
Creates a hash from the given text.
- disseminate.utils.string.nicejoin(*items, sep=', ', term=' and ')¶
Join a sequence of strings with a separator and an alternative terminal separator.
- Parameters
- *itemsTuple[str]
A sequence of strings to join.
- sepOptional[str]
The separator to use for all items, other than the last item.
- termOptional[str]
The last separator to use in joining items.
- Returns
- joined_stringstr
The joined string.
Examples
>>> nicejoin('Bill', 'Ted', 'Frances') 'Bill, Ted and Frances' >>> nicejoin('Bill', 'Ted', 'Frances', term=' or ') 'Bill, Ted or Frances' >>> nicejoin('Bill', 'Ted', 'Frances') 'Bill, Ted and Frances' >>> nicejoin('Bill', 'Ted') 'Bill and Ted' >>> nicejoin('Bill') 'Bill'
- disseminate.utils.string.replace_macros(s, *dicts)¶
Replace the macros and return a processed string.
Macros are simple string replacements from context entries whose keys start with the tag_prefix character (e.g. ‘@test’). The process_context_asts will ignore macro context entries that have keys which start with this prefix. This is to preserve macros as strings.
- Parameters
- sstr
The input string to replace macros within.
- *dictsTuple[dict]
One or more dicts containing variables defined for a specific document. Values will be replaced with the first dict found with that value.
- Returns
- processed_stringstr
A string with the macros replaced.
- Raises
- MacroNotFound
Raises a MacroNotFound exception if a macro was included, but it could not be found.
- disseminate.utils.string.slugify(text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-', save_order=False, stopwords=(), regex_pattern=None, lowercase=True, replacements=())¶
Make a slug from the given text. :param text (str): initial text :param entities (bool): converts html entities to unicode :param decimal (bool): converts html decimal to unicode :param hexadecimal (bool): converts html hexadecimal to unicode :param max_length (int): output string length :param word_boundary (bool): truncates to complete word even if length ends up shorter than max_length :param save_order (bool): if parameter is True and max_length > 0 return whole words in the initial order :param separator (str): separator between words :param stopwords (iterable): words to discount :param regex_pattern (str): regex pattern for allowed characters :param lowercase (bool): activate case sensitivity by setting it to False :param replacements (iterable): list of replacement rules e.g. [[‘|’, ‘or’], [‘%’, ‘percent’]] :return (str):
- disseminate.utils.string.space_indent(s, number=4)¶
Indent a text block by the specified number of spaces.
- Parameters
- sstr
The string the indent
- numberOptional[int]
The number of spaces to indent by.
- Returns
- indented_stringstr
The indented string.
Examples
>>> space_indent('my test') ' my test' >>> t=space_indent("my block\n of text\nwith indents.") >>> print(t) my block of text with indents.
- disseminate.utils.string.str_to_dict(string, strip_quotes=True)¶
Parse a string into a dict.
- Parameters
- stringstr
The string with dict-like entries separated by colons.
- strip_quotesOptional[bool]
If True, matched quotes (‘”) are stripped at the ends of value strings.
- Returns
- parsed_dictdict
The parsed dict with keys and values as strings.
Examples
>>> string = ''' ... first: one ... second: ... multiline ... entry ... third: 3 ... ''' >>> d = str_to_dict(string) >>> d == {'first': 'one', 'second': ' multiline\n entry', 'third': '3'} True
- disseminate.utils.string.str_to_list(string)¶
Parse a string into a list.
- Parameters
- stringstr
The string with list entries separated by semicolons, newlines or commas.
- Returns
- parsed_listlist
The parsed list with the string split into string pieces.
Examples
>>> str_to_list('one, two, three') ['one', 'two', 'three'] >>> str_to_list('one, 1; two, 2; three, 3') ['one, 1', 'two, 2', 'three, 3'] >>> str_to_list(''' ... one, 1 ... two, 2 ... three, 3''') ['one, 1', 'two, 2', 'three, 3'] >>> str_to_list("friends") ['friends']
- disseminate.utils.string.strip_end_quotes(s)¶
Strip matched quotes from the ends a string.
- Parameters
- sstr
String to strip matched quotes.
- Returns
- stripped_stringstr
The same string with matched quotes striped.
Examples
>>> strip_end_quotes('This is my "test" string.') 'This is my "test" string.' >>> strip_end_quotes('"This is my test string"') 'This is my test string' >>> strip_end_quotes('"This is my test string "') 'This is my test string '
- disseminate.utils.string.strip_multi_newlines(s)¶
Strip multiple consecutive newlines in a string.
- Parameters
- sstr
String to strip multiple consecutive newlines from.
- Returns
- stripped_stringstr
The same string with multiple consecutive newlines replaced to single newlines.
Examples
>>> strip_multi_newlines("This is my\nfirst string.") 'This is my\nfirst string.' >>> strip_multi_newlines("This is my\n\nsecond string.") 'This is my\nsecond string.'
- disseminate.utils.string.stub(s)¶
Takes a string, like a docstring, and returns only the first line.
- Parameters
- sstr
The string to generate a stub for.
- Returns
- stubstr
The string with its first line stripped.
Examples
>>> stub(stub.__doc__) 'Takes a string, like a docstring, and returns only the first line.'
- disseminate.utils.string.titlelize(string, truncate=True, capitalize=False)¶
Given a string, generate a condensed title.
>>> titlelize("My example caption. It has 2 sentences") 'My example caption' >>> titlelize("My example caption. It has 2 sentences", capitalize=True) 'My Example Caption'