msgid"This module provides utilities for common tasks involving the :keyword:`with` statement. For more information see also :ref:`typecontextmanager` and :ref:`context-managers`."
msgstr"این ماژول ابزارهایی برای انجام کارهای رایج مربوط به دستور :keyword:`with` ارائه میدهد. برای اطلاعات بیشتر، :ref:`typecontextmanager` و :ref:`context-managers` را نیز ببینید."
msgid"Utilities"
msgstr"ابزارهای کاربردی"
msgid"Functions and classes provided:"
msgstr"توابع و کلاسهای ارائهشده:"
msgid"An :term:`abstract base class` for classes that implement :meth:`~object.__enter__` and :meth:`~object.__exit__`. A default implementation for :meth:`~object.__enter__` is provided which returns ``self`` while :meth:`~object.__exit__` is an abstract method which by default returns ``None``. See also the definition of :ref:`typecontextmanager`."
msgstr"یک :term:`کلاس پایه انتزاعی <abstract base class>` برای کلاسهایی که :meth:`~object.__enter__` و :meth:`~object.__exit__` را پیادهسازی میکنند. یک پیادهسازی پیشفرض برای :meth:`~object.__enter__` ارائه شده است که ``self`` را برمیگرداند، در حالی که :meth:`~object.__exit__` یک متد انتزاعی است که بهطور پیشفرض ``None`` را برمیگرداند. همچنین تعریف :ref:`typecontextmanager` را نیز ببینید."
msgid"An :term:`abstract base class` for classes that implement :meth:`~object.__aenter__` and :meth:`~object.__aexit__`. A default implementation for :meth:`~object.__aenter__` is provided which returns ``self`` while :meth:`~object.__aexit__` is an abstract method which by default returns ``None``. See also the definition of :ref:`async-context-managers`."
msgstr"یک :term:`کلاس پایه انتزاعی <abstract base class>` برای کلاسهایی که :meth:`~object.__aenter__` و :meth:`~object.__aexit__` را پیادهسازی میکنند. پیادهسازی پیشفرضی برای :meth:`~object.__aenter__` فراهم شده است که ``self`` را برمیگرداند، در حالی که :meth:`~object.__aexit__` یک متد انتزاعی است که بهطور پیشفرض ``None`` را برمیگرداند. همچنین تعریف :ref:`async-context-managers` را ببینید."
msgid"This function is a :term:`decorator` that can be used to define a factory function for :keyword:`with` statement context managers, without needing to create a class or separate :meth:`~object.__enter__` and :meth:`~object.__exit__` methods."
msgstr"این تابع یک :term:`دکوراتور <decorator>` است که میتوان از آن برای تعریف یک تابع کارخانهای برای مدیران زمینهی دستور :keyword:`with` استفاده کرد، بدون نیاز به ایجاد یک کلاس یا متدهای جداگانهی :meth:`~object.__enter__` و :meth:`~object.__exit__`."
msgid"While many objects natively support use in with statements, sometimes a resource needs to be managed that isn't a context manager in its own right, and doesn't implement a ``close()`` method for use with ``contextlib.closing``."
msgstr"در حالی که بسیاری از شیءها بهصورت ذاتی از استفاده در دستورهای with پشتیبانی میکنند، گاهی نیاز است منبعی مدیریت شود که بهخودیخود مدیر زمینه نیست و متد ``close()`` را برای استفاده با ``contextlib.closing`` پیادهسازی نمیکند."
msgid"An abstract example would be the following to ensure correct resource management::"
msgstr"برای اطمینان از مدیریت صحیح منابع، یک مثال انتزاعی بهصورت زیر خواهد بود::"
msgid""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def managed_resource(*args, **kwds):\n"
" # Code to acquire resource, e.g.:\n"
" resource = acquire_resource(*args, **kwds)\n"
" try:\n"
" yield resource\n"
" finally:\n"
" # Code to release resource, e.g.:\n"
" release_resource(resource)"
msgstr""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def managed_resource(*args, **kwds):\n"
" # Code to acquire resource, e.g.:\n"
" resource = acquire_resource(*args, **kwds)\n"
" try:\n"
" yield resource\n"
" finally:\n"
" # Code to release resource, e.g.:\n"
" release_resource(resource)"
msgid"The function can then be used like this::"
msgstr"سپس میتوان از تابع به این صورت استفاده کرد::"
msgid""
">>> with managed_resource(timeout=3600) as resource:\n"
"... # Resource is released at the end of this block,\n"
"... # even if code in the block raises an exception"
msgstr""
">>> with managed_resource(timeout=3600) as resource:\n"
"... # Resource is released at the end of this block,\n"
"... # even if code in the block raises an exception"
msgid"The function being decorated must return a :term:`generator`-iterator when called. This iterator must yield exactly one value, which will be bound to the targets in the :keyword:`with` statement's :keyword:`!as` clause, if any."
msgstr"تابع آرایهشده باید هنگام فراخوانی، یک پیمایهی :term:`تولیدگر <generator>` را برگرداند. این پیمایشگر باید دقیقاً یک مقدار تولید کند که در صورت وجود بند :keyword:`!as` در دستور :keyword:`with`، به اهداف موجود در آن بند متصل خواهد شد."
msgid"At the point where the generator yields, the block nested in the :keyword:`with` statement is executed. The generator is then resumed after the block is exited. If an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred. Thus, you can use a :keyword:`try`...\\ :keyword:`except`...\\ :keyword:`finally` statement to trap the error (if any), or ensure that some cleanup takes place. If an exception is trapped merely in order to log it or to perform some action (rather than to suppress it entirely), the generator must reraise that exception. Otherwise the generator context manager will indicate to the :keyword:`!with` statement that the exception has been handled, and execution will resume with the statement immediately following the :keyword:`!with` statement."
msgstr "در نقطهای که تولیدگر yield میکند، بلوک نهفته در دستور :keyword:`with` اجرا میشود. سپس پس از خروج از بلوک، تولیدگر از سر گرفته میشود. اگر یک استثنای مدیریتنشده در بلوک رخ دهد، در نقطهای که yield رخ داده است، درون تولیدگر دوباره پرتاب میشود. بنابراین، میتوانید از یک دستور :keyword:`try`...\\ :keyword:`except`...\\ :keyword:`finally` برای رهگیری خطا (در صورت وجود) یا اطمینان از انجام برخی پاکسازیها استفاده کنید. اگر یک استثنا صرفاً برای ثبت آن یا انجام عملی (نه برای سرکوب کامل آن) گرفته شود، تولیدگر باید آن استثنا را دوباره پرتاب کند. در غیر این صورت، مدیر زمینهی تولیدگر به دستور :keyword:`!with` اطلاع میدهد که استثنا مدیریت شده است، و اجرا با دستور بلافاصله پس از دستور :keyword:`!with` از سر گرفته میشود."
msgid":deco:`contextmanager` uses :class:`ContextDecorator` so the context managers it creates can be used as decorators as well as in :keyword:`with` statements. When used as a decorator, a new generator instance is implicitly created on each function call (this allows the otherwise \"one-shot\" context managers created by :deco:`contextmanager` to meet the requirement that context managers support multiple invocations in order to be used as decorators)."
msgstr":deco:`contextmanager` از :class:`ContextDecorator` استفاده میکند، بنابراین مدیرهای زمینهای که ایجاد میکند میتوانند هم بهعنوان دکوراتور و هم در دستورهای :keyword:`with` استفاده شوند. هنگام استفاده بهعنوان دکوراتور، در هر فراخوانی تابع، یک نمونهی جدید از تولیدگر بهطور ضمنی ایجاد میشود (این امر به مدیرهای زمینهای که توسط :deco:`contextmanager` ایجاد میشوند و در غیر این صورت «یکبارمصرف» هستند، اجازه میدهد الزام پشتیبانی از چندین فراخوانی برای استفاده بهعنوان دکوراتور را برآورده کنند)."
msgid"Use of :class:`ContextDecorator`."
msgstr"استفاده از :class:`ContextDecorator`."
msgid"Similar to :deco:`~contextlib.contextmanager`, but creates an :ref:`asynchronous context manager <async-context-managers>`."
msgstr"مشابه :deco:`~contextlib.contextmanager`، اما یک :ref:`مدیر زمینه ناهمگام <async-context-managers>` ایجاد میکند."
msgid"This function is a :term:`decorator` that can be used to define a factory function for :keyword:`async with` statement asynchronous context managers, without needing to create a class or separate :meth:`~object.__aenter__` and :meth:`~object.__aexit__` methods. It must be applied to an :term:`asynchronous generator` function."
msgstr"این تابع یک :term:`دکوراتور <decorator>` است که میتوان از آن برای تعریف یک تابع کارخانهای برای مدیران زمینه ناهمگام دستور :keyword:`async with` استفاده کرد، بدون نیاز به ایجاد یک کلاس یا متدهای جداگانهی :meth:`~object.__aenter__` و :meth:`~object.__aexit__`. این دکوراتور باید به یک تابع :term:`تولیدگر ناهمگام <asynchronous generator>` اعمال شود."
msgid"A simple example::"
msgstr"یک مثال ساده::"
msgid""
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def get_connection():\n"
" conn = await acquire_db_connection()\n"
" try:\n"
" yield conn\n"
" finally:\n"
" await release_db_connection(conn)\n"
"\n"
"async def get_all_users():\n"
" async with get_connection() as conn:\n"
" return conn.query('SELECT ...')"
msgstr""
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def get_connection():\n"
" conn = await acquire_db_connection()\n"
" try:\n"
" yield conn\n"
" finally:\n"
" await release_db_connection(conn)\n"
"\n"
"async def get_all_users():\n"
" async with get_connection() as conn:\n"
" return conn.query('SELECT ...')"
msgid"Context managers defined with :deco:`asynccontextmanager` can be used either as decorators or with :keyword:`async with` statements::"
msgstr"مدیران زمینه تعریفشده با :deco:`asynccontextmanager` را میتوان هم بهعنوان دکوراتور و هم با دستورهای :keyword:`async with` استفاده کرد::"
msgid""
"import time\n"
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def timeit():\n"
" now = time.monotonic()\n"
" try:\n"
" yield\n"
" finally:\n"
" print(f'it took {time.monotonic() - now}s to run')\n"
"\n"
"@timeit()\n"
"async def main():\n"
" # ... async code ..."
msgstr""
"import time\n"
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def timeit():\n"
" now = time.monotonic()\n"
" try:\n"
" yield\n"
" finally:\n"
" print(f'it took {time.monotonic() - now}s to run')\n"
"\n"
"@timeit()\n"
"async def main():\n"
" # ... async code ..."
msgid"When used as a decorator, a new generator instance is implicitly created on each function call. This allows the otherwise \"one-shot\" context managers created by :deco:`asynccontextmanager` to meet the requirement that context managers support multiple invocations in order to be used as decorators."
msgstr"هنگام استفاده بهعنوان دکوراتور، در هر فراخوانی تابع، یک نمونه تولیدگر جدید بهطور ضمنی ایجاد میشود. این امر به مدیرهای زمینه ایجادشده توسط :deco:`asynccontextmanager`، که در غیر این صورت «یکبارمصرف» هستند، اجازه میدهد الزامی را برآورده کنند که طبق آن مدیرهای زمینه باید برای استفاده بهعنوان دکوراتور از فراخوانیهای متعدد پشتیبانی کنند."
msgid"Async context managers created with :deco:`asynccontextmanager` can be used as decorators."
msgstr"مدیرهای زمینه ناهمگام ایجادشده با :deco:`asynccontextmanager` را میتوان بهعنوان دکوراتور استفاده کرد."
msgid"Return a context manager that closes *thing* upon completion of the block. This is basically equivalent to::"
msgstr"یک مدیر زمینه برمیگرداند که *thing* را پس از تکمیل بلوک میبندد. این اساساً معادل است با::"
msgid""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def closing(thing):\n"
" try:\n"
" yield thing\n"
" finally:\n"
" thing.close()"
msgstr""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def closing(thing):\n"
" try:\n"
" yield thing\n"
" finally:\n"
" thing.close()"
msgid"And lets you write code like this::"
msgstr"و به شما امکان میدهد کدی مانند این بنویسید::"
msgid""
"from contextlib import closing\n"
"from urllib.request import urlopen\n"
"\n"
"with closing(urlopen('https://www.python.org')) as page:\n"
" for line in page:\n"
" print(line)"
msgstr""
"from contextlib import closing\n"
"from urllib.request import urlopen\n"
"\n"
"with closing(urlopen('https://www.python.org')) as page:\n"
" for line in page:\n"
" print(line)"
msgid"without needing to explicitly close ``page``. Even if an error occurs, ``page.close()`` will be called when the :keyword:`with` block is exited."
msgstr"بدون نیاز به بستن ``page`` بهصراحت. حتی اگر خطایی رخ دهد، هنگام خروج از بلوک :keyword:`with`، ``page.close()`` فراخوانی خواهد شد."
msgid"Most types managing resources support the :term:`context manager` protocol, which closes *thing* on leaving the :keyword:`with` statement. As such, :func:`!closing` is most useful for third party types that don't support context managers. This example is purely for illustration purposes, as :func:`~urllib.request.urlopen` would normally be used in a context manager."
msgstr"بیشتر انواعی که منابع را مدیریت میکنند، از پروتکل :term:`مدیر زمینه <context manager>` پشتیبانی میکنند، که *شیء* را در هنگام خروج از دستور :keyword:`with` میبندد. از این رو، :func:`!closing` برای انواع شخص ثالثی که از مدیران زمینه پشتیبانی نمیکنند، بیشترین کاربرد را دارد. این مثال صرفاً برای اهداف نمایشی است، زیرا :func:`~urllib.request.urlopen` بهطور معمول در یک مدیر زمینه استفاده میشود."
msgid"Return an async context manager that calls the ``aclose()`` method of *thing* upon completion of the block. This is basically equivalent to::"
msgstr"یک مدیر زمینه ناهمگام برمیگرداند که پس از تکمیل بلوک، متد ``aclose()`` متعلق به *thing* را فراخوانی میکند. این اساساً معادل است با::"
msgid""
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def aclosing(thing):\n"
" try:\n"
" yield thing\n"
" finally:\n"
" await thing.aclose()"
msgstr""
"from contextlib import asynccontextmanager\n"
"\n"
"@asynccontextmanager\n"
"async def aclosing(thing):\n"
" try:\n"
" yield thing\n"
" finally:\n"
" await thing.aclose()"
msgid"Significantly, ``aclosing()`` supports deterministic cleanup of async generators when they happen to exit early by :keyword:`break` or an exception. For example::"
msgstr"بهطور قابلتوجهی، ``aclosing()`` از پاکسازی قطعی تولیدگرهای ناهمگام در صورتی که بهواسطهی :keyword:`break` یا یک استثنا زودتر خارج شوند، پشتیبانی میکند. برای مثال::"
msgid""
"from contextlib import aclosing\n"
"\n"
"async with aclosing(my_generator()) as values:\n"
" async for value in values:\n"
" if value == 42:\n"
" break"
msgstr""
"from contextlib import aclosing\n"
"\n"
"async with aclosing(my_generator()) as values:\n"
" async for value in values:\n"
" if value == 42:\n"
" break"
msgid"This pattern ensures that the generator's async exit code is executed in the same context as its iterations (so that exceptions and context variables work as expected, and the exit code isn't run after the lifetime of some task it depends on)."
msgstr"این الگو تضمین میکند که کد خروج ناهمگام تولیدگر در همان زمینهای اجرا شود که تکرارهای آن اجرا میشوند (تا استثناها و متغیرهای زمینه همانطور که انتظار میرود کار کنند و کد خروج پس از طول عمر وظیفهی که به آن وابسته است اجرا نشود)."
msgid"Return a context manager that returns *enter_result* from :meth:`~object.__enter__`, but otherwise does nothing. It is intended to be used as a stand-in for an optional context manager, for example::"
msgstr"یک مدیر زمینه برمیگرداند که *enter_result* را از :meth:`~object.__enter__` برمیگرداند، اما در غیر این صورت هیچ کاری انجام نمیدهد. این برای استفاده بهعنوان جایگزینی برای یک مدیر زمینه اختیاری در نظر گرفته شده است، برای مثال::"
msgid""
"def myfunction(arg, ignore_exceptions=False):\n"
" if ignore_exceptions:\n"
" # Use suppress to ignore all exceptions.\n"
" cm = contextlib.suppress(Exception)\n"
" else:\n"
" # Do not ignore any exceptions, cm has no effect.\n"
" cm = contextlib.nullcontext()\n"
" with cm:\n"
" # Do something"
msgstr""
"def myfunction(arg, ignore_exceptions=False):\n"
" if ignore_exceptions:\n"
" # Use suppress to ignore all exceptions.\n"
" cm = contextlib.suppress(Exception)\n"
" else:\n"
" # Do not ignore any exceptions, cm has no effect.\n"
" cm = contextlib.nullcontext()\n"
" with cm:\n"
" # Do something"
msgid"An example using *enter_result*::"
msgstr"مثالی با استفاده از *enter_result*::"
msgid""
"def process_file(file_or_path):\n"
" if isinstance(file_or_path, str):\n"
" # If string, open file\n"
" cm = open(file_or_path)\n"
" else:\n"
" # Caller is responsible for closing file\n"
" cm = nullcontext(file_or_path)\n"
"\n"
" with cm as file:\n"
" # Perform processing on the file"
msgstr""
"def process_file(file_or_path):\n"
" if isinstance(file_or_path, str):\n"
" # If string, open file\n"
" cm = open(file_or_path)\n"
" else:\n"
" # Caller is responsible for closing file\n"
" cm = nullcontext(file_or_path)\n"
"\n"
" with cm as file:\n"
" # Perform processing on the file"
msgid"It can also be used as a stand-in for :ref:`asynchronous context managers <async-context-managers>`::"
msgstr"همچنین میتواند بهعنوان جایگزینی برای :ref:`مدیران زمینه ناهمگام <async-context-managers>` استفاده شود::"
msgid""
"async def send_http(session=None):\n"
" if not session:\n"
" # If no http session, create it with aiohttp\n"
" cm = aiohttp.ClientSession()\n"
" else:\n"
" # Caller is responsible for closing the session\n"
" cm = nullcontext(session)\n"
"\n"
" async with cm as session:\n"
" # Send http requests with session"
msgstr""
"async def send_http(session=None):\n"
" if not session:\n"
" # If no http session, create it with aiohttp\n"
" cm = aiohttp.ClientSession()\n"
" else:\n"
" # Caller is responsible for closing the session\n"
" cm = nullcontext(session)\n"
"\n"
" async with cm as session:\n"
" # Send http requests with session"
msgid":term:`asynchronous context manager` support was added."
msgstr"پشتیبانی از :term:`asynchronous context manager` افزوده شد."
msgid"Return a context manager that suppresses any of the specified exceptions if they occur in the body of a :keyword:`!with` statement and then resumes execution with the first statement following the end of the :keyword:`!with` statement."
msgstr"یک مدیر زمینه برمیگرداند که در صورت وقوع هر یک از استثناهای مشخصشده در بدنهی دستور :keyword:`!with`، آنها را سرکوب میکند و سپس اجرا را با نخستین دستور پس از پایان دستور :keyword:`!with` از سر میگیرد."
msgid"As with any other mechanism that completely suppresses exceptions, this context manager should be used only to cover very specific errors where silently continuing with program execution is known to be the right thing to do."
msgstr"همانطور که در مورد هر سازوکار دیگری که استثناها را بهطور کامل سرکوب میکند، این مدیر زمینه باید فقط برای پوشش خطاهای بسیار خاصی استفاده شود که در آنها ادامهی بیصدای اجرای برنامه کار درست شناخته میشود."
msgid"For example::"
msgstr"برای مثال::"
msgid""
"from contextlib import suppress\n"
"\n"
"with suppress(FileNotFoundError):\n"
" os.remove('somefile.tmp')\n"
"\n"
"with suppress(FileNotFoundError):\n"
" os.remove('someotherfile.tmp')"
msgstr""
"from contextlib import suppress\n"
"\n"
"with suppress(FileNotFoundError):\n"
" os.remove('somefile.tmp')\n"
"\n"
"with suppress(FileNotFoundError):\n"
" os.remove('someotherfile.tmp')"
msgid"This code is equivalent to::"
msgstr"این کد معادل است با::"
msgid""
"try:\n"
" os.remove('somefile.tmp')\n"
"except FileNotFoundError:\n"
" pass\n"
"\n"
"try:\n"
" os.remove('someotherfile.tmp')\n"
"except FileNotFoundError:\n"
" pass"
msgstr""
"try:\n"
" os.remove('somefile.tmp')\n"
"except FileNotFoundError:\n"
" pass\n"
"\n"
"try:\n"
" os.remove('someotherfile.tmp')\n"
"except FileNotFoundError:\n"
" pass"
msgid"This context manager is :ref:`reentrant <reentrant-cms>`."
msgstr"این مدیر زمینه :ref:`بازورودپذیر <reentrant-cms>` است."
msgid"If the code within the :keyword:`!with` block raises a :exc:`BaseExceptionGroup`, suppressed exceptions are removed from the group. Any exceptions of the group which are not suppressed are re-raised in a new group which is created using the original group's :meth:`~BaseExceptionGroup.derive` method."
msgstr"اگر کد درون بلوک :keyword:`!with` یک :exc:`BaseExceptionGroup` را پرتاب کند، استثناهای مهارشده از گروه حذف میشوند. هر یک از استثناهای گروه که مهارنشده باشند، در گروه جدیدی که با استفاده از متد :meth:`~BaseExceptionGroup.derive` گروه اصلی ایجاد شده است، دوباره پرتاب میشوند."
msgid"``suppress`` now supports suppressing exceptions raised as part of a :exc:`BaseExceptionGroup`."
msgstr"``suppress`` اکنون از مهار استثناهایی که بهعنوان بخشی از یک :exc:`BaseExceptionGroup` پرتاب میشوند، پشتیبانی میکند."
msgid"Context manager for temporarily redirecting :data:`sys.stdout` to another :term:`file object`."
msgstr"مدیر زمینه برای هدایت موقت :data:`sys.stdout` به یک :term:`شیء پرونده <file object>` دیگر."
msgid"This tool adds flexibility to existing functions or classes whose output is hardwired to :data:`sys.stdout`."
msgstr"این ابزار انعطافپذیری را به توابع یا کلاسهای موجودی اضافه میکند که خروجی آنها به :data:`sys.stdout` وصل شده است."
msgid"For example, the output of :func:`help` normally is sent to *sys.stdout*. You can capture that output in a string by redirecting the output to an :class:`io.StringIO` object. The replacement stream is returned from the :meth:`~object.__enter__` method and so is available as the target of the :keyword:`with` statement::"
msgstr"برای مثال، خروجی :func:`help` بهطور معمول به *sys.stdout* فرستاده میشود. شما میتوانید با هدایت خروجی به یک شیء :class:`io.StringIO`، آن خروجی را در یک رشته ذخیره کنید. جریان جایگزین از متد :meth:`~object.__enter__` بازگردانده میشود و بنابراین بهعنوان هدف دستور :keyword:`with` در دسترس است::"
msgid""
"with redirect_stdout(io.StringIO()) as f:\n"
" help(pow)\n"
"s = f.getvalue()"
msgstr""
"with redirect_stdout(io.StringIO()) as f:\n"
" help(pow)\n"
"s = f.getvalue()"
msgid"To send the output of :func:`help` to a file on disk, redirect the output to a regular file::"
msgstr"برای ارسال خروجی :func:`help` به یک پرونده روی دیسک، خروجی را به یک پرونده معمولی تغییر مسیر دهید::"
msgid""
"with open('help.txt', 'w') as f:\n"
" with redirect_stdout(f):\n"
" help(pow)"
msgstr""
"with open('help.txt', 'w') as f:\n"
" with redirect_stdout(f):\n"
" help(pow)"
msgid"To send the output of :func:`help` to *sys.stderr*::"
msgstr"برای ارسال خروجی :func:`help` به *sys.stderr*::"
msgid""
"with redirect_stdout(sys.stderr):\n"
" help(pow)"
msgstr""
"with redirect_stdout(sys.stderr):\n"
" help(pow)"
msgid"Note that the global side effect on :data:`sys.stdout` means that this context manager is not suitable for use in library code and most threaded applications. It also has no effect on the output of subprocesses. However, it is still a useful approach for many utility scripts."
msgstr"توجه داشته باشید که اثر جانبی سراسری بر :data:`sys.stdout` به این معناست که این مدیر زمینه برای استفاده در کد کتابخانهای و بیشتر برنامههای نخی مناسب نیست. همچنین بر خروجی زیرفرایندها اثری ندارد. با این حال، هنوز هم رویکردی مفید برای بسیاری از اسکریپتهای کاربردی است."
msgid"Similar to :func:`~contextlib.redirect_stdout` but redirecting the global :data:`sys.stderr` to another :term:`file object`."
msgstr"مشابه :func:`~contextlib.redirect_stdout` اما با هدایت :data:`sys.stderr` سراسری به یک :term:`شیء پرونده <file object>` دیگر."
msgid"Non parallel-safe context manager to change the current working directory. As this changes a global state, the working directory, it is not suitable for use in most threaded or async contexts. It is also not suitable for most non-linear code execution, like generators, where the program execution is temporarily relinquished -- unless explicitly desired, you should not yield when this context manager is active."
msgstr"مدیر زمینهای که از نظر موازیسازی ایمن نیست، برای تغییر پوشه کاری جاری است. از آنجا که این کار یک وضعیت سراسری، یعنی پوشه کاری، را تغییر میدهد، برای استفاده در بیشتر زمینههای نخی یا ناهمگام مناسب نیست. همچنین برای بیشتر اجراهای غیرخطی کد، مانند تولیدگرها، مناسب نیست؛ جاهایی که اجرای برنامه بهطور موقت واگذار میشود -- مگر اینکه صریحاً مورد نظر باشد، نباید هنگامی که این مدیر زمینه فعال است، yield کنید."
msgid"This is a simple wrapper around :func:`~os.chdir`, it changes the current working directory upon entering and restores the old one on exit."
msgstr"این پوششی ساده بر روی :func:`~os.chdir` است که هنگام ورود، پوشه کاری جاری را تغییر میدهد و هنگام خروج، پوشه پیشین را بازمیگرداند."
msgid"A base class that enables a context manager to also be used as a decorator."
msgstr"کلاس پایهای که به یک مدیر زمینه امکان میدهد تا بهعنوان یک دکوراتور نیز استفاده شود."
msgid"Context managers inheriting from ``ContextDecorator`` have to implement :meth:`~object.__enter__` and :meth:`~object.__exit__` as normal. ``__exit__`` retains its optional exception handling even when used as a decorator."
msgstr"مدیران زمینهای که از ``ContextDecorator`` ارثبری میکنند باید :meth:`~object.__enter__` و :meth:`~object.__exit__` را بهصورت عادی پیادهسازی کنند. ``__exit__`` مدیریت اختیاری استثنا را حتی هنگام استفاده بهعنوان دکوراتور حفظ میکند."
msgid"``ContextDecorator`` is used by :deco:`contextmanager`, so you get this functionality automatically."
msgstr":deco:`contextmanager` از ``ContextDecorator`` استفاده میکند، بنابراین این قابلیت بهصورت خودکار در دسترس شما قرار میگیرد."
msgid"Example of ``ContextDecorator``::"
msgstr"مثالی از ``ContextDecorator``::"
msgid""
"from contextlib import ContextDecorator\n"
"\n"
"class mycontext(ContextDecorator):\n"
" def __enter__(self):\n"
" print('Starting')\n"
" return self\n"
"\n"
" def __exit__(self, *exc):\n"
" print('Finishing')\n"
" return False"
msgstr""
"from contextlib import ContextDecorator\n"
"\n"
"class mycontext(ContextDecorator):\n"
" def __enter__(self):\n"
" print('Starting')\n"
" return self\n"
"\n"
" def __exit__(self, *exc):\n"
" print('Finishing')\n"
" return False"
msgid"The class can then be used like this::"
msgstr"سپس میتوان از کلاس به این صورت استفاده کرد::"
msgid""
">>> @mycontext()\n"
"... def function():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> function()\n"
"Starting\n"
"The bit in the middle\n"
"Finishing\n"
"\n"
">>> with mycontext():\n"
"... print('The bit in the middle')\n"
"...\n"
"Starting\n"
"The bit in the middle\n"
"Finishing"
msgstr""
">>> @mycontext()\n"
"... def function():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> function()\n"
"Starting\n"
"The bit in the middle\n"
"Finishing\n"
"\n"
">>> with mycontext():\n"
"... print('The bit in the middle')\n"
"...\n"
"Starting\n"
"The bit in the middle\n"
"Finishing"
msgid"This change is just syntactic sugar for any construct of the following form::"
msgstr"این تغییر صرفاً قند نحوی برای هر ساختاری با شکل زیر است::"
msgid""
"def f():\n"
" with cm():\n"
" # Do stuff"
msgstr""
"def f():\n"
" with cm():\n"
" # Do stuff"
msgid"``ContextDecorator`` lets you instead write::"
msgstr"``ContextDecorator`` به شما اجازه میدهد در عوض بنویسید::"
msgid""
"@cm()\n"
"def f():\n"
" # Do stuff"
msgstr""
"@cm()\n"
"def f():\n"
" # Do stuff"
msgid"It makes it clear that the ``cm`` applies to the whole function, rather than just a piece of it (and saving an indentation level is nice, too)."
msgstr"این امر روشن میکند که ``cm`` به کل تابع اعمال میشود، نه فقط به بخشی از آن (و یک سطح تورفتگی کمتر نیز خوب است)."
msgid"Existing context managers that already have a base class can be extended by using ``ContextDecorator`` as a mixin class::"
msgstr"مدیران زمینه موجودی که از قبل یک کلاس پایه دارند، میتوانند با استفاده از ``ContextDecorator`` بهعنوان کلاس میکساین گسترش یابند::"
msgid"As the decorated function must be able to be called multiple times, the underlying context manager must support use in multiple :keyword:`with` statements. If this is not the case, then the original construct with the explicit :keyword:`!with` statement inside the function should be used."
msgstr"از آنجا که تابع دکوراتشده باید بتواند چندین بار فراخوانی شود، مدیر زمینه زیربنایی باید از استفاده در چندین دستور :keyword:`with` پشتیبانی کند. در غیر این صورت، باید از ساختار اصلی با دستور صریح :keyword:`!with` درون تابع استفاده شود."
msgid"When the decorated callable is a generator function, coroutine function, or asynchronous generator function, the returned wrapper is of the same kind and keeps the context manager open for the lifetime of the iteration or await rather than only for the call that creates the generator or coroutine object. Wrapped generators and asynchronous generators are explicitly closed when iteration ends, as if by :func:`closing` or :func:`aclosing`."
msgstr"هنگامی که شیء فراخوانیپذیرِ تزئینشده یک تابع تولیدگر، تابع همروال یا تابع تولیدگر ناهمگام باشد، پوششِ بازگرداندهشده از همان نوع است و مدیر زمینه را برای طول عمر پیمایش یا await باز نگه میدارد، نه فقط برای فراخوانیای که شیء تولیدگر یا همروال را ایجاد میکند. تولیدگرهای پوششدادهشده و تولیدگرهای ناهمگام، هنگامی که پیمایش به پایان میرسد، بهطور صریح بسته میشوند، درست مانند زمانی که توسط :func:`closing` یا :func:`aclosing` بسته میشوند."
msgid"For asynchronous generators the wrapper re-yields each value with ``async for``; values sent with :meth:`~agen.asend` and exceptions thrown with :meth:`~agen.athrow` are not forwarded to the wrapped generator."
msgstr"برای تولیدگرهای ناهمگام، پوشش هر مقدار را با ``async for`` دوباره تولید (yield) میکند؛ مقادیر ارسالشده با :meth:`~agen.asend` و استثناهای پرتابشده با :meth:`~agen.athrow` به تولیدگر پوششدادهشده فوروارد نمیشوند."
msgid"Decorating a generator function, coroutine function, or asynchronous generator function now keeps the context manager open across iteration or await. Previously the context manager exited as soon as the generator or coroutine object was created."
msgstr"تزئین کردن یک تابع تولیدگر، تابع همروال یا تابع تولیدگر ناهمگام اکنون باعث میشود مدیر زمینه در طول پیمایش یا await باز بماند. پیشتر، مدیر زمینه بهمحض ایجاد شدن شیء تولیدگر یا همروال خارج میشد."
msgid"Similar to :class:`ContextDecorator`, but the context manager is entered and exited with :keyword:`async with`. Decorate coroutine functions and asynchronous generator functions with this class; the returned wrapper is of the same kind."
msgstr"مشابه :class:`ContextDecorator`، اما ورود و خروج از مدیر زمینه با :keyword:`async with` انجام میشود. توابع همروال و توابع تولیدگر ناهمگام را با این کلاس تزئین کنید؛ پوششِ بازگرداندهشده از همان نوع است."
msgid"Synchronous functions and generators are accepted, but the wrapper is always asynchronous, so the decorated callable must then be awaited or iterated with ``async for``. If that change of calling convention is not intended, use :class:`ContextDecorator` instead."
msgstr"توابع همگام و تولیدگرها پذیرفته میشوند، اما پوشش همیشه ناهمگام است، بنابراین شیء فراخوانیپذیرِ تزئینشده باید با ``async for`` منتظر بماند (await شود) یا پیمایش شود. اگر آن تغییر در قرارداد فراخوانی مورد نظر نیست، به جای آن از :class:`ContextDecorator` استفاده کنید."
msgid"Example of ``AsyncContextDecorator``::"
msgstr"مثالی از ``AsyncContextDecorator``::"
msgid""
"from asyncio import run\n"
"from contextlib import AsyncContextDecorator\n"
"\n"
"class mycontext(AsyncContextDecorator):\n"
" async def __aenter__(self):\n"
" print('Starting')\n"
" return self\n"
"\n"
" async def __aexit__(self, *exc):\n"
" print('Finishing')\n"
" return False"
msgstr""
"from asyncio import run\n"
"from contextlib import AsyncContextDecorator\n"
"\n"
"class mycontext(AsyncContextDecorator):\n"
" async def __aenter__(self):\n"
" print('Starting')\n"
" return self\n"
"\n"
" async def __aexit__(self, *exc):\n"
" print('Finishing')\n"
" return False"
msgid""
">>> @mycontext()\n"
"... async def function():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> run(function())\n"
"Starting\n"
"The bit in the middle\n"
"Finishing\n"
"\n"
">>> async def function():\n"
"... async with mycontext():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> run(function())\n"
"Starting\n"
"The bit in the middle\n"
"Finishing"
msgstr""
">>> @mycontext()\n"
"... async def function():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> run(function())\n"
"Starting\n"
"The bit in the middle\n"
"Finishing\n"
"\n"
">>> async def function():\n"
"... async with mycontext():\n"
"... print('The bit in the middle')\n"
"...\n"
">>> run(function())\n"
"Starting\n"
"The bit in the middle\n"
"Finishing"
msgid"Decorating an asynchronous generator function now keeps the context manager open across iteration. Previously the context manager exited as soon as the generator object was created. Synchronous functions and synchronous generator functions are also now accepted, with an asynchronous wrapper returned."
msgstr"تزئین کردن یک تابع تولیدگر ناهمگام اکنون باعث میشود مدیر زمینه در طول پیمایش باز بماند. پیشتر، مدیر زمینه بهمحض ایجاد شدن شیء تولیدگر خارج میشد. توابع همگام و توابع تولیدگر همگام نیز اکنون پذیرفته میشوند و یک پوشش ناهمگام بازگردانده میشود."
msgid"A context manager that is designed to make it easy to programmatically combine other context managers and cleanup functions, especially those that are optional or otherwise driven by input data."
msgstr"یک مدیر زمینه که طراحی شده است تا ترکیب سایر مدیرهای زمینه و توابع پاکسازی را بهصورت برنامهای آسان کند، بهویژه آنهایی که اختیاری هستند یا بهنحوی دیگر توسط دادههای ورودی هدایت میشوند."
msgid"For example, a set of files may easily be handled in a single with statement as follows::"
msgstr"برای مثال، مجموعهای از پروندهها را میتوان بهراحتی در یک دستور with واحد بهصورت زیر مدیریت کرد::"
msgid""
"with ExitStack() as stack:\n"
" files = [stack.enter_context(open(fname)) for fname in filenames]\n"
" # All opened files will automatically be closed at the end of\n"
" # the with statement, even if attempts to open files later\n"
" # in the list raise an exception"
msgstr""
"with ExitStack() as stack:\n"
" files = [stack.enter_context(open(fname)) for fname in filenames]\n"
" # All opened files will automatically be closed at the end of\n"
" # the with statement, even if attempts to open files later\n"
" # in the list raise an exception"
msgid"The :meth:`~object.__enter__` method returns the :class:`ExitStack` instance, and performs no additional operations."
msgstr"متد :meth:`~object.__enter__` نمونه :class:`ExitStack` را برمیگرداند و هیچ عملیات اضافی انجام نمیدهد."
msgid"Each instance maintains a stack of registered callbacks that are called in reverse order when the instance is closed (either explicitly or implicitly at the end of a :keyword:`with` statement). Note that callbacks are *not* invoked implicitly when the context stack instance is garbage collected."
msgstr"هر نمونه پشتهای از کالبکهای ثبتشده را نگهداری میکند که هنگام بسته شدن نمونه، با ترتیب معکوس فراخوانی میشوند (چه بهصورت صریح و چه بهصورت ضمنی در پایان یک دستور :keyword:`with`). توجه داشته باشید که هنگام زبالهروبی نمونهی پشتهی زمینه، کالبکها بهصورت ضمنی *فراخوانی نمیشوند*."
msgid"This stack model is used so that context managers that acquire their resources in their ``__init__`` method (such as file objects) can be handled correctly."
msgstr"از این مدل پشته استفاده میشود تا مدیران زمینهای که منابع خود را در متد ``__init__`` خود کسب میکنند (مانند اشیای پرونده) بتوانند بهدرستی مدیریت شوند."
msgid"Since registered callbacks are invoked in the reverse order of registration, this ends up behaving as if multiple nested :keyword:`with` statements had been used with the registered set of callbacks. This even extends to exception handling - if an inner callback suppresses or replaces an exception, then outer callbacks will be passed arguments based on that updated state."
msgstr"از آنجا که کالبکهای ثبتشده به ترتیب معکوس ثبت فراخوانی میشوند، این موضوع در نهایت رفتاری مانند آن دارد که گویی چندین دستور تودرتوی :keyword:`with` با مجموعهی کالبکهای ثبتشده استفاده شده باشد. این موضوع حتی به مدیریت استثنا نیز گسترش مییابد؛ اگر یک کالبک داخلی استثنایی را سرکوب یا جایگزین کند، کالبکهای بیرونی آرگومانهایی بر اساس آن وضعیت بهروزشده دریافت خواهند کرد."
msgid"This is a relatively low level API that takes care of the details of correctly unwinding the stack of exit callbacks. It provides a suitable foundation for higher level context managers that manipulate the exit stack in application specific ways."
msgstr"این یک API نسبتاً سطح پایین است که جزئیات باز کردن صحیح پشتهی کالبکهای خروج را مدیریت میکند. این API پایهای مناسب برای مدیرهای زمینه سطح بالاتر فراهم میکند که پشتهی خروج را به روشهای مختص برنامه دستکاری میکنند."
msgid"Enters a new context manager and adds its :meth:`~object.__exit__` method to the callback stack. The return value is the result of the context manager's own :meth:`~object.__enter__` method."
msgstr"وارد یک مدیر زمینه جدید میشود و متد :meth:`~object.__exit__` آن را به پشتهی کالبک اضافه میکند. مقدار بازگشتی، نتیجهی متد :meth:`~object.__enter__` خود مدیر زمینه است."
msgid"These context managers may suppress exceptions just as they normally would if used directly as part of a :keyword:`with` statement."
msgstr"این مدیران زمینه ممکن است استثناها را مهار کنند، دقیقاً همانطور که معمولاً در صورت استفادهی مستقیم بهعنوان بخشی از دستور :keyword:`with` این کار را میکنند."
msgid"Raises :exc:`TypeError` instead of :exc:`AttributeError` if *cm* is not a context manager."
msgstr"اگر *cm* یک مدیر زمینه نباشد، بهجای :exc:`AttributeError`، :exc:`TypeError` پرتاب میشود."
msgid"Added support for arbitrary descriptors :meth:`!__enter__` and :meth:`!__exit__`."
msgstr"پشتیبانی از توصیفگرهای دلخواه :meth:`!__enter__` و :meth:`!__exit__` اضافه شد."
msgid"Adds a context manager's :meth:`~object.__exit__` method to the callback stack."
msgstr"متد :meth:`~object.__exit__`\\ مدیر زمینه را به پشتهی کالبک اضافه میکند."
msgid"As ``__enter__`` is *not* invoked, this method can be used to cover part of an :meth:`~object.__enter__` implementation with a context manager's own :meth:`~object.__exit__` method."
msgstr"از آنجا که ``__enter__`` فراخوانی *نمیشود*، میتوان از این متد برای پوشش دادن بخشی از پیادهسازی :meth:`~object.__enter__` با متد :meth:`~object.__exit__` خودِ مدیر زمینه استفاده کرد."
msgid"If passed an object that is not a context manager, this method assumes it is a callback with the same signature as a context manager's :meth:`~object.__exit__` method and adds it directly to the callback stack."
msgstr"اگر شیءای که مدیریتگر زمینه نیست به این متد داده شود، این متد فرض میکند که آن شیء یک کالبک با همان امضای متد :meth:`~object.__exit__` مدیریتگر زمینه است و آن را مستقیماً به پشته کالبک اضافه میکند."
msgid"By returning true values, these callbacks can suppress exceptions the same way context manager :meth:`~object.__exit__` methods can."
msgstr"این کالبکها میتوانند با برگرداندن مقادیر درست، استثناها را دقیقاً مانند متدهای :meth:`~object.__exit__` مدیر زمینه مهار کنند."
msgid"The passed in object is returned from the function, allowing this method to be used as a function decorator."
msgstr"شیء دادهشده از تابع بازگردانده میشود و امکان استفاده از این متد بهعنوان دکوراتور تابع را فراهم میکند."
msgid"Added support for arbitrary descriptors :meth:`!__exit__`."
msgstr"پشتیبانی از توصیفگرهای دلخواه :meth:`!__exit__` اضافه شد."
msgid"Accepts an arbitrary callback function and arguments and adds it to the callback stack."
msgstr"یک تابع کالبک دلخواه و آرگومانها را میپذیرد و آن را به پشته کالبک اضافه میکند."
msgid"Unlike the other methods, callbacks added this way cannot suppress exceptions (as they are never passed the exception details)."
msgstr"برخلاف متدهای دیگر، کالبکهایی که به این روش افزوده میشوند نمیتوانند استثناها را مهار کنند (زیرا جزئیات استثنا هرگز به آنها ارسال نمیشود)."
msgid"The passed in callback is returned from the function, allowing this method to be used as a function decorator."
msgstr"کالبک ورودی از تابع بازگردانده میشود و به این متد اجازه میدهد بهعنوان دکوراتور تابع استفاده شود."
msgid"Transfers the callback stack to a fresh :class:`ExitStack` instance and returns it. No callbacks are invoked by this operation - instead, they will now be invoked when the new stack is closed (either explicitly or implicitly at the end of a :keyword:`with` statement)."
msgstr"پشتهی کالبکها را به یک نمونهی تازهی :class:`ExitStack` منتقل میکند و آن را برمیگرداند. هیچ کالبکی با این عملیات فراخوانی نمیشود — در عوض، از این پس آنها زمانی فراخوانی خواهند شد که پشتهی جدید بسته شود (چه بهصورت صریح و چه بهصورت ضمنی در پایان یک دستور :keyword:`with`)."
msgid"For example, a group of files can be opened as an \"all or nothing\" operation as follows::"
msgstr"برای مثال، میتوان گروهی از پروندهها را بهعنوان یک عملیات «همه یا هیچ» بهصورت زیر باز کرد::"
msgid""
"with ExitStack() as stack:\n"
" files = [stack.enter_context(open(fname)) for fname in filenames]\n"
" # Hold onto the close method, but don't call it yet.\n"
" close_files = stack.pop_all().close\n"
" # If opening any file fails, all previously opened files will be\n"
" # closed automatically. If all files are opened successfully,\n"
" # they will remain open even after the with statement ends.\n"
" # close_files() can then be invoked explicitly to close them all."
msgstr""
"with ExitStack() as stack:\n"
" files = [stack.enter_context(open(fname)) for fname in filenames]\n"
" # Hold onto the close method, but don't call it yet.\n"
" close_files = stack.pop_all().close\n"
" # If opening any file fails, all previously opened files will be\n"
" # closed automatically. If all files are opened successfully,\n"
" # they will remain open even after the with statement ends.\n"
" # close_files() can then be invoked explicitly to close them all."
msgid"Immediately unwinds the callback stack, invoking callbacks in the reverse order of registration. For any context managers and exit callbacks registered, the arguments passed in will indicate that no exception occurred."
msgstr"بلافاصله پشتهی کالبک را باز میکند و کالبکها را به ترتیب معکوس ثبت فراخوانی میکند. به ازای هر مدیر زمینه و کالبک خروج ثبتشده، آرگومانهای ارسالشده نشان میدهند که هیچ استثنایی رخ نداده است."
msgid"An :ref:`asynchronous context manager <async-context-managers>`, similar to :class:`ExitStack`, that supports combining both synchronous and asynchronous context managers, as well as having coroutines for cleanup logic."
msgstr"یک :ref:`مدیر زمینه ناهمگام <async-context-managers>`، مشابه :class:`ExitStack`، که از ترکیب مدیرهای زمینه همگام و ناهمگام، و همچنین از داشتن همروالهایی برای منطق پاکسازی پشتیبانی میکند."
msgid"The :meth:`~ExitStack.close` method is not implemented; :meth:`aclose` must be used instead."
msgstr"متد :meth:`~ExitStack.close` پیادهسازی نشده است؛ باید بهجای آن از :meth:`aclose` استفاده شود."
msgid"Similar to :meth:`ExitStack.enter_context` but expects an asynchronous context manager."
msgstr"مشابه :meth:`ExitStack.enter_context` است، اما انتظار یک مدیر زمینه ناهمگام را دارد."
msgid"Raises :exc:`TypeError` instead of :exc:`AttributeError` if *cm* is not an asynchronous context manager."
msgstr"اگر *cm* یک مدیر زمینهی ناهمگام نباشد، بهجای :exc:`AttributeError`، :exc:`TypeError` را پرتاب میکند."
msgid"Added support for arbitrary descriptors :meth:`!__aenter__` and :meth:`!__aexit__`."
msgstr"پشتیبانی از توصیفگرهای دلخواه :meth:`!__aenter__` و :meth:`!__aexit__` اضافه شد."
msgid"Similar to :meth:`ExitStack.push` but expects either an asynchronous context manager or a coroutine function."
msgstr"مشابه :meth:`ExitStack.push` است، اما انتظار دارد که یا یک مدیر زمینه ناهمگام باشد یا یک تابع همروال."
msgid"Added support for arbitrary descriptors :meth:`!__aexit__`."
msgstr"پشتیبانی از توصیفگرهای دلخواه :meth:`!__aexit__` اضافه شد."
msgid"Similar to :meth:`ExitStack.callback` but expects a coroutine function."
msgstr"مشابه :meth:`ExitStack.callback` است، اما یک تابع همروال انتظار میرود."
msgid"Similar to :meth:`ExitStack.close` but properly handles awaitables."
msgstr"مشابه :meth:`ExitStack.close` است، اما awaitableها را بهدرستی مدیریت میکند."
msgid"Continuing the example for :deco:`asynccontextmanager`::"
msgstr"در ادامهی مثال برای :deco:`asynccontextmanager`::"
" # All opened connections will automatically be released at the end of\n"
" # the async with statement, even if attempts to open a connection\n"
" # later in the list raise an exception."
msgid"Examples and Recipes"
msgstr"مثالها و دستور پختها"
msgid"This section describes some examples and recipes for making effective use of the tools provided by :mod:`!contextlib`."
msgstr"این بخش چند مثال و راهکار را برای استفادهی مؤثر از ابزارهای فراهمشده توسط :mod:`!contextlib` شرح میدهد."
msgid"Supporting a variable number of context managers"
msgstr"پشتیبانی از تعداد متغیری از مدیران زمینه"
msgid"The primary use case for :class:`ExitStack` is the one given in the class documentation: supporting a variable number of context managers and other cleanup operations in a single :keyword:`with` statement. The variability may come from the number of context managers needed being driven by user input (such as opening a user specified collection of files), or from some of the context managers being optional::"
msgstr"کاربرد اصلی :class:`ExitStack` همان موردی است که در مستندات کلاس آمده است: پشتیبانی از تعداد متغیری از مدیرهای زمینه و سایر عملیات پاکسازی در یک دستور :keyword:`with`. این تغییرپذیری ممکن است ناشی از تعداد مدیرهای زمینه مورد نیاز باشد که توسط ورودی کاربر تعیین میشود (مانند باز کردن مجموعهای از پروندههای مشخصشده توسط کاربر)، یا ناشی از اختیاری بودن برخی از مدیرهای زمینه باشد::"
" # Perform operations that use the acquired resources"
msgid"As shown, :class:`ExitStack` also makes it quite easy to use :keyword:`with` statements to manage arbitrary resources that don't natively support the context management protocol."
msgstr"همانطور که نشان داده شد، :class:`ExitStack` همچنین استفاده از دستورهای :keyword:`with` را برای مدیریت منابع دلخواهی که بهصورت ذاتی از پروتکل مدیریت زمینه پشتیبانی نمیکنند، بسیار آسان میسازد."
msgid"Catching exceptions from ``__enter__`` methods"
msgstr"گرفتن استثناها از متدهای ``__enter__``"
msgid"It is occasionally desirable to catch exceptions from an :meth:`~object.__enter__` method implementation, *without* inadvertently catching exceptions from the :keyword:`with` statement body or the context manager's :meth:`~object.__exit__` method. By using :class:`ExitStack` the steps in the context management protocol can be separated slightly in order to allow this::"
msgstr"گاهی اوقات مطلوب است استثناهای پیادهسازی متد :meth:`~object.__enter__` گرفته شوند، *بدون* آنکه بهطور ناخواسته استثناهای بدنهی دستور :keyword:`with` یا متد :meth:`~object.__exit__` مدیر زمینه گرفته شوند. با استفاده از :class:`ExitStack` میتوان مراحل پروتکل مدیریت زمینه را کمی از هم جدا کرد تا این امکان فراهم شود::"
msgid""
"stack = ExitStack()\n"
"try:\n"
" x = stack.enter_context(cm)\n"
"except Exception:\n"
" # handle __enter__ exception\n"
"else:\n"
" with stack:\n"
" # Handle normal case"
msgstr""
"stack = ExitStack()\n"
"try:\n"
" x = stack.enter_context(cm)\n"
"except Exception:\n"
" # handle __enter__ exception\n"
"else:\n"
" with stack:\n"
" # Handle normal case"
msgid"Actually needing to do this is likely to indicate that the underlying API should be providing a direct resource management interface for use with :keyword:`try`/:keyword:`except`/:keyword:`finally` statements, but not all APIs are well designed in that regard. When a context manager is the only resource management API provided, then :class:`ExitStack` can make it easier to handle various situations that can't be handled directly in a :keyword:`with` statement."
msgstr"نیاز واقعی به انجام این کار احتمالاً نشان میدهد که API زیربنایی باید یک رابط مستقیم مدیریت منابع برای استفاده در دستورهای :keyword:`try`/:keyword:`except`/:keyword:`finally` فراهم کند، اما همهی APIها از این نظر بهخوبی طراحی نشدهاند. هنگامی که یک مدیر زمینه تنها API ارائهشده برای مدیریت منابع باشد، :class:`ExitStack` میتواند مدیریت موقعیتهای مختلفی را که نمیتوان آنها را مستقیماً در یک دستور :keyword:`with` مدیریت کرد، آسانتر کند."
msgid"Cleaning up in an ``__enter__`` implementation"
msgstr"پاکسازی در پیادهسازی ``__enter__``"
msgid"As noted in the documentation of :meth:`ExitStack.push`, this method can be useful in cleaning up an already allocated resource if later steps in the :meth:`~object.__enter__` implementation fail."
msgstr"همانطور که در مستندات :meth:`ExitStack.push` ذکر شده است، این متد میتواند در پاکسازی منبعی که از پیش تخصیص دادهشده است مفید باشد، اگر مراحل بعدی در پیادهسازی :meth:`~object.__enter__` با شکست مواجه شوند."
msgid"Here's an example of doing this for a context manager that accepts resource acquisition and release functions, along with an optional validation function, and maps them to the context management protocol::"
msgstr"در اینجا مثالی از انجام این کار برای یک مدیر زمینه آورده شده است که توابع تهیه و آزادسازی منبع را بههمراه یک تابع اعتبارسنجی اختیاری میپذیرد و آنها را به پروتکل مدیریت زمینه نگاشت میکند::"
" # The validation check passed and didn't raise an exception\n"
" # Accordingly, we want to keep the resource, and pass it\n"
" # back to our caller\n"
" stack.pop_all()\n"
"\n"
" def __enter__(self):\n"
" resource = self.acquire_resource()\n"
" with self._cleanup_on_error():\n"
" if not self.check_resource_ok(resource):\n"
" msg = \"Failed validation for {!r}\"\n"
" raise RuntimeError(msg.format(resource))\n"
" return resource\n"
"\n"
" def __exit__(self, *exc_details):\n"
" # We don't need to duplicate any of our resource release logic\n"
" self.release_resource()"
msgid"Replacing any use of ``try-finally`` and flag variables"
msgstr"جایگزینی هرگونه استفاده از ``try-finally`` و متغیرهای پرچمی"
msgid"A pattern you will sometimes see is a ``try-finally`` statement with a flag variable to indicate whether or not the body of the ``finally`` clause should be executed. In its simplest form (that can't already be handled just by using an ``except`` clause instead), it looks something like this::"
msgstr"الگویی که گاهی خواهید دید، یک دستور ``try-finally`` همراه با یک متغیر پرچم است که نشان میدهد آیا بدنهی بند ``finally`` باید اجرا شود یا خیر. در سادهترین شکل خود (که از قبل نتوان آن را صرفاً با استفاده از یک بند ``except`` مدیریت کرد)، چیزی شبیه به این است::"
msgid""
"cleanup_needed = True\n"
"try:\n"
" result = perform_operation()\n"
" if result:\n"
" cleanup_needed = False\n"
"finally:\n"
" if cleanup_needed:\n"
" cleanup_resources()"
msgstr""
"cleanup_needed = True\n"
"try:\n"
" result = perform_operation()\n"
" if result:\n"
" cleanup_needed = False\n"
"finally:\n"
" if cleanup_needed:\n"
" cleanup_resources()"
msgid"As with any ``try`` statement based code, this can cause problems for development and review, because the setup code and the cleanup code can end up being separated by arbitrarily long sections of code."
msgstr"مانند هر کد مبتنی بر دستور ``try``، این موضوع میتواند برای توسعه و بازبینی مشکلساز شود، زیرا ممکن است در نهایت کد راهاندازی و کد پاکسازی توسط بخشهایی از کد با طول دلخواه از هم جدا شوند."
msgid":class:`ExitStack` makes it possible to instead register a callback for execution at the end of a ``with`` statement, and then later decide to skip executing that callback::"
msgstr":class:`ExitStack` این امکان را فراهم میکند که در عوض، یک کالبک را برای اجرا در پایان یک دستور ``with`` ثبت کنید و سپس بعداً تصمیم بگیرید که از اجرای آن کالبک صرفنظر کنید::"