def r_isinstore(store, excep=None, msg=None): ''' this will cunstruct a test function that will test if an object exists in a given store or raise an exception. ''' name = hasattr(store, '__name__') and store.__name__ or store.__class__.__name__ return func.raise_on_false(isinstore(store), 'r_isin' + name,\ excep != None and NameError \ or excep, \ msg != None and ('object not found in store %s.' % store) \ or msg)
import pli.functional as func #----------------------------------------------------------------------- #--------------------------------------------------------------string--- def string(obj): ''' return true if the object is either str or unicode. ''' return type(obj) in (str, unicode) isstring = string #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - r_string = func.raise_on_false(string, TypeError, 'r_string', 'object is nither str or unicode.') #-------------------------------------------------------------isemail--- EMAIL = '^\s*[-_a-z0-9.]+@[-_a-z0-9.]+\.\w+\s*$' _email_pattern = re.compile(EMAIL, re.I) #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def isemail(obj): ''' return true if the obj is compatible with the email format spec (see EMAIL). ''' return _email_pattern.match(obj) != None #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - r_isemail = func.raise_on_false(isemail, TypeError, 'r_isemail', 'object does not match email pattern.')