#NEWLINE# def crontab( month = '*' , day = '*' , day_of_week = '*' , hour = '*' , minute = '*' ) : #NEWLINE# #INDENT# '\n    Convert a "crontab"-style set of parameters into a test function that will\n    return True when the given datetime matches the parameters set forth in\n    the crontab.\n\n    Acceptable inputs:\n    * = every distinct value\n    */n = run every "n" times, i.e. hours=\'*/4\' == 0, 4, 8, 12, 16, 20\n    m-n = run every time m..n\n    m,n = run on m and n\n    ' #NEWLINE# validation = ( ( 'm' , month , range ( 1 , 13 ) ) , ( 'd' , day , range ( 1 , 32 ) ) , ( 'w' , day_of_week , range ( 7 ) ) , ( 'H' , hour , range ( 24 ) ) , ( 'M' , minute , range ( 60 ) ) ) #NEWLINE# cron_settings = [ ] #NEWLINE# for ( date_str , value , acceptable ) in validation : #NEWLINE# #INDENT# settings = set ( [ ] ) #NEWLINE# if isinstance ( value , int ) : #NEWLINE# #INDENT# value = str ( value ) #NEWLINE# #UNINDENT# for piece in value . split ( ',' ) : #NEWLINE# #INDENT# if ( piece == '*' ) : #NEWLINE# #INDENT# settings . update ( acceptable ) #NEWLINE# continue #NEWLINE# #UNINDENT# if piece . isdigit ( ) : #NEWLINE# #INDENT# piece = int ( piece ) #NEWLINE# if ( piece not in acceptable ) : #NEWLINE# #INDENT# raise ValueError ( ( '%d is not a valid input' % piece ) ) #NEWLINE# #UNINDENT# settings . add ( piece ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dash_match = dash_re . match ( piece ) #NEWLINE# if dash_match : #NEWLINE# #INDENT# ( lhs , rhs ) = map ( int , dash_match . groups ( ) ) #NEWLINE# if ( ( lhs not in acceptable ) or ( rhs not in acceptable ) ) : #NEWLINE# #INDENT# raise ValueError ( ( '%s is not a valid input' % piece ) ) #NEWLINE# #UNINDENT# settings . update ( range ( lhs , ( rhs + 1 ) ) ) #NEWLINE# continue #NEWLINE# #UNINDENT# every_match = every_re . match ( piece ) #NEWLINE# if every_match : #NEWLINE# #INDENT# interval = int ( every_match . groups ( ) [ 0 ] ) #NEWLINE# settings . update ( acceptable [ : : interval ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# cron_settings . append ( sorted ( list ( settings ) ) ) #NEWLINE# #UNINDENT# def validate_date( dt ) : #NEWLINE# #INDENT# ( _ , m , d , H , M , _ , w , _ , _ ) = dt . timetuple ( ) #NEWLINE# w = ( ( w + 1 ) % 7 ) #NEWLINE# for ( date_piece , selection ) in zip ( [ m , d , w , H , M ] , cron_settings ) : #NEWLINE# #INDENT# if ( date_piece not in selection ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# return True #NEWLINE# #UNINDENT# return validate_date