-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy path2.6.po
5057 lines (4430 loc) · 170 KB
/
2.6.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-2022, Python Software Foundation
# This file is distributed under the same license as the Python package.
#
# Translators:
msgid ""
msgstr ""
"Project-Id-Version: Python 3.13\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-10 00:13+0000\n"
"PO-Revision-Date: 2018-05-23 16:20+0000\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"
#: ../../whatsnew/2.6.rst:5
msgid "What's New in Python 2.6"
msgstr "Python 2.6 有什麼新功能"
#: ../../whatsnew/2.6.rst:0
msgid "Author"
msgstr "作者"
#: ../../whatsnew/2.6.rst:7
msgid "A.M. Kuchling (amk at amk.ca)"
msgstr "A.M. Kuchling (amk at amk.ca)"
#: ../../whatsnew/2.6.rst:50
msgid ""
"This article explains the new features in Python 2.6, released on October 1, "
"2008. The release schedule is described in :pep:`361`."
msgstr ""
"本文介紹了已於 2008 年 10 月 1 日發布的 Python 2.6 有哪些新功能。發布時間表請"
"見 :pep:`361`。"
#: ../../whatsnew/2.6.rst:53
msgid ""
"The major theme of Python 2.6 is preparing the migration path to Python 3.0, "
"a major redesign of the language. Whenever possible, Python 2.6 "
"incorporates new features and syntax from 3.0 while remaining compatible "
"with existing code by not removing older features or syntax. When it's not "
"possible to do that, Python 2.6 tries to do what it can, adding "
"compatibility functions in a :mod:`future_builtins` module and a :option:"
"`!-3` switch to warn about usages that will become unsupported in 3.0."
msgstr ""
#: ../../whatsnew/2.6.rst:62
msgid ""
"Some significant new packages have been added to the standard library, such "
"as the :mod:`multiprocessing` and :mod:`json` modules, but there aren't many "
"new features that aren't related to Python 3.0 in some way."
msgstr ""
#: ../../whatsnew/2.6.rst:67
msgid ""
"Python 2.6 also sees a number of improvements and bugfixes throughout the "
"source. A search through the change logs finds there were 259 patches "
"applied and 612 bugs fixed between Python 2.5 and 2.6. Both figures are "
"likely to be underestimates."
msgstr ""
#: ../../whatsnew/2.6.rst:72
msgid ""
"This article doesn't attempt to provide a complete specification of the new "
"features, but instead provides a convenient overview. For full details, you "
"should refer to the documentation for Python 2.6. If you want to understand "
"the rationale for the design and implementation, refer to the PEP for a "
"particular new feature. Whenever possible, \"What's New in Python\" links to "
"the bug/patch item for each change."
msgstr ""
#: ../../whatsnew/2.6.rst:88
msgid "Python 3.0"
msgstr "Python 3.0"
#: ../../whatsnew/2.6.rst:90
msgid ""
"The development cycle for Python versions 2.6 and 3.0 was synchronized, with "
"the alpha and beta releases for both versions being made on the same days. "
"The development of 3.0 has influenced many features in 2.6."
msgstr ""
#: ../../whatsnew/2.6.rst:95
msgid ""
"Python 3.0 is a far-ranging redesign of Python that breaks compatibility "
"with the 2.x series. This means that existing Python code will need some "
"conversion in order to run on Python 3.0. However, not all the changes in "
"3.0 necessarily break compatibility. In cases where new features won't "
"cause existing code to break, they've been backported to 2.6 and are "
"described in this document in the appropriate place. Some of the 3.0-"
"derived features are:"
msgstr ""
#: ../../whatsnew/2.6.rst:104
msgid ""
"A :meth:`__complex__` method for converting objects to a complex number."
msgstr ""
#: ../../whatsnew/2.6.rst:105
msgid "Alternate syntax for catching exceptions: ``except TypeError as exc``."
msgstr ""
#: ../../whatsnew/2.6.rst:106
msgid ""
"The addition of :func:`functools.reduce` as a synonym for the built-in :func:"
"`reduce` function."
msgstr ""
#: ../../whatsnew/2.6.rst:109
msgid ""
"Python 3.0 adds several new built-in functions and changes the semantics of "
"some existing builtins. Functions that are new in 3.0 such as :func:`bin` "
"have simply been added to Python 2.6, but existing builtins haven't been "
"changed; instead, the :mod:`future_builtins` module has versions with the "
"new 3.0 semantics. Code written to be compatible with 3.0 can do ``from "
"future_builtins import hex, map`` as necessary."
msgstr ""
#: ../../whatsnew/2.6.rst:117
msgid ""
"A new command-line switch, :option:`!-3`, enables warnings about features "
"that will be removed in Python 3.0. You can run code with this switch to "
"see how much work will be necessary to port code to 3.0. The value of this "
"switch is available to Python code as the boolean variable :data:`sys."
"py3kwarning`, and to C extension code as :c:data:`!Py_Py3kWarningFlag`."
msgstr ""
#: ../../whatsnew/2.6.rst:126
msgid ""
"The 3\\ *xxx* series of PEPs, which contains proposals for Python 3.0. :pep:"
"`3000` describes the development process for Python 3.0. Start with :pep:"
"`3100` that describes the general goals for Python 3.0, and then explore the "
"higher-numbered PEPs that propose specific features."
msgstr ""
#: ../../whatsnew/2.6.rst:134
msgid "Changes to the Development Process"
msgstr ""
#: ../../whatsnew/2.6.rst:136
msgid ""
"While 2.6 was being developed, the Python development process underwent two "
"significant changes: we switched from SourceForge's issue tracker to a "
"customized Roundup installation, and the documentation was converted from "
"LaTeX to reStructuredText."
msgstr ""
#: ../../whatsnew/2.6.rst:143
msgid "New Issue Tracker: Roundup"
msgstr ""
#: ../../whatsnew/2.6.rst:145
msgid ""
"For a long time, the Python developers had been growing increasingly annoyed "
"by SourceForge's bug tracker. SourceForge's hosted solution doesn't permit "
"much customization; for example, it wasn't possible to customize the life "
"cycle of issues."
msgstr ""
#: ../../whatsnew/2.6.rst:150
msgid ""
"The infrastructure committee of the Python Software Foundation therefore "
"posted a call for issue trackers, asking volunteers to set up different "
"products and import some of the bugs and patches from SourceForge. Four "
"different trackers were examined: `Jira <https://www.atlassian.com/software/"
"jira/>`__, `Launchpad <https://launchpad.net/>`__, `Roundup <https://roundup."
"sourceforge.io/>`__, and `Trac <https://trac.edgewall.org/>`__. The "
"committee eventually settled on Jira and Roundup as the two candidates. "
"Jira is a commercial product that offers no-cost hosted instances to free-"
"software projects; Roundup is an open-source project that requires "
"volunteers to administer it and a server to host it."
msgstr ""
#: ../../whatsnew/2.6.rst:164
msgid ""
"After posting a call for volunteers, a new Roundup installation was set up "
"at https://bugs.python.org. One installation of Roundup can host multiple "
"trackers, and this server now also hosts issue trackers for Jython and for "
"the Python web site. It will surely find other uses in the future. Where "
"possible, this edition of \"What's New in Python\" links to the bug/patch "
"item for each change."
msgstr ""
#: ../../whatsnew/2.6.rst:172
msgid ""
"Hosting of the Python bug tracker is kindly provided by `Upfront Systems "
"<https://upfrontsoftware.co.za>`__ of Stellenbosch, South Africa. Martin "
"von Löwis put a lot of effort into importing existing bugs and patches from "
"SourceForge; his scripts for this import operation are at ``https://svn."
"python.org/view/tracker/importer/`` and may be useful to other projects "
"wishing to move from SourceForge to Roundup."
msgstr ""
#: ../../whatsnew/2.6.rst:182
msgid "https://bugs.python.org"
msgstr "https://bugs.python.org"
#: ../../whatsnew/2.6.rst:183
msgid "The Python bug tracker."
msgstr "Python 問題追蹤系統。"
#: ../../whatsnew/2.6.rst:185
msgid "https://bugs.jython.org:"
msgstr "https://bugs.jython.org:"
#: ../../whatsnew/2.6.rst:186
msgid "The Jython bug tracker."
msgstr "Jython 問題追蹤系統。"
#: ../../whatsnew/2.6.rst:188
msgid "https://roundup.sourceforge.io/"
msgstr "https://roundup.sourceforge.io/"
#: ../../whatsnew/2.6.rst:189
msgid "Roundup downloads and documentation."
msgstr ""
#: ../../whatsnew/2.6.rst:191
msgid "https://svn.python.org/view/tracker/importer/"
msgstr "https://svn.python.org/view/tracker/importer/"
#: ../../whatsnew/2.6.rst:192
msgid "Martin von Löwis's conversion scripts."
msgstr "Martin von Löwis 的轉換腳本。"
#: ../../whatsnew/2.6.rst:195
msgid "New Documentation Format: reStructuredText Using Sphinx"
msgstr "新文件格式:使用 Sphinx 的 reStructuredText"
#: ../../whatsnew/2.6.rst:197
msgid ""
"The Python documentation was written using LaTeX since the project started "
"around 1989. In the 1980s and early 1990s, most documentation was printed "
"out for later study, not viewed online. LaTeX was widely used because it "
"provided attractive printed output while remaining straightforward to write "
"once the basic rules of the markup were learned."
msgstr ""
#: ../../whatsnew/2.6.rst:204
msgid ""
"Today LaTeX is still used for writing publications destined for printing, "
"but the landscape for programming tools has shifted. We no longer print out "
"reams of documentation; instead, we browse through it online and HTML has "
"become the most important format to support. Unfortunately, converting LaTeX "
"to HTML is fairly complicated and Fred L. Drake Jr., the long-time Python "
"documentation editor, spent a lot of time maintaining the conversion "
"process. Occasionally people would suggest converting the documentation "
"into SGML and later XML, but performing a good conversion is a major task "
"and no one ever committed the time required to finish the job."
msgstr ""
#: ../../whatsnew/2.6.rst:215
msgid ""
"During the 2.6 development cycle, Georg Brandl put a lot of effort into "
"building a new toolchain for processing the documentation. The resulting "
"package is called Sphinx, and is available from https://www.sphinx-doc.org/."
msgstr ""
#: ../../whatsnew/2.6.rst:220
msgid ""
"Sphinx concentrates on HTML output, producing attractively styled and modern "
"HTML; printed output is still supported through conversion to LaTeX. The "
"input format is reStructuredText, a markup syntax supporting custom "
"extensions and directives that is commonly used in the Python community."
msgstr ""
#: ../../whatsnew/2.6.rst:226
msgid ""
"Sphinx is a standalone package that can be used for writing, and almost two "
"dozen other projects (`listed on the Sphinx web site <https://www.sphinx-doc."
"org/en/master/examples.html>`__) have adopted Sphinx as their documentation "
"tool."
msgstr ""
#: ../../whatsnew/2.6.rst:233
msgid "`Documenting Python <https://devguide.python.org/documenting/>`__"
msgstr "`Python 文件撰寫 <https://devguide.python.org/documenting/>`__"
#: ../../whatsnew/2.6.rst:234
msgid "Describes how to write for Python's documentation."
msgstr "說明如何為 Python 撰寫文件。"
#: ../../whatsnew/2.6.rst:236
msgid "`Sphinx <https://www.sphinx-doc.org/>`__"
msgstr "`Sphinx <https://www.sphinx-doc.org/>`__"
#: ../../whatsnew/2.6.rst:237
msgid "Documentation and code for the Sphinx toolchain."
msgstr "Sphinx 工具鏈的文件和程式碼。"
#: ../../whatsnew/2.6.rst:239
msgid "`Docutils <https://docutils.sourceforge.io>`__"
msgstr "`Docutils <https://docutils.sourceforge.io>`__"
#: ../../whatsnew/2.6.rst:240
msgid "The underlying reStructuredText parser and toolset."
msgstr "底層 reStructuredText 剖析器和工具集。"
#: ../../whatsnew/2.6.rst:246
msgid "PEP 343: The 'with' statement"
msgstr "PEP 343:'with' 陳述式"
#: ../../whatsnew/2.6.rst:248
msgid ""
"The previous version, Python 2.5, added the ':keyword:`with`' statement as "
"an optional feature, to be enabled by a ``from __future__ import "
"with_statement`` directive. In 2.6 the statement no longer needs to be "
"specially enabled; this means that :keyword:`!with` is now always a "
"keyword. The rest of this section is a copy of the corresponding section "
"from the \"What's New in Python 2.5\" document; if you're familiar with the "
"':keyword:`!with`' statement from Python 2.5, you can skip this section."
msgstr ""
#: ../../whatsnew/2.6.rst:257
msgid ""
"The ':keyword:`with`' statement clarifies code that previously would use "
"``try...finally`` blocks to ensure that clean-up code is executed. In this "
"section, I'll discuss the statement as it will commonly be used. In the "
"next section, I'll examine the implementation details and show how to write "
"objects for use with this statement."
msgstr ""
#: ../../whatsnew/2.6.rst:263
msgid ""
"The ':keyword:`with`' statement is a control-flow structure whose basic "
"structure is::"
msgstr ""
#: ../../whatsnew/2.6.rst:266
msgid ""
"with expression [as variable]:\n"
" with-block"
msgstr ""
"with expression [as variable]:\n"
" with-block"
#: ../../whatsnew/2.6.rst:269
msgid ""
"The expression is evaluated, and it should result in an object that supports "
"the context management protocol (that is, has :meth:`~object.__enter__` and :"
"meth:`~object.__exit__` methods)."
msgstr ""
#: ../../whatsnew/2.6.rst:273
msgid ""
"The object's :meth:`~object.__enter__` is called before *with-block* is "
"executed and therefore can run set-up code. It also may return a value that "
"is bound to the name *variable*, if given. (Note carefully that *variable* "
"is *not* assigned the result of *expression*.)"
msgstr ""
#: ../../whatsnew/2.6.rst:278
msgid ""
"After execution of the *with-block* is finished, the object's :meth:`~object."
"__exit__` method is called, even if the block raised an exception, and can "
"therefore run clean-up code."
msgstr ""
#: ../../whatsnew/2.6.rst:282
msgid ""
"Some standard Python objects now support the context management protocol and "
"can be used with the ':keyword:`with`' statement. File objects are one "
"example::"
msgstr ""
#: ../../whatsnew/2.6.rst:285
msgid ""
"with open('/etc/passwd', 'r') as f:\n"
" for line in f:\n"
" print line\n"
" ... more processing code ..."
msgstr ""
"with open('/etc/passwd', 'r') as f:\n"
" for line in f:\n"
" print line\n"
" ... 更多處理程式碼 ..."
#: ../../whatsnew/2.6.rst:290
msgid ""
"After this statement has executed, the file object in *f* will have been "
"automatically closed, even if the :keyword:`for` loop raised an exception "
"part-way through the block."
msgstr ""
#: ../../whatsnew/2.6.rst:296
msgid ""
"In this case, *f* is the same object created by :func:`open`, because :meth:"
"`~object.__enter__` returns *self*."
msgstr ""
#: ../../whatsnew/2.6.rst:299
msgid ""
"The :mod:`threading` module's locks and condition variables also support "
"the ':keyword:`with`' statement::"
msgstr ""
#: ../../whatsnew/2.6.rst:302
msgid ""
"lock = threading.Lock()\n"
"with lock:\n"
" # Critical section of code\n"
" ..."
msgstr ""
#: ../../whatsnew/2.6.rst:307
msgid ""
"The lock is acquired before the block is executed and always released once "
"the block is complete."
msgstr ""
#: ../../whatsnew/2.6.rst:310
msgid ""
"The :func:`localcontext` function in the :mod:`decimal` module makes it easy "
"to save and restore the current decimal context, which encapsulates the "
"desired precision and rounding characteristics for computations::"
msgstr ""
#: ../../whatsnew/2.6.rst:314
msgid ""
"from decimal import Decimal, Context, localcontext\n"
"\n"
"# Displays with default precision of 28 digits\n"
"v = Decimal('578')\n"
"print v.sqrt()\n"
"\n"
"with localcontext(Context(prec=16)):\n"
" # All code in this block uses a precision of 16 digits.\n"
" # The original context is restored on exiting the block.\n"
" print v.sqrt()"
msgstr ""
#: ../../whatsnew/2.6.rst:329
msgid "Writing Context Managers"
msgstr ""
#: ../../whatsnew/2.6.rst:331
msgid ""
"Under the hood, the ':keyword:`with`' statement is fairly complicated. Most "
"people will only use ':keyword:`!with`' in company with existing objects and "
"don't need to know these details, so you can skip the rest of this section "
"if you like. Authors of new objects will need to understand the details of "
"the underlying implementation and should keep reading."
msgstr ""
#: ../../whatsnew/2.6.rst:337
msgid "A high-level explanation of the context management protocol is:"
msgstr ""
#: ../../whatsnew/2.6.rst:339
msgid ""
"The expression is evaluated and should result in an object called a "
"\"context manager\". The context manager must have :meth:`~object."
"__enter__` and :meth:`~object.__exit__` methods."
msgstr ""
#: ../../whatsnew/2.6.rst:343
msgid ""
"The context manager's :meth:`~object.__enter__` method is called. The value "
"returned is assigned to *VAR*. If no ``as VAR`` clause is present, the "
"value is simply discarded."
msgstr ""
#: ../../whatsnew/2.6.rst:347
msgid "The code in *BLOCK* is executed."
msgstr "*BLOCK* 中的程式碼會被執行。"
#: ../../whatsnew/2.6.rst:349
msgid ""
"If *BLOCK* raises an exception, the context manager's :meth:`~object."
"__exit__` method is called with three arguments, the exception details "
"(``type, value, traceback``, the same values returned by :func:`sys."
"exc_info`, which can also be ``None`` if no exception occurred). The "
"method's return value controls whether an exception is re-raised: any false "
"value re-raises the exception, and ``True`` will result in suppressing it. "
"You'll only rarely want to suppress the exception, because if you do the "
"author of the code containing the ':keyword:`with`' statement will never "
"realize anything went wrong."
msgstr ""
#: ../../whatsnew/2.6.rst:358
msgid ""
"If *BLOCK* didn't raise an exception, the :meth:`~object.__exit__` method "
"is still called, but *type*, *value*, and *traceback* are all ``None``."
msgstr ""
#: ../../whatsnew/2.6.rst:361
msgid ""
"Let's think through an example. I won't present detailed code but will only "
"sketch the methods necessary for a database that supports transactions."
msgstr ""
#: ../../whatsnew/2.6.rst:364
msgid ""
"(For people unfamiliar with database terminology: a set of changes to the "
"database are grouped into a transaction. Transactions can be either "
"committed, meaning that all the changes are written into the database, or "
"rolled back, meaning that the changes are all discarded and the database is "
"unchanged. See any database textbook for more information.)"
msgstr ""
#: ../../whatsnew/2.6.rst:370
msgid ""
"Let's assume there's an object representing a database connection. Our goal "
"will be to let the user write code like this::"
msgstr ""
#: ../../whatsnew/2.6.rst:373
msgid ""
"db_connection = DatabaseConnection()\n"
"with db_connection as cursor:\n"
" cursor.execute('insert into ...')\n"
" cursor.execute('delete from ...')\n"
" # ... more operations ..."
msgstr ""
"db_connection = DatabaseConnection()\n"
"with db_connection as cursor:\n"
" cursor.execute('insert into ...')\n"
" cursor.execute('delete from ...')\n"
" # ... 更多操作 ..."
#: ../../whatsnew/2.6.rst:379
msgid ""
"The transaction should be committed if the code in the block runs flawlessly "
"or rolled back if there's an exception. Here's the basic interface for :"
"class:`DatabaseConnection` that I'll assume::"
msgstr ""
#: ../../whatsnew/2.6.rst:383
msgid ""
"class DatabaseConnection:\n"
" # Database interface\n"
" def cursor(self):\n"
" \"Returns a cursor object and starts a new transaction\"\n"
" def commit(self):\n"
" \"Commits current transaction\"\n"
" def rollback(self):\n"
" \"Rolls back current transaction\""
msgstr ""
#: ../../whatsnew/2.6.rst:392
msgid ""
"The :meth:`~object.__enter__` method is pretty easy, having only to start a "
"new transaction. For this application the resulting cursor object would be "
"a useful result, so the method will return it. The user can then add ``as "
"cursor`` to their ':keyword:`with`' statement to bind the cursor to a "
"variable name. ::"
msgstr ""
#: ../../whatsnew/2.6.rst:397
msgid ""
"class DatabaseConnection:\n"
" ...\n"
" def __enter__(self):\n"
" # Code to start a new transaction\n"
" cursor = self.cursor()\n"
" return cursor"
msgstr ""
#: ../../whatsnew/2.6.rst:404
msgid ""
"The :meth:`~object.__exit__` method is the most complicated because it's "
"where most of the work has to be done. The method has to check if an "
"exception occurred. If there was no exception, the transaction is "
"committed. The transaction is rolled back if there was an exception."
msgstr ""
#: ../../whatsnew/2.6.rst:409
msgid ""
"In the code below, execution will just fall off the end of the function, "
"returning the default value of ``None``. ``None`` is false, so the "
"exception will be re-raised automatically. If you wished, you could be more "
"explicit and add a :keyword:`return` statement at the marked location. ::"
msgstr ""
#: ../../whatsnew/2.6.rst:414
msgid ""
"class DatabaseConnection:\n"
" ...\n"
" def __exit__(self, type, value, tb):\n"
" if tb is None:\n"
" # No exception, so commit\n"
" self.commit()\n"
" else:\n"
" # Exception occurred, so rollback.\n"
" self.rollback()\n"
" # return False"
msgstr ""
#: ../../whatsnew/2.6.rst:429
msgid "The contextlib module"
msgstr "contextlib 模組"
#: ../../whatsnew/2.6.rst:431
msgid ""
"The :mod:`contextlib` module provides some functions and a decorator that "
"are useful when writing objects for use with the ':keyword:`with`' statement."
msgstr ""
#: ../../whatsnew/2.6.rst:434
msgid ""
"The decorator is called :func:`contextmanager`, and lets you write a single "
"generator function instead of defining a new class. The generator should "
"yield exactly one value. The code up to the :keyword:`yield` will be "
"executed as the :meth:`~object.__enter__` method, and the value yielded will "
"be the method's return value that will get bound to the variable in the ':"
"keyword:`with`' statement's :keyword:`!as` clause, if any. The code after "
"the :keyword:`!yield` will be executed in the :meth:`~object.__exit__` "
"method. Any exception raised in the block will be raised by the :keyword:`!"
"yield` statement."
msgstr ""
#: ../../whatsnew/2.6.rst:443
msgid ""
"Using this decorator, our database example from the previous section could "
"be written as::"
msgstr ""
#: ../../whatsnew/2.6.rst:446
msgid ""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def db_transaction(connection):\n"
" cursor = connection.cursor()\n"
" try:\n"
" yield cursor\n"
" except:\n"
" connection.rollback()\n"
" raise\n"
" else:\n"
" connection.commit()\n"
"\n"
"db = DatabaseConnection()\n"
"with db_transaction(db) as cursor:\n"
" ..."
msgstr ""
"from contextlib import contextmanager\n"
"\n"
"@contextmanager\n"
"def db_transaction(connection):\n"
" cursor = connection.cursor()\n"
" try:\n"
" yield cursor\n"
" except:\n"
" connection.rollback()\n"
" raise\n"
" else:\n"
" connection.commit()\n"
"\n"
"db = DatabaseConnection()\n"
"with db_transaction(db) as cursor:\n"
" ..."
#: ../../whatsnew/2.6.rst:463
msgid ""
"The :mod:`contextlib` module also has a ``nested(mgr1, mgr2, ...)`` function "
"that combines a number of context managers so you don't need to write nested "
"':keyword:`with`' statements. In this example, the single ':keyword:`!"
"with`' statement both starts a database transaction and acquires a thread "
"lock::"
msgstr ""
#: ../../whatsnew/2.6.rst:468
msgid ""
"lock = threading.Lock()\n"
"with nested (db_transaction(db), lock) as (cursor, locked):\n"
" ..."
msgstr ""
"lock = threading.Lock()\n"
"with nested (db_transaction(db), lock) as (cursor, locked):\n"
" ..."
#: ../../whatsnew/2.6.rst:472
msgid ""
"Finally, the :func:`closing` function returns its argument so that it can be "
"bound to a variable, and calls the argument's ``.close()`` method at the end "
"of the block. ::"
msgstr ""
#: ../../whatsnew/2.6.rst:476
msgid ""
"import urllib, sys\n"
"from contextlib import closing\n"
"\n"
"with closing(urllib.urlopen('http://www.yahoo.com')) as f:\n"
" for line in f:\n"
" sys.stdout.write(line)"
msgstr ""
"import urllib, sys\n"
"from contextlib import closing\n"
"\n"
"with closing(urllib.urlopen('http://www.yahoo.com')) as f:\n"
" for line in f:\n"
" sys.stdout.write(line)"
#: ../../whatsnew/2.6.rst:486
msgid ":pep:`343` - The \"with\" statement"
msgstr ":pep:`343` - \"with\" 陳述式"
#: ../../whatsnew/2.6.rst:487
msgid ""
"PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland, "
"Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a "
"':keyword:`with`' statement, which can be helpful in learning how the "
"statement works."
msgstr ""
#: ../../whatsnew/2.6.rst:492
msgid "The documentation for the :mod:`contextlib` module."
msgstr ":mod:`contextlib` 模組的文件。"
#: ../../whatsnew/2.6.rst:499
msgid "PEP 366: Explicit Relative Imports From a Main Module"
msgstr ""
#: ../../whatsnew/2.6.rst:501
msgid ""
"Python's :option:`-m` switch allows running a module as a script. When you "
"ran a module that was located inside a package, relative imports didn't work "
"correctly."
msgstr ""
#: ../../whatsnew/2.6.rst:505
msgid ""
"The fix for Python 2.6 adds a :attr:`module.__package__` attribute. When "
"this attribute is present, relative imports will be relative to the value of "
"this attribute instead of the :attr:`~module.__name__` attribute."
msgstr ""
#: ../../whatsnew/2.6.rst:510
msgid ""
"PEP 302-style importers can then set :attr:`~module.__package__` as "
"necessary. The :mod:`runpy` module that implements the :option:`-m` switch "
"now does this, so relative imports will now work correctly in scripts "
"running from inside a package."
msgstr ""
#: ../../whatsnew/2.6.rst:520
msgid "PEP 370: Per-user ``site-packages`` Directory"
msgstr ""
#: ../../whatsnew/2.6.rst:522
msgid ""
"When you run Python, the module search path ``sys.path`` usually includes a "
"directory whose path ends in ``\"site-packages\"``. This directory is "
"intended to hold locally installed packages available to all users using a "
"machine or a particular site installation."
msgstr ""
#: ../../whatsnew/2.6.rst:527
msgid ""
"Python 2.6 introduces a convention for user-specific site directories. The "
"directory varies depending on the platform:"
msgstr ""
#: ../../whatsnew/2.6.rst:530
msgid "Unix and Mac OS X: :file:`~/.local/`"
msgstr "Unix 和 Mac OS X::file:`~/.local/`"
#: ../../whatsnew/2.6.rst:531
msgid "Windows: :file:`%APPDATA%/Python`"
msgstr "Windows::file:`%APPDATA%/Python`"
#: ../../whatsnew/2.6.rst:533
msgid ""
"Within this directory, there will be version-specific subdirectories, such "
"as :file:`lib/python2.6/site-packages` on Unix/Mac OS and :file:`Python26/"
"site-packages` on Windows."
msgstr ""
#: ../../whatsnew/2.6.rst:537
msgid ""
"If you don't like the default directory, it can be overridden by an "
"environment variable. :envvar:`PYTHONUSERBASE` sets the root directory used "
"for all Python versions supporting this feature. On Windows, the directory "
"for application-specific data can be changed by setting the :envvar:"
"`APPDATA` environment variable. You can also modify the :file:`site.py` "
"file for your Python installation."
msgstr ""
#: ../../whatsnew/2.6.rst:544
msgid ""
"The feature can be disabled entirely by running Python with the :option:`-s` "
"option or setting the :envvar:`PYTHONNOUSERSITE` environment variable."
msgstr ""
#: ../../whatsnew/2.6.rst:550
msgid ":pep:`370` - Per-user ``site-packages`` Directory"
msgstr ""
#: ../../whatsnew/2.6.rst:551
msgid "PEP written and implemented by Christian Heimes."
msgstr "由 Christian Heimes 撰寫 PEP 與實作。"
#: ../../whatsnew/2.6.rst:559
msgid "PEP 371: The ``multiprocessing`` Package"
msgstr ""
#: ../../whatsnew/2.6.rst:561
msgid ""
"The new :mod:`multiprocessing` package lets Python programs create new "
"processes that will perform a computation and return a result to the "
"parent. The parent and child processes can communicate using queues and "
"pipes, synchronize their operations using locks and semaphores, and can "
"share simple arrays of data."
msgstr ""
#: ../../whatsnew/2.6.rst:567
msgid ""
"The :mod:`multiprocessing` module started out as an exact emulation of the :"
"mod:`threading` module using processes instead of threads. That goal was "
"discarded along the path to Python 2.6, but the general approach of the "
"module is still similar. The fundamental class is the :class:`Process`, "
"which is passed a callable object and a collection of arguments. The :meth:"
"`start` method sets the callable running in a subprocess, after which you "
"can call the :meth:`is_alive` method to check whether the subprocess is "
"still running and the :meth:`join` method to wait for the process to exit."
msgstr ""
#: ../../whatsnew/2.6.rst:577
msgid ""
"Here's a simple example where the subprocess will calculate a factorial. "
"The function doing the calculation is written strangely so that it takes "
"significantly longer when the input argument is a multiple of 4."
msgstr ""
#: ../../whatsnew/2.6.rst:584
msgid ""
"import time\n"
"from multiprocessing import Process, Queue\n"
"\n"
"\n"
"def factorial(queue, N):\n"
" \"Compute a factorial.\"\n"
" # If N is a multiple of 4, this function will take much longer.\n"
" if (N % 4) == 0:\n"
" time.sleep(.05 * N/4)\n"
"\n"
" # Calculate the result\n"
" fact = 1L\n"
" for i in range(1, N+1):\n"
" fact = fact * i\n"
"\n"
" # Put the result on the queue\n"
" queue.put(fact)\n"
"\n"
"if __name__ == '__main__':\n"
" queue = Queue()\n"
"\n"
" N = 5\n"
"\n"
" p = Process(target=factorial, args=(queue, N))\n"
" p.start()\n"
" p.join()\n"
"\n"
" result = queue.get()\n"
" print 'Factorial', N, '=', result"
msgstr ""
"import time\n"
"from multiprocessing import Process, Queue\n"
"\n"
"\n"
"def factorial(queue, N):\n"
" \"計算階乘。\"\n"
" # 如果 N 是 4 的倍數,則此函式將花費更多時間。\n"
" if (N % 4) == 0:\n"
" time.sleep(.05 * N/4)\n"
"\n"
" # 計算結果\n"
" fact = 1L\n"
" for i in range(1, N+1):\n"
" fact = fact * i\n"
"\n"
" # 將結果放進佇列裡\n"
" queue.put(fact)\n"
"\n"
"if __name__ == '__main__':\n"
" queue = Queue()\n"
"\n"
" N = 5\n"
"\n"
" p = Process(target=factorial, args=(queue, N))\n"
" p.start()\n"
" p.join()\n"
"\n"
" result = queue.get()\n"
" print 'Factorial', N, '=', result"
#: ../../whatsnew/2.6.rst:614
msgid ""
"A :class:`~queue.Queue` is used to communicate the result of the factorial. "
"The :class:`~queue.Queue` object is stored in a global variable. The child "
"process will use the value of the variable when the child was created; "
"because it's a :class:`~queue.Queue`, parent and child can use the object to "
"communicate. (If the parent were to change the value of the global "
"variable, the child's value would be unaffected, and vice versa.)"
msgstr ""
#: ../../whatsnew/2.6.rst:622
msgid ""
"Two other classes, :class:`Pool` and :class:`Manager`, provide higher-level "
"interfaces. :class:`Pool` will create a fixed number of worker processes, "
"and requests can then be distributed to the workers by calling :meth:`apply` "
"or :meth:`apply_async` to add a single request, and :meth:`map` or :meth:"
"`map_async` to add a number of requests. The following code uses a :class:"
"`Pool` to spread requests across 5 worker processes and retrieve a list of "
"results::"
msgstr ""
#: ../../whatsnew/2.6.rst:630
msgid ""
"from multiprocessing import Pool\n"
"\n"
"def factorial(N, dictionary):\n"
" \"Compute a factorial.\"\n"
" ...\n"
"p = Pool(5)\n"
"result = p.map(factorial, range(1, 1000, 10))\n"
"for v in result:\n"
" print v"
msgstr ""
"from multiprocessing import Pool\n"
"\n"
"def factorial(N, dictionary):\n"
" \"計算階乘。\"\n"
" ...\n"
"p = Pool(5)\n"
"result = p.map(factorial, range(1, 1000, 10))\n"
"for v in result:\n"
" print v"
#: ../../whatsnew/2.6.rst:640
msgid "This produces the following output::"
msgstr ""
#: ../../whatsnew/2.6.rst:642
msgid ""
"1\n"
"39916800\n"
"51090942171709440000\n"
"8222838654177922817725562880000000\n"
"33452526613163807108170062053440751665152000000000\n"
"..."
msgstr ""
"1\n"
"39916800\n"
"51090942171709440000\n"
"8222838654177922817725562880000000\n"
"33452526613163807108170062053440751665152000000000\n"
"..."
#: ../../whatsnew/2.6.rst:649
msgid ""
"The other high-level interface, the :class:`Manager` class, creates a "
"separate server process that can hold master copies of Python data "
"structures. Other processes can then access and modify these data "
"structures using proxy objects. The following example creates a shared "
"dictionary by calling the :meth:`dict` method; the worker processes then "
"insert values into the dictionary. (Locking is not done for you "
"automatically, which doesn't matter in this example. :class:`Manager`'s "
"methods also include :meth:`Lock`, :meth:`RLock`, and :meth:`Semaphore` to "
"create shared locks.)"
msgstr ""
#: ../../whatsnew/2.6.rst:661
msgid ""
"import time\n"
"from multiprocessing import Pool, Manager\n"
"\n"
"def factorial(N, dictionary):\n"
" \"Compute a factorial.\"\n"
" # Calculate the result\n"
" fact = 1L\n"
" for i in range(1, N+1):\n"
" fact = fact * i\n"
"\n"
" # Store result in dictionary\n"
" dictionary[N] = fact\n"
"\n"
"if __name__ == '__main__':\n"
" p = Pool(5)\n"
" mgr = Manager()\n"
" d = mgr.dict() # Create shared dictionary\n"
"\n"
" # Run tasks using the pool\n"
" for N in range(1, 1000, 10):\n"
" p.apply_async(factorial, (N, d))\n"
"\n"
" # Mark pool as closed -- no more tasks can be added.\n"
" p.close()\n"
"\n"
" # Wait for tasks to exit\n"
" p.join()\n"
"\n"
" # Output results\n"
" for k, v in sorted(d.items()):\n"