warpops

This module gathers text operations that are wrapped from standard python functions

alltrue

class textops.alltrue

Return True if all elements of the input are true

Returns:True if all elements of the input are true
Return type:bool

Examples

>>> '\n\n' | alltrue()
False
>>> 'a\n\n' | alltrue()
False
>>> 'a\nb\n' | alltrue()
True
>>> 'a\nb\nc' | alltrue()
True
>>> ['',''] >> alltrue()
False
>>> ['1','2'] >> alltrue()
True
>>> [True,False] >> alltrue()
False
>>> [True,True] >> alltrue()
True

anytrue

class textops.anytrue

Return True if any element of the input is true

Returns:True if any element of the input is true
Return type:bool

Examples

>>> '\n\n' | anytrue()
False
>>> 'a\n\n' | anytrue()
True
>>> 'a\nb\n' | anytrue()
True
>>> 'a\nb\nc' | anytrue()
True
>>> [0,0] >> anytrue()
False
>>> [0,1] >> anytrue()
True
>>> [1,2] >> anytrue()
True
>>> [False,False] >> anytrue()
False
>>> [True,False] >> anytrue()
True
>>> [True,True] >> anytrue()
True

dosort

class textops.dosort([cmp[, key[, reverse]]])

Sort input text

Return a new sorted list from the input text. The sorting is done on a by-line/list item basis.

Parameters:
  • cmp (callable) – specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument, ex: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
  • key (callable) – specifies a function of one argument that is used to extract a comparison key from each list element, ex: key=str.lower. The default value is None (compare the elements directly).
  • reverse (bool) – If set to True, then the list elements are sorted as if each comparison were reversed.
Returns:

The sorted input text

Return type:

generator

Examples

>>> 'a\nd\nc\nb' | dosort().tolist()
['a', 'b', 'c', 'd']
>>> 'a\nd\nc\nb' >> dosort()
['a', 'b', 'c', 'd']
>>> 'a\nd\nc\nb' >> dosort(reverse=True)
['d', 'c', 'b', 'a']
>>> 'a\nB\nc' >> dosort()
['B', 'a', 'c']
>>> 'a\nB\nc' >> dosort(cmp=lambda x,y:cmp(x.lower(),y.lower()))
['a', 'B', 'c']
>>> [('a',3),('c',1),('b',2)] >> dosort()
[('a', 3), ('b', 2), ('c', 1)]
>>> [('a',3),('c',1),('b',2)] >> dosort(key=lambda x:x[1])
[('c', 1), ('b', 2), ('a', 3)]
>>> [{'k':3},{'k':1},{'k':2}] >> dosort(key=lambda x:x['k'])
[{'k': 1}, {'k': 2}, {'k': 3}]

getmax

class textops.getmax([key])

get the max value

Return the largest item/line from the input.

Parameters:key (callable) – specifies a function of one argument that is used to extract a comparison key from each list element, ex: key=str.lower.
Returns:The largest item/line
Return type:object

Examples

>>> 'E\nc\na' | getmax()
'c'
>>> 'E\nc\na' >> getmax()
'c'
>>> 'E\nc\na' >> getmax(key=str.lower)
'E'

getmin

class textops.getmin([key])

get the min value

Return the smallest item/line from the input.

Parameters:key (callable) – specifies a function of one argument that is used to extract a comparison key from each list element, ex: key=str.lower.
Returns:The smallest item/line
Return type:object

Examples

>>> 'c\nE\na' | getmin()
'E'
>>> 'c\nE\na' >> getmin()
'E'
>>> 'c\nE\na' >> getmin(key=str.lower)
'a'

linenbr

class textops.linenbr(start=0)

Enumerate input text lines

add a column to the input text with the line number within.

Parameters:start (int) – starting number (default : 0)
Returns:input text with line numbering
Return type:generator

Examples

>>> 'a\nb\nc' >> linenbr()
[(0, 'a'), (1, 'b'), (2, 'c')]
>>> 'a\nb\nc' | linenbr(1).tolist()
[(1, 'a'), (2, 'b'), (3, 'c')]

resub

class textops.resub(pattern, repl, string, count=0, flags=0)

Substitute a regular expression within a string or a list of strings

It uses re.sub() to replace the input text.

Parameters:
  • pattern (str) – Split string by the occurrences of pattern
  • repl (str) – Replacement string.
  • count (int) – the maximum number of pattern occurrences to be replaced
  • flags (int) – regular expression flags (re.I etc...). Only available in Python 2.7+
Returns:

The replaced text

Return type:

str or list

Examples

>>> 'Words, words, words.' | resub('[Ww]ords','Mots')
'Mots, Mots, Mots.'
>>> ['Words1 words2', 'words', 'words.' ] >> resub('[Ww]ords','Mots',1)
['Mots1 words2', 'Mots', 'Mots.']