{"id":2452,"date":"2015-08-02T14:07:37","date_gmt":"2015-08-02T06:07:37","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2452"},"modified":"2015-08-02T14:07:37","modified_gmt":"2015-08-02T06:07:37","slug":"python%e4%b8%ad%e7%9a%84assert","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2452.html","title":{"rendered":"Python\u4e2d\u7684assert"},"content":{"rendered":"<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u5728\u901b<a href=\"http:\/\/zhuoqiang.me\/password-storage-and-python-example.html\" target=\"_blank\">blog<\/a>\u7684\u65f6\u5019\u770b\u5230\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\u7528\u5230\u4e86\u4e0d\u5c11 assert \uff0c\u867d\u7136\u77e5\u9053\u5b57\u9762\u610f\u601d\uff0c\u4f46\u4e0d\u6e05\u695a\u5177\u4f53\u7684\u4f5c\u7528\u4ee5\u53ca\u4f18\u70b9\uff0c\u56e0\u6b64\uff0c\u6709\u5fc5\u8981\u5b66\u4e60\u4e00\u4e0b\u3002<\/p>\n<pre class=\"lang:default decode:true\">import os\nfrom hashlib import sha256\nfrom hmac import HMAC\n\ndef encrypt_password(password, salt=None):\n    \"\"\"Hash password on the fly.\"\"\"\n    if salt is None:\n        salt = os.urandom(8) # 64 bits.\n\n    assert 8 == len(salt)\n    assert isinstance(salt, str)\n\n    if isinstance(password, unicode):\n        password = password.encode('UTF-8')\n\n    assert isinstance(password, str)\n\n    result = password\n    for i in xrange(10):\n        result = HMAC(result, salt, sha256).digest()\n\n    return salt + result\n\nhashed = encrypt_password('secret password')    # binary data\n\ndef validate_password(hashed, input_password):\n    return hashed == encrypt_password(input_password, salt=hashed[:8])\n\nassert validate_password(hashed, 'secret password')<\/pre>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>python assert<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/UsingAssertionsEffectively\" target=\"_blank\">https:\/\/wiki.python.org\/moin\/UsingAssertionsEffectively<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/reference\/simple_stmts.html#the-assert-statement\" target=\"_blank\">https:\/\/docs.python.org\/2\/reference\/simple_stmts.html#the-assert-statement<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5142418\/what-is-the-use-of-assert-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/5142418\/what-is-the-use-of-assert-in-python<\/a><\/li>\n<li><a href=\"http:\/\/programmers.stackexchange.com\/questions\/15515\/using-assertions-versus-throwing-exceptions\" target=\"_blank\">http:\/\/programmers.stackexchange.com\/questions\/15515\/using-assertions-versus-throwing-exceptions<\/a><\/li>\n<li><a href=\"http:\/\/blog.jobbole.com\/76285\/\" target=\"_blank\">http:\/\/blog.jobbole.com\/76285\/<\/a><\/li>\n<li><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_76e94d210100vz37.html\" target=\"_blank\">http:\/\/blog.sina.com.cn\/s\/blog_76e94d210100vz37.html<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>Assert statements are a convenient way to <strong><span style=\"color: #ff0000;\">insert debugging assertions<\/span><\/strong> into a program:<\/p>\n<blockquote>\n<pre class=\"lang:default decode:true\">assert_stmt ::= \"assert\" <a href=\"https:\/\/docs.python.org\/2\/reference\/expressions.html#grammar-token-expression\" target=\"_blank\">expression<\/a> [\",\" expression]<\/pre>\n<\/blockquote>\n<p>The simple form, assert expression, is equivalent to<\/p>\n<pre class=\"lang:default decode:true\">if __debug__:\n    if not expression: raise AssertionError<\/pre>\n<p>The extended form, assert expression1, expression2, is equivalent to<\/p>\n<pre class=\"lang:default decode:true\">if __debug__:\n    if not expression1: raise AssertionError(expression2)<\/pre>\n<p>##<\/p>\n<p>The\u00a0assert\u00a0statement exists in mostly every other programming language out there. When you do&#8230;<\/p>\n<pre class=\"lang:default decode:true\">assert a_condition<\/pre>\n<p><span style=\"line-height: 1.5;\">&#8230; you&#8217;re telling the program to test that condition, and trigger an error if the condition is false.<\/span><\/p>\n<p>In Python, it&#8217;s roughly equivalent to this:<\/p>\n<pre class=\"lang:default decode:true\">if not condition:\n    raise AssertionError()<\/pre>\n<p>##<\/p>\n<ol>\n<li>assert\u8bed\u53e5<strong>\u7528\u6765\u58f0\u660e\u67d0\u4e2a\u6761\u4ef6\u662f\u771f\u7684<\/strong>\u3002<\/li>\n<li>\u5982\u679c\u4f60<strong><span style=\"color: #ff0000;\">\u975e\u5e38\u786e\u4fe1<\/span><\/strong>\u67d0\u4e2a\u4f60\u4f7f\u7528\u7684\u5217\u8868\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u4f60<span style=\"color: #ff0000;\"><strong>\u60f3\u8981\u68c0\u9a8c\u8fd9\u4e00\u70b9<\/strong><\/span>\uff0c\u5e76\u4e14\u5728\u5b83\u975e\u771f\u7684\u65f6\u5019\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\uff0c\u90a3\u4e48assert\u8bed\u53e5\u662f\u5e94\u7528\u5728\u8fd9\u79cd\u60c5\u5f62\u4e0b\u7684\u7406\u60f3\u8bed\u53e5\u3002<\/li>\n<li>\u5f53assert\u8bed\u53e5\u5931\u8d25\u7684\u65f6\u5019\uff0c\u4f1a\u5f15\u53d1AssertionError\u3002<\/li>\n<\/ol>\n<p>##<\/p>\n<h6>\u4e0b\u9762\u662f\u6211\u5efa\u8bae\u7684\u4e0d\u8981\u7528\u65ad\u8a00\u7684\u573a\u666f\uff1a<\/h6>\n<ul>\n<li>\u4e0d\u8981\u7528\u5b83\u6d4b\u8bd5\u7528\u6237\u63d0\u4f9b\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u4e0d\u8981\u7528\u65ad\u8a00\u6765\u68c0\u67e5\u4f60\u89c9\u5f97\u5728\u4f60\u7684\u7a0b\u5e8f\u7684\u5e38\u89c4\u4f7f\u7528\u65f6\u4f1a\u51fa\u9519\u7684\u5730\u65b9<\/strong>\u3002<span style=\"color: #ff0000;\"><strong>\u65ad\u8a00\u662f\u7528\u6765\u68c0\u67e5\u975e\u5e38\u7f55\u89c1\u7684\u95ee\u9898<\/strong><\/span>\u3002\u4f60\u7684\u7528\u6237\u4e0d\u5e94\u8be5\u770b\u5230\u4efb\u4f55\u65ad\u8a00\u9519\u8bef\uff0c\u5982\u679c\u4ed6\u4eec\u770b\u5230\u4e86\uff0c\u8fd9\u662f\u4e00\u4e2abug\uff0c\u4fee\u590d\u5b83\u3002<\/li>\n<li>\u6709\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u7528\u65ad\u8a00\u662f\u56e0\u4e3a\u5b83\u6bd4\u7cbe\u786e\u7684\u68c0\u67e5\u8981\u77ed\uff0c\u5b83\u4e0d\u5e94\u8be5\u662f\u61d2\u7801\u519c\u7684\u5077\u61d2\u65b9\u5f0f\u3002<\/li>\n<li>\u4e0d\u8981\u7528\u5b83\u6765\u68c0\u67e5\u5bf9\u516c\u5171\u5e93\u7684\u8f93\u5165\u53c2\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u63a7\u5236\u8c03\u7528\u8005\uff0c\u6240\u4ee5\u4e0d\u80fd\u4fdd\u8bc1\u8c03\u7528\u8005\u4f1a\u4e0d\u4f1a\u6253\u7834\u53cc\u65b9\u7684\u7ea6\u5b9a\u3002<\/li>\n<li><span style=\"color: #ff0000;\"><strong>\u4e0d\u8981\u4e3a\u4f60\u89c9\u5f97\u53ef\u4ee5\u6062\u590d\u7684\u9519\u8bef\u7528\u65ad\u8a00<\/strong><\/span>\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e0d\u7528\u6539\u5728\u4ea7\u54c1\u4ee3\u7801\u91cc\u6355\u6349\u5230\u65ad\u8a00\u9519\u8bef\u3002<\/li>\n<li>\u4e0d\u8981\u7528\u592a\u591a\u65ad\u8a00\u4ee5\u81f3\u4e8e\u8ba9\u4ee3\u7801\u5f88\u6666\u6da9\u3002<\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f18\u7531\uff1a \u5728\u901bblog\u7684\u65f6\u5019\u770b\u5230\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\u7528\u5230\u4e86\u4e0d\u5c11 assert \uff0c\u867d\u7136\u77e5\u9053\u5b57\u9762\u610f\u601d\uff0c\u4f46\u4e0d\u6e05\u695a\u5177\u4f53\u7684\u4f5c\u7528\u4ee5\u53ca [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[544,8],"class_list":["post-2452","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-assert","tag-python"],"views":2888,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2452"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2452\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}