
यह मेरे टेलीग्राम-चैनल @pythonetc से पायथन और प्रोग्रामिंग के बारे में सुझावों और ट्रिक्स का एक नया चयन है।
Ations
पिछला प्रकाशन ।
अलग-अलग
asyncio
कार्यों में स्पष्ट रूप से अलग-अलग ढेर होते हैं। आप वर्तमान में चल रहे कार्यों और
task.get_stack()
को प्रत्येक कार्य के लिए स्टैक प्राप्त करने के लिए
asyncio.all_tasks()
का उपयोग करके किसी भी क्षण उन सभी को देख सकते हैं।
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()
सीधे स्टैक ऑब्जेक्ट के साथ खिलवाड़ करने से बचने के लिए और
task.print_stack()
मॉड्यूल का उपयोग करने के बजाय आप
task.print_stack()
को कॉल कर सकते हैं।
आप स्ट्रिंग के
translate
विधि के साथ एक स्ट्रिंग के पात्रों का अनुवाद या हटा सकते हैं (जैसे
tr
यूटिलिटी करता है):
>>> 'Hello, world!'.translate({ ... ord(','): ';', ... ord('o'): '0', ... }) 'Hell0; w0rld!'
translate
का एकमात्र तर्क वर्णों (या कोड) के लिए एक शब्दकोश मैपिंग वर्ण कोड है। यह आमतौर पर
str.maketrans
स्थैतिक विधि के साथ इस तरह के शब्दकोश बनाने के लिए अधिक सुविधाजनक है:
>>> 'Hello, world!'.translate(str.maketrans({ ... ',': ';', ... 'o': '0', ... })) 'Hell0; w0rld!'
या यहां तक कि:
>>> 'Hello, world!'.translate(str.maketrans( ... ',o', ';0' ... )) 'Hell0; w0rld!'
तीसरा तर्क पात्रों को हटाने के लिए है:
>>> tr = str.maketrans(',o', ';0', '!') >>> tr {44: 59, 111: 48, 33: None} >>> 'Hello, world!'.translate(tr) 'Hell0; w0rld'
mypy
अभी तक पुनरावर्ती प्रकार की परिभाषाओं का समर्थन नहीं करता है:
from typing import Optional, Dict from pathlib import Path TreeDict = Dict[str, 'TreeDict'] def tree(path: Path) -> TreeDict: return { f.name: tree(f) if f.is_dir() else None for f in path.iterdir() }
त्रुटि
Cannot resolve name "TreeDict" (possible cyclic definition)
।
यहां बने रहें:
https://github.com/python/mypy/issues/731साधारण फ़ंक्शन को केवल पुनरावर्ती बनने के लिए स्वयं को कॉल करने की आवश्यकता है। जनरेटर के लिए यह इतना आसान नहीं है: आपको आमतौर पर पुनरावर्ती जनरेटर के लिए
yield from
का उपयोग करना होगा:
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)))
आप न केवल चर के साथ बल्कि किसी भी अभिव्यक्ति के
for
उपयोग कर सकते हैं। इसका मूल्यांकन प्रत्येक पुनरावृत्ति पर किया जाता है:
>>> log2 = {} >>> key = 1 >>> for log2[key] in range(100): ... key *= 2 ... >>> log2[16] 4 >>> log2[1024] 10