{"id":187482,"date":"2013-07-23T19:30:03","date_gmt":"2013-07-23T15:30:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=187482"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=187482","title":{"rendered":"<span class=\"post_title\">Python: \u0437\u0430\u0434\u0435\u043a\u043e\u0440\u0438\u0440\u0443\u0435\u043c-\u043a\u0430 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b. \u0421\u043d\u043e\u0432\u0430<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0433\u043e\u0434\u0443 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0432 <a href=\"http:\/\/habrahabr.ru\/post\/141411\/\">\u0434\u0432\u0443\u0445<\/a> <a href=\"http:\/\/habrahabr.ru\/post\/141501\/\">\u0447\u0430\u0441\u0442\u044f\u0445<\/a> \u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430\u0445. \u0426\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 <i>cut to the chase<\/i> \u0438 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c\u0438, \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0435\u0449\u0451 \u0431\u043e\u043b\u0435\u0435 \u043c\u0443\u0434\u0440\u0451\u043d\u044b\u0445, \u0447\u0435\u043c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445.<br \/>  \u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u044f \u2014 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b, \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e C#) \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0432\u044b\u0441\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0438 \u0441 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u043c\u0438, \u043d\u043e \u043f\u0440\u0438\u0432\u044b\u043a\u0448\u0438\u0435, \u0447\u0442\u043e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u2014 \u044d\u0442\u043e \u00ab\u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f\u00bb, \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0438. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u041f\u0438\u0442\u043e\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0431\u0440\u043e\u0441\u0430\u044e\u0449\u0430\u044f\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u0442\u0430\u043a\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u2014 \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0434 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c <i>\u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/i> \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/7ab\/96e\/ebb\/7ab96eebbd0f8788d92860c9c04a57b1.png\"\/><\/p>\n<p>  \u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? <a name=\"habracut\"><\/a> \u041d\u0438\u0447\u0435\u0433\u043e \u0445\u0438\u0442\u0440\u043e\u0433\u043e: \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u0443\u0435\u043c\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u00ab\u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u0443\u044e\u00bb:<\/p>\n<pre><code class=\"python\">def timed(fn):     def decorated(*x):         start = time()         result = fn(*x)         print &quot;Executing %s took %d ms&quot; % (fn.__name__, (time()-start)*1000)         return result     return decorated  @timed def cpuload():     load = psutil.cpu_percent()     print &quot;cpuload() returns %d&quot; % load     return load  print &quot;cpuload.__name__==&quot; + cpuload.__name__ print &quot;CPU load is %d%%&quot; % cpuload() <\/code><\/pre>\n<p><sup>(<a href=\"http:\/\/pastebin.com\/1fFX4hJU\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a>)<\/sup>  <\/p>\n<blockquote>\n<pre> cpuload.__name__==decorated cpuload() returns 16 Executing cpuload took 105 ms CPU load is 16% <\/pre>\n<\/blockquote>\n<p> \u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <code>@timed def cpuload(): ...<\/code> \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 <code>def cpuload(): ...; cpuload=timed(cpuload)<\/code>, \u0442\u0430\u043a \u0447\u0442\u043e \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u043c\u044f <code>cpuload<\/code> \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>decorated<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>timed<\/code>, \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u043e\u0439 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>cpuload<\/code> \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e <code>fn<\/code>. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u0438 \u0432\u0438\u0434\u0438\u043c <code>cpuload.__name__==decorated<\/code><\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043b\u044e\u0431\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u00ab\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438\u00bb (\u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432):<\/p>\n<pre><code class=\"python\">def repeat(times):     &quot;&quot;&quot; \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 times \u0440\u0430\u0437, \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 &quot;&quot;&quot;     def decorator(fn):         def decorated2(*x):             total = 0             for i in range(times):                 total += fn(*x)             return total \/ times         return decorated2     return decorator  @repeat(5) def cpuload():     &quot;&quot;&quot; \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 cpuload \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c &quot;&quot;&quot;  print &quot;cpuload.__name__==&quot; + cpuload.__name__ print &quot;CPU load is %d%%&quot; % cpuload() <\/code><\/pre>\n<p><sup>(<a href=\"http:\/\/pastebin.com\/4XELYWhB\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a>)<\/sup>  <\/p>\n<blockquote>\n<pre> cpuload.__name__==decorated2 cpuload() returns 7 cpuload() returns 16 cpuload() returns 0 cpuload() returns 0 cpuload() returns 33 CPU load is 11% <\/pre>\n<\/blockquote>\n<p> \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f <code>repeat(5)<\/code> \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>decorator<\/code>, \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u0430\u044f \u043d\u0430 <code>times=5<\/code>. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430; \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u043c\u0435\u0435\u043c <code>def cpuload(): ...; cpuload=repeat(5)(cpuload)<\/code><\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u2014 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e. \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0432 <code>@timed @repeat(5) def cpuload():<\/code> \u2014 \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c  <\/p>\n<blockquote>\n<pre> cpuload.__name__==decorated cpuload() returns 28 cpuload() returns 16 cpuload() returns 0 cpuload() returns 0 cpuload() returns 0 Executing decorated2 took 503 ms CPU load is 9% <\/pre>\n<\/blockquote>\n<p> \u0410 \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u2014 <code>@repeat(5) @timed def cpuload():<\/code> \u2014 \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c  <\/p>\n<blockquote>\n<pre> cpuload.__name__==decorated2 cpuload() returns 16 Executing cpuload took 100 ms cpuload() returns 14 Executing cpuload took 109 ms cpuload() returns 0 Executing cpuload took 101 ms cpuload() returns 0 Executing cpuload took 100 ms cpuload() returns 0 Executing cpuload took 99 ms CPU load is 6% <\/pre>\n<\/blockquote>\n<p> \u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u043b\u043e\u0441\u044c \u0432 <code>cpuload=timed(repeat(5)(cpuload))<\/code>, \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u2014 \u0432 <code>cpuload=repeat(5)(timed(cpuload))<\/code>. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0438 \u043d\u0430 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439: \u043f\u043e \u043d\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0432 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0446\u0438\u0438 \u2014 <u>\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440, \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440<\/u>:  <\/p>\n<pre><code class=\"python\">def toggle(decorator):     &quot;&quot;&quot; \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c &quot;\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c&quot; \u0438 &quot;\u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0442\u044c&quot; \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 &quot;&quot;&quot;     def new_decorator(fn):         decorated = decorator(fn)          def new_decorated(*x):             if decorator.enabled:                 return decorated(*x)             else:                 return fn(*x)          return new_decorated      decorator.enabled = True     return new_decorator  @toggle(timed) def cpuload():     &quot;&quot;&quot; \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 cpuload \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c &quot;&quot;&quot;  print &quot;cpuload.__name__==&quot; + cpuload.__name__ print &quot;CPU load is %d%%&quot; % cpuload() timed.enabled = False print &quot;CPU load is %d%%&quot; % cpuload() <\/code><\/pre>\n<p><sup>(<a href=\"http:\/\/pastebin.com\/JkEFdt1P\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a>)<\/sup>  <\/p>\n<blockquote>\n<pre> cpuload.__name__==new_decorated cpuload() returns 28 Executing cpuload took 101 ms CPU load is 28% cpuload() returns 0 CPU load is 0% <\/pre>\n<\/blockquote>\n<p> \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c\/\u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0435 <code>enabled<\/code> \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u041f\u0438\u0442\u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u00ab\u043d\u0430\u043b\u0435\u043f\u0438\u0442\u044c\u00bb \u043d\u0430 \u043b\u044e\u0431\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b.<\/p>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>toggle<\/code> \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 <u>\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432<\/u>:<\/p>\n<pre><code class=\"python\">@toggle def timed(fn):     &quot;&quot;&quot; \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 timed \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c &quot;&quot;&quot;  @toggle def repeat(times):     &quot;&quot;&quot; \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 repeat \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c &quot;&quot;&quot;  @timed @repeat(5) def cpuload():     &quot;&quot;&quot; \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 cpuload \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c &quot;&quot;&quot;  print &quot;cpuload.__name__==&quot; + cpuload.__name__ print &quot;CPU load is %d%%&quot; % cpuload() timed.enabled = False print &quot;CPU load is %d%%&quot; % cpuload() <\/code><\/pre>\n<p><sup>(<a href=\"http:\/\/pastebin.com\/1VFvS7jf\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a>)<\/sup>  <\/p>\n<blockquote>\n<pre> cpuload.__name__==new_decorated cpuload() returns 28 cpuload() returns 0 cpuload() returns 0 cpuload() returns 0 cpuload() returns 0 Executing decorated2 took 501 ms CPU load is 5% cpuload() returns 0 cpuload() returns 16 cpuload() returns 14 cpuload() returns 16 cpuload() returns 0 Executing decorated2 took 500 ms CPU load is 9% <\/pre>\n<\/blockquote>\n<p> \u0413\u043c\u2026 \u043d\u0435\u0442, \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e! \u041d\u043e \u043f\u043e\u0447\u0435\u043c\u0443?<br \/>  \u041f\u043e\u0447\u0435\u043c\u0443 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>timed<\/code> \u043d\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0441\u044f \u043f\u0440\u0438 \u0432\u0442\u043e\u0440\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 <code>cpuload<\/code>?<\/p>\n<p>  \u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u043c\u044f <code>timed<\/code> \u0443 \u043d\u0430\u0441 \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c, \u0442.\u0435. \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>new_decorated<\/code>; \u0437\u043d\u0430\u0447\u0438\u0442, \u0441\u0442\u0440\u043e\u0447\u043a\u0430 <code>timed.enabled = False<\/code> \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>new_decorated<\/code> \u2014 \u043e\u0431\u0449\u0435\u0439 \u00ab\u043e\u0431\u0451\u0440\u0442\u043a\u0438\u00bb \u043e\u0431\u043e\u0438\u0445 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u0432. \u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0432\u043d\u0443\u0442\u0440\u0438 <code>new_decorated<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>if decorator.enabled:<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c <code>if new_decorator.enabled:<\/code>, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 <code>timed.enabled = False<\/code> \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u0430 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430.<\/p>\n<p>  \u0418\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u044d\u0442\u043e\u0442 \u0431\u0430\u0433: \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c <code>enabled<\/code> \u043d\u0430 \u00ab\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u043c\u00bb \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0435, \u043a\u0430\u043a \u0438 \u043f\u0440\u0435\u0436\u0434\u0435 \u2014 \u043d\u0430\u043b\u0435\u043f\u0438\u043c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>new_decorated<\/code> \u043f\u0430\u0440\u0443 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:<\/p>\n<pre><code class=\"python\">def toggle(decorator):     &quot;&quot;&quot; \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c &quot;\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c&quot; \u0438 &quot;\u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0442\u044c&quot; \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 &quot;&quot;&quot;     def new_decorator(fn):         decorated = decorator(fn)          def new_decorated(*x): # \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439             if decorator.enabled:                 return decorated(*x)             else:                 return fn(*x)          return new_decorated      def enable():         decorator.enabled = True     def disable():         decorator.enabled = False     new_decorator.enable = enable     new_decorator.disable = disable     enable()     return new_decorator  print &quot;cpuload.__name__==&quot; + cpuload.__name__ print &quot;CPU load is %d%%&quot; % cpuload() timed.disable() print &quot;CPU load is %d%%&quot; % cpuload() <\/code><\/pre>\n<p><sup>(<a href=\"http:\/\/pastebin.com\/nbYks8mY\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a \u0446\u0435\u043b\u0438\u043a\u043e\u043c<\/a>)<\/sup><br \/>  \u0416\u0435\u043b\u0430\u0435\u043c\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442 \u2014 <code>timed<\/code> \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u043b\u0441\u044f, \u043d\u043e <code>repeat<\/code> \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:  <\/p>\n<blockquote>\n<pre> cpuload.__name__==new_decorated cpuload() returns 14 cpuload() returns 16 cpuload() returns 0 cpuload() returns 0 cpuload() returns 0 Executing decorated2 took 503 ms<\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/079\/bfd\/fd9\/079bfdfd9a0bba366e511e2ceeb1ca5e.jpg\" align=\"right\"\/><\/p>\n<pre> CPU load is 6% cpuload() returns 0 cpuload() returns 0 cpuload() returns 7 cpuload() returns 0 cpuload() returns 0 CPU load is 1% <\/pre>\n<\/blockquote>\n<p> \u042d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u043e\u0447\u0430\u0440\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u041f\u0438\u0442\u043e\u043d\u0430 \u2014 \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b, \u043d\u043e \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043c\u0435\u0442\u043e\u0434\u044b. \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0441\u0438\u0434\u044f\u0442 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043f\u043e\u0433\u043e\u043d\u044f\u044e\u0442.    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/187482\/\"> http:\/\/habrahabr.ru\/post\/187482\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0433\u043e\u0434\u0443 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0431\u044b\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u0440\u0430\u0437\u0432\u0451\u0440\u043d\u0443\u0442\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0432 <a href=\"http:\/\/habrahabr.ru\/post\/141411\/\">\u0434\u0432\u0443\u0445<\/a> <a href=\"http:\/\/habrahabr.ru\/post\/141501\/\">\u0447\u0430\u0441\u0442\u044f\u0445<\/a> \u043e \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430\u0445. \u0426\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u2014 <i>cut to the chase<\/i> \u0438 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u043d\u044f\u0442\u044c\u0441\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c\u0438, \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0435\u0449\u0451 \u0431\u043e\u043b\u0435\u0435 \u043c\u0443\u0434\u0440\u0451\u043d\u044b\u0445, \u0447\u0435\u043c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0430\u0442\u044c\u044f\u0445.<br \/>  \u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u044f \u2014 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b, \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e C#) \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0432\u044b\u0441\u0448\u0435\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0438 \u0441 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f\u043c\u0438, \u043d\u043e \u043f\u0440\u0438\u0432\u044b\u043a\u0448\u0438\u0435, \u0447\u0442\u043e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u2014 \u044d\u0442\u043e \u00ab\u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f\u00bb, \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0438. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u041f\u0438\u0442\u043e\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0431\u0440\u043e\u0441\u0430\u044e\u0449\u0430\u044f\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u0442\u0430\u043a\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c \u2014 \u0442\u043e, \u0447\u0442\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0434 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c <i>\u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/i> \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/7ab\/96e\/ebb\/7ab96eebbd0f8788d92860c9c04a57b1.png\"\/><\/p>\n<p>  \u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442? <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-187482","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/187482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187482"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/187482\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}