4.16. foundations.strings

strings.py

Platform:
Windows, Linux, Mac Os X.
Description:
This module defines various strings manipulation objects.

Others:

4.16.1. Module Attributes

foundations.strings.LOGGER
foundations.strings.ASCII_CHARACTERS

4.16.2. Functions

foundations.strings.toString(data, encoding=u'utf-8', errors=u'ignore')[source]

This definition converts given data to unicode string using package default settings.

Usage:

>>> toString("myData")
u'myData'
>>> toString("汉字/漢字")
u'汉字/漢字'
Parameters:
  • data – Data to convert. ( Object )
  • encoding – File encoding codec. ( String )
  • errors – File encoding errors handling. ( String )
Returns:

Unicode data. ( String )

foundations.strings.getNiceName(name)[source]

This definition 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 – Current string to be nicified. ( String )
Returns:Nicified string. ( String )
foundations.strings.getVersionRank(version)[source]

This definition 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 – Current version to calculate rank. ( String )
Returns:Rank. ( Integer )
foundations.strings.getSplitextBasename(path)[source]

This definition gets the basename of a path without its extension.

Usage:

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

This definition 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 – Iterables to retrieve common ancestor from. ( Iterables )
Returns:Common ancestor. ( Iterable )
foundations.strings.getCommonPathsAncestor(*args)[source]

This definition gets common paths ancestor of given paths.

Usage:

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

This method 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 – Data to extract words from. ( String )
Returns:Words. ( List )
foundations.strings.filterWords(words, filtersIn=None, filtersOut=None, flags=0)[source]

This method 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 – Regex filters in list. ( Tuple / List )
  • filtersIn – Regex filters out list. ( Tuple / List )
  • flags – Regex flags. ( Integer )
Returns:

Filtered words. ( List )

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

This definition replaces the data occurences 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 – String to manipulate. ( String )
  • data – Replacement occurences. ( Dictionary )
Returns:

Manipulated string. ( String )

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

This definition removes the pattern occurences in the string and strip the result.

Usage:

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

Manipulated string. ( String )

foundations.strings.toForwardSlashes(data)[source]

This definition converts backward slashes to forward slashes.

Usage:

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

This definition converts forward slashes to backward slashes.

Usage:

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

This definition 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 – Windows path. ( String )
Returns:Path converted to Posix path. ( String )
foundations.strings.getNormalizedPath(path)[source]

This definition normalizes a path, escaping slashes if needed on Windows.

Usage:

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

This definition returns a random sequence.

Usage:

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

This definition check if given data string is an email.

Usage:

>>> isEmail("john.doe@domain.com")
True
>>> isEmail("john.doe:domain.com")
False
Parameters:data – Data to check. ( String )
Returns:Is email. ( Boolean )
foundations.strings.isWebsite(data)[source]

This definition check if given data string is a website.

Usage:

>>> isWebsite("http://www.domain.com")
True
>>> isWebsite("domain.com")
False
Parameters:data – Data to check. ( String )
Returns:Is website. ( Boolean )

Table Of Contents

Previous topic

4.15. foundations.rotatingBackup

Next topic

4.17. foundations.tcpServer

This Page