noc.core.text

Module Contents

noc.core.text.rx_header_start
noc.core.text.rx_col
noc.core.text.parse_table(s, allow_wrap=False, allow_extend=False, expand_columns=False, max_width=0, footer=None, n_row_delim='', expand_tabs=True)
Parse string containing table an return a list of table rows.
Each row is a list of cells.
Columns are determined by a sequences of ---- or ==== which are
determines rows bounds.
Examples:
First Second Third
----- ------ -----
a b c
ddd eee fff
Will be parsed down to the [["a","b","c"],["ddd","eee","fff"]]
Parameters:
  • s (str) – Table for parsing
  • allow_wrap (bool) – Union if cell contins multiple line
  • allow_extend (bool) – Check if column on row longest then column width, enlarge it and shift rest of columns
  • expand_columns (bool) – Expand columns covering all available width
  • max_width (int) – Max table width, if table width < max_width extend length, else - nothing
  • footer (string) – stop iteration if match expression footer
  • n_row_delim (string) – Append delimiter to next cell line
  • expand_tabs (bool) – Apply expandtabs() to each line
noc.core.text.rx_html_tags
noc.core.text.strip_html_tags(s)
noc.core.text.xml_to_table(s, root, row)
>>> xml_to_table('<?xml version="1.0" encoding="UTF-8" ?><response><action><row><a>1</a><b>2</b></row><row><a>3</a><b>4</b></row></action></response>','action','row') # noqa
[{'a': '1', 'b': '2'}, {'a': '3', 'b': '4'}]
noc.core.text.list_to_ranges(s)
>>> list_to_ranges([])
''
>>> list_to_ranges([1])
'1'
>>> list_to_ranges([1,2])
'1-2'
>>> list_to_ranges([1,2,3])
'1-3'
>>> list_to_ranges([1,2,3,5])
'1-3,5'
>>> list_to_ranges([1,2,3,5,6,7])
'1-3,5-7'
>>> list_to_ranges(range(1,4001))
'1-4000'
noc.core.text.rx_range
noc.core.text.ranges_to_list(s, splitter=', ')
>>> ranges_to_list("1")
[1]
>>> ranges_to_list("1, 2")
[1, 2]
>>> ranges_to_list("1, 10-12")
[1, 10, 11, 12]
>>> ranges_to_list("1, 10-12, 15, 17-19")
[1, 10, 11, 12, 15, 17, 18, 19]
noc.core.text.replace_re_group(expr, group, pattern)
>>> replace_re_group("nothing","(?P<groupname>","groupvalue")
'nothing'
>>> replace_re_group("the (?P<groupname>simple) test","(?P<groupname>","groupvalue")
'the groupvalue test'
>>> replace_re_group("the (?P<groupname> nested (test)>)","(?P<groupname>","groupvalue")
'the groupvalue'
noc.core.text.indent(text, n=4)
Indent each line of text with spaces
Parameters:
  • text – text
  • n – amount of spaces to ident
>>> indent("")
''
>>> indent("the quick brown fox\njumped over an lazy dog\nend")
'    the quick brown fox\n    jumped over an lazy dog\n    end'
noc.core.text.split_alnum(s)
Split line to a sequence of iterating alpha and digit strings
Parameters:s (str) –
Returns:list
Return type:list
>>> split_alnum("Fa 0/1")
['Fa ', 0, '/', 1]
>>> split_alnum("Fa 0/1.15")
['Fa ', 0, '/', 1, '.', 15]
>>> split_alnum("ge-1/0/1")
['ge-', 1, '/', 0, '/', 1]
>>> split_alnum("ge-1/0/1.15")
['ge-', 1, '/', 0, '/', 1, '.', 15]
noc.core.text.rx_notspace
noc.core.text.find_indented(s)
Parses following text structure:
section 1 header
line 1 line 2
section 2 header
line 1 line 2
>>> find_idented("section0\nsection 1\n  line 1-1\n  line 1-2\n\n"                     "section 2\n  line 2-1\n  line 2-2")
['section 1

line 1-1 line 1-2’, ‘section 2 line 2-1 line 2-2’]

param s:
return:
noc.core.text.rx_safe_path
noc.core.text.quote_safe_path(d)
noc.core.text.to_seconds(v)
Convert string value to seconds.
Available acronyms are h, d, w, m, y
noc.core.text.rx_non_numbers
noc.core.text.ch_escape(s)
noc.core.text.ESC_REPLACEMENTS
noc.core.text.rx_escape
noc.core.text.tsv_escape(text)