in Education by
I want to create a function which will check if it is a correct hour to make an action, but I want it to be flexible and check condition for every pair of args given as an input to function. I wrote some code how it should look like in theory and now I'm trying to figure out how to write this in code. def report_time(greater_than1=0, lower_than1=24, greater_than2=0, lower_than2=24, greater_than3=0, lower_than3=24, ... greater_thanN=0, lower_thanN=24): if greater_than1 < datetime.now().hour < lower_than1: logger.info('Hour is correct') return True if greater_than2 < datetime.now().hour < lower_than2: logger.info('Hour is correct') return True if greater_than3 < datetime.now().hour < lower_than3: logger.info('Hour is correct') return True ... if greater_thanN < datetime.now().hour < lower_thanN: logger.info('Hour is correct') return True Examples of usage: foo = report_time(16, 18) foo = report_time(16, 18, 23, 24) foo = report_time(16, 18, 23, 24, ..., 3, 5) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A better alternative would be to make the function accept only a pair of parameters and then iterate over all pairs outside of the function and check if on any step a True was returned: def report_time(greater_than=0, lower_than=24): return greater_than < datetime.now().hour < lower_than start_times = [10, 12, 20] end_times = [11, 15, 22] for start, end in zip(start_times, end_times): if report_time(start, end): logger.info('Hour is correct') break This can be shortened using map and any: valid_times = map(report_time, start_times, end_times) if any(valid_times): logger.info('Hour is correct') Also, as mentioned by @AzatIbrakov in his comment to another answer, it will be better if you work with tuples. You can use filter in this case: def within_limits(limits=(0, 24)): return limits[0] < datetime.now().hour < limits[1] time_limits = [(10, 11), (12, 15), (20, 22)] if any(filter(within_limits, time_limits)): logger.info('Hour is correct')

Related questions

0 votes
    I've been struggling with some issues relating to referencing child controls within a FormView. Another developer ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    State true or false: SAP systems are divided into different clients depending on business requirement, the data can be accessed within the client only. A. True B. False...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    The .. becomes different shapes depending on the task you are performing. (a) Active tab (b) Insertion point (c) ... (e) None of above Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    The __________ requires that each transaction Ti executes in two or three different phases in its lifetime ... topic in section Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Still new to Haskell, I have hit a wall with the following: I am trying to define some type ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    The following python program can work with ____ parameters. def f(x): def f1(*args, **kwargs): print("Sanfoundry") return x(*args, * ... ) return f1 a) any number of b) 0 c) 1 d) 2...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I have the dict as below, if same value is found more than once then my dict key must be created with incremental ... : ['COMPILE'], } Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    _______ allows you to modify the error behavior so that you can browse the function call stack (a) debug() ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    class Modulus { public static void main(String args[]) { double a = 25.64; int b = 25; a = a % 10; b = b % 10; ... (a + + b); } } Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    What does *args and **kwargs mean?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    I have a major problem which I can't solve. I'm editing question in order to simplify what really ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    A therapist at a free university clinic treats elementary school children with behavior problems who are referred ... the following statements about parental permission is correct?...
asked Mar 1, 2021 in Education by Editorial Staff
0 votes
    Approximate amount spent on security detection and defense technologies to identify and stop advanced threats is _______. A. $850000 B. $750000 C. $650000 D. $550000...
asked Feb 26, 2023 in Technology by JackTerrance
0 votes
    The amount of output of one unit received by another unit depends on what? (a) output unit (b) input unit (c) activation value (d) weight Please answer the above question....
asked Sep 21, 2022 in Education by JackTerrance
...