# SOME DESCRIPTIVE TITLE. # Copyright (C) 2001 Python Software Foundation # This file is distributed under the same license as the Python package. # FIRST AUTHOR , YEAR. # # Translators: # Alireza Shabani (Revisto) , 2025 # Danial Behzadi , 2025 # Rafael Fontenelle , 2025 # Sepehr Rasouli , 2026 # arman mohebali , 2026 # msgid "" msgstr "" "Project-Id-Version: Python 3.14\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2026-10-01 14:41+0330\n" "PO-Revision-Date: 2026-09-14 10:22+0330\n" "Last-Translator: Sepehr Rasouli , 2026\n" "Language-Team: Persian (https://github.com/python/python-docs-fa/)\n" "Language: fa\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" "X-Generator: Poedit 3.6\n" msgid ":mod:`!contextlib` --- Utilities for :keyword:`!with`\\ -statement contexts" msgstr ":mod:`!contextlib` --- ابزارهایی برای زمینه‌های دستور :keyword:`!with`" msgid "**Source code:** :source:`Lib/contextlib.py`" msgstr "**کد منبع:** :source:`Lib/contextlib.py`" 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:`کلاس پایه انتزاعی ` برای کلاس‌هایی که :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:`کلاس پایه انتزاعی ` برای کلاس‌هایی که :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:`دکوراتور ` است که می‌توان از آن برای تعریف یک تابع کارخانه‌ای برای مدیران زمینه‌ی دستور :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:`تولیدگر ` را برگرداند. این پیمایش‌گر باید دقیقاً یک مقدار تولید کند که در صورت وجود بند :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 `." msgstr "مشابه :deco:`~contextlib.contextmanager`، اما یک :ref:`مدیر زمینه ناهمگام ` ایجاد می‌کند." 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:`دکوراتور ` است که می‌توان از آن برای تعریف یک تابع کارخانه‌ای برای مدیران زمینه ناهمگام دستور :keyword:`async with` استفاده کرد، بدون نیاز به ایجاد یک کلاس یا متدهای جداگانه‌ی :meth:`~object.__aenter__` و :meth:`~object.__aexit__`. این دکوراتور باید به یک تابع :term:`تولیدگر ناهمگام ` اعمال شود." 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:`مدیر زمینه ` پشتیبانی می‌کنند، که *شیء* را در هنگام خروج از دستور :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 `::" msgstr "همچنین می‌تواند به‌عنوان جایگزینی برای :ref:`مدیران زمینه ناهمگام ` استفاده شود::" 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 `." msgstr "این مدیر زمینه :ref:`بازورودپذیر ` است." 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:`شیء پرونده ` دیگر." 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:`شیء پرونده ` دیگر." 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 "" "from contextlib import ContextDecorator\n" "\n" "class mycontext(ContextBaseClass, ContextDecorator):\n" " def __enter__(self):\n" " return self\n" "\n" " def __exit__(self, *exc):\n" " return False" msgstr "" "from contextlib import ContextDecorator\n" "\n" "class mycontext(ContextBaseClass, ContextDecorator):\n" " def __enter__(self):\n" " return self\n" "\n" " def __exit__(self, *exc):\n" " return False" 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 `, similar to :class:`ExitStack`, that supports combining both synchronous and asynchronous context managers, as well as having coroutines for cleanup logic." msgstr "یک :ref:`مدیر زمینه ناهمگام `، مشابه :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`::" msgid "" "async with AsyncExitStack() as stack:\n" " connections = [await stack.enter_async_context(get_connection())\n" " for i in range(5)]\n" " # 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." msgstr "" "async with AsyncExitStack() as stack:\n" " connections = [await stack.enter_async_context(get_connection())\n" " for i in range(5)]\n" " # 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`. این تغییرپذیری ممکن است ناشی از تعداد مدیرهای زمینه مورد نیاز باشد که توسط ورودی کاربر تعیین می‌شود (مانند باز کردن مجموعه‌ای از پرونده‌های مشخص‌شده توسط کاربر)، یا ناشی از اختیاری بودن برخی از مدیرهای زمینه باشد::" msgid "" "with ExitStack() as stack:\n" " for resource in resources:\n" " stack.enter_context(resource)\n" " if need_special_resource():\n" " special = acquire_special_resource()\n" " stack.callback(release_special_resource, special)\n" " # Perform operations that use the acquired resources" msgstr "" "with ExitStack() as stack:\n" " for resource in resources:\n" " stack.enter_context(resource)\n" " if need_special_resource():\n" " special = acquire_special_resource()\n" " stack.callback(release_special_resource, special)\n" " # 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 "در اینجا مثالی از انجام این کار برای یک مدیر زمینه آورده شده است که توابع تهیه و آزادسازی منبع را به‌همراه یک تابع اعتبارسنجی اختیاری می‌پذیرد و آن‌ها را به پروتکل مدیریت زمینه نگاشت می‌کند::" msgid "" "from contextlib import contextmanager, AbstractContextManager, ExitStack\n" "\n" "class ResourceManager(AbstractContextManager):\n" "\n" " def __init__(self, acquire_resource, release_resource, check_resource_ok=None):\n" " self.acquire_resource = acquire_resource\n" " self.release_resource = release_resource\n" " if check_resource_ok is None:\n" " def check_resource_ok(resource):\n" " return True\n" " self.check_resource_ok = check_resource_ok\n" "\n" " @contextmanager\n" " def _cleanup_on_error(self):\n" " with ExitStack() as stack:\n" " stack.push(self)\n" " yield\n" " # 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()" msgstr "" "from contextlib import contextmanager, AbstractContextManager, ExitStack\n" "\n" "class ResourceManager(AbstractContextManager):\n" "\n" " def __init__(self, acquire_resource, release_resource, check_resource_ok=None):\n" " self.acquire_resource = acquire_resource\n" " self.release_resource = release_resource\n" " if check_resource_ok is None:\n" " def check_resource_ok(resource):\n" " return True\n" " self.check_resource_ok = check_resource_ok\n" "\n" " @contextmanager\n" " def _cleanup_on_error(self):\n" " with ExitStack() as stack:\n" " stack.push(self)\n" " yield\n" " # 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`` ثبت کنید و سپس بعداً تصمیم بگیرید که از اجرای آن کال‌بک صرف‌نظر کنید::" msgid "" "from contextlib import ExitStack\n" "\n" "with ExitStack() as stack:\n" " stack.callback(cleanup_resources)\n" " result = perform_operation()\n" " if result:\n" " stack.pop_all()" msgstr "" "from contextlib import ExitStack\n" "\n" "with ExitStack() as stack:\n" " stack.callback(cleanup_resources)\n" " result = perform_operation()\n" " if result:\n" " stack.pop_all()" msgid "This allows the intended cleanup behaviour to be made explicit up front, rather than requiring a separate flag variable." msgstr "این امکان را فراهم می‌کند که رفتار پاک‌سازی موردنظر از همان ابتدا به‌صورت صریح مشخص شود، به جای اینکه به یک متغیر پرچم جداگانه نیاز باشد." msgid "If a particular application uses this pattern a lot, it can be simplified even further by means of a small helper class::" msgstr "اگر یک برنامه خاص زیاد از این الگو استفاده کند، می‌توان آن را با استفاده از یک کلاس کمکی کوچک حتی بیشتر ساده‌تر کرد::" msgid "" "from contextlib import ExitStack\n" "\n" "class Callback(ExitStack):\n" " def __init__(self, callback, /, *args, **kwds):\n" " super().__init__()\n" " self.callback(callback, *args, **kwds)\n" "\n" " def cancel(self):\n" " self.pop_all()\n" "\n" "with Callback(cleanup_resources) as cb:\n" " result = perform_operation()\n" " if result:\n" " cb.cancel()" msgstr "" "from contextlib import ExitStack\n" "\n" "class Callback(ExitStack):\n" " def __init__(self, callback, /, *args, **kwds):\n" " super().__init__()\n" " self.callback(callback, *args, **kwds)\n" "\n" " def cancel(self):\n" " self.pop_all()\n" "\n" "with Callback(cleanup_resources) as cb:\n" " result = perform_operation()\n" " if result:\n" " cb.cancel()" msgid "If the resource cleanup isn't already neatly bundled into a standalone function, then it is still possible to use the decorator form of :meth:`ExitStack.callback` to declare the resource cleanup in advance::" msgstr "اگر پاک‌سازی منابع از قبل به‌خوبی در یک تابع مستقل گنجانده نشده باشد، همچنان می‌توان از قالب دکوراتوری :meth:`ExitStack.callback` برای اعلام پاک‌سازی منابع از پیش استفاده کرد::" msgid "" "from contextlib import ExitStack\n" "\n" "with ExitStack() as stack:\n" " @stack.callback\n" " def cleanup_resources():\n" " ...\n" " result = perform_operation()\n" " if result:\n" " stack.pop_all()" msgstr "" "from contextlib import ExitStack\n" "\n" "with ExitStack() as stack:\n" " @stack.callback\n" " def cleanup_resources():\n" " ...\n" " result = perform_operation()\n" " if result:\n" " stack.pop_all()" msgid "Due to the way the decorator protocol works, a callback function declared this way cannot take any parameters. Instead, any resources to be released must be accessed as closure variables." msgstr "به دلیل نحوه‌ی کار پروتکل دکوراتور، یک تابع کال‌بک که به این روش تعریف شود، نمی‌تواند هیچ پارامتری بگیرد. در عوض، هر منبعی که باید آزاد شود، باید به‌صورت متغیرهای بستار (closure variables) در دسترس باشد." msgid "Using a context manager as a function decorator" msgstr "استفاده از مدیر زمینه به‌عنوان دکوراتور تابع" msgid ":class:`ContextDecorator` makes it possible to use a context manager in both an ordinary ``with`` statement and also as a function decorator." msgstr ":class:`ContextDecorator` امکان استفاده از یک مدیر زمینه را هم در یک دستور ``with`` معمولی و هم به‌عنوان دکوراتور تابع فراهم می‌کند." msgid "For example, it is sometimes useful to wrap functions or groups of statements with a logger that can track the time of entry and time of exit. Rather than writing both a function decorator and a context manager for the task, inheriting from :class:`ContextDecorator` provides both capabilities in a single definition::" msgstr "برای مثال، گاهی اوقات مفید است که توابع یا گروه‌هایی از دستورها را با یک گزارش‌گیر پوشش دهید تا بتواند زمان ورود و زمان خروج را پیگیری کند. به جای نوشتن هر دو دکوراتور تابع و مدیر زمینه برای این کار، ارث‌بری از :class:`ContextDecorator` هر دو قابلیت را در یک تعریف واحد فراهم می‌کند::" msgid "" "from contextlib import ContextDecorator\n" "import logging\n" "\n" "logging.basicConfig(level=logging.INFO)\n" "\n" "class track_entry_and_exit(ContextDecorator):\n" " def __init__(self, name):\n" " self.name = name\n" "\n" " def __enter__(self):\n" " logging.info('Entering: %s', self.name)\n" "\n" " def __exit__(self, exc_type, exc, exc_tb):\n" " logging.info('Exiting: %s', self.name)" msgstr "" "from contextlib import ContextDecorator\n" "import logging\n" "\n" "logging.basicConfig(level=logging.INFO)\n" "\n" "class track_entry_and_exit(ContextDecorator):\n" " def __init__(self, name):\n" " self.name = name\n" "\n" " def __enter__(self):\n" " logging.info('Entering: %s', self.name)\n" "\n" " def __exit__(self, exc_type, exc, exc_tb):\n" " logging.info('Exiting: %s', self.name)" msgid "Instances of this class can be used as both a context manager::" msgstr "نمونه‌های این کلاس را می‌توان هم به‌عنوان یک مدیر زمینه استفاده کرد::" msgid "" "with track_entry_and_exit('widget loader'):\n" " print('Some time consuming activity goes here')\n" " load_widget()" msgstr "" "with track_entry_and_exit('widget loader'):\n" " print('Some time consuming activity goes here')\n" " load_widget()" msgid "And also as a function decorator::" msgstr "و همچنین به‌عنوان دکوراتور تابع::" msgid "" "@track_entry_and_exit('widget loader')\n" "def activity():\n" " print('Some time consuming activity goes here')\n" " load_widget()" msgstr "" "@track_entry_and_exit('widget loader')\n" "def activity():\n" " print('Some time consuming activity goes here')\n" " load_widget()" msgid "Note that there is one additional limitation when using context managers as function decorators: there's no way to access the return value of :meth:`~object.__enter__`. If that value is needed, then it is still necessary to use an explicit ``with`` statement." msgstr "توجه داشته باشید که هنگام استفاده از مدیرهای زمینه به‌عنوان دکوراتورهای تابع، یک محدودیت اضافی وجود دارد: هیچ راهی برای دسترسی به مقدار بازگشتی :meth:`~object.__enter__` وجود ندارد. اگر به آن مقدار نیاز دارید، همچنان لازم است از یک دستور ``with`` صریح استفاده کنید." msgid ":pep:`343` - The \"with\" statement" msgstr ":pep:`343` - دستور «with»" msgid "The specification, background, and examples for the Python :keyword:`with` statement." msgstr "مشخصات، پیشینه و مثال‌های دستور :keyword:`with` در پایتون." msgid "Single use, reusable and reentrant context managers" msgstr "مدیران زمینه‌ی یک‌بارمصرف، بازاستفاده‌پذیر و بازورودپذیر" msgid "Most context managers are written in a way that means they can only be used effectively in a :keyword:`with` statement once. These single use context managers must be created afresh each time they're used - attempting to use them a second time will trigger an exception or otherwise not work correctly." msgstr "بیشتر مدیرهای زمینه به‌گونه‌ای نوشته شده‌اند که فقط یک بار می‌توان به‌طور مؤثر در یک دستور :keyword:`with` از آن‌ها استفاده کرد. این مدیرهای زمینه‌ی یک‌بارمصرف باید هر بار که استفاده می‌شوند، از نو ایجاد شوند؛ تلاش برای استفاده از آن‌ها برای بار دوم باعث پرتاب یک استثنا می‌شود یا در غیر این صورت به‌درستی کار نخواهد کرد." msgid "This common limitation means that it is generally advisable to create context managers directly in the header of the :keyword:`with` statement where they are used (as shown in all of the usage examples above)." msgstr "این محدودیت رایج به این معناست که معمولاً توصیه می‌شود مدیرهای زمینه را مستقیماً در سرآیند دستور :keyword:`with` در محل استفاده ایجاد کنید (همان‌طور که در تمام مثال‌های استفاده بالا نشان داده شده است)." msgid "Files are an example of effectively single use context managers, since the first :keyword:`with` statement will close the file, preventing any further IO operations using that file object." msgstr "پرونده‌ها نمونه‌ای از مدیران زمینه‌ای هستند که عملاً یک‌بارمصرف‌اند، زیرا اولین دستور :keyword:`with` پرونده را خواهد بست و از هرگونه عملیات ورودی/خروجی بیشتر با استفاده از آن شیء پرونده جلوگیری می‌کند." msgid "Context managers created using :deco:`contextmanager` are also single use context managers, and will complain about the underlying generator failing to yield if an attempt is made to use them a second time::" msgstr "مدیرهای زمینه‌ای که با استفاده از :deco:`contextmanager` ایجاد می‌شوند نیز مدیرهای زمینه یک‌بارمصرف هستند و اگر تلاش شود برای بار دوم از آن‌ها استفاده شود، از yield نکردن تولیدگر زیربنایی شکایت خواهند کرد::" msgid "" ">>> from contextlib import contextmanager\n" ">>> @contextmanager\n" "... def singleuse():\n" "... print(\"Before\")\n" "... yield\n" "... print(\"After\")\n" "...\n" ">>> cm = singleuse()\n" ">>> with cm:\n" "... pass\n" "...\n" "Before\n" "After\n" ">>> with cm:\n" "... pass\n" "...\n" "Traceback (most recent call last):\n" " ...\n" "RuntimeError: generator didn't yield" msgstr "" ">>> from contextlib import contextmanager\n" ">>> @contextmanager\n" "... def singleuse():\n" "... print(\"Before\")\n" "... yield\n" "... print(\"After\")\n" "...\n" ">>> cm = singleuse()\n" ">>> with cm:\n" "... pass\n" "...\n" "Before\n" "After\n" ">>> with cm:\n" "... pass\n" "...\n" "Traceback (most recent call last):\n" " ...\n" "RuntimeError: generator didn't yield" msgid "Reentrant context managers" msgstr "مدیرهای زمینه‌ی بازورودپذیر" msgid "More sophisticated context managers may be \"reentrant\". These context managers can not only be used in multiple :keyword:`with` statements, but may also be used *inside* a :keyword:`!with` statement that is already using the same context manager." msgstr "مدیرهای زمینه پیشرفته‌تر ممکن است «بازورودپذیر» (reentrant) باشند. این مدیرهای زمینه نه‌تنها می‌توانند در چندین دستور :keyword:`with` استفاده شوند، بلکه ممکن است *داخل* یک دستور :keyword:`!with` که از قبل از همان مدیر زمینه استفاده می‌کند نیز استفاده شوند." msgid ":class:`threading.RLock` is an example of a reentrant context manager, as are :func:`suppress`, :func:`redirect_stdout`, and :func:`chdir`. Here's a very simple example of reentrant use::" msgstr ":class:`threading.RLock` نمونه‌ای از یک مدیر زمینه‌ی بازورودپذیر است، همان‌طور که :func:`suppress`، :func:`redirect_stdout` و :func:`chdir` نیز چنین هستند. در اینجا یک نمونه‌ی بسیار ساده از استفاده‌ی بازورودپذیر آمده است::" msgid "" ">>> from contextlib import redirect_stdout\n" ">>> from io import StringIO\n" ">>> stream = StringIO()\n" ">>> write_to_stream = redirect_stdout(stream)\n" ">>> with write_to_stream:\n" "... print(\"This is written to the stream rather than stdout\")\n" "... with write_to_stream:\n" "... print(\"This is also written to the stream\")\n" "...\n" ">>> print(\"This is written directly to stdout\")\n" "This is written directly to stdout\n" ">>> print(stream.getvalue())\n" "This is written to the stream rather than stdout\n" "This is also written to the stream" msgstr "" ">>> from contextlib import redirect_stdout\n" ">>> from io import StringIO\n" ">>> stream = StringIO()\n" ">>> write_to_stream = redirect_stdout(stream)\n" ">>> with write_to_stream:\n" "... print(\"This is written to the stream rather than stdout\")\n" "... with write_to_stream:\n" "... print(\"This is also written to the stream\")\n" "...\n" ">>> print(\"This is written directly to stdout\")\n" "This is written directly to stdout\n" ">>> print(stream.getvalue())\n" "This is written to the stream rather than stdout\n" "This is also written to the stream" msgid "Real world examples of reentrancy are more likely to involve multiple functions calling each other and hence be far more complicated than this example." msgstr "نمونه‌های دنیای واقعی از بازورودپذیری (reentrancy) بیشتر احتمال دارد شامل چندین تابع باشند که یکدیگر را فراخوانی می‌کنند و بنابراین بسیار پیچیده‌تر از این مثال باشند." msgid "Note also that being reentrant is *not* the same thing as being thread safe. :func:`redirect_stdout`, for example, is definitely not thread safe, as it makes a global modification to the system state by binding :data:`sys.stdout` to a different stream." msgstr "همچنین توجه داشته باشید که بازورودپذیر بودن، همان ایمنی در برابر نخ‌ها *نیست*. برای مثال، :func:`redirect_stdout` به‌طور قطع نخ‌ایمن نیست، زیرا با اتصال :data:`sys.stdout` به یک جریان دیگر، تغییری سراسری در وضعیت سیستم ایجاد می‌کند." msgid "Reusable context managers" msgstr "مدیران زمینه قابل‌استفاده مجدد" msgid "Distinct from both single use and reentrant context managers are \"reusable\" context managers (or, to be completely explicit, \"reusable, but not reentrant\" context managers, since reentrant context managers are also reusable). These context managers support being used multiple times, but will fail (or otherwise not work correctly) if the specific context manager instance has already been used in a containing with statement." msgstr "مدیران زمینه «قابل‌استفاده مجدد» از هر دو نوع مدیر زمینه یک‌بارمصرف و بازورودپذیر متمایز هستند (یا برای صراحت کامل، مدیران زمینه «قابل‌استفاده مجدد، اما نه بازورودپذیر»، زیرا مدیران زمینه بازورودپذیر نیز قابل‌استفاده مجدد هستند). این مدیران زمینه از استفاده شدن چندین بار پشتیبانی می‌کنند، اما اگر نمونه خاص مدیر زمینه از قبل در یک دستور with دربرگیرنده استفاده شده باشد، شکست می‌خورند (یا در غیر این صورت به‌درستی کار نمی‌کنند)." msgid ":class:`threading.Lock` is an example of a reusable, but not reentrant, context manager (for a reentrant lock, it is necessary to use :class:`threading.RLock` instead)." msgstr ":class:`threading.Lock` نمونه‌ای از یک مدیر زمینه قابل استفاده مجدد، اما غیربازورود است (برای یک قفل بازورود، لازم است به‌جای آن از :class:`threading.RLock` استفاده شود)." msgid "Another example of a reusable, but not reentrant, context manager is :class:`ExitStack`, as it invokes *all* currently registered callbacks when leaving any with statement, regardless of where those callbacks were added::" msgstr "مثال دیگری از یک مدیر زمینه قابل‌استفاده مجدد، اما نه بازورودپذیر ، :class:`ExitStack` است، زیرا هنگام خروج از هر دستور with، *همه‌ی* کال‌بک‌های ثبت‌شده‌ی کنونی را، صرف‌نظر از اینکه آن کال‌بک‌ها کجا افزوده شده باشند، فراخوانی می‌کند::" msgid "" ">>> from contextlib import ExitStack\n" ">>> stack = ExitStack()\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from first context\")\n" "... print(\"Leaving first context\")\n" "...\n" "Leaving first context\n" "Callback: from first context\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from second context\")\n" "... print(\"Leaving second context\")\n" "...\n" "Leaving second context\n" "Callback: from second context\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from outer context\")\n" "... with stack:\n" "... stack.callback(print, \"Callback: from inner context\")\n" "... print(\"Leaving inner context\")\n" "... print(\"Leaving outer context\")\n" "...\n" "Leaving inner context\n" "Callback: from inner context\n" "Callback: from outer context\n" "Leaving outer context" msgstr "" ">>> from contextlib import ExitStack\n" ">>> stack = ExitStack()\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from first context\")\n" "... print(\"Leaving first context\")\n" "...\n" "Leaving first context\n" "Callback: from first context\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from second context\")\n" "... print(\"Leaving second context\")\n" "...\n" "Leaving second context\n" "Callback: from second context\n" ">>> with stack:\n" "... stack.callback(print, \"Callback: from outer context\")\n" "... with stack:\n" "... stack.callback(print, \"Callback: from inner context\")\n" "... print(\"Leaving inner context\")\n" "... print(\"Leaving outer context\")\n" "...\n" "Leaving inner context\n" "Callback: from inner context\n" "Callback: from outer context\n" "Leaving outer context" msgid "As the output from the example shows, reusing a single stack object across multiple with statements works correctly, but attempting to nest them will cause the stack to be cleared at the end of the innermost with statement, which is unlikely to be desirable behaviour." msgstr "همان‌طور که خروجی مثال نشان می‌دهد، استفاده مجدد از یک شیء پشته در چندین دستور with به‌درستی کار می‌کند، اما تلاش برای تودرتو کردن آن‌ها باعث می‌شود پشته در پایان داخلی‌ترین دستور with پاک شود، که بعید است رفتار مطلوبی باشد." msgid "Using separate :class:`ExitStack` instances instead of reusing a single instance avoids that problem::" msgstr "استفاده از نمونه‌های جداگانه :class:`ExitStack` به جای استفاده مجدد از یک نمونه واحد، از آن مشکل اجتناب می‌کند::" msgid "" ">>> from contextlib import ExitStack\n" ">>> with ExitStack() as outer_stack:\n" "... outer_stack.callback(print, \"Callback: from outer context\")\n" "... with ExitStack() as inner_stack:\n" "... inner_stack.callback(print, \"Callback: from inner context\")\n" "... print(\"Leaving inner context\")\n" "... print(\"Leaving outer context\")\n" "...\n" "Leaving inner context\n" "Callback: from inner context\n" "Leaving outer context\n" "Callback: from outer context" msgstr "" ">>> from contextlib import ExitStack\n" ">>> with ExitStack() as outer_stack:\n" "... outer_stack.callback(print, \"Callback: from outer context\")\n" "... with ExitStack() as inner_stack:\n" "... inner_stack.callback(print, \"Callback: from inner context\")\n" "... print(\"Leaving inner context\")\n" "... print(\"Leaving outer context\")\n" "...\n" "Leaving inner context\n" "Callback: from inner context\n" "Leaving outer context\n" "Callback: from outer context" #~ msgid "Similar to :class:`ContextDecorator` but only for asynchronous functions." #~ msgstr "مشابه :class:`ContextDecorator` اما فقط برای توابع ناهمگام."