The Python Oracle

python: run the second condition after the first one becomes false

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC D Schuberts Piano Sonata D 850 in D

--

Chapters
00:00 Question
01:08 Accepted answer (Score 3)
01:55 Thank you

--

Full question
https://stackoverflow.com/questions/6946...

Accepted answer links:
[decorators]: https://www.python.org/dev/peps/pep-0318/
[How to make function decorators and chain them together?]: https://stackoverflow.com/q/739654/12416...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #process #conditionalstatements

#avk47



ACCEPTED ANSWER

Score 3


We can leverage decorators1 here to enforce the invariant.

def fill_if_empty(func):
    def wrapper(*args, **kwargs):
        if YOUR_CONDITION_TO_CHECK_FOR_EMPTY_DIR:
            """
            fill empty directory here.
            """
            process_user_input()

        func(*args, **kwargs)
    return wrapper

@fill_if_empty
def do_process_on_the_full_directory():
    """
    Run some process on directory
    """
    pass

do_process_on_full_directory() 

1. Checkout this post to know more about decorators: How to make function decorators and chain them together?