{"id":297177,"date":"2020-01-15T15:00:27","date_gmt":"2020-01-15T15:00:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297177"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297177","title":{"rendered":"\u041f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 @pythonetc, \u0434\u0435\u043a\u0430\u0431\u0440\u044c 2019"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/mailru\/blog\/484004\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lg\/iw\/08\/lgiw08nne7atizbiqlxpyucngnq.jpeg\"><\/p>\n<p>  \u041d\u043e\u0432\u0430\u044f \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u0440\u043e Python \u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043c\u043e\u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430 @pythonetc.<\/p>\n<p>  \u2190 <a href=\"https:\/\/habr.com\/ru\/search\/?q=pythonetc#h\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438<\/a><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0440\u0430\u0437\u043d\u044b\u0435 <code>asyncio<\/code>-\u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0442\u0435\u043a\u0438. \u041c\u043e\u0436\u043d\u043e \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0438\u0445 \u0432\u0441\u0435, \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>asyncio.all_tasks()<\/code> \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0441\u0435\u0439\u0447\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0438, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>task.get_stack()<\/code> \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0441\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"python\">import linecache import asyncio import random   async def producer(queue):     while True:         await queue.put(random.random())         await asyncio.sleep(0.01)  async def avg_printer(queue):     total = 0     cnt = 0     while True:         while queue.qsize():             x = await queue.get()             total += x             cnt += 1             queue.task_done()         print(total \/ cnt)         await asyncio.sleep(1)  async def monitor():     while True:         await asyncio.sleep(1.9)         for task in asyncio.all_tasks():             if task is not asyncio.current_task():                 f  = task.get_stack()[-1]                 last_line = linecache.getline(                     f.f_code.co_filename,                     f.f_lineno,                     f.f_globals,                 )                 print(task)                 print('\\t', last_line.strip())          print()  async def main():     loop = asyncio.get_event_loop()      queue = asyncio.Queue()      loop.create_task(producer(queue))     loop.create_task(producer(queue))     loop.create_task(producer(queue))      loop.create_task(avg_printer(queue))     loop.create_task(monitor())    loop = asyncio.get_event_loop() loop.create_task(main()) loop.run_forever()<\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043c\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c \u0441\u0442\u0435\u043a\u0430 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c <code>linecache<\/code>, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>task.print_stack()<\/code>.<\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>translate<\/code> \u0438\u0437 <code>str<\/code> \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 (\u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 <code>tr<\/code>):<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; 'Hello, world!'.translate({ ...     ord(','): ';', ...     ord('o'): '0', ... }) 'Hell0; w0rld!'<\/code><\/pre>\n<p>  \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c <code>translate<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043a\u043e\u0434\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441 \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c\u0438 (\u0438\u043b\u0438 \u043a\u043e\u0434\u0430\u043c). \u041e\u0431\u044b\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 <code>str.maketrans<\/code>:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; 'Hello, world!'.translate(str.maketrans({ ...     ',': ';', ...     'o': '0', ... })) 'Hell0; w0rld!'<\/code><\/pre>\n<p>  \u0418\u043b\u0438 \u0434\u0430\u0436\u0435:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; 'Hello, world!'.translate(str.maketrans( ...     ',o', ';0' ... )) 'Hell0; w0rld!'<\/code><\/pre>\n<p>  \u0422\u0440\u0435\u0442\u0438\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; tr = str.maketrans(',o', ';0', '!') &gt;&gt;&gt; tr {44: 59, 111: 48, 33: None} &gt;&gt;&gt; 'Hello, world!'.translate(tr) 'Hell0; w0rld'<\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  <code>mypy<\/code> \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432:<\/p>\n<pre><code class=\"python\">from typing import Optional, Dict from pathlib import Path  TreeDict = Dict[str, 'TreeDict']   def tree(path: Path) -&gt; TreeDict:     return {         f.name: tree(f) if f.is_dir() else None         for f in path.iterdir()     }<\/code><\/pre>\n<p>  \u041f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435: <code>Cannot resolve name \"TreeDict\" (possible cyclic definition)<\/code>.<\/p>\n<p>  \u0421\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/github.com\/python\/mypy\/issues\/731\">https:\/\/github.com\/python\/mypy\/issues\/731<\/a><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u0442\u0430\u043b\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439, \u0435\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f. \u041d\u043e \u0441 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438 \u043d\u0435 \u0432\u0441\u0451 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e: \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>yield from<\/code> \u0438\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432:<\/p>\n<pre><code class=\"python\">from operator import itemgetter   tree = {     'imgs': {         '1.png': None,         '2.png': None,         'photos': {             'me.jpg': None         },     },     'MANIFEST': None, }   def flatten_tree(tree):     for name, children in sorted(         tree.items(),         key=itemgetter(0)     ):         yield name         if children:             yield from flatten_tree(children)   print(list(flatten_tree(tree)))<\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>for<\/code> \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438, \u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0441 \u043b\u044e\u0431\u044b\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c. \u041e\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; log2 = {} &gt;&gt;&gt; key = 1 &gt;&gt;&gt; for log2[key] in range(100): ...     key *= 2 ... &gt;&gt;&gt; log2[16] 4 &gt;&gt;&gt; log2[1024] 10 <\/code><\/pre>\n<\/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=\"https:\/\/habr.com\/ru\/company\/mailru\/blog\/484004\/\"> https:\/\/habr.com\/ru\/company\/mailru\/blog\/484004\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/mailru\/blog\/484004\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/lg\/iw\/08\/lgiw08nne7atizbiqlxpyucngnq.jpeg\"><\/p>\n<p>  \u041d\u043e\u0432\u0430\u044f \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u0440\u043e Python \u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043c\u043e\u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430 @pythonetc.<\/p>\n<p>  \u2190 <a href=\"https:\/\/habr.com\/ru\/search\/?q=pythonetc#h\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438<\/a><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0440\u0430\u0437\u043d\u044b\u0435 <code>asyncio<\/code>-\u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0442\u0435\u043a\u0438. \u041c\u043e\u0436\u043d\u043e \u0432 \u043b\u044e\u0431\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0438\u0445 \u0432\u0441\u0435, \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>asyncio.all_tasks()<\/code> \u0432\u0441\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0441\u0435\u0439\u0447\u0430\u0441 \u0437\u0430\u0434\u0430\u0447\u0438, \u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>task.get_stack()<\/code> \u043f\u043e\u043b\u0443\u0447\u0438\u0432 \u0441\u0442\u0435\u043a\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.  <\/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-297177","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297177","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=297177"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297177\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}