{"id":145121,"date":"2019-05-26T08:11:29","date_gmt":"2019-05-26T00:11:29","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=145121"},"modified":"2019-05-20T10:14:14","modified_gmt":"2019-05-20T02:14:14","slug":"use-black-tool","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/use-black-tool.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Black \u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177"},"content":{"rendered":"

python \u662f\u5f53\u4eca\u4f7f\u7528\u6700\u591a\u7684\u6d41\u884c\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\uff0c\u56e0\u4e3a\uff1a\u5b83\u662f\u5f00\u6e90\u7684\uff0c\u5b83\u6709\u5e7f\u6cdb\u7684\u7528\u9014\uff08\u4f8b\u5982 Web \u7f16\u7a0b\u3001\u4e1a\u52a1\u5e94\u7528\u3001\u6e38\u620f\u3001\u79d1\u5b66\u7f16\u7a0b\u7b49\u7b49\uff09\uff0c\u5b83\u6709\u4e00\u4e2a\u5145\u6ee1\u6d3b\u529b\u548c\u4e13\u6ce8\u7684\u793e\u533a\u652f\u6301\u5b83\u3002\u8fd9\u4e2a\u793e\u533a\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728 Python Package Index\uff08PyPI\uff09\u4e2d\u6709\u5982\u6b64\u5e9e\u5927\u3001\u591a\u6837\u5316\u7684\u8f6f\u4ef6\u5305\uff0c\u7528\u4ee5\u6269\u5c55\u548c\u6539\u8fdb Python \u5e76\u89e3\u51b3\u4e0d\u53ef\u907f\u514d\u7684\u95ee\u9898\u3002<\/p>\n

\u5728\u672c\u7cfb\u5217\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e03\u4e2a\u53ef\u4ee5\u5e2e\u52a9\u4f60\u89e3\u51b3\u5e38\u89c1 Python \u95ee\u9898\u7684 PyPI \u5e93\u3002\u5728\u7b2c\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u4e86 Cython\u3002\u4eca\u5929\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 Black \u8fd9\u4e2a\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002<\/p>\n

Black<\/strong><\/div>\n

\u6709\u65f6\u521b\u610f\u53ef\u80fd\u662f\u4e00\u4ef6\u7f8e\u5999\u7684\u4e8b\u60c5\u3002\u6709\u65f6\u5b83\u53ea\u662f\u4e00\u79cd\u75db\u82e6\u3002\u6211\u559c\u6b22\u521b\u9020\u6027\u5730\u89e3\u51b3\u96be\u9898\uff0c\u4f46\u6211\u5e0c\u671b\u6211\u7684 Python \u683c\u5f0f\u5c3d\u53ef\u80fd\u4e00\u81f4\u3002\u6ca1\u6709\u4eba\u5bf9\u4f7f\u7528\u201c\u6709\u8da3\u201d\u7f29\u8fdb\u7684\u4ee3\u7801\u5370\u8c61\u6df1\u523b\u3002<\/p>\n

\u4f46\u662f\u6bd4\u4e0d\u4e00\u81f4\u7684\u683c\u5f0f\u66f4\u7cdf\u7cd5\u7684\u662f\u9664\u4e86\u68c0\u67e5\u683c\u5f0f\u4e4b\u5916\u4ec0\u4e48\u90fd\u6ca1\u6709\u505a\u7684\u4ee3\u7801\u5ba1\u67e5\u3002\u8fd9\u5bf9\u5ba1\u67e5\u8005\u6765\u8bf4\u5f88\u70e6\u4eba\uff0c\u5bf9\u4e8e\u88ab\u5ba1\u67e5\u8005\u6765\u8bf4\u751a\u81f3\u66f4\u70e6\u4eba\u3002\u5f53\u4f60\u7684 linter \u544a\u8bc9\u4f60\u4ee3\u7801\u7f29\u8fdb\u4e0d\u6b63\u786e\u65f6\uff0c\u4f46\u6ca1\u6709\u63d0\u793a\u6b63\u786e<\/em>\u7684\u7f29\u8fdb\u91cf\uff0c\u8fd9\u4e5f\u4f1a\u4ee4\u4eba\u6c14\u6124\u3002<\/p>\n

\u4f7f\u7528 Black\uff0c\u5b83\u4e0d\u4f1a\u544a\u8bc9\u4f60\u8981<\/em>\u505a\u4ec0\u4e48\uff0c\u5b83\u662f\u4e00\u4e2a\u4f18\u826f\u3001\u52e4\u594b\u7684\u673a\u5668\u4eba\uff1a\u5b83\u5c06\u4e3a\u4f60\u4fee\u590d\u4ee3\u7801\u3002<\/p>\n

\u8981\u4e86\u89e3\u5b83\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u8bf7\u968f\u610f\u5199\u4e00\u4e9b\u975e\u5e38\u4e0d\u4e00\u81f4\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\uff1a<\/p>\n

def add(a, b): return a+b\r\n\r\ndef mult(a, b):\r\nreturn \\\r\na * b<\/pre>\n

Black \u62b1\u6028\u4e86\u4e48\uff1f\u5e76\u6ca1\u6709\uff0c\u5b83\u4e3a\u4f60\u4fee\u590d\u4e86\uff01<\/p>\n

$ black math \r\nreformatted math\r\nAll done! ✨ 🍰 ✨\r\n1 file reformatted.\r\n$ cat math \r\ndef add(a, b):\r\nreturn a + b\r\ndef mult(a, b):\r\nreturn a * b<\/pre>\n

Black \u786e\u5b9e\u63d0\u4f9b\u4e86\u62a5\u9519\u800c\u4e0d\u662f\u4fee\u590d\u7684\u9009\u9879\uff0c\u751a\u81f3\u8fd8\u6709\u8f93\u51fa diff \u7f16\u8f91\u6837\u5f0f\u7684\u9009\u9879\u3002\u8fd9\u4e9b\u9009\u9879\u5728\u6301\u7eed\u96c6\u6210 \uff08CI\uff09\u7cfb\u7edf\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u5728\u672c\u5730\u5f3a\u5236\u8fd0\u884c Black\u3002\u6b64\u5916\uff0c\u5982\u679c diff \u8f93\u51fa\u88ab\u8bb0\u5f55\u5230 CI \u8f93\u51fa\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5c06\u5176\u7c98\u8d34\u5230 patch \u4e2d\uff0c\u4ee5\u4fbf\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u4f60\u9700\u8981\u4fee\u590d\u8f93\u51fa\uff0c\u4f46\u65e0\u6cd5\u672c\u5730\u5b89\u88c5 Black \u4f7f\u7528\u3002<\/p>\n

$ black --check --diff bad \r\n--- math 2019-04-09 17:24:22.747815 +0000\r\n+++ math 2019-04-09 17:26:04.269451 +0000\r\n@@ -1,7 +1,7 @@\r\n-def add(a, b): return a + b\r\n+def add(a, b):\r\n+ return a + b\r\n\r\ndef mult(a, b):\r\n- return \\\r\n- a * b\r\n+ return a * b\r\n\r\nwould reformat math\r\nAll done! 💥 💔 💥\r\n1 file would be reformatted.\r\n$ echo $?\r\n1<\/pre>\n

\u5728\u672c\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd attrs \uff0c\u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u7f16\u5199\u7b80\u6d01\u3001\u6b63\u786e\u7684\u4ee3\u7801\u7684\u5e93\u3002<\/p>\n


\n

via: https:\/\/opensource.com\/article\/19\/5\/python-black<\/a><\/p>\n

\u4f5c\u8005\uff1aMoshe Zadka<\/a> \u9009\u9898\uff1alujun9972<\/a> \u8bd1\u8005\uff1ageekpi<\/a> \u6821\u5bf9\uff1awxy<\/a><\/p>\n

\u672c\u6587\u7531 LCTT<\/a> \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a> \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

python \u662f\u5f53\u4eca\u4f7f\u7528\u6700\u591a\u7684\u6d41\u884c\u7f16\u7a0b\u8bed\u8a00\u4e4b\u4e00\uff0c\u56e0\u4e3a\uff1a\u5b83\u662f\u5f00\u6e90\u7684\uff0c\u5b83\u6709\u5e7f\u6cdb\u7684\u7528\u9014\uff08\u4f8b\u5982 Web \u7f16\u7a0b\u3001\u4e1a\u52a1\u5e94\u7528 […]<\/p>\n","protected":false},"author":323,"featured_media":111901,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-145121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/323"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=145121"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145121\/revisions"}],"predecessor-version":[{"id":145334,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/145121\/revisions\/145334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/111901"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=145121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=145121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=145121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}