9.1. A few additional String operations
Before starting, let’s assume we already imported nettoolkit as below. it will be used than after for each function.
>>> from nettoolkit.nettoolkit_common import STR
9.1.1. string functions:
There are many string methods available under STR
class in nettoolkit
.
- Such available functions are:
found(), foundPos()
find_within(), string_within(), suffix_index_within()
find_all(), find_any(), find_multi()
update()
replace_dual_and_split(), finddualnreplacesingle()
indention()
is_blank_line()
is_hostname_line(), hostname(), hostname_from_cli()
shrink_if(), if_prefix(), if_suffix()
if_standardize()
string_concate()
right(), mid()
delete_trailing_remarks()
to_list(), to_set()
prepend_bgp_as()
9.1.1.1. found()
Checks sub-string withing a string and provides True/False based on finding.
This is python native str.find() alternative, to use in conditions. b/c native function returns -1 if not found, which becomes True in codition check if not matched exclusively with -1.
>>> STR.found("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "A") True
9.1.1.2. foundPos()
Checks sub-string withing a string and returns position index based on finding.
This is exactly as native str.find()
>>> STR.foundPos("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "A") 31
9.1.1.3. find_within()
Finds characters between prefix and suffix substrings from string
returns a tuple with ( match string, index )
>>> STR.find_within("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "BROWN", "OVER") (' FOX JUMP ', 25)
9.1.1.4. string_within()
Finds characters between prefix and suffix substrings from string
returns only match string
>>> STR.string_within("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "BROWN", "OVER") ' FOX JUMP '
9.1.1.5. suffix_index_within()
Finds characters between prefix and suffix substrings from string
returns only match string start index
>>> STR.suffix_index_within("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "BROWN", "OVER") 25
9.1.1.6. find_all()
Search for multiple substrings (list, tuple, set) within string.
all sub-strings should be found in order to return True.
- additional arguments that can be added are:
start = Optional: integer - position/index to start search from
count = Optional: integer - count of characters to seach from start index
>>> f1 = ("LAZY", "BROWN", "QUICK") >>> f2 = ("LAZY", "RED", "QUICK") >>> STR.find_all("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f1) True >>> STR.find_all("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f2) False
9.1.1.7. find_any()
Search for multiple substrings (list, tuple, set) within string.
any sub-strings should be found in order to return True.
- additional arguments that can be added are:
start = Optional: integer - position/index to start search from
count = Optional: integer - count of characters to seach from start index
>>> f1 = ("LAZY", "BROWN", "QUICK") >>> f2 = ("LAZY", "RED", "QUICK") >>> STR.find_any("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f1) True >>> STR.find_any("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f2) True
9.1.1.8. find_multi()
Search for multiple substrings (list, tuple, set) within string.
returns Either boolean for each sub-str match or the index values.
- additional arguments that can be added are:
start = Optional: integer - position/index to start search from
count = Optional: integer - count of characters to seach from start index
index = Optional: Bool - False to get boolean instead of indexes, (default: True)
>>> f1 = ("LAZY", "BROWN", "QUICK") >>> f2 = ("LAZY", "RED", "QUICK") >>> STR.find_multi("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f1) [30, 10, 4] >>> STR.find_multi("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f2) [30, -1, 4] >>> STR.find_multi("THE QUICK BROWN FOX JUMP OVER LAZY DOG", f2, index=False) [True, False, True]
9.1.1.9. update()
Updates string for search item with replace item
This is same as native str.replace()
>>> STR.update("THE QUICK BROWN FOX JUMP OVER LAZY DOG", "DOG", "GOAT") 'THE QUICK BROWN FOX JUMP OVER LAZY GOAT'
9.1.1.10. replace_dual_and_split()
Finds subsequent characters in string and replace those with single. And splits the string using provided Find character (duo).
>>> s = "SRNO____ITEM_____DESCRIPTION________QTY______AMOUNT" >>> STR.replace_dual_and_split(s, " ") ['SRNO', 'ITEM', 'DESCRIPTION', 'QTY', 'AMOUNT']
9.1.1.11. finddualnreplacesingle()
Finds subsequent characters in string and replace those with single.
>>> s = "SRNO____ITEM_____DESCRIPTION________QTY______AMOUNT" >>> STR.finddualnreplacesingle(s, "_") 'SRNO_ITEM_DESCRIPTION_QTY_AMOUNT'
9.1.1.12. indention()
get string indention value
>>> s = " this is indented line" >>> STR.indention(s) 4 # there are four spaces there as indention
9.1.1.13. is_blank_line()
provided string/line a blank line or not
>>> s = " \n" >>> STR.is_blank_line(s) True
9.1.1.14. is_hostname_line()
string/line containing hostname of device
>>> line = "somehostname> show ip int brie" >>> STR.is_hostname_line(line, "somehostname") True
9.1.1.15. hostname()
returns hostname of device from paramiko netconnection
>>> STR.hostname(net_connect) # where net_connect is active paramiko netconnection //hostname//
9.1.1.16. hostname_from_cli()
input standard text input line, for which command was entered.
hostname from command line
>>> cmd = "sh int status" >>> line = "somehostname> sh int status" >>> STR.hostname_from_cli(line, cmd) 'somehostname'
9.1.1.17. shrink_if()
Interface Name shortening, input length will decide number of charactes to be included in shortened output
>>> STR.shrink_if("FastEthernet0/1", 2) 'Fa0/1'
9.1.1.18. if_standardize()
Interface Name shortening/expanding, interface type string length will be auto-calculated default here.
>>> STR.if_standardize("Giga0/1") 'GigabitEthernet0/1' >>> STR.if_standardize("Giga0/1", expand=False) 'Gi0/1'
9.1.1.19. if_prefix()
Interface type
>>> STR.if_prefix("FastEthernet0/1") 'FastEthernet'
9.1.1.20. if_suffix()
Interface number
>>> STR.if_suffix("FastEthernet0/1") '0/1'
9.1.1.21. string_concate()
Concatenate strings s and s1 with conjuctor conj
>>> s1 = "this is beginning" >>> s2 = "this is end" >>> conj = " <-> " >>> STR.string_concate(s1, s2, conj) 'this is beginning <-> this is end'
9.1.1.22. right()
N-number of characters from right side of string
>>> s = "THE QUICK BROWN FOX JUMP OVER LAZY DOG" >>> STR.right(s, 10) 'R LAZY DOG'
9.1.1.23. mid()
N-number of characters from given position in string
Default n-characters is till end
>>> s = "THE QUICK BROWN FOX JUMP OVER LAZY DOG" >>> STR.mid(s, 11, 5) 'BROWN'
9.1.1.24. delete_trailing_remarks()
Deletes trailing remarks from Juniper config line/string
>>> s = ' root-authentication encrypted-password "$9$xxxxxxxx"; ## encrypted-pass' >>> STR.delete_trailing_remarks(s) ' root-authentication encrypted-password "$9$xxxxxxxx";'
9.1.1.25. to_list()
Returns list for the provided string - s
split by Carriage Return
>>> multiline_str = """This is line 1 this one is 2nd this is 3rd and so on""" >>> STR.to_list(multiline_str) ['This is line 1\n', 'this one is 2nd\n', 'this is 3rd\n', 'and so on\n']
9.1.1.26. to_set()
Returns set for the provided string - s
split by Carriage Return and Commas
>>> list_of_ips = """1.1.1.1 2.2.2.2,3.3.3.3 4.4.4.4""" >>> >>> STR.to_set(list_of_ips) {'1.1.1.1', '3.3.3.3', '2.2.2.2', '4.4.4.4'}
9.1.1.27. prepend_bgp_as()
‘n’ number of BGP AS Number prepending string
>>> STR.prepend_bgp_as("12345", 10) '12345 12345 12345 12345 12345 12345 12345 12345 12345 12345'
9.1.1.28. header_indexes()
returns index numbers (range) for the provided header line items.
>>> h = " Port Speed Duplex Mode Vlan InterfaceType" >>> STR.header_indexes(h) OrderedDict([('', [0, 1]), ('Port', [1, 17]), ('Speed', [17, 26]), ('Duplex', [26, 36]), ('Mode', [36, 47]), ('Vlan', [47, 56]), ('InterfaceType', [56, 90])])