main entry point


In [ ]:
if __name__ == '__main__':
    // do stuff
    // ...

not testable. only by command line: python app.py


In [ ]:
def main():
    // do stuff
    // ...
    
    
if __name__ == '__main__':
    main()

in test.py


In [ ]:
def test_main():
    from app import main
    
    main()

if command line tool


In [ ]:
def main(argv):
    // do stuff
    // ...
    
    
if __name__ == '__main__':
    import sys
    main(sys.argv)

implicit dependencies


In [ ]:
def create_author(author):
    db = get_db()
    db.create_author(author)

In [ ]:
def create_author(author, db):
    db.create_author(author)

In [ ]:
def create_author(author, create):
    create(author)
    
create_author(author, db.create_author)

step by step


In [ ]:
class persistence_functions:
    def __init__(self):
        self.db = get_db()
        
    def create_author(self, author):
        self.db.create_author(author)

In [ ]:
def create_author_testable(author, db):
    db.create_author(author)


class persistence_functions:
    def __init__(self):
        self.db = get_db()
        
    def create_author(self, author):
        create_author_testable(author, self.db)

In [ ]:
def create_author_testable(author, db):
    db.create_author(author)


class persistence_functions:
    def __init__(self, db=None):
        if db is None:
            db = get_db()
            
        self.db = db
        
    def create_author(self, author):
        create_author_testable(author, self.db)