cast¶
This modules provides casting features, that is to force the output type
pretty¶
- class textops.pretty()¶
Pretty format the input text
Returns: Converted result as a pretty string ( uses pprint.PrettyPrinter.pformat() ) Return type: str Examples:
>>> s = ''' ... a:val1 ... b: ... c:val3 ... d: ... e ... : val5 ... f ... :val6 ... g:val7 ... f: val8''' >>> print s | parse_indented() {'a': 'val1', 'b': {'c': 'val3', 'd': {'e': 'val5', 'f': 'val6'}, 'g': 'val7'}, 'f': 'val8'} >>> print s | parse_indented().pretty() { 'a': 'val1', 'b': { 'c': 'val3', 'd': { 'e': 'val5', 'f': 'val6'}, 'g': 'val7'}, 'f': 'val8'}
todatetime¶
- class textops.todatetime()¶
Convert the result to a datetime python object
Returns: converted result as a datetime python object Return type: datetime Examples
>>> '2015-10-28' | todatetime() datetime.datetime(2015, 10, 28, 0, 0) >>> '2015-10-28 22:33:00' | todatetime() datetime.datetime(2015, 10, 28, 22, 33) >>> '2015-10-28 22:33:44' | todatetime() datetime.datetime(2015, 10, 28, 22, 33, 44) >>> '2014-07-08T09:02:21.377' | todatetime() datetime.datetime(2014, 7, 8, 9, 2, 21, 377000) >>> '28-10-2015' | todatetime() datetime.datetime(2015, 10, 28, 0, 0) >>> '10-28-2015' | todatetime() datetime.datetime(2015, 10, 28, 0, 0) >>> '10-11-2015' | todatetime() datetime.datetime(2015, 10, 11, 0, 0)
todict¶
- class textops.todict()¶
Converts list or 2 items-tuples into dict
Returns: Converted result as a dict Return type: dict Examples:
>>> [ ('a',1), ('b',2), ('c',3) ] | echo().todict() {'a': 1, 'c': 3, 'b': 2}
tofloat¶
- class textops.tofloat()¶
Convert the result to a float
Returns: converted result as an int or list of int Return type: str or list Examples
>>> '53' | tofloat() 53.0 >>> 'not a float' | tofloat() 0.0 >>> '3.14' | tofloat() 3.14 >>> '3e3' | tofloat() 3000.0 >>> ['53','not an int','3.14'] | tofloat() [53.0, 0.0, 3.14]
toint¶
- class textops.toint()¶
Convert the result to an integer
Returns: converted result as an int or list of int Return type: str or list Examples
>>> '53' | toint() 53 >>> 'not an int' | toint() 0 >>> '3.14' | toint() 3 >>> '3e3' | toint() 3000 >>> ['53','not an int','3.14'] | toint() [53, 0, 3]
tolist¶
- class textops.tolist(return_if_none=None)¶
Convert the result to a list
If the input text is a string, it is put in a list.If the input text is a list : nothing is done.If the input text is a generator : it is converted into a list
Parameters: return_if_none (str) – the object to return if the input text is None (Default : None) Returns: converted result as a string Return type: str Examples
>>> 'hello' | tolist() ['hello'] >>> ['hello','world'] | tolist() ['hello', 'world'] >>> type(None|tolist()) <type 'NoneType'> >>> def g(): yield 'hello' ... >>> g()|tolist() ['hello']
toliste¶
- class textops.toliste(return_if_none='')¶
Convert the result to a list
If the input text is a string, it is put in a list.If the input text is a list : nothing is done.If the input text is a generator : it is converted into a list
Parameters: return_if_none (str) – the object to return if the input text is None (Default : empty list) Returns: converted result as a string Return type: str Examples
>>> 'hello' | toliste() ['hello'] >>> type(None|toliste()) <class 'textops.base.ListExt'> >>> None|toliste() []
toslug¶
- class textops.toslug()¶
Convert a string to a slug
Returns: a slug Return type: str Examples
>>> 'this is my article' | toslug() 'this-is-my-article' >>> 'this%%% is### my___article' | toslug() 'this-is-my-article'
tostr¶
- class textops.tostr(join_str='\n', return_if_none=None)¶
Convert the result to a string
If the input text is a list or a generator, it will join all the lines with a newline.If the input text is None, None is NOT converted to a string : None is returned
Parameters: Returns: converted result as a string
Return type: str or None
Examples
>>> 'line1\nline2' | tostr() 'line1\nline2' >>> ['line1','line2'] | tostr() 'line1\nline2' >>> ['line1','line2'] | tostr('---') 'line1---line2' >>> def g(): yield 'hello';yield 'world' ... >>> g()|tostr() 'hello\nworld' >>> type(None | tostr()) <type 'NoneType'> >>> None | tostr(return_if_none='N/A') 'N/A'
tostre¶
- class textops.tostre(join_str='\n', return_if_none='')¶
Convert the result to a string
If the input text is a list or a generator, it will join all the lines with a newline.If the input text is None, None is converted to an empty string.
Parameters: Returns: converted result as a string
Return type: str
Examples
>>> ['line1','line2'] | tostre() 'line1\nline2' >>> type(None | tostre()) <class 'textops.base.StrExt'> >>> None | tostre() ''