3. CString module¶
- class PythonExtensionsCollection.String.CString.CString[source]¶
Bases:
object
Contains 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
,None
in case ofsFolderName
is not found insidesStartPath
. In case of more than one parent folder is foundsDestPath
contains the first result andlistDestPaths
contains all results.listDestPaths (list)
If
sFolderName
contains a single folder name this list contains only one element that issDestPath
. In case ofFolderName
contains a semicolon separated list of several folder names this list contains all found paths of the given folder names.listDestPaths
isNone
(and not an empty list!) in case ofsFolderName
is not found insidesStartPath
.sDestFile (string)
Path and name of
sFileName
, in case ofsFileName
is given and found insidelistDestPaths
. In case of more than one file is foundsDestFile
contains the first result andlistDestFiles
contains all results.sDestFile
isNone
in case ofsFileName
isNone
and also in case ofsFileName
is not found insidelistDestPaths
(and therefore also in case ofsFolderName
is not found insidesStartPath
).listDestFiles (list)
Contains all positions of
sFileName
found insidelistDestPaths
.listDestFiles
isNone
(and not an empty list!) in case ofsFileName
isNone
and also in case ofsFileName
is not found insidelistDestPaths
(and therefore also in case ofsFolderName
is not found insidesStartPath
).sDestPathParent (string)
The parent folder of
sDestPath
,None
in case ofsFolderName
is not found insidesStartPath
(sDestPath
isNone
).
- static FormatResult(sMethod='', bSuccess=True, sResult='')[source]¶
Method:
FormatResult
Formats the result string
sResult
depending onbSuccess
:bSuccess
isTrue
indicates successbSuccess
isFalse
indicates an errorbSuccess
isNone
indicates 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
True
then returned path contains masked backslashes as separator, otherwise slashessReferencePathAbs (string, optional)
In case of
sPath
is 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
True
then the returned path is encapsulated in quotes - in case of the path contains blanksbExpandEnvVars (boolean; optional; default: True)
If
True
then in the returned path environment variables are resolved, otherwise not.bMask (boolean; optional; default: True; requires bWin=True)
If
bWin
isTrue
andbMask
isTrue
then the returned path contains masked backslashes as separator. IfbWin
isTrue
andbMask
isFalse
then the returned path contains single backslashes only - this might be required for applications, that are not able to handle masked backslashes. In case ofbWin
isFalse
bMask
has no effect.Returns:
sPath (string)
The normalized path (is
None
in case ofsPath
isNone
)
- 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
True
in case of all criteria are fulfilled, andFalse
in 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 isNone
this filter has no influence on the result.In case of all filters are
None
(default) the return value isTrue
(except the string itself isNone
or the string is empty andbSkipBlankStrings
isTrue
).In case of the string is
None
, the return value isFalse
, because nothing concrete can be done withNone
strings.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
bAck
of this method withTrue
.The final return value
bAck
of this method is a logical join (AND
) of all individual acknowledges (exceptbSkipBlankStrings
andsComment
; in case of their criteria are not fulfilled, immediatelyFalse
is 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
bSkipBlankStrings
isTrue
, the method immediately returnsFalse
and all other filters are ignored
sComment
In case of the input string starts with the string
sComment
, the method immediately returnsFalse
and all other filters are ignoredLeading blanks within the input string have no effect
The decision also depends on
bCaseSensitive
The 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
sStartsWith
Leading blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sEndsWith
Trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sStartsNotWith
Leading blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sEndsNotWith
Trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sContains
at any positionLeading and trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sContainsNot
at any positionLeading and trailing blanks within the input string have no effect
The decision also depends on
bCaseSensitive
More 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
sInclRegEx
matches the input stringLeading and trailing blanks within the input string are considered
bCaseSensitive
has 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
sExclRegEx
does not match the input stringLeading and trailing blanks within the input string are considered
bCaseSensitive
has 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’sTrue
in 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)