4.18. foundations.strings

strings.py

Platform:
Windows, Linux, Mac Os X.
Description:
Defines various strings manipulation objects.

Others:

4.18.1. Module Attributes

foundations.strings.LOGGER
foundations.strings.ASCII_CHARACTERS
foundations.strings.toString

4.18.2. Functions

foundations.strings.getNiceName(name)[source]

Converts a string to nice string: currentLogText -> Current Log Text.

Usage:

>>> getNiceName("getMeANiceName")
u'Get Me A Nice Name'
>>> getNiceName("__getMeANiceName")
u'__Get Me A Nice Name'
Parameters:name (unicode) – Current string to be nicified.
Returns:Nicified string.
Return type:unicode
foundations.strings.getVersionRank(version)[source]

Converts a version string to it’s rank.

Usage:

>>> getVersionRank("4.2.8")
4002008000000
>>> getVersionRank("4.0")
4000000000000
>>> getVersionRank("4.2.8").__class__
<type 'int'>
Parameters:version (unicode) – Current version to calculate rank.
Returns:Rank.
Return type:int
foundations.strings.getSplitextBasename(path)[source]

Gets the basename of a path without its extension.

Usage:

>>> getSplitextBasename("/Users/JohnDoe/Documents/Test.txt")
u'Test'
Parameters:path (unicode) – Path to extract the basename without extension.
Returns:Splitext basename.
Return type:unicode
foundations.strings.getCommonAncestor(*args)[source]

Gets common ancestor of given iterables.

Usage:

>>> getCommonAncestor(("1", "2", "3"), ("1", "2", "0"), ("1", "2", "3", "4"))
(u'1', u'2')
>>> getCommonAncestor("azerty", "azetty", "azello")
u'aze'
Parameters:*args ([iterable]) – Iterables to retrieve common ancestor from.
Returns:Common ancestor.
Return type:iterable
foundations.strings.getCommonPathsAncestor(*args)[source]

Gets common paths ancestor of given paths.

Usage:

>>> getCommonPathsAncestor("/Users/JohnDoe/Documents", "/Users/JohnDoe/Documents/Test.txt")
u'/Users/JohnDoe/Documents'
Parameters:*args ([unicode]) – Paths to retrieve common ancestor from.
Returns:Common path ancestor.
Return type:unicode
foundations.strings.getWords(data)[source]

Extracts the words from given string.

Usage:

>>> getWords("Users are: John Doe, Jane Doe, Z6PO.")
[u'Users', u'are', u'John', u'Doe', u'Jane', u'Doe', u'Z6PO']
Parameters:data (unicode) – Data to extract words from.
Returns:Words.
Return type:list
foundations.strings.filterWords(words, filtersIn=None, filtersOut=None, flags=0)[source]

Filters the words using the given filters.

Usage:

>>> filterWords(["Users", "are", "John", "Doe", "Jane", "Doe", "Z6PO"], filtersIn=("John", "Doe"))
[u'John', u'Doe', u'Doe']
>>> filterWords(["Users", "are", "John", "Doe", "Jane", "Doe", "Z6PO"], filtersIn=("\w*r",))
[u'Users', u'are']
>>> filterWords(["Users", "are", "John", "Doe", "Jane", "Doe", "Z6PO"], filtersOut=("\w*o",))
[u'Users', u'are', u'Jane', u'Z6PO']
Parameters:
  • filtersIn (tuple or list) – Regex filters in list.
  • filtersIn – Regex filters out list.
  • flags (int) – Regex flags.
Returns:

Filtered words.

Return type:

list

foundations.strings.replace(string, data)[source]

Replaces the data occurrences in the string.

Usage:

>>> replace("Users are: John Doe, Jane Doe, Z6PO.", {"John" : "Luke", "Jane" : "Anakin", "Doe" : "Skywalker",
 "Z6PO" : "R2D2"})
u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.'
Parameters:
  • string (unicode) – String to manipulate.
  • data (dict) – Replacement occurrences.
Returns:

Manipulated string.

Return type:

unicode

foundations.strings.removeStrip(string, pattern)[source]

Removes the pattern occurrences in the string and strip the result.

Usage:

>>> removeStrip("John Doe", "John")
u'Doe'
Parameters:
  • string (unicode) – String to manipulate.
  • pattern (unicode) – Replacement pattern.
Returns:

Manipulated string.

Return type:

unicode

foundations.strings.toForwardSlashes(data)[source]

Converts backward slashes to forward slashes.

Usage:

>>> toForwardSlashes("To\Forward\Slashes")
u'To/Forward/Slashes'
Parameters:data (unicode) – Data to convert.
Returns:Converted path.
Return type:unicode
foundations.strings.toBackwardSlashes(data)[source]

Converts forward slashes to backward slashes.

Usage:

>>> toBackwardSlashes("/Users/JohnDoe/Documents")
u'\Users\JohnDoe\Documents'
Parameters:data (unicode) – Data to convert.
Returns:Converted path.
Return type:unicode
foundations.strings.toPosixPath(path)[source]

Converts Windows path to Posix path while stripping drives letters and network server slashes.

Usage:

>>> toPosixPath("c:\Users\JohnDoe\Documents")
u'/Users/JohnDoe/Documents'
Parameters:path (unicode) – Windows path.
Returns:Path converted to Posix path.
Return type:unicode
foundations.strings.getNormalizedPath(path)[source]

Normalizes a path, escaping slashes if needed on Windows.

Usage:

>>> getNormalizedPath("C:\Users/johnDoe\Documents")
u'C:\Users\JohnDoe\Documents'
Parameters:path (unicode) – Path to normalize.
Returns:Normalized path.
Return type:unicode
foundations.strings.getRandomSequence(length=8)[source]

Returns a random sequence.

Usage:

>>> getRandomSequence()
u'N_mYO7g5'
Parameters:length (int) – Length of the sequence.
Returns:Random sequence.
Return type:unicode
foundations.strings.isEmail(data)[source]

Check if given data string is an email.

Usage:

>>> isEmail("[email protected]")
True
>>> isEmail("john.doe:domain.com")
False
Parameters:data (unicode) – Data to check.
Returns:Is email.
Return type:bool
foundations.strings.isWebsite(url)[source]

Check if given url string is a website.

Usage:

>>> isWebsite("http://www.domain.com")
True
>>> isWebsite("domain.com")
False
Parameters:data (unicode) – Data to check.
Returns:Is website.
Return type:bool