Search code examples
pythonmongodbtornadotornado-motormotorengine

Why use assert when saving new record in MotorEngine Tornado?


Just wondering what is the point of using the Assert in the callback in MotorEngine when creating a new record. I am not getting it and documentation is not clear. What is the goal of telling that the employee is called Bernardo in the callback? You are not even passing the value to the function...

MotorEngine is an async ORM for Tornado and MongoDB. https://motorengine.readthedocs.org/en/latest/getting-started.html#creating-a-new-instance

Many thanks!

def create_employee():
    emp = Employee(first_name="Bernardo", last_name="Heynemann", employee_id=1532)
    emp.save(handle_employee_saved)

def handle_employee_saved(emp):
    try:
        assert emp is not None
        assert emp.employee_id == 1532
    finally:
        io_loop.stop()

io_loop.add_timeout(1, create_employee)
io_loop.start()

Solution

  • I believe the documentation is using those asserts as a way to show you what values are received by the callback. It could as easily have had a comment:

    # emp is an object with employee_id of 1532
    

    ... but the asserts are also tested by doctest to ensure the documentation remains up-to-date. In your actual application you wouldn't use asserts like this at all.