{"id":2260,"date":"2020-12-07T13:03:49","date_gmt":"2020-12-07T04:03:49","guid":{"rendered":"http:\/\/141.164.34.82\/?p=2260"},"modified":"2020-12-07T13:11:50","modified_gmt":"2020-12-07T04:11:50","slug":"python-%eb%94%94%eb%b2%84%ea%b9%85-print-%ed%95%a8%ec%88%98-%ea%b0%80%eb%a1%9c%ec%b1%84%ea%b8%b0-2","status":"publish","type":"post","link":"http:\/\/ds.sumeun.org\/?p=2260","title":{"rendered":"Python \ub514\ubc84\uae45: `newprint` (ver 0.9)"},"content":{"rendered":"<ul>\n<li>\uc774 \uae00\uc740 &lt;<a href=\"http:\/\/141.164.34.82\/?p=2119\">\uc608\uc804 \uae00: Python \ub514\ubc84\uae45: `print` \ud568\uc218 \uac00\ub85c\ucc44\uae30<\/a>&gt;\uc744 \uc218\uc815\/\ubcf4\uc644\ud588\uc2b5\ub2c8\ub2e4.<\/li>\n<\/ul>\n<h2>\ub514\ubc84\uae45<\/h2>\n<blockquote><p>\ub514\ubc84\uae45\uc740 \ud504\ub85c\uadf8\ub798\uba38\uc758 \uc219\uba85\uc774\ub2e4.<\/p><\/blockquote>\n<p>\uc0ac\ub78c\ub4e4\uc740 \ud504\ub85c\uadf8\ub798\uba38\uac00 \ud558\ub294 \uc77c\uc758 80%\uac00 \ub514\ubc84\uae45\uc774\ub77c\uace0\ub3c4 \ud55c\ub2e4.<\/p>\n<h2>Python\uacfc \ub514\ubc84\uae45<\/h2>\n<p>\ud30c\uc774\uc36c\uc744 \uc4f0\uba74\uc11c \ubd88\ud3b8\ud55c \uc810\uc758 \ud558\ub098\ub294 <strong>\ub514\ubc84\uae45<\/strong>\uc774\ub2e4. \ud2b9\ud788 \uc2e4\ud589\ud558\ub294\ub370 \uc2dc\uac04\uc774 \ub9ce\uc774 \uac78\ub9ac\ub294 \ud504\ub85c\uadf8\ub7a8\uc758 \uacbd\uc6b0\ub294 \uc2e4\ud589 \uacfc\uc815\uc744 \uacc4\uc18d \uc549\uc544\uc11c \ubaa8\ub2c8\ud130\ub9c1\ud558\uae30 \ud798\ub4e4\uae30 \ub54c\ubb38\uc5d0 \uc5ec\ub7ec \uac00\uc9c0 \uc218\ub2e8\uc744 \ub9c8\ub828\ud574\uc57c \ud55c\ub2e4. \ud55c\uac00\uc9c0 \ubc29\ubc95\uc740 \ucd9c\ub825\uc744 \ud654\uc77c\ub85c \uc800\uc7a5\ud558\ub294 \uac83\uc774\ub2e4. \ud558\uc9c0\ub9cc \uc5b4\ub5bb\uac8c?<\/p>\n<h2>\uba87 \uac00\uc9c0 \uac00\ub2a5\uc131<\/h2>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/4675728\/redirect-stdout-to-a-file-in-python\">Redirect stout to a file in Python<\/a><\/li>\n<li>\uc720\ub2c9\uc2a4\ub97c \uc4f4\ub2e4\uba74, \uc720\ub2c9\uc2a4 \ucc28\uc6d0\uc5d0\uc11c \ubaa8\ub4e0 \ucd9c\ub825\uacfc \uc5d0\ub7ec \uba54\uc2dc\uc9c0\ub97c \ud654\uc77c\ub85c \ubcf4\ub0b8\ub2e4.<\/li>\n<\/ul>\n<pre>python3 -u foo.py 2&gt;&amp;1 | tee log.txt\r\n<\/pre>\n<ul>\n<li>Python <a href=\"https:\/\/docs.python.org\/ko\/3.7\/howto\/logging.html\">logging<\/a> module\ub97c \uc0ac\uc6a9\ud55c\ub2e4.<\/li>\n<\/ul>\n<p>\uc704\uc758 \ubc29\ubc95\ub4e4\uc744 \uc2dc\ub3c4\ud574 \ubcf8 \uacb0\uacfc, \ubaa8\ub450 \uadf8\ub2e4\uc9c0 \ub9cc\uc871\uc2a4\ub7fd\uc9c0 \ubabb\ud588\ub2e4.<\/p>\n<ul>\n<li>\ud45c\uc900\ucd9c\ub825 <code>stout<\/code>\uc744 \ud654\uc77c\ub85c \ubcf4\ub0b4\ub294 \uac83\uc740 \uc0dd\uac01\ubcf4\ub2e4 \uc798 \ub418\uc9c0 \uc54a\uc558\ub2e4.<\/li>\n<li><code>python3 -u foo.py 2&gt;&amp;1 | tee filename.txt<\/code>\uc758 \uacbd\uc6b0\ub294 \uac00\uc7a5 \uac04\ub2e8\ud558\uac8c \uc4f8 \uc218 \uc788\ub294 \ubc29\ubc95\uc774\uc9c0\ub9cc, \uc2e4\ud589 \uc2dc\uac04\uc774 \uae38\uc5b4\uc9c0\uba74 <code>filename.txt<\/code>\uac00 \uc6a9\ub7c9\uc774 \uc9c0\ub098\uce58\uac8c \ucee4\uc9c0\ub294 \ubb38\uc81c\uac00 \uc0dd\uae34\ub2e4.<\/li>\n<li><code>logging<\/code>\uc740 \uc88b\uc740 \ubc29\ubc95\uc774\uc9c0\ub9cc, \uae30\uc874\uc758 \uc18c\uc2a4\ub97c \uace0\uccd0\uc57c \ud55c\ub2e4\ub294 \ubb38\uc81c\uac00 \uc788\ub2e4. \uadf8\ub9ac\uace0 <code>logging<\/code>\uc744 \uc0ac\uc6a9\ud558\ub294 \ubc29\ubc95\uc744 \ubc30\uc6cc\uc57c \ud55c\ub2e4!<\/li>\n<\/ul>\n<h2><code>newprint.py<\/code><\/h2>\n<p>\uadf8\ub798\uc11c \uae30\uc874\uc758 <code>print<\/code>\ub97c \uadf8\ub300\ub85c \uc4f8 \uc218 \uc788\ub294 \ubc29\ubc95\uc744 \uace0\uc548\ud558\uac8c \ub418\uc5c8\ub2e4. \ub2e4\uc74c\uc744 <code>newprint.py<\/code>\ub85c \uc800\uc7a5\ud55c \ud6c4, <code>print<\/code>\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub514\ubc84\uae45\uc5d0 \ud544\uc694\ud55c \ub0b4\uc6a9\uc744 \ud654\uba74\uacfc \ud654\uc77c\uc5d0 \ub3d9\uc2dc\uc5d0 \ucd9c\ub825\ud558\uace0\uc790 \ud560 \ub54c, \ucf54\ub4dc\uc758 \uccab \uba38\ub9ac\uc5d0 <code>import newprint<\/code>, <code>from newprint import print<\/code>\ub9cc \uc0bd\uc785\ud558\uba74, <code>print<\/code> \ub0b4\uc6a9\uc774 <code>log_\ub144-\uc6d4-\uc77c.py<\/code>\ub85c \uc800\uc7a5\ub41c\ub2e4.<\/p>\n<pre># save the following as newprint.py\r\n\r\nfrom datetime import datetime, date\r\nimport sys\r\nprint0 = print\r\n\r\ndef print2(*argv, sep=' ', end='\\n', file=sys.stdout, flush=False, **kwarg):\r\n    \"\"\"\r\n    python\uc758 print\ub97c \uac00\ub85c\ucc44\uc11c \ud654\uba74\uacfc log file\uc5d0 \ub3d9\uc2dc\uc5d0 \ucd9c\ub825\ud569\ub2c8\ub2e4. \r\n    version 0.9(2020-12-07)\r\n\r\n    Args:\r\n        end : what will be printed at the end\r\n        file : log filename\r\n\r\n\r\n    \"\"\"    \r\n\r\n\r\n    file_debug = kwarg.pop('file_debug', fnDebug)\r\n    if file_debug != fnDebug:\r\n        f_debug = open(file_debug, 'a')\r\n    else:\r\n        f_debug = fDebug\r\n    #file = kwarg.pop('file', sys.stdout)\r\n    if len(kwarg.keys()) &gt; 0:\r\n        raise ValueError('Unknown arguments found ' + ''.join(kwarg.keys()))\r\n\r\n    if not noprint_display and file is sys.stdout:\r\n        print0(*argv, sep = sep, end = end, file=file, flush = flush, **kwarg)\r\n    if not noprint_file and file is not sys.stdout:  \r\n        print0(*argv, sep = sep, end = end, file=file, flush = flush, **kwarg)\r\n    if not noprint_debug:\r\n        print0(*argv, sep = sep, end = end, file=f_debug, flush = flush, **kwarg, )\r\n\r\n\r\n# debug = True\r\n# if debug:        \r\nprint = print2\r\n\r\ntoday = date.today()\r\nfnDebug = 'log_'+today.strftime(\"%Y-%m-%d\")+'.txt'\r\nfDebug = open(fnDebug, 'a')\r\n\r\nnoprint_display = False\r\nnoprint_file = False\r\nnoprint_debug = False\r\n<\/pre>\n<p>\uc608\ub97c \ub4e4\uc5b4 \ub2e4\uc74c\uacfc \uac19\uc774 \uc4f0\ub294 \uac83\uc774\ub2e4.<\/p>\n<pre>import newprint\r\nfrom newprint import print0, print2, print\r\n\r\nfrom datetime import datetime, date\r\ntoday = date.today()\r\nprint('Current time : ', today)\r\n<\/pre>\n<h3><code>log<\/code> \ud654\uc77c \ubcc0\uacbd<\/h3>\n<p>\ub9cc\uc57d \ud504\ub85c\uadf8\ub7a8\uc774 \ud558\ub8e8\ub97c \ub118\uc5b4 \uc2e4\ud589\ub418\uc5b4 <code>log-().txt<\/code>\uc758 \ub0a0\uc9dc\ub97c \ubc14\uafd4\uc57c \ud55c\ub2e4\uba74 \ub2e4\uc74c\uc758 \ub8e8\ud2f4\uc73c\ub85c \uac00\ub2a5\ud558\ub2e4.<\/p>\n<pre>today = date.today()\r\nif (today &gt; newprint.today):\r\n    newprint.fDebug.close() \r\n    newprint.today = today\r\n    newprint.fnDebug = 'log_'+today.strftime(\"%Y-%m-%d\")+'.txt'\r\n    newprint.fDebug = open(newprint.fnDebug, 'a')   \r\n<\/pre>\n<h3>\uadf8 \ubc16\uc758 \uc124\uc815<\/h3>\n<ul>\n<li><code>newprint.noprint_display = True<\/code> : <code>cron<\/code>\uc5d0 \uc0ac\uc6a9\ub41c\ub2e4\uba74, \uc2a4\ud06c\ub9b0\uc5d0 \ucd9c\ub825\ub418\ub294 \uc544\uc6c3\ud48b \uc790\uccb4\ub97c \uc18c\uac70\ud558\ub294 \uac8c \ub098\uc744 \uc218 \uc788\ub2e4.<\/li>\n<li><code>newprint.noprint_file = True<\/code> : file\ub85c \ucd9c\ub825\ub418\ub294 \ubd80\ubd84\ub9cc \uc18c\uac70\ud55c\ub2e4<\/li>\n<li><code>newprint.noprint_debug = True<\/code> : log \ud654\uc77c\ub9cc \uc5b5\uc81c\ud55c\ub2e4.<\/li>\n<\/ul>\n<h3>\ud14c\uc2a4\ud305<\/h3>\n<pre>import newprint\r\nfrom newprint import print0, print2, print\r\n\r\nnewprint.fnDebug='test-newprint-debug.txt'\r\nnewprint.fDebug=open(newprint.fnDebug, 'a')\r\n\r\nfile = open('test-newprint-file.txt', 'a')\r\n\r\nprint('*** testing_newprint')\r\nprint('*** testing_newprint', file=file)\r\nprint('*** testing_newprint', file_debug='test-newprint-debug2.txt')\r\n\r\nnewprint.noprint_display = True\r\n\r\nprint('testing_newprint with noprint_display')\r\nprint('testing_newprint with noprint_display', file=file)\r\nprint('testing_newprint with noprint_display', file_debug='test-newprint-debug2.txt')\r\n\r\nnewprint.noprint_display = False\r\nnewprint.noprint_file = True\r\n\r\nprint('testing_newprint with noprint_file')\r\nprint('testing_newprint with noprint_file', file=file)\r\nprint('testing_newprint with noprint_file', file_debug='test-newprint-debug2.txt')\r\n\r\nnewprint.noprint_display = True\r\nnewprint.noprint_file = True\r\n\r\nprint('testing_newprint with noprint_file and noprint_display')\r\nprint('testing_newprint with noprint_file and noprint_display', file=file)\r\nprint('testing_newprint with noprint_file and noprint_display', file_debug='test-newprint-debug2.txt')\r\n\r\nnewprint.noprint_display = False\r\nnewprint.noprint_file = False\r\nnewprint.noprint_debug = True\r\nprint('testing_newprint with noprint_debug')\r\nprint('testing_newprint with noprint_debug', file=file)\r\nprint('testing_newprint with noprint_debug', file_debug='test-newprint-debug2.txt')\r\n\r\nprint = print0 # back to original print\r\nprint('test ending: check the files...')\r\nprint('- test-newprint-debug.txt')\r\nprint('- test-newprint-debug2.txt')\r\nprint('- test-newprint-file.txt')```\r\n<\/pre>\n<h2>\ub9c8\ubb34\ub9ac<\/h2>\n<ul>\n<li>\ud639\uc2dc \uc880 \ub354 \ub098\uc740 \ubc29\ubc95\uc774 \uc788\ub2e4\uba74 \ub313\uae00\ub85c \uc54c\ub824\uc8fc\uc138\uc694!<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\uc774 \uae00\uc740 &lt;\uc608\uc804 \uae00: Python \ub514\ubc84\uae45: `print` \ud568\uc218 \uac00\ub85c\ucc44\uae30&gt;\uc744 \uc218\uc815\/\ubcf4\uc644\ud588\uc2b5\ub2c8\ub2e4. \ub514\ubc84\uae45 \ub514\ubc84\uae45\uc740 \ud504\ub85c\uadf8\ub798\uba38\uc758 \uc219\uba85\uc774\ub2e4. \uc0ac\ub78c\ub4e4\uc740 \ud504\ub85c\uadf8\ub798\uba38\uac00 \ud558\ub294 \uc77c\uc758 80%\uac00 \ub514\ubc84\uae45\uc774\ub77c\uace0\ub3c4 \ud55c\ub2e4. Python\uacfc \ub514\ubc84\uae45 \ud30c\uc774\uc36c\uc744 \uc4f0\uba74\uc11c \ubd88\ud3b8\ud55c \uc810\uc758 \ud558\ub098\ub294 \ub514\ubc84\uae45\uc774\ub2e4. \ud2b9\ud788 \uc2e4\ud589\ud558\ub294\ub370 \uc2dc\uac04\uc774 \ub9ce\uc774 \uac78\ub9ac\ub294 \ud504\ub85c\uadf8\ub7a8\uc758 \uacbd\uc6b0\ub294 \uc2e4\ud589 \uacfc\uc815\uc744 \uacc4\uc18d \uc549\uc544\uc11c \ubaa8\ub2c8\ud130\ub9c1\ud558\uae30 \ud798\ub4e4\uae30 \ub54c\ubb38\uc5d0 \uc5ec\ub7ec \uac00\uc9c0 \uc218\ub2e8\uc744 \ub9c8\ub828\ud574\uc57c \ud55c\ub2e4. \ud55c\uac00\uc9c0 \ubc29\ubc95\uc740 \ucd9c\ub825\uc744 \ud654\uc77c\ub85c \uc800\uc7a5\ud558\ub294 \uac83\uc774\ub2e4. \ud558\uc9c0\ub9cc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2122,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[264],"tags":[432,340,433,435,434,436],"jetpack_featured_media_url":"http:\/\/ds.sumeun.org\/wp-content\/uploads\/2020\/01\/newprint.png","_links":{"self":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2260"}],"collection":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2260"}],"version-history":[{"count":4,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2260\/revisions"}],"predecessor-version":[{"id":2264,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/posts\/2260\/revisions\/2264"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=\/wp\/v2\/media\/2122"}],"wp:attachment":[{"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2260"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ds.sumeun.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}