Repository navigation
Expand file tree
/
Copy pathcontextlib.po
More file actions
1311 lines (1139 loc) · 83.6 KB
/
Copy pathcontextlib.po
File metadata and controls
1311 lines (1139 loc) · 83.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SOME DESCRIPTIVE TITLE.
# Copyright (C) 2001 Python Software Foundation
# This file is distributed under the same license as the Python package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
# Translators:
# Alireza Shabani (Revisto) <theRevisto@gmail.com>, 2025
# Danial Behzadi <dani.behzi@ubuntu.com>, 2025
# Rafael Fontenelle <rffontenelle@gmail.com>, 2025
# Sepehr Rasouli <sepehrrasouli06@gmail.com>, 2026
#
msgid ""
msgstr ""
"Project-Id-Version: Python 3.14\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-09-16 12:56+0000\n"
"PO-Revision-Date: 2026-09-14 10:22+0330\n"
"Last-Translator: Sepehr Rasouli <sepehrrasouli06@gmail.com>, 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:`کلاس پایه انتزاعی <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 file or file-like object."
msgstr "مدیر زمینه برای تغییر مسیر موقت :data:`sys.stdout` به یک پرونده یا شیء شبهپرونده دیگر."
msgid "This tool adds flexibility to existing functions or classes whose output is hardwired to stdout."
msgstr "این ابزار، انعطافپذیری را به توابع یا کلاسهای موجودی میافزاید که خروجی آنها به 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 :data:`sys.stderr` to another file or file-like object."
msgstr "مشابه :func:`~contextlib.redirect_stdout` است، اما :data:`sys.stderr` را به پرونده یا شیء شبهپرونده دیگری تغییر مسیر میدهد."
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 "Similar to :class:`ContextDecorator` but only for asynchronous functions."
msgstr "مشابه :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 "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 "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 "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 "Similar to :meth:`ExitStack.push` but expects either an asynchronous context manager or a coroutine function."
msgstr "مشابه :meth:`ExitStack.push` است، اما انتظار دارد که یا یک مدیر زمینه ناهمگام باشد یا یک تابع همروال."
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"