-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathlibrary.po
1403 lines (1240 loc) · 54.9 KB
/
library.po
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
# Copyright (C) 2001-2024, Python Software Foundation
# This file is distributed under the same license as the Python package.
#
# Translators:
# jerrychen <[email protected]>, 2016
# Ching-Lung Chuang, 2015
msgid ""
msgstr ""
"Project-Id-Version: Python 3.13\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-09-23 07:52+0800\n"
"PO-Revision-Date: 2023-02-18 13:22+0800\n"
"Last-Translator: Adrian Liaw <[email protected]>\n"
"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-"
"tw)\n"
"Language: zh_TW\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Poedit 3.2.2\n"
#: ../../faq/library.rst:5
msgid "Library and Extension FAQ"
msgstr "函式庫和擴充功能的常見問題"
#: ../../faq/library.rst:8
msgid "Contents"
msgstr "目錄"
#: ../../faq/library.rst:12
msgid "General Library Questions"
msgstr "常見函式問題"
#: ../../faq/library.rst:15
msgid "How do I find a module or application to perform task X?"
msgstr "我如何找到執行任務 X 的模組或應用程式?"
#: ../../faq/library.rst:17
#, fuzzy
msgid ""
"Check :ref:`the Library Reference <library-index>` to see if there's a "
"relevant standard library module. (Eventually you'll learn what's in the "
"standard library and will be able to skip this step.)"
msgstr ""
"檢查 :ref:`函式庫參考 <library-index>` 以查看是否有相關的標準函式庫模組。 "
"(最終你將了解標準函式庫中的內容,並且能夠跳過這一步。)"
#: ../../faq/library.rst:21
#, fuzzy
msgid ""
"For third-party packages, search the `Python Package Index <https://pypi."
"org>`_ or try `Google <https://www.google.com>`_ or another web search "
"engine. Searching for \"Python\" plus a keyword or two for your topic of "
"interest will usually find something helpful."
msgstr ""
"對於第三方包,搜尋`Python 包索引<https://pypi.org>`_ 或嘗試`Google <https://"
"www.google.com>`_ 或其他網路搜尋引擎。搜尋 \"Python\" 加上你感興趣的主題的一"
"兩個關鍵字通常會找到有用的東西。"
#: ../../faq/library.rst:28
msgid "Where is the math.py (socket.py, regex.py, etc.) source file?"
msgstr "哪裡可以找到 math.py (socket.py, regex.py, 等...) 來源檔案?"
#: ../../faq/library.rst:30
#, fuzzy
msgid ""
"If you can't find a source file for a module it may be a built-in or "
"dynamically loaded module implemented in C, C++ or other compiled language. "
"In this case you may not have the source file or it may be something like :"
"file:`mathmodule.c`, somewhere in a C source directory (not on the Python "
"Path)."
msgstr ""
"如果找不到模組的源檔案,它可能是用 C、C++ 或其他編譯語言實作的內置或動態載入"
"的模組。在這種情況下,你可能沒有源檔案,或者它可能類似於 :file:`mathmodule."
"c`,位於 C 源目錄中(不在 Python 路徑中)。"
#: ../../faq/library.rst:35
msgid "There are (at least) three kinds of modules in Python:"
msgstr "有(至少)三種 Python 模組:"
#: ../../faq/library.rst:37
msgid "modules written in Python (.py);"
msgstr "以 Python 編寫的模組 (.py);"
#: ../../faq/library.rst:38
msgid ""
"modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);"
msgstr "用 C 編寫並動態載入的模組(.dll、.pyd、.so、.sl 等);"
#: ../../faq/library.rst:39
msgid ""
"modules written in C and linked with the interpreter; to get a list of "
"these, type::"
msgstr "用 C 編寫並與直譯器鏈接的模組;要獲得這些 list,請輸入: ::"
#: ../../faq/library.rst:42
msgid ""
"import sys\n"
"print(sys.builtin_module_names)"
msgstr ""
#: ../../faq/library.rst:47
msgid "How do I make a Python script executable on Unix?"
msgstr "我如何使 Python script 執行在 Unix?"
#: ../../faq/library.rst:49
#, fuzzy
msgid ""
"You need to do two things: the script file's mode must be executable and the "
"first line must begin with ``#!`` followed by the path of the Python "
"interpreter."
msgstr ""
"你需要作兩件事:腳本程式必須可以被執行而且第一行必須 ``#!`` 開頭後面接上 "
"Python 直譯器的路徑。"
#: ../../faq/library.rst:53
#, fuzzy
msgid ""
"The first is done by executing ``chmod +x scriptfile`` or perhaps ``chmod "
"755 scriptfile``."
msgstr ""
"第一個是通過執行 ``chmod +x scriptfile`` 或者 ``chmod 755 scriptfile`` 來完成"
"的。"
#: ../../faq/library.rst:56
#, fuzzy
msgid ""
"The second can be done in a number of ways. The most straightforward way is "
"to write ::"
msgstr "第二個可以通過多種方式完成。最直接的方法是寫: ::"
#: ../../faq/library.rst:59
msgid "#!/usr/local/bin/python"
msgstr "#!/usr/local/bin/python"
#: ../../faq/library.rst:61
#, fuzzy
msgid ""
"as the very first line of your file, using the pathname for where the Python "
"interpreter is installed on your platform."
msgstr "作為檔案的第一行,使用 Python 直譯器在你的平台上的安裝位置的路徑名。"
#: ../../faq/library.rst:64
#, fuzzy
msgid ""
"If you would like the script to be independent of where the Python "
"interpreter lives, you can use the :program:`env` program. Almost all Unix "
"variants support the following, assuming the Python interpreter is in a "
"directory on the user's :envvar:`PATH`::"
msgstr ""
"如果你希望腳本獨立於 Python 直譯器所在的位置,你可以使用 :program:`env` 程"
"式。幾乎所有 Unix 變體都支援以下內容,假設 Python 直譯器位於使用者的 :envvar:"
"`PATH` 上的目錄中: ::"
#: ../../faq/library.rst:69
msgid "#!/usr/bin/env python"
msgstr "#!/usr/bin/env python"
#: ../../faq/library.rst:71
#, fuzzy
msgid ""
"*Don't* do this for CGI scripts. The :envvar:`PATH` variable for CGI "
"scripts is often very minimal, so you need to use the actual absolute "
"pathname of the interpreter."
msgstr ""
"*不要*對 CGI 腳本執行此操作。 CGI 腳本的 :envvar:`PATH` 變數通常非常小,因此"
"你需要使用直譯器的實際絕對路徑名。"
#: ../../faq/library.rst:75
#, fuzzy
msgid ""
"Occasionally, a user's environment is so full that the :program:`/usr/bin/"
"env` program fails; or there's no env program at all. In that case, you can "
"try the following hack (due to Alex Rezinsky):"
msgstr ""
"有時,使用者的環境太滿以至於:program:`/usr/bin/env` 程式失敗;或者根本就沒有 "
"env 程式。在這種情況下,你可以嘗試以下 hack(由於 Alex Rezinsky):"
#: ../../faq/library.rst:79
msgid ""
"#! /bin/sh\n"
"\"\"\":\"\n"
"exec python $0 ${1+\"$@\"}\n"
"\"\"\""
msgstr ""
"#! /bin/sh\n"
"\"\"\":\"\n"
"exec python $0 ${1+\"$@\"}\n"
"\"\"\""
#: ../../faq/library.rst:86
#, fuzzy
msgid ""
"The minor disadvantage is that this defines the script's __doc__ string. "
"However, you can fix that by adding ::"
msgstr ""
"次要缺點是這定義了腳本的 __doc__ 字串。但是,你可以通過新增來解決這個問"
"題: ::"
#: ../../faq/library.rst:89
msgid "__doc__ = \"\"\"...Whatever...\"\"\""
msgstr ""
#: ../../faq/library.rst:94
msgid "Is there a curses/termcap package for Python?"
msgstr "是否有適用於 Python 的 curses/termcap 套件?"
#: ../../faq/library.rst:98
#, fuzzy
msgid ""
"For Unix variants: The standard Python source distribution comes with a "
"curses module in the :source:`Modules` subdirectory, though it's not "
"compiled by default. (Note that this is not available in the Windows "
"distribution -- there is no curses module for Windows.)"
msgstr ""
"對於 Unix 變體:標準 Python 源程式碼分發版在 :source:`Modules` 子目錄中附帶一"
"個 curses 模組,但預設情況下未編譯它。(請注意,這在 Windows 發行版中不可用——"
"沒有適用於 Windows 的 curses 模組。)"
#: ../../faq/library.rst:103
#, fuzzy
msgid ""
"The :mod:`curses` module supports basic curses features as well as many "
"additional functions from ncurses and SYSV curses such as colour, "
"alternative character set support, pads, and mouse support. This means the "
"module isn't compatible with operating systems that only have BSD curses, "
"but there don't seem to be any currently maintained OSes that fall into this "
"category."
msgstr ""
":mod:`curses` 模組支援基本的 curses 功能以及 ncurses 和 SYSV curses 的許多附"
"加功能,例如顏色、替代字元集支援、鍵盤和鼠標支援。這意味著該模組與僅具有 BSD "
"curses 的作業系統不相容,但似乎沒有任何目前維護的作業系統屬於此類型。"
#: ../../faq/library.rst:111
msgid "Is there an equivalent to C's onexit() in Python?"
msgstr "Python 中是否有等同於 C 的 onexit() 的函式?"
#: ../../faq/library.rst:113
#, fuzzy
msgid ""
"The :mod:`atexit` module provides a register function that is similar to "
"C's :c:func:`!onexit`."
msgstr ""
":mod:`atexit` 模組提供了一個類似於 C 的 :c:func:`onexit` 的寄存器函式。"
#: ../../faq/library.rst:118
msgid "Why don't my signal handlers work?"
msgstr "為什麼我的信號處理程式不起作用?"
#: ../../faq/library.rst:120
#, fuzzy
msgid ""
"The most common problem is that the signal handler is declared with the "
"wrong argument list. It is called as ::"
msgstr "最常見的問題是信號處理程式是用錯誤的引數列表聲明的。它被稱為: ::"
#: ../../faq/library.rst:123
msgid "handler(signum, frame)"
msgstr "handler(signum, frame)"
#: ../../faq/library.rst:125
msgid "so it should be declared with two parameters::"
msgstr "所以它應該用兩個參數聲明: ::"
#: ../../faq/library.rst:127
msgid ""
"def handler(signum, frame):\n"
" ..."
msgstr ""
"def handler(signum, frame):\n"
" ..."
#: ../../faq/library.rst:132
msgid "Common tasks"
msgstr "常見課題"
#: ../../faq/library.rst:135
msgid "How do I test a Python program or component?"
msgstr "如何測試 Python 程式或元件?"
#: ../../faq/library.rst:137
#, fuzzy
msgid ""
"Python comes with two testing frameworks. The :mod:`doctest` module finds "
"examples in the docstrings for a module and runs them, comparing the output "
"with the expected output given in the docstring."
msgstr ""
"Python 帶有兩個測試框架。 :mod:`doctest` 模組在模組的文件字串中查詢示例並運行"
"它們,將輸出與文件字串中給出的預期輸出進行比較。"
#: ../../faq/library.rst:141
#, fuzzy
msgid ""
"The :mod:`unittest` module is a fancier testing framework modelled on Java "
"and Smalltalk testing frameworks."
msgstr ""
":mod:`unittest` 模組是一個更高階的測試框架,它以 Java 和 Smalltalk 測試框架為"
"模型。"
#: ../../faq/library.rst:144
#, fuzzy
msgid ""
"To make testing easier, you should use good modular design in your program. "
"Your program should have almost all functionality encapsulated in either "
"functions or class methods -- and this sometimes has the surprising and "
"delightful effect of making the program run faster (because local variable "
"accesses are faster than global accesses). Furthermore the program should "
"avoid depending on mutating global variables, since this makes testing much "
"more difficult to do."
msgstr ""
"為了使測試更容易,你應該在程式中使用良好的模組化設計。你的程式應該將幾乎所有"
"功能都封裝在函式或類別方法中——這有時會產生使程式運行得更快的令人驚訝和令人愉"
"快的效果(因為局部變數存取比全局存取更快)。此外,該程式應避免依賴於可變的全"
"局變數,因為這會使測試變得更加困難。"
#: ../../faq/library.rst:152
#, fuzzy
msgid "The \"global main logic\" of your program may be as simple as ::"
msgstr "你程式的「全局主邏輯」可能像一樣簡單: ::"
#: ../../faq/library.rst:154
msgid ""
"if __name__ == \"__main__\":\n"
" main_logic()"
msgstr ""
#: ../../faq/library.rst:157
#, fuzzy
msgid "at the bottom of the main module of your program."
msgstr "在你的程式主模組的底端。"
#: ../../faq/library.rst:159
#, fuzzy
msgid ""
"Once your program is organized as a tractable collection of function and "
"class behaviours, you should write test functions that exercise the "
"behaviours. A test suite that automates a sequence of tests can be "
"associated with each module. This sounds like a lot of work, but since "
"Python is so terse and flexible it's surprisingly easy. You can make coding "
"much more pleasant and fun by writing your test functions in parallel with "
"the \"production code\", since this makes it easy to find bugs and even "
"design flaws earlier."
msgstr ""
"一旦你的程式被組織為函式和類別行為的易於處理的集合,你就應該編寫測試函式來執"
"行這些行為。可將一系列測試自動化的測試套件與每個模組相關聯。這聽起來像是很多"
"工作,但由於 Python 如此簡潔和靈活,所以它非常容易。通過與 \"生產程式碼\" 並"
"行編寫測試函式,你可以使編碼變得更加愉快和有趣,因為這使得更早地發現錯誤甚至"
"設計缺陷變得容易。"
#: ../../faq/library.rst:167
#, fuzzy
msgid ""
"\"Support modules\" that are not intended to be the main module of a program "
"may include a self-test of the module. ::"
msgstr "不打算成為程式主要模組的 \"支援模組\" 可能包括模組的自檢: ::"
#: ../../faq/library.rst:170
msgid ""
"if __name__ == \"__main__\":\n"
" self_test()"
msgstr ""
#: ../../faq/library.rst:173
#, fuzzy
msgid ""
"Even programs that interact with complex external interfaces may be tested "
"when the external interfaces are unavailable by using \"fake\" interfaces "
"implemented in Python."
msgstr ""
"即使是與復雜外部介面交互的程式也可以在外部介面不可用時通過使用 Python 中實作"
"的 \"假\" 介面進行測試。"
#: ../../faq/library.rst:179
#, fuzzy
msgid "How do I create documentation from doc strings?"
msgstr "如何從文件字串建立文件?"
#: ../../faq/library.rst:181
#, fuzzy
msgid ""
"The :mod:`pydoc` module can create HTML from the doc strings in your Python "
"source code. An alternative for creating API documentation purely from "
"docstrings is `epydoc <https://epydoc.sourceforge.net/>`_. `Sphinx <https://"
"www.sphinx-doc.org>`_ can also include docstring content."
msgstr ""
":mod:`pydoc` 模組可以從 Python 源程式碼中的文件字串建立 HTML。純粹從文件字串"
"建立 API 文件的另一種方法是 `epydoc <https://epydoc.sourceforge.net/>`_。 "
"`Sphinx <https://www.sphinx-doc.org>`_ 也可以包含文件字串內容。"
#: ../../faq/library.rst:188
#, fuzzy
msgid "How do I get a single keypress at a time?"
msgstr "我如何一次獲得一個按鍵?"
#: ../../faq/library.rst:190
#, fuzzy
msgid ""
"For Unix variants there are several solutions. It's straightforward to do "
"this using curses, but curses is a fairly large module to learn."
msgstr ""
"對於 Unix 變體,有幾種解決方案。使用 curses 執行此操作很簡單,但 curses 是一"
"個需要學習的相當大的模組。"
#: ../../faq/library.rst:234
msgid "Threads"
msgstr "執行緒"
#: ../../faq/library.rst:237
msgid "How do I program using threads?"
msgstr "如何使用執行緒編寫程式?"
#: ../../faq/library.rst:239
#, fuzzy
msgid ""
"Be sure to use the :mod:`threading` module and not the :mod:`_thread` "
"module. The :mod:`threading` module builds convenient abstractions on top of "
"the low-level primitives provided by the :mod:`_thread` module."
msgstr ""
"請務必使用 :mod:`threading` 模組而不是 :mod:`_thread` 模組。 :mod:"
"`threading` 模組在 :mod:`_thread` 模組提供的低階原語之上構建方便的抽象。"
#: ../../faq/library.rst:245
msgid "None of my threads seem to run: why?"
msgstr "我的執行緒似乎都沒有運行:為什麼?"
#: ../../faq/library.rst:247
#, fuzzy
msgid ""
"As soon as the main thread exits, all threads are killed. Your main thread "
"is running too quickly, giving the threads no time to do any work."
msgstr ""
"一旦主執行緒退出,所有執行緒都會被殺死。你的主執行緒運行得太快,執行緒沒有時"
"間做任何工作。"
#: ../../faq/library.rst:250
#, fuzzy
msgid ""
"A simple fix is to add a sleep to the end of the program that's long enough "
"for all the threads to finish::"
msgstr ""
"一個簡單的修復方法是在程式末尾新增一個足夠長的睡眠,讓所有執行緒都完成: ::"
#: ../../faq/library.rst:253
msgid ""
"import threading, time\n"
"\n"
"def thread_task(name, n):\n"
" for i in range(n):\n"
" print(name, i)\n"
"\n"
"for i in range(10):\n"
" T = threading.Thread(target=thread_task, args=(str(i), i))\n"
" T.start()\n"
"\n"
"time.sleep(10) # <---------------------------!"
msgstr ""
#: ../../faq/library.rst:265
#, fuzzy
msgid ""
"But now (on many platforms) the threads don't run in parallel, but appear to "
"run sequentially, one at a time! The reason is that the OS thread scheduler "
"doesn't start a new thread until the previous thread is blocked."
msgstr ""
"但是現在(在許多平台上)執行緒不是並行運行的,而是看起來是順序運行的,一次一"
"個!原因是作業系統執行緒調度程式在前一個執行緒被阻塞之前不會啟動一個新執行"
"緒。"
#: ../../faq/library.rst:269
#, fuzzy
msgid "A simple fix is to add a tiny sleep to the start of the run function::"
msgstr "一個簡單的修復方法是在運行函式的開頭新增一個小睡眠: ::"
#: ../../faq/library.rst:271
msgid ""
"def thread_task(name, n):\n"
" time.sleep(0.001) # <--------------------!\n"
" for i in range(n):\n"
" print(name, i)\n"
"\n"
"for i in range(10):\n"
" T = threading.Thread(target=thread_task, args=(str(i), i))\n"
" T.start()\n"
"\n"
"time.sleep(10)"
msgstr ""
#: ../../faq/library.rst:282
#, fuzzy
msgid ""
"Instead of trying to guess a good delay value for :func:`time.sleep`, it's "
"better to use some kind of semaphore mechanism. One idea is to use the :mod:"
"`queue` module to create a queue object, let each thread append a token to "
"the queue when it finishes, and let the main thread read as many tokens from "
"the queue as there are threads."
msgstr ""
"與其嘗試為 :func:`time.sleep` 猜測一個好的延遲值,不如使用某種信號量機制。一"
"種想法是使用 :mod:`queue` 模組建立一個隊列物件,讓每個執行緒在完成時向隊列新"
"增一個權杖,並讓主執行緒從隊列中讀取與執行緒數一樣多的權杖。"
#: ../../faq/library.rst:290
#, fuzzy
msgid "How do I parcel out work among a bunch of worker threads?"
msgstr "我如何在一堆工作執行緒中分配工作?"
#: ../../faq/library.rst:292
#, fuzzy
msgid ""
"The easiest way is to use the :mod:`concurrent.futures` module, especially "
"the :mod:`~concurrent.futures.ThreadPoolExecutor` class."
msgstr ""
"最簡單的方法是使用 :mod:`concurrent.futures` 模組,尤其是 :mod:`~concurrent."
"futures.ThreadPoolExecutor` 類別。"
#: ../../faq/library.rst:295
#, fuzzy
msgid ""
"Or, if you want fine control over the dispatching algorithm, you can write "
"your own logic manually. Use the :mod:`queue` module to create a queue "
"containing a list of jobs. The :class:`~queue.Queue` class maintains a list "
"of objects and has a ``.put(obj)`` method that adds items to the queue and a "
"``.get()`` method to return them. The class will take care of the locking "
"necessary to ensure that each job is handed out exactly once."
msgstr ""
"或者,如果你想對調度演算法進行精細控制,你可以手動編寫自己的邏輯。使用 :mod:"
"`queue` 模組建立一個包含作業列表的隊列。 :class:`~queue.Queue` 類別維護一個物"
"件列表,並有一個 `.put(obj)`` 方法將項目新增到隊列和一個 ``.get()`` 方法回傳"
"它們。該類別將負責必要的鎖定,以確保每個作業都恰好分發一次。"
#: ../../faq/library.rst:302
#, fuzzy
msgid "Here's a trivial example::"
msgstr "這是一個簡單的例子: ::"
#: ../../faq/library.rst:304
msgid ""
"import threading, queue, time\n"
"\n"
"# The worker thread gets jobs off the queue. When the queue is empty, it\n"
"# assumes there will be no more work and exits.\n"
"# (Realistically workers will run until terminated.)\n"
"def worker():\n"
" print('Running worker')\n"
" time.sleep(0.1)\n"
" while True:\n"
" try:\n"
" arg = q.get(block=False)\n"
" except queue.Empty:\n"
" print('Worker', threading.current_thread(), end=' ')\n"
" print('queue empty')\n"
" break\n"
" else:\n"
" print('Worker', threading.current_thread(), end=' ')\n"
" print('running with argument', arg)\n"
" time.sleep(0.5)\n"
"\n"
"# Create queue\n"
"q = queue.Queue()\n"
"\n"
"# Start a pool of 5 workers\n"
"for i in range(5):\n"
" t = threading.Thread(target=worker, name='worker %i' % (i+1))\n"
" t.start()\n"
"\n"
"# Begin adding work to the queue\n"
"for i in range(50):\n"
" q.put(i)\n"
"\n"
"# Give threads time to run\n"
"print('Main thread sleeping')\n"
"time.sleep(5)"
msgstr ""
#: ../../faq/library.rst:340
#, fuzzy
msgid "When run, this will produce the following output:"
msgstr "運行時,這將產生以下輸出:"
#: ../../faq/library.rst:342
msgid ""
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Main thread sleeping\n"
"Worker <Thread(worker 1, started 130283832797456)> running with argument 0\n"
"Worker <Thread(worker 2, started 130283824404752)> running with argument 1\n"
"Worker <Thread(worker 3, started 130283816012048)> running with argument 2\n"
"Worker <Thread(worker 4, started 130283807619344)> running with argument 3\n"
"Worker <Thread(worker 5, started 130283799226640)> running with argument 4\n"
"Worker <Thread(worker 1, started 130283832797456)> running with argument 5\n"
"..."
msgstr ""
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Running worker\n"
"Main thread sleeping\n"
"Worker <Thread(worker 1, started 130283832797456)> running with argument 0\n"
"Worker <Thread(worker 2, started 130283824404752)> running with argument 1\n"
"Worker <Thread(worker 3, started 130283816012048)> running with argument 2\n"
"Worker <Thread(worker 4, started 130283807619344)> running with argument 3\n"
"Worker <Thread(worker 5, started 130283799226640)> running with argument 4\n"
"Worker <Thread(worker 1, started 130283832797456)> running with argument 5\n"
"..."
#: ../../faq/library.rst:358
#, fuzzy
msgid ""
"Consult the module's documentation for more details; the :class:`~queue."
"Queue` class provides a featureful interface."
msgstr ""
"有關更多詳細資訊,請參閱模組的文件; :class:`~queue.Queue` 類別提供了一個功能"
"強大的介面。"
#: ../../faq/library.rst:363
#, fuzzy
msgid "What kinds of global value mutation are thread-safe?"
msgstr "什麼樣的全局值突變是執行緒安全的?"
#: ../../faq/library.rst:365
#, fuzzy
msgid ""
"A :term:`global interpreter lock` (GIL) is used internally to ensure that "
"only one thread runs in the Python VM at a time. In general, Python offers "
"to switch among threads only between bytecode instructions; how frequently "
"it switches can be set via :func:`sys.setswitchinterval`. Each bytecode "
"instruction and therefore all the C implementation code reached from each "
"instruction is therefore atomic from the point of view of a Python program."
msgstr ""
"內部使用 :term:`全局直譯器鎖 (GIL, global interpreter lock)`\\ 來確保一次只有"
"一個執行緒在 Python VM 中運行。通常,Python 僅提供位元組碼指令之間的執行緒切"
"換;可以通過 :func:`sys.setswitchinterval` 設定它切換的頻率。因此,從 Python "
"程式的角度來看,每條位元組碼指令以及從每條指令到達的所有 C 實作程式碼都是原子"
"的。"
#: ../../faq/library.rst:372
#, fuzzy
msgid ""
"In theory, this means an exact accounting requires an exact understanding of "
"the PVM bytecode implementation. In practice, it means that operations on "
"shared variables of built-in data types (ints, lists, dicts, etc) that "
"\"look atomic\" really are."
msgstr ""
"從理論上講,這意味著準確的記賬需要對 PVM 位元組碼實作有準確的理解。實際上,這"
"意味著對 \"看起來原子\" 的內置資料型別(整數、列表、字典等)的共享變數的操作"
"確實是原子的。"
#: ../../faq/library.rst:377
#, fuzzy
msgid ""
"For example, the following operations are all atomic (L, L1, L2 are lists, "
"D, D1, D2 are dicts, x, y are objects, i, j are ints)::"
msgstr ""
"例如,以下操作都是原子的(L、L1、L2 是列表,D、D1、D2 是字典,x、y 是物件,"
"i、j 是整數): ::"
#: ../../faq/library.rst:380
msgid ""
"L.append(x)\n"
"L1.extend(L2)\n"
"x = L[i]\n"
"x = L.pop()\n"
"L1[i:j] = L2\n"
"L.sort()\n"
"x = y\n"
"x.field = y\n"
"D[x] = y\n"
"D1.update(D2)\n"
"D.keys()"
msgstr ""
"L.append(x)\n"
"L1.extend(L2)\n"
"x = L[i]\n"
"x = L.pop()\n"
"L1[i:j] = L2\n"
"L.sort()\n"
"x = y\n"
"x.field = y\n"
"D[x] = y\n"
"D1.update(D2)\n"
"D.keys()"
#: ../../faq/library.rst:392
msgid "These aren't::"
msgstr "這些不是: ::"
#: ../../faq/library.rst:394
msgid ""
"i = i+1\n"
"L.append(L[-1])\n"
"L[i] = L[j]\n"
"D[x] = D[x] + 1"
msgstr ""
"i = i+1\n"
"L.append(L[-1])\n"
"L[i] = L[j]\n"
"D[x] = D[x] + 1"
#: ../../faq/library.rst:399
#, fuzzy
msgid ""
"Operations that replace other objects may invoke those other objects' :meth:"
"`~object.__del__` method when their reference count reaches zero, and that "
"can affect things. This is especially true for the mass updates to "
"dictionaries and lists. When in doubt, use a mutex!"
msgstr ""
"替換其他物件的操作可能會在引用計數達到零時叫用其他物件的 :meth:`__del__` 方"
"法,這可能會影響事情。對於字典和列表的大量更新尤其如此。如有疑問,請使用互斥"
"體!"
#: ../../faq/library.rst:406
msgid "Can't we get rid of the Global Interpreter Lock?"
msgstr "不能擺脫全局直譯器鎖嗎?"
#: ../../faq/library.rst:408
#, fuzzy
msgid ""
"The :term:`global interpreter lock` (GIL) is often seen as a hindrance to "
"Python's deployment on high-end multiprocessor server machines, because a "
"multi-threaded Python program effectively only uses one CPU, due to the "
"insistence that (almost) all Python code can only run while the GIL is held."
msgstr ""
":term:`global interpreter lock` (GIL) 通常被視為 Python 在高端多處理器服務器"
"機器上部署的障礙,因為多執行緒 Python 程式實際上只使用一個 CPU,因為堅持(幾"
"乎)所有 Python 程式碼只能在持有 GIL 的情況下運行。"
#: ../../faq/library.rst:413
msgid ""
"With the approval of :pep:`703` work is now underway to remove the GIL from "
"the CPython implementation of Python. Initially it will be implemented as "
"an optional compiler flag when building the interpreter, and so separate "
"builds will be available with and without the GIL. Long-term, the hope is "
"to settle on a single build, once the performance implications of removing "
"the GIL are fully understood. Python 3.13 is likely to be the first release "
"containing this work, although it may not be completely functional in this "
"release."
msgstr ""
#: ../../faq/library.rst:422
#, fuzzy
msgid ""
"The current work to remove the GIL is based on a `fork of Python 3.9 with "
"the GIL removed <https://github.com/colesbury/nogil>`_ by Sam Gross. Prior "
"to that, in the days of Python 1.5, Greg Stein actually implemented a "
"comprehensive patch set (the \"free threading\" patches) that removed the "
"GIL and replaced it with fine-grained locking. Adam Olsen did a similar "
"experiment in his `python-safethread <https://code.google.com/archive/p/"
"python-safethread>`_ project. Unfortunately, both of these earlier "
"experiments exhibited a sharp drop in single-thread performance (at least "
"30% slower), due to the amount of fine-grained locking necessary to "
"compensate for the removal of the GIL. The Python 3.9 fork is the first "
"attempt at removing the GIL with an acceptable performance impact."
msgstr ""
"回到 Python 1.5 時代,Greg Stein 實際上實作了一個全面的補丁集( \"自由執行緒"
"\" 補丁),刪除了 GIL 並用細粒度鎖定取而代之。 Adam Olsen 最近在他的 `python-"
"safethread <https://code.google.com/archive/p/python-safethread>`_ 項目中做了"
"一個類似的實驗。不幸的是,這兩個實驗都表現出單執行緒性能的急劇下降(至少慢了 "
"30%),這是由於需要大量的細粒度鎖定來補償 GIL 的移除。"
#: ../../faq/library.rst:437
#, fuzzy
msgid ""
"The presence of the GIL in current Python releases doesn't mean that you "
"can't make good use of Python on multi-CPU machines! You just have to be "
"creative with dividing the work up between multiple *processes* rather than "
"multiple *threads*. The :class:`~concurrent.futures.ProcessPoolExecutor` "
"class in the new :mod:`concurrent.futures` module provides an easy way of "
"doing so; the :mod:`multiprocessing` module provides a lower-level API in "
"case you want more control over dispatching of tasks."
msgstr ""
"這並不意味著你不能在多 CPU 機器上用好 Python!你只需要創造性地將工作分配給多"
"個*行程*而不是多個*執行緒*。新的 :mod:`concurrent.futures` 模組中的 :class:"
"`~concurrent.futures.ProcessPoolExecutor` 類別提供了一種簡單的方法; :mod:"
"`multiprocessing` 模組提供了一個較低階別的 API,以防你希望更好地控制任務的調"
"度。"
#: ../../faq/library.rst:446
#, fuzzy
msgid ""
"Judicious use of C extensions will also help; if you use a C extension to "
"perform a time-consuming task, the extension can release the GIL while the "
"thread of execution is in the C code and allow other threads to get some "
"work done. Some standard library modules such as :mod:`zlib` and :mod:"
"`hashlib` already do this."
msgstr ""
"明智地使用 C 擴充也會有所幫助;如果你使用 C 擴充來執行耗時任務,則該擴充可以"
"在執行執行緒在 C 程式碼中時釋放 GIL,並允許其他執行緒完成一些工作。一些標準函"
"式庫模組,例如 :mod:`zlib` 和 :mod:`hashlib` 已經這樣做了。"
#: ../../faq/library.rst:452
msgid ""
"An alternative approach to reducing the impact of the GIL is to make the GIL "
"a per-interpreter-state lock rather than truly global. This was :ref:`first "
"implemented in Python 3.12 <whatsnew312-pep684>` and is available in the C "
"API. A Python interface to it is expected in Python 3.13. The main "
"limitation to it at the moment is likely to be 3rd party extension modules, "
"since these must be written with multiple interpreters in mind in order to "
"be usable, so many older extension modules will not be usable."
msgstr ""
#: ../../faq/library.rst:462
msgid "Input and Output"
msgstr "輸入與輸出"
#: ../../faq/library.rst:465
msgid "How do I delete a file? (And other file questions...)"
msgstr "如何刪除檔案?(以及其他檔案問題...)"
#: ../../faq/library.rst:467
#, fuzzy
msgid ""
"Use ``os.remove(filename)`` or ``os.unlink(filename)``; for documentation, "
"see the :mod:`os` module. The two functions are identical; :func:`~os."
"unlink` is simply the name of the Unix system call for this function."
msgstr ""
"使用 ``os.remove(filename)`` 或 ``os.unlink(filename)``;有關文件,請參閱 :"
"mod:`os` 模組。這兩個功能是相同的; :func:`~os.unlink` 只是這個函式的 Unix 系"
"統呼叫的名稱。"
#: ../../faq/library.rst:471
#, fuzzy
msgid ""
"To remove a directory, use :func:`os.rmdir`; use :func:`os.mkdir` to create "
"one. ``os.makedirs(path)`` will create any intermediate directories in "
"``path`` that don't exist. ``os.removedirs(path)`` will remove intermediate "
"directories as long as they're empty; if you want to delete an entire "
"directory tree and its contents, use :func:`shutil.rmtree`."
msgstr ""
"要刪除目錄,請使用 :func:`os.rmdir`;使用 :func:`os.mkdir` 建立一個。 ``os."
"makedirs(path)`` 將在 ``path`` 中建立任何不存在的中間目錄。 ``os."
"removedirs(path)`` 將刪除中間目錄,只要它們是空的;如果要刪除整個目錄樹及其內"
"容,請使用 :func:`shutil.rmtree`。"
#: ../../faq/library.rst:477
msgid "To rename a file, use ``os.rename(old_path, new_path)``."
msgstr "要重新命名檔案,請使用 ``os.rename(old_path, new_path)``。"
#: ../../faq/library.rst:479
#, fuzzy
msgid ""
"To truncate a file, open it using ``f = open(filename, \"rb+\")``, and use "
"``f.truncate(offset)``; offset defaults to the current seek position. "
"There's also ``os.ftruncate(fd, offset)`` for files opened with :func:`os."
"open`, where *fd* is the file descriptor (a small integer)."
msgstr ""
"要截斷一個檔案,使用``f = open(filename, \"rb+\")``打開它,然後使用``f."
"truncate(offset)``;偏移量預設為目前搜尋位置。對於使用 :func:`os.open` 打開的"
"檔案,還有 ``os.ftruncate(fd, offset)``,其中 *fd* 是檔案描述器(一個小整"
"數)。"
#: ../../faq/library.rst:484
#, fuzzy
msgid ""
"The :mod:`shutil` module also contains a number of functions to work on "
"files including :func:`~shutil.copyfile`, :func:`~shutil.copytree`, and :"
"func:`~shutil.rmtree`."
msgstr ""
":mod:`shutil` 模組還包含許多用於處理檔案的函式,包括:func:`~shutil."
"copyfile`、:func:`~shutil.copytree` 和:func:`~shutil.rmtree`。"
#: ../../faq/library.rst:490
msgid "How do I copy a file?"
msgstr "如何複製檔案?"
#: ../../faq/library.rst:492
#, fuzzy
msgid ""
"The :mod:`shutil` module contains a :func:`~shutil.copyfile` function. Note "
"that on Windows NTFS volumes, it does not copy `alternate data streams "
"<https://en.wikipedia.org/wiki/NTFS#Alternate_data_stream_(ADS)>`_ nor "
"`resource forks <https://en.wikipedia.org/wiki/Resource_fork>`__ on macOS "
"HFS+ volumes, though both are now rarely used. It also doesn't copy file "
"permissions and metadata, though using :func:`shutil.copy2` instead will "
"preserve most (though not all) of it."
msgstr ""
":mod:`shutil` 模組包含一個 :func:`~shutil.copyfile` 函式。請注意,在 Windows "
"NTFS 卷上,它不會複製 `alternate data streams <https://en.wikipedia.org/wiki/"
"NTFS#Alternate_data_stream_(ADS)>`_ 也不會複製 `resource forks <https://en."
"wikipedia. org/wiki/Resource_fork>`__ 在 macOS HFS+ 卷上,儘管現在兩者都很少"
"使用。它也不會複製檔案權限和元資料,儘管使用 :func:`shutil.copy2` 會保留其中"
"的大部分(儘管不是全部)。"
#: ../../faq/library.rst:503
msgid "How do I read (or write) binary data?"
msgstr "如何讀取(或寫入)二進位制資料?"
#: ../../faq/library.rst:505
#, fuzzy
msgid ""
"To read or write complex binary data formats, it's best to use the :mod:"
"`struct` module. It allows you to take a string containing binary data "
"(usually numbers) and convert it to Python objects; and vice versa."
msgstr ""
"要讀取或寫入複雜的二進制資料格式,最好使用 :mod:`struct` 模組。它允許你取得包"
"含二進制資料(通常是數字)的字串並將其轉換為 Python 物件;反之亦然。"
#: ../../faq/library.rst:509
#, fuzzy
msgid ""
"For example, the following code reads two 2-byte integers and one 4-byte "
"integer in big-endian format from a file::"
msgstr ""
"例如,以下程式碼從一個檔案中以大端格式讀取兩個 2 位元組整數和一個 4 位元組整"
"數: ::"
#: ../../faq/library.rst:512
msgid ""
"import struct\n"
"\n"
"with open(filename, \"rb\") as f:\n"
" s = f.read(8)\n"
" x, y, z = struct.unpack(\">hhl\", s)"
msgstr ""
"import struct\n"
"\n"
"with open(filename, \"rb\") as f:\n"
" s = f.read(8)\n"
" x, y, z = struct.unpack(\">hhl\", s)"
#: ../../faq/library.rst:518
#, fuzzy
msgid ""
"The '>' in the format string forces big-endian data; the letter 'h' reads "
"one \"short integer\" (2 bytes), and 'l' reads one \"long integer\" (4 "
"bytes) from the string."
msgstr ""
"格式字串中的 \">\" 強制使用大端資料;字母 'h' 讀取一個 \"短整數\" (2 位元"
"組), 'l' 從字串中讀取一個 \"長整數\" (4 位元組)。"
#: ../../faq/library.rst:522
#, fuzzy
msgid ""
"For data that is more regular (e.g. a homogeneous list of ints or floats), "
"you can also use the :mod:`array` module."
msgstr ""
"對於更規則的資料(例如,整數或浮點數的同類列表),你還可以使用 :mod:`array` "
"模組。"
#: ../../faq/library.rst:527
#, fuzzy
msgid ""
"To read and write binary data, it is mandatory to open the file in binary "
"mode (here, passing ``\"rb\"`` to :func:`open`). If you use ``\"r\"`` "
"instead (the default), the file will be open in text mode and ``f.read()`` "
"will return :class:`str` objects rather than :class:`bytes` objects."
msgstr ""
"要讀取和寫入二進制資料,必須以二進制模式打開檔案(這裡,將 ``\"rb\"`` 傳遞"
"給 :func:`open`)。如果你改用 ``\"r\"``(預設設定),檔案將以文本模式打開,並"
"且 ``f.read()`` 將回傳 str 物件而不是 bytes ` 物件。"
#: ../../faq/library.rst:535
#, fuzzy
msgid "I can't seem to use os.read() on a pipe created with os.popen(); why?"
msgstr "我似乎無法在用 os.popen() 建立的管道上使用 os.read();為什麼?"
#: ../../faq/library.rst:537
#, fuzzy
msgid ""
":func:`os.read` is a low-level function which takes a file descriptor, a "
"small integer representing the opened file. :func:`os.popen` creates a high-"
"level file object, the same type returned by the built-in :func:`open` "
"function. Thus, to read *n* bytes from a pipe *p* created with :func:`os."
"popen`, you need to use ``p.read(n)``."
msgstr ""
":func:`os.read` 是一個低階函式,它接受一個檔案描述器,一個代表打開檔案的小整"
"數。 :func:`os.popen` 建立一個高階檔案物件,與內置的 :func:`open` 函式回傳的"
"型別相同。因此,要從使用 :func:`os.popen` 建立的管道 *p* 中讀取 *n* 個位元"
"組,你需要使用 ``p.read(n)``。"
#: ../../faq/library.rst:623
msgid "How do I access the serial (RS232) port?"
msgstr "如何存取序列 (RS232) 連接埠?"
#: ../../faq/library.rst:625
msgid "For Win32, OSX, Linux, BSD, Jython, IronPython:"
msgstr "對於 Win32、OSX、Linux、BSD、Jython、IronPython:"
#: ../../faq/library.rst:627
msgid ":pypi:`pyserial`"
msgstr ":pypi:`pyserial`"
#: ../../faq/library.rst:629
msgid "For Unix, see a Usenet post by Mitch Chapman:"
msgstr "對於 Unix,請參閱 Mitch Chapman 的 Usenet 貼文:"
#: ../../faq/library.rst:631
msgid "https://groups.google.com/[email protected]"
msgstr "https://groups.google.com/[email protected]"
#: ../../faq/library.rst:635
msgid "Why doesn't closing sys.stdout (stdin, stderr) really close it?"
msgstr "為什麼關閉 sys.stdout (stdin, stderr) 並沒有真正關閉它?"
#: ../../faq/library.rst:637
msgid ""
"Python :term:`file objects <file object>` are a high-level layer of "