简体   繁体   English

在MotorEngine Tornado中保存新记录时,为什么要使用assert?

[英]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. 只是想知道在创建新记录时在MotorEngine的回调中使用Assert有什么意义。 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? 告诉员工在回调中被称为Bernardo的目标是什么? You are not even passing the value to the function... 您甚至没有将值传递给函数...

MotorEngine is an async ORM for Tornado and MongoDB. MotorEngine是Tornado和MongoDB的异步ORM。 https://motorengine.readthedocs.org/en/latest/getting-started.html#creating-a-new-instance 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()

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. ...但是断言也会由doctest进行测试,以确保文档保持最新状态。 In your actual application you wouldn't use asserts like this at all. 在您的实际应用程序中,您根本不会使用这样的断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM