3. CString module¶
- class PythonExtensionsCollection.String.CString.CString[source]¶
Bases:
objectContains some string computation methods like e.g. normalizing a path.
- static DetectParentPath(sStartPath=None, sFolderName=None, sFileName=None)[source]¶
Method:
DetectParentPath
Computes the path to any parent folder inside a given path. Optionally DetectParentPath is able to search for files inside the parent folder.
Args:
sStartPath (string)
The path in which to search for a parent folder
sFolderName (string)
The name of the folder to search for within
sStartPath. It is possible to provide more than one folder name separated by semicolonsFileName (string, optional)
The name of a file to search within the detected parent folder
Returns:
sDestPath (string)
Path and name of parent folder found inside
sStartPath,Nonein case ofsFolderNameis not found insidesStartPath. In case of more than one parent folder is foundsDestPathcontains the first result andlistDestPathscontains all results.listDestPaths (list)
If
sFolderNamecontains a single folder name this list contains only one element that issDestPath. In case ofFolderNamecontains a semicolon separated list of several folder names this list contains all found paths of the given folder names.listDestPathsisNone(and not an empty list!) in case ofsFolderNameis not found insidesStartPath.sDestFile (string)
Path and name of
sFileName, in case ofsFileNameis given and found insidelistDestPaths. In case of more than one file is foundsDestFilecontains the first result andlistDestFilescontains all results.sDestFileisNonein case ofsFileNameisNoneand also in case ofsFileNameis not found insidelistDestPaths(and therefore also in case ofsFolderNameis not found insidesStartPath).listDestFiles (list)
Contains all positions of
sFileNamefound insidelistDestPaths.listDestFilesisNone(and not an empty list!) in case ofsFileNameisNoneand also in case ofsFileNameis not found insidelistDestPaths(and therefore also in case ofsFolderNameis not found insidesStartPath).sDestPathParent (string)
The parent folder of
sDestPath,Nonein case ofsFolderNameis not found insidesStartPath(sDestPathisNone).
- static FormatResult(sMethod='', bSuccess=True, sResult='')[source]¶
Method:
FormatResult
Formats the result string
sResultdepending onbSuccess:bSuccessisTrueindicates successbSuccessisFalseindicates an errorbSuccessisNoneindicates an exception
Additionally the name of the method that causes the result, can be provided (optional). This is useful for debugging.
Returns the formatted result string.
- static NormalizePath(sPath=None, bWin=False, sReferencePathAbs=None, bConsiderBlanks=False, bExpandEnvVars=True, bMask=True)[source]¶
Method:
NormalizePath
Normalizes local paths, paths to local network resources and internet addresses
Args:
sPath (string)
The path to be normalized
bWin (boolean; optional; default: False)
If
Truethen returned path contains masked backslashes as separator, otherwise slashessReferencePathAbs (string, optional)
In case of
sPathis relative andsReferencePathAbs(expected to be absolute) is given, then the returned absolute path is a join of both input pathsbConsiderBlanks (boolean; optional; default: False)
If
Truethen the returned path is encapsulated in quotes - in case of the path contains blanksbExpandEnvVars (boolean; optional; default: True)
If
Truethen in the returned path environment variables are resolved, otherwise not.bMask (boolean; optional; default: True; requires bWin=True)
If
bWinisTrueandbMaskisTruethen the returned path contains masked backslashes as separator. IfbWinisTrueandbMaskisFalsethen the returned path contains single backslashes only - this might be required for applications, that are not able to handle masked backslashes. In case ofbWinisFalsebMaskhas no effect.Returns:
sPath (string)
The normalized path (is
Nonein case ofsPathisNone)
- static StringFilter(sString=None, bCaseSensitive=True, bSkipBlankStrings=True, sComment=None, sStartsWith=None, sEndsWith=None, sStartsNotWith=None, sEndsNotWith=None, sContains=None, sContainsNot=None, sInclRegEx=None, sExclRegEx=None, bDebug=False)[source]¶
During the computation of strings there might occur the need to get to know if this string fulfils certain criteria or not. Such a criterion can e.g. be that the string contains a certain substring. Also an inverse logic might be required: In this case the criterion is that the string does not contain this substring.
It might also be required to combine several criteria to a final conclusion if in total the criterion for a string is fulfilled or not. For example: The string must start with the string prefix and must also contain either the string substring1 or the string substring2 but must also not end with the string suffix.
This method provides a bunch of predefined filters that can be used singly or combined to come to a final conclusion if the string fulfils all criteria or not.
The filters are divided into three different types:
Filters that are interpreted as raw strings (called ‘standard filters’; no wild cards supported)
Filters that are interpreted as regular expressions (called ‘regular expression based filters’; the syntax of regular expressions has to be considered)
Boolean switches (e.g. indicating if also an empty string is accepted or not)
The input string might contain leading and trailing blanks and tabs. This kind of horizontal space is removed from the input string before the standard filters start their work (except the regular expression based filters).
The regular expression based filters consider the original input string (including the leading and trailing space).
The outcome is that in case of the leading and trailing space shall be part of the criterion, the regular expression based filters can be used only.
It is possible to decide if the standard filters shall work case sensitive or not. This decision has no effect on the regular expression based filters.
The regular expression based filters always work with the original input string that is not modified in any way.
Except the regular expression based filters it is possible to provide more than one string for every standard filter (must be a semikolon separated list in this case). A semicolon that shall be part of the search string, has to be masked in this way:
\;.This method returns a boolean value that is
Truein case of all criteria are fulfilled, andFalsein case of some or all of them are not fulfilled.The default value for all filters is
None(exceptbSkipBlankStrings). In case of a filter value isNonethis filter has no influence on the result.In case of all filters are
None(default) the return value isTrue(except the string itself isNoneor the string is empty andbSkipBlankStringsisTrue).In case of the string is
None, the return value isFalse, because nothing concrete can be done withNonestrings.Internally every filter has his own individual acknowledge that indicates if the criterion of this filter is fulfilled or not.
The meaning of criterion fulfilled of a filter is that the filter supports the final return value
bAckof this method withTrue.The final return value
bAckof this method is a logical join (AND) of all individual acknowledges (exceptbSkipBlankStringsandsComment; in case of their criteria are not fulfilled, immediatelyFalseis returned).Summarized:
Filters are used to define criteria
The return value of this method provides the conclusion - indicating if all criteria are fulfilled or not
The following filters are available:
bSkipBlankStrings
Like already mentioned above leading and trailing spaces are removed from the input string at the beginning
In case of the result is an empty string and
bSkipBlankStringsisTrue, the method immediately returnsFalseand all other filters are ignored
sComment
In case of the input string starts with the string
sComment, the method immediately returnsFalseand all other filters are ignoredLeading blanks within the input string have no effect
The decision also depends on
bCaseSensitiveThe idea behind this decision is: Ignore a string that is commented out
sStartsWith
The criterion of this filter is fulfilled in case of the input string starts with the string
sStartsWithLeading blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
OR)
sEndsWith
The criterion of this filter is fulfilled in case of the input string ends with the string
sEndsWithTrailing blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
OR)
sStartsNotWith
The criterion of this filter is fulfilled in case of the input string does not start with the string
sStartsNotWithLeading blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
AND)
sEndsNotWith
The criterion of this filter is fulfilled in case of the input string does not end with the string
sEndsNotWithTrailing blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
AND)
sContains
The criterion of this filter is fulfilled in case of the input string contains the string
sContainsat any positionLeading and trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
OR)
sContainsNot
The criterion of this filter is fulfilled in case of the input string does not contain the string
sContainsNotat any positionLeading and trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitiveMore than one string can be provided (semicolon separated; logical join:
AND)
sInclRegEx
Include filter based on regular expressions (consider the syntax of regular expressions!)
The criterion of this filter is fulfilled in case of the regular expression
sInclRegExmatches the input stringLeading and trailing blanks within the input string are considered
bCaseSensitivehas no effectA semicolon separated list of several regular expressions is not supported
sExclRegEx
Exclude filter based on regular expressions (consider the syntax of regular expressions!)
The criterion of this filter is fulfilled in case of the regular expression
sExclRegExdoes not match the input stringLeading and trailing blanks within the input string are considered
bCaseSensitivehas no effectA semicolon separated list of several regular expressions is not supported
Further parameter:
sString
The input string that has to be investigated.
bCaseSensitive (boolean, optional, default:
True)If
True, the standard filters work case sensitive, otherwise not.bDebug (boolean, optional, default:
False)If
True, additional output is printed to console (e.g. the decision of every single filter), otherwise not.Examples:
Returns
True:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = "Sp", sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = "beats", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
Returns
False:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = "Sp", sEndsWith = None, sStartsNotWith = None, sEndsNotWith = "minute", sContains = "beats", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
Returns
True:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = None, sContainsNot = "Beats", sInclRegEx = None, sExclRegEx = None)
Returns
True:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = None, sContainsNot = None, sInclRegEx = r"\d{2}", sExclRegEx = None)
Returns
False:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = "Speed", sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = None, sContainsNot = None, sInclRegEx = r"\d{3}", sExclRegEx = None)
Returns
False:bAck = CString.StringFilter(sString = "Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = "Speed", sEndsWith = "minute", sStartsNotWith = "speed", sEndsNotWith = None, sContains = "beats", sContainsNot = None, sInclRegEx = r"\d{2}", sExclRegEx = r"\d{2}")
Returns
False:bAck = CString.StringFilter(sString = " ", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = None, sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
Returns
False:bAck = CString.StringFilter(sString = "# Speed is 25 beats per minute", bCaseSensitive = True, bSkipBlankStrings = True, sComment = "#", sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = "beats", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
Returns
False:bAck = CString.StringFilter(sString = " Alpha is not beta; and beta is not gamma ", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = " Alpha ", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
Because blanks around search strings (here
" Alpha ") are considered, whereas the blanks around the input string are removed before computation. Therefore" Alpha "cannot be found within the (shortened) input string.This alternative solution returns
True:bAck = CString.StringFilter(sString = " Alpha is not beta; and beta is not gamma ", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = None, sContainsNot = None, sInclRegEx = r"\s{3}Alpha", sExclRegEx = None)
Returns
True:bAck = CString.StringFilter(sString = "Alpha is not beta; and beta is not gamma", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = "beta; and", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)
The meaning of
"beta; and"is: The criterion is fulfilled in case of either"beta"or" and"can be found. That’sTruein this example - but this has nothing to do with the fact, that also this string"beta; and"can be found. A semicolon that shall be part of the search, has to be masked!The meaning of
"beta\; not"in the following example is: The criterion is fulfilled in case of"beta; not"can be found.That’s not
True. Therefore the method returnsFalse:bAck = CString.StringFilter(sString = "Alpha is not beta; and beta is not gamma", bCaseSensitive = True, bSkipBlankStrings = True, sComment = None, sStartsWith = None, sEndsWith = None, sStartsNotWith = None, sEndsNotWith = None, sContains = r"beta\; not", sContainsNot = None, sInclRegEx = None, sExclRegEx = None)