fandango.functional module¶
Description¶
- fandango.functional::
- contains functional programming methods for python, it should use only python main library methods and not be dependent of any other module
Contents
- fandango.functional module
- Description
- Variables
- Classes
- Functions
- now
- text2list
- isGenerator
- shape
- toRegexp
- toString
- randomize
- isMapping
- obj2str
- clsplit
- str2type
- djoin
- signedint2bin
- isIterable
- seqdiff
- time2tuple
- evalF
- int2bool
- int2char
- toSequence
- matchTuples
- pgrep
- isNumber
- inCl
- str2int
- pfilter
- isString
- reldiff
- pmatch
- rtf2plain
- setitem
- html2text
- getitem
- testF
- ifThen
- matchCl
- rms
- everyone
- isDictionary
- str2epoch
- matchAll
- isNone
- negbin
- retry
- xor
- replaceCl
- bool2int
- time2date
- randpop
- isSequence
- pslice
- splitCl
- code2atoms
- last
- mysql2time
- toStringList
- int2hex
- str2bool
- date2time
- epoch2str
- first
- sortedRe
- char2int
- absdiff
- matchAny
- isFalse
- text2tuples
- unicode2str
- dict2json
- bin2unsigned
- date2str
- toList
- doc2str
- splitList
- isNested
- contains
- clmatch
- anyone
- filtersmart
- hex2int
- str2float
- isBool
- iif
- str2time
- kmap
- time2str
- isRegexp
- time2gmt
- isDate
- list2str
- join
- retried
- bin2signedint
- isTrue
- ctime2time
- pindex
- timezone
- avg
- isNaN
- dict2str
- toCl
- str2list
- evalX
- setlocal
- tuple2time
- int2bin
- matchMap
- notNone
- str2lines
- clsub
- searchCl
- floor
- clsearch
- tuples2text
- raw autodoc
Variables¶
END_OF_TIME¶
- fandango.functional.END_OF_TIME¶
int(x=0) -> int or long int(x, base=10) -> int or long
Convert a number or string to an integer, or return 0 if no arguments are given. If x is floating point, the conversion truncates towards zero. If x is outside the integer range, the function returns a long instead.
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-‘ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4
Classes¶
Piped¶
- class fandango.functional.Piped(method, *args, **kwargs)[source]¶
- This class gives a “Pipeable” interface to a python method:
- cat | Piped(method,args) | Piped(list) list(method(args,cat))
e.g.: class grep:
#keep only lines that match the regexp def __init__(self,pat,flags=0):
self.fun = re.compile(pat,flags).match- def __ror__(self,input):
- return ifilter(self.fun,input) #imap,izip,count,ifilter could ub useful
cat(‘filename’) | grep(‘myname’) | printlines
- itertools = <module 'itertools' from '/usr/lib64/python2.7/lib-dynload/itertools.so'>¶
Functions¶
clsplit¶
- fandango.functional.clsplit(exp, seq, inclusive=False)¶
Split an string by occurences of exp
evalF¶
- fandango.functional.evalF(formula)[source]¶
Returns a function that executes the formula passes as argument. The formula should use x,y,z as predefined arguments, or use args[..] array instead
e.g.: map(evalF(“x>2”),range(5)) : [False, False, False, True, True]
It is optimized to be efficient (but still 50% slower than a pure lambda)
toSequence¶
- fandango.functional.toSequence(val, default=[], check=<function isSequence at 0x7eff5ced8a28>)¶
ifThen¶
- fandango.functional.ifThen(condition, callback, falsables=())[source]¶
This function allows to execute a callable on an object only if it has a valid value. ifThen(value,callable) will return callable(value) only if value is not in falsables.
It is a List-like method, it can be combined with fandango.excepts.trial
str2epoch¶
- fandango.functional.str2epoch(seq='', cad='')¶
Parameters: seq – Date must be in ((Y-m-d|d/m/Y) (H:M[:S]?)) format or -N [d/m/y/s/h] See RAW_TIME and TIME_UNITS to see the units used for pattern matching.
The conversion itself is done by time.strptime method.
Parameters: cad – You can pass a custom time format
replaceCl¶
- fandango.functional.replaceCl(exp, repl, seq, regexp=True, lower=False)[source]¶
Replaces caseless expression exp by repl in string seq repl can be string or callable(matchobj) ; to reuse matchobj.group(x) if needed in the replacement string lower argument controls whether replaced string should be always lower case or not
clmatch¶
- fandango.functional.clmatch(exp, seq, terminate=False, extend=False)¶
Returns a caseless match between expression and given string
filtersmart¶
- fandango.functional.filtersmart(sequence,filters=['any_filter','+all_filter','!neg_filter'])[source]¶
appies a list of filters to a sequence of strings, behavior of filters depends on first filter character:
‘[a-zA-Z0-9] : an individual filter matches all strings that contain it, one matching filter is enough ‘!’ : negate, discards all matching values ‘+’ : complementary, it must match all complementaries and at least a ‘normal filter’ to be valid ‘^’ : matches string since the beginning (startswith instead of contains) ‘$’ : matches the end of strings ‘,’ : will be used as filter separator if a single string is provided
iif¶
- fandango.functional.iif(condition, truepart, falsepart=None, forward=False)[source]¶
if condition is boolean return (falsepart,truepart)[condition] if condition is callable returns truepart if condition(tp) else falsepart if forward is True condition(truepart) is returned instead of truepart if forward is callable, forward(truepart) is returned instead
str2time¶
- fandango.functional.str2time(seq='', cad='')[source]¶
Parameters: seq – Date must be in ((Y-m-d|d/m/Y) (H:M[:S]?)) format or -N [d/m/y/s/h] See RAW_TIME and TIME_UNITS to see the units used for pattern matching.
The conversion itself is done by time.strptime method.
Parameters: cad – You can pass a custom time format
evalX¶
- fandango.functional.evalX(target, _locals=None, modules=None, instances=None, _trace=False, _exception=<type 'exceptions.Exception'>)[source]¶
evalX is an enhanced eval function capable of evaluating multiple types and import modules if needed. The _locals/modules/instances dictionaries WILL BE UPDATED with the result of the code! (if ‘=’ or import are used) It is used by some fandango classes to send python code to remote threads; that will evaluate and return the values as pickle objects.
- target may be:
- dictionary of built-in types (pickable): {‘__target__’:callable or method_name,’__args__’:[],’__class_’:’‘,’__module’:’‘,’__class_args__’:[]}
- string to eval: eval(‘import $MODULE’ or ‘$VAR=code()’ or ‘code()’)
- list if list[0] is callable: value = list[0](*list[1:])
- callable: value = callable()
clsub¶
- fandango.functional.clsub(exp, repl, seq, regexp=True, lower=False)¶
Replaces caseless expression exp by repl in string seq repl can be string or callable(matchobj) ; to reuse matchobj.group(x) if needed in the replacement string lower argument controls whether replaced string should be always lower case or not
clsearch¶
- fandango.functional.clsearch(exp, seq, terminate=False, extend=False)¶
Returns a caseless regular expression search between expression and given string
raw autodoc¶
- class fandango.functional.Piped(method, *args, **kwargs)[source]
- This class gives a “Pipeable” interface to a python method:
- cat | Piped(method,args) | Piped(list) list(method(args,cat))
e.g.: class grep:
#keep only lines that match the regexp def __init__(self,pat,flags=0):
self.fun = re.compile(pat,flags).match- def __ror__(self,input):
- return ifilter(self.fun,input) #imap,izip,count,ifilter could ub useful
cat(‘filename’) | grep(‘myname’) | printlines
- itertools = <module 'itertools' from '/usr/lib64/python2.7/lib-dynload/itertools.so'>
- fandango.functional.absdiff(x, y, floor=0.01)[source]
Checks absolute difference <floor between x and y floor would be a decimal value, e.g. 0.05
- fandango.functional.anyone(seq, method=<type 'bool'>)[source]
Returns first that is true or last that is false
- fandango.functional.avg(seq)[source]
- fandango.functional.bin2signedint(x, N=16)[source]
Converts an string with a binary number into a signed integer
- fandango.functional.bin2unsigned(c)[source]
- fandango.functional.bool2int(seq)[source]
Converts a boolean array to an unsigned integer
- fandango.functional.char2int(c)[source]
ord(c)
- fandango.functional.clmatch(exp, seq, terminate=False, extend=False)
Returns a caseless match between expression and given string
- fandango.functional.clsearch(exp, seq, terminate=False, extend=False)
Returns a caseless regular expression search between expression and given string
- fandango.functional.clsplit(exp, seq, inclusive=False)
Split an string by occurences of exp
- fandango.functional.clsub(exp, repl, seq, regexp=True, lower=False)
Replaces caseless expression exp by repl in string seq repl can be string or callable(matchobj) ; to reuse matchobj.group(x) if needed in the replacement string lower argument controls whether replaced string should be always lower case or not
- fandango.functional.code2atoms(code)[source]
- fandango.functional.contains(a, b, regexp=True)[source]
Returns a in b; using a as regular expression if wanted
- fandango.functional.ctime2time(time_struct)[source]
- fandango.functional.date2str(date)[source]
- fandango.functional.date2time(date)[source]
- fandango.functional.dict2json(dct, filename=None, throw=False, recursive=True, encoding='latin-1')[source]
It will check that all objects in dict are serializable. If throw is False, a corrected dictionary will be returned. If filename is given, dict will be saved as a .json file.
- fandango.functional.dict2str(s, sep=':\t', linesep='\n', listsep='\n\t')[source]
- fandango.functional.djoin(a, b)[source]
This method merges dictionaries and/or lists
- fandango.functional.doc2str(obj)[source]
- fandango.functional.epoch2str(epoch=None, cad='%Y-%m-%d %H:%M:%S')
- fandango.functional.evalF(formula)[source]
Returns a function that executes the formula passes as argument. The formula should use x,y,z as predefined arguments, or use args[..] array instead
e.g.: map(evalF(“x>2”),range(5)) : [False, False, False, True, True]
It is optimized to be efficient (but still 50% slower than a pure lambda)
- fandango.functional.evalX(target, _locals=None, modules=None, instances=None, _trace=False, _exception=<type 'exceptions.Exception'>)[source]
evalX is an enhanced eval function capable of evaluating multiple types and import modules if needed. The _locals/modules/instances dictionaries WILL BE UPDATED with the result of the code! (if ‘=’ or import are used) It is used by some fandango classes to send python code to remote threads; that will evaluate and return the values as pickle objects.
- target may be:
- dictionary of built-in types (pickable): {‘__target__’:callable or method_name,’__args__’:[],’__class_’:’‘,’__module’:’‘,’__class_args__’:[]}
- string to eval: eval(‘import $MODULE’ or ‘$VAR=code()’ or ‘code()’)
- list if list[0] is callable: value = list[0](*list[1:])
- callable: value = callable()
- fandango.functional.everyone(seq, method=<type 'bool'>)[source]
Returns last that is true or first that is false
- fandango.functional.filtersmart(sequence,filters=['any_filter','+all_filter','!neg_filter'])[source]
appies a list of filters to a sequence of strings, behavior of filters depends on first filter character:
‘[a-zA-Z0-9] : an individual filter matches all strings that contain it, one matching filter is enough ‘!’ : negate, discards all matching values ‘+’ : complementary, it must match all complementaries and at least a ‘normal filter’ to be valid ‘^’ : matches string since the beginning (startswith instead of contains) ‘$’ : matches the end of strings ‘,’ : will be used as filter separator if a single string is provided
- fandango.functional.first(seq, default=<type 'exceptions.Exception'>)[source]
Returns first element of sequence
- fandango.functional.floor(x, unit=1)[source]
Returns greatest multiple of ‘unit’ below ‘x’
- fandango.functional.getitem(mapping, key)[source]
- fandango.functional.hex2int(c)[source]
- fandango.functional.html2text(txt)[source]
- class fandango.functional.iPiped(method, *args, **kwargs)[source]
Used to pipe methods that already return iterators e.g.: hdb.keys() | iPiped(filter,partial(fandango.inCl,’elotech’)) | plist
- fandango.functional.ifThen(condition, callback, falsables=())[source]
This function allows to execute a callable on an object only if it has a valid value. ifThen(value,callable) will return callable(value) only if value is not in falsables.
It is a List-like method, it can be combined with fandango.excepts.trial
- fandango.functional.iif(condition, truepart, falsepart=None, forward=False)[source]
if condition is boolean return (falsepart,truepart)[condition] if condition is callable returns truepart if condition(tp) else falsepart if forward is True condition(truepart) is returned instead of truepart if forward is callable, forward(truepart) is returned instead
- fandango.functional.inCl(exp, seq, regexp=True)[source]
Returns a caseless “in” boolean function, using regex if wanted
- fandango.functional.int2bin(n)[source]
- fandango.functional.int2bool(dec, N=16)[source]
Converts an integer to a binary represented as a boolean array
- fandango.functional.int2char(n)[source]
unichr(n)
- fandango.functional.int2hex(n)[source]
- fandango.functional.isBool(seq, is_zero=True)[source]
- fandango.functional.isDate(seq)[source]
- fandango.functional.isDictionary(seq)[source]
It includes dicts and also nested lists
- fandango.functional.isFalse(seq)[source]
- fandango.functional.isGenerator(seq)[source]
- fandango.functional.isIterable(seq)[source]
It includes dicts and listlikes but not strings
- fandango.functional.isMapping(seq)
It includes dicts and also nested lists
- fandango.functional.isNaN(seq)[source]
- fandango.functional.isNested(seq, strict=False)[source]
- fandango.functional.isNone(seq)[source]
- fandango.functional.isNumber(seq)[source]
- fandango.functional.isRegexp(seq, wildcards='^$*+?{[]\\|()')[source]
This function is just a hint, use it with care.
- fandango.functional.isSequence(seq, INCLUDE_GENERATORS=True)[source]
It excludes Strings, dictionaries but includes generators
- fandango.functional.isString(seq)[source]
- fandango.functional.isTrue(arg)[source]
Returns True if arg is not None, not False and not an empty iterable.
- fandango.functional.join(*seqs)[source]
It returns a list containing the objects of all given sequences.
- fandango.functional.kmap(method, keys, values=None, sort=True)[source]
- fandango.functional.last(seq, MAX=1000, default=<type 'exceptions.Exception'>)[source]
Returns last element of sequence
- fandango.functional.list2str(s, separator='\t', MAX_LENGTH=255)[source]
- fandango.functional.matchAll(exprs, seq)[source]
Returns a list of matched strings from sequence. If sequence is list it returns exp as a list.
- fandango.functional.matchAny(exprs, seq)[source]
Returns seq if any of the expressions in exp is matched, if not it returns None
- fandango.functional.matchCl(exp, seq, terminate=False, extend=False)[source]
Returns a caseless match between expression and given string
- fandango.functional.matchMap(mapping, key, regexp=True, default=<type 'exceptions.Exception'>)[source]
from a mapping type (dict or tuples list) with strings as keys it returns the value from the matched key or raises KeyError exception
- fandango.functional.matchTuples(mapping, key, value)[source]
mapping is a (regexp,[regexp]) tuple list where it is verified that value matches any from the matched key
- fandango.functional.mysql2time(mysql_time)[source]
- fandango.functional.negbin(old)[source]
Given a binary number as an string, it returns all bits negated
- fandango.functional.notNone(arg, default=None)[source]
Returns arg if not None, else returns default.
- fandango.functional.now()[source]
- fandango.functional.obj2str(obj, sep=', ', linesep='\n')[source]
- fandango.functional.pfilter(meth=<type 'bool'>, *args)
- fandango.functional.pgrep(exp)
- fandango.functional.pindex(i)
- fandango.functional.pmatch(exp)
- fandango.functional.pslice(i, j)
- fandango.functional.ptext = <fandango.functional.iPiped instance at 0x7eff5ced90e0>¶
Note of the author: This methods are not intended to be universal, are just practical for general Tango application purposes.
- fandango.functional.randomize(seq)[source]
- fandango.functional.randpop(seq)[source]
- fandango.functional.reldiff(x, y, floor=None)[source]
Checks relative (%) difference <floor between x and y floor would be a decimal value, e.g. 0.05
- fandango.functional.replaceCl(exp, repl, seq, regexp=True, lower=False)[source]
Replaces caseless expression exp by repl in string seq repl can be string or callable(matchobj) ; to reuse matchobj.group(x) if needed in the replacement string lower argument controls whether replaced string should be always lower case or not
- fandango.functional.retried(retries=3, pause=0)[source]
- fandango.functional.retry(callable, retries=3, pause=0, args=[], kwargs={})[source]
- fandango.functional.rms(seq)[source]
- fandango.functional.rtf2plain(t, e='[<][^>]*[>]')[source]
- fandango.functional.searchCl(exp, seq, terminate=False, extend=False)[source]
Returns a caseless regular expression search between expression and given string
- fandango.functional.seqdiff(x, y, method=<function reldiff at 0x7eff5ced7230>, floor=None)[source]
Being x and y two arrays it checks (method) difference <floor between the elements of them. floor would be a decimal value, e.g. 0.05
- fandango.functional.setitem(mapping, key, value)[source]
- fandango.functional.setlocal(key, value)[source]
- fandango.functional.shape(seq)[source]
Returns the N dimensions of a python sequence
- fandango.functional.signedint2bin(x, N=16)[source]
It converts an integer to an string with its binary representation
- fandango.functional.sortedRe(iterator, order)[source]
Returns a list sorted using regular expressions. order = list of regular expressions to match (‘[a-z]’,’[0-9].*’,’.*’)
- fandango.functional.splitCl(exp, seq, inclusive=False)[source]
Split an string by occurences of exp
- fandango.functional.splitList(seq, split)[source]
splits a list in lists of ‘split’ size
- fandango.functional.str2bool(seq)[source]
It parses true/yes/no/false/1/0 as booleans
- fandango.functional.str2epoch(seq='', cad='')
Parameters: seq – Date must be in ((Y-m-d|d/m/Y) (H:M[:S]?)) format or -N [d/m/y/s/h] See RAW_TIME and TIME_UNITS to see the units used for pattern matching.
The conversion itself is done by time.strptime method.
Parameters: cad – You can pass a custom time format
- fandango.functional.str2float(seq)[source]
It returns the first float (x.ye-z) encountered in the string
- fandango.functional.str2int(seq)[source]
It returns the first integer encountered in the string
- fandango.functional.str2lines(s, length=80, separator='\n')[source]
- fandango.functional.str2list(s, separator='', regexp=False, sep_offset=0)[source]
Arguments allow to split by regexp and to keep or not the separator character sep_offset = 0 : do not keep sep_offset = -1 : keep with posterior sep_offset = 1 : keep with precedent
- fandango.functional.str2time(seq='', cad='')[source]
Parameters: seq – Date must be in ((Y-m-d|d/m/Y) (H:M[:S]?)) format or -N [d/m/y/s/h] See RAW_TIME and TIME_UNITS to see the units used for pattern matching.
The conversion itself is done by time.strptime method.
Parameters: cad – You can pass a custom time format
- fandango.functional.str2type(seq, use_eval=True, sep_exp='[,;\\ ]+')[source]
Tries to convert string to an standard python type. If use_eval is True, then it tries to evaluate as code. Lines separated by sep_exp will be automatically split
- fandango.functional.testF(f, args=[], t=5.0)[source]
it returns how many times f(*args) can be executed in t seconds
- fandango.functional.text2list(s, separator='\n')[source]
- fandango.functional.text2tuples(s, separator='\t')[source]
- fandango.functional.time2date(epoch=None)[source]
- fandango.functional.time2gmt(epoch=None)[source]
- fandango.functional.time2str(epoch=None, cad='%Y-%m-%d %H:%M:%S')[source]
- fandango.functional.time2tuple(epoch=None)[source]
- fandango.functional.timezone()[source]
- fandango.functional.toCl(exp, terminate=False, wildcards=('*', ' '), lower=True)[source]
Replaces * by .* and ? by . in the given expression.
- fandango.functional.toList(val, default=[], check=<function isSequence at 0x7eff5ced8a28>)[source]
- fandango.functional.toRegexp(exp, terminate=False)[source]
Case sensitive version of the previous one, for backwards compatibility
- fandango.functional.toSequence(val, default=[], check=<function isSequence at 0x7eff5ced8a28>)
- fandango.functional.toString(*val)[source]
- fandango.functional.toStringList(seq)[source]
- fandango.functional.tuple2time(tup)[source]
- fandango.functional.tuples2text(s, separator='\t', lineseparator='\n')[source]
- fandango.functional.unicode2str(obj)[source]
Converts an unpacked unicode object (json) to nested python primitives (map,list,str)
- fandango.functional.xor(A, B)[source]
Returns (A and not B) or (not A and B); the difference with A^B is that it works also with different types and returns one of the two objects..
- class fandango.functional.zPiped(*args)[source]
Returns a callable that applies elements of a list of tuples to a set of functions e.g. [(1,2),(3,0)] | zPiped(str,bool) | plist => [(‘1’,True),(‘3’,False)]