From 83def443cf68293ed445f9c53a89f5f7fd220c33 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Sat, 24 Feb 2024 16:04:51 +0800 Subject: [PATCH] feat: progress from weblate --- howto/enum.po | 664 +++++++++++++++++++++++++++------- howto/functional.po | 706 ++++++++++++++++++++++++++++++++---- howto/logging.po | 539 +++++++++++++++++++++++----- howto/regex.po | 853 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 2393 insertions(+), 369 deletions(-) diff --git a/howto/enum.po b/howto/enum.po index d7b391b8a7..cf3751c0db 100644 --- a/howto/enum.po +++ b/howto/enum.po @@ -3,86 +3,109 @@ # This file is distributed under the same license as the Python package. # FIRST AUTHOR , YEAR. # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-11-30 00:03+0000\n" -"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" -"Last-Translator: FULL NAME \n" +"POT-Creation-Date: 2024-02-24 16:01+0800\n" +"PO-Revision-Date: 2023-03-20 19:30+0000\n" +"Last-Translator: CTHua \n" "Language-Team: LANGUAGE \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=2; plural=n != 1;\n" +"X-Generator: Weblate 4.16.4\n" #: ../../howto/enum.rst:3 msgid "Enum HOWTO" -msgstr "" +msgstr "Enum(枚舉)教學" #: ../../howto/enum.rst:9 +#, fuzzy msgid "" "An :class:`Enum` is a set of symbolic names bound to unique values. They " "are similar to global variables, but they offer a more useful :func:" "`repr()`, grouping, type-safety, and a few other features." msgstr "" +":class:`Enum` 是一組綁定唯一值的符號名稱集合。「Enum」與全域變數類似,但提供" +"更有用的 :func:`repr()`,分組功能、型別安全以及其他若干特殊功能。" #: ../../howto/enum.rst:13 +#, fuzzy msgid "" "They are most useful when you have a variable that can take one of a limited " "selection of values. For example, the days of the week::" -msgstr "" +msgstr "當你有一個變數可以取值為限定的一部分時,最有用。例如:一周中的日期:" #: ../../howto/enum.rst:26 +#, fuzzy msgid "Or perhaps the RGB primary colors::" -msgstr "" +msgstr "或許是 RGB 基本色彩:" #: ../../howto/enum.rst:34 +#, fuzzy msgid "" "As you can see, creating an :class:`Enum` is as simple as writing a class " "that inherits from :class:`Enum` itself." msgstr "" +"你可以看出來,創建一個:class:`Enum`就像編寫一個從自身繼承的:class:`Enum`類" +"別。" #: ../../howto/enum.rst:37 +#, fuzzy msgid "Case of Enum Members" -msgstr "" +msgstr "列舉型別成員的情況" #: ../../howto/enum.rst:39 +#, fuzzy msgid "" "Because Enums are used to represent constants, and to help avoid issues with " "name clashes between mixin-class methods/attributes and enum names, we " "strongly recommend using UPPER_CASE names for members, and will be using " "that style in our examples." msgstr "" +"由於枚舉(Enums)用於表示常數,我們建議使用大寫命名法(UPPER_CASE),以此命名" +"成員。在我們的範例中也會採用這種風格。" #: ../../howto/enum.rst:44 +#, fuzzy msgid "" "Depending on the nature of the enum a member's value may or may not be " "important, but either way that value can be used to get the corresponding " "member::" msgstr "" +"根據 enum 的性質,成員的值可能很重要,也可能不太重要,但無論如何這個值都可以" +"用來取得對應的成員。" #: ../../howto/enum.rst:51 +#, fuzzy msgid "" "As you can see, the ``repr()`` of a member shows the enum name, the member " "name, and the value. The ``str()`` of a member shows only the enum name and " "member name::" msgstr "" +"你可以看到,一個成員的 ``repr()`` 會顯示枚舉名稱、成員名稱和值。而該成員的 " +"``str()`` 僅會顯示枚舉名稱和成員名稱:" #: ../../howto/enum.rst:58 +#, fuzzy msgid "The *type* of an enumeration member is the enum it belongs to::" -msgstr "" +msgstr "列舉成員的*型別*即其所屬的列舉:注意:保留rst格式符號" #: ../../howto/enum.rst:65 +#, fuzzy msgid "Enum members have an attribute that contains just their :attr:`name`::" -msgstr "" +msgstr "列舉成員具有一個屬性,其中僅包含它們的 :attr:`name`(名稱):" #: ../../howto/enum.rst:70 +#, fuzzy msgid "Likewise, they have an attribute for their :attr:`value`::" -msgstr "" +msgstr "同樣地,它們具有一個屬性用於它們的 :attr:`value` 值:" #: ../../howto/enum.rst:76 +#, fuzzy msgid "" "Unlike many languages that treat enumerations solely as name/value pairs, " "Python Enums can have behavior added. For example, :class:`datetime.date` " @@ -92,93 +115,124 @@ msgid "" "to the :class:`Weekday` enum to extract the day from the :class:`date` " "instance and return the matching enum member::" msgstr "" +"與其他把列舉視為純名稱/值對的語言不同,Python 的 Enums 可添加行為。例如,:" +"class:`datetime.date` 有兩個回傳週幾星期幾的方法::meth:`weekday` 和 :meth:" +"`isoweekday`。差異在於一個從0-6算起,另一個從1-7算起。我們可以新增一個方法到:" +"class:`Weekday` 列舉中 ,以提取日期實例的天數並回傳相應的枚舉成員來追蹤它自 " +"己 。" #: ../../howto/enum.rst:88 +#, fuzzy msgid "The complete :class:`Weekday` enum now looks like this::" -msgstr "" +msgstr ":class:`Weekday` 的完整列舉現在看起來像是這樣的:" #: ../../howto/enum.rst:103 +#, fuzzy msgid "Now we can find out what today is! Observe::" -msgstr "" +msgstr "現在我們可以找出今天是哪一天了!觀察:" #: ../../howto/enum.rst:109 +#, fuzzy msgid "" "Of course, if you're reading this on some other day, you'll see that day " "instead." -msgstr "" +msgstr "當然,如果你是在其他日期閱讀這篇文章,你會看到該天的日期。" #: ../../howto/enum.rst:111 +#, fuzzy msgid "" "This :class:`Weekday` enum is great if our variable only needs one day, but " "what if we need several? Maybe we're writing a function to plot chores " "during a week, and don't want to use a :class:`list` -- we could use a " "different type of :class:`Enum`::" msgstr "" +"這個 :class:`Weekday` 列舉型別對於只需要一天的變數很方便,但如果我們需要多天" +"呢?也許我們正在撰寫一個函式,要在整週繪製家務事項,而不想使用 :class:`list` " +"-- 我們可以使用另一種 :class:`Enum` 型別:" #: ../../howto/enum.rst:126 +#, fuzzy msgid "" "We've changed two things: we're inherited from :class:`Flag`, and the values " "are all powers of 2." -msgstr "" +msgstr "我們做了兩件事:一是繼承 `Flag` 類別,二是所有的值都是2的乘方。" #: ../../howto/enum.rst:129 +#, fuzzy msgid "" "Just like the original :class:`Weekday` enum above, we can have a single " "selection::" -msgstr "" +msgstr "就像原始的 :class:`Weekday` 枚舉一樣,我們可以進行單一選擇:" #: ../../howto/enum.rst:135 +#, fuzzy msgid "" "But :class:`Flag` also allows us to combine several members into a single " "variable::" -msgstr "" +msgstr "但是 :class:`Flag` 也允許我們將數個成員結合為一個變數::" #: ../../howto/enum.rst:142 +#, fuzzy msgid "You can even iterate over a :class:`Flag` variable::" -msgstr "" +msgstr "你甚至可以遍歷一個 :class:`Flag` 變數:" #: ../../howto/enum.rst:149 +#, fuzzy msgid "Okay, let's get some chores set up::" -msgstr "" +msgstr "好的,讓我們進行一些必要設定:" #: ../../howto/enum.rst:157 +#, fuzzy msgid "And a function to display the chores for a given day::" -msgstr "" +msgstr "以下是給定一個日期的家務事項顯示函式:" #: ../../howto/enum.rst:167 +#, fuzzy msgid "" "In cases where the actual values of the members do not matter, you can save " "yourself some work and use :func:`auto()` for the values::" msgstr "" +"如果成員的實際值不重要,您可以省去一些工作,並使用 :func:`auto()` 替代數值::" #: ../../howto/enum.rst:186 +#, fuzzy msgid "Programmatic access to enumeration members and their attributes" -msgstr "" +msgstr "可以用程式存取列舉值及其屬性" #: ../../howto/enum.rst:188 +#, fuzzy msgid "" "Sometimes it's useful to access members in enumerations programmatically (i." "e. situations where ``Color.RED`` won't do because the exact color is not " "known at program-writing time). ``Enum`` allows such access::" msgstr "" +"有時候,以程式方式存取列舉中的成員是很有用且必要的(例如在編寫程式時無法確定" +"正確顏色,因此使用 ``Color.RED`` 就不合適)。在這種情況下,可以利用 ``Enum`` " +"來存取:" #: ../../howto/enum.rst:197 +#, fuzzy msgid "If you want to access enum members by *name*, use item access::" -msgstr "" +msgstr "如果你想要透過 *名稱* 存取枚舉成員,請使用項目存取:" #: ../../howto/enum.rst:204 +#, fuzzy msgid "If you have an enum member and need its :attr:`name` or :attr:`value`::" msgstr "" +"如果你有一個列舉型別的成員,並需要獲取其 :attr:`name` 或 :attr:`value`屬性:" #: ../../howto/enum.rst:214 +#, fuzzy msgid "Duplicating enum members and values" -msgstr "" +msgstr "複製列舉成員和值" #: ../../howto/enum.rst:216 +#, fuzzy msgid "Having two enum members with the same name is invalid::" -msgstr "" +msgstr "擁有兩個同名的列舉成員是無效的:" #: ../../howto/enum.rst:226 +#, fuzzy msgid "" "However, an enum member can have other names associated with it. Given two " "entries ``A`` and ``B`` with the same value (and ``A`` defined first), ``B`` " @@ -186,100 +240,136 @@ msgid "" "will return the member ``A``. By-name lookup of ``A`` will return the " "member ``A``. By-name lookup of ``B`` will also return the member ``A``::" msgstr "" +"然而,列舉成員可以有其它名稱與之相關聯。假設有兩個項目 ``A`` 與 ``B``,且其值" +"相同 (且``A``定義在前面), 則 ``B`` 是成員 ``A`` 的別名。通過取得 \"by-" +"value\"屬性來查找 \"A\"的值會返回成員\"A\"; 通過 \"by-name\"方式查找\"A\"也" +"會返回成員\"A\";通過 \"by-name\" 方式查找\"B\",同樣也會返回 成員“A” :" #: ../../howto/enum.rst:247 +#, fuzzy msgid "" "Attempting to create a member with the same name as an already defined " "attribute (another member, a method, etc.) or attempting to create an " "attribute with the same name as a member is not allowed." msgstr "" +"嘗試建立一個與已定義的屬性(另一個成員、方法等)同名的成員,或者嘗試建立一個" +"與成 員同名的屬性是不被允許的。" #: ../../howto/enum.rst:253 +#, fuzzy msgid "Ensuring unique enumeration values" -msgstr "" +msgstr "確保列舉值唯一" #: ../../howto/enum.rst:255 +#, fuzzy msgid "" "By default, enumerations allow multiple names as aliases for the same value. " "When this behavior isn't desired, you can use the :func:`unique` decorator::" msgstr "" +"預設情況下,枚舉型別允許使用多個名稱作為相同值的別名。當不希望這種行為時,可" +"以使用 :func:`unique` 裝飾器:" #: ../../howto/enum.rst:272 +#, fuzzy msgid "Using automatic values" -msgstr "" +msgstr "使用自動產生的值" #: ../../howto/enum.rst:274 +#, fuzzy msgid "If the exact value is unimportant you can use :class:`auto`::" -msgstr "" +msgstr "如果精確值不重要,可以使用:class:`auto`:" #: ../../howto/enum.rst:285 +#, fuzzy msgid "" "The values are chosen by :func:`_generate_next_value_`, which can be " "overridden::" msgstr "" +"這段文字的翻譯如下:值是由 :func:`_generate_next_value_` 決定的,可以被覆寫:" #: ../../howto/enum.rst:304 +#, fuzzy msgid "" "The :meth:`_generate_next_value_` method must be defined before any members." -msgstr "" +msgstr "在任何成員之前都必須先定義 :meth:`_generate_next_value_` 方法。" #: ../../howto/enum.rst:307 +#, fuzzy msgid "Iteration" -msgstr "" +msgstr "迭代" #: ../../howto/enum.rst:309 +#, fuzzy msgid "Iterating over the members of an enum does not provide the aliases::" -msgstr "" +msgstr "逐一列舉枚舉型別的成員時不提供其別名::" #: ../../howto/enum.rst:316 +#, fuzzy msgid "" "Note that the aliases ``Shape.ALIAS_FOR_SQUARE`` and ``Weekday.WEEKEND`` " "aren't shown." msgstr "" +"注意:別名 ``Shape.ALIAS_FOR_SQUARE`` 和 ``Weekday.WEEKEND`` 沒有顯示。" #: ../../howto/enum.rst:318 +#, fuzzy msgid "" "The special attribute ``__members__`` is a read-only ordered mapping of " "names to members. It includes all names defined in the enumeration, " "including the aliases::" msgstr "" +"特殊屬性 ``__members__`` 是一個只能讀取的有序映射,從名稱到成員。它包括了枚舉" +"中定義的所有名稱,包括別名。" #: ../../howto/enum.rst:330 +#, fuzzy msgid "" "The ``__members__`` attribute can be used for detailed programmatic access " "to the enumeration members. For example, finding all the aliases::" msgstr "" +"``__members__`` 屬性可用於對枚舉成員進行詳細的編程訪問。例如,查找所有別名:" #: ../../howto/enum.rst:338 +#, fuzzy msgid "" "Aliases for flags include values with multiple flags set, such as ``3``, and " "no flags set, i.e. ``0``." msgstr "" +"輸入參數的別名可以使用在有多個指令旗標時,例如 `3`;也可用於無任何指令旗標" +"時,即 `0`。" #: ../../howto/enum.rst:343 +#, fuzzy msgid "Comparisons" -msgstr "" +msgstr "比較" #: ../../howto/enum.rst:345 +#, fuzzy msgid "Enumeration members are compared by identity::" -msgstr "" +msgstr "列舉成員按身份(identity)進行比較::" #: ../../howto/enum.rst:354 +#, fuzzy msgid "" "Ordered comparisons between enumeration values are *not* supported. Enum " "members are not integers (but see `IntEnum`_ below)::" msgstr "" +"不支援列舉值之間的排序比較。列舉成員並非整數(但下方可參考 `IntEnum`_):" #: ../../howto/enum.rst:362 +#, fuzzy msgid "Equality comparisons are defined though::" -msgstr "" +msgstr "等式比較是透過以下定義:" #: ../../howto/enum.rst:371 +#, fuzzy msgid "" "Comparisons against non-enumeration values will always compare not equal " "(again, :class:`IntEnum` was explicitly designed to behave differently, see " "below)::" msgstr "" +"對不包含列舉值的比較總是會得到「不相等」(再一次地,:class:`IntEnum` 是有特別" +"定義的行為,詳情見下文):" #: ../../howto/enum.rst:380 msgid "" @@ -290,9 +380,10 @@ msgstr "" #: ../../howto/enum.rst:385 msgid "Allowed members and attributes of enumerations" -msgstr "" +msgstr "列舉型別中的允許成員和屬性" #: ../../howto/enum.rst:387 +#, fuzzy msgid "" "Most of the examples above use integers for enumeration values. Using " "integers is short and handy (and provided by default by the `Functional " @@ -300,18 +391,26 @@ msgid "" "doesn't care what the actual value of an enumeration is. But if the value " "*is* important, enumerations can have arbitrary values." msgstr "" +"大部分上面的範例都使用整數來作為枚舉值。使用整數即方便又快速(而且Functional " +"API預設也會支援),但不是強制性的做法。在極大多數情況下,一個資料列舉實際所代" +"表的值不重要。但如果該值很重要,您仍可以隨意指定任何需求所涵蓋到之枚舉值。" #: ../../howto/enum.rst:393 +#, fuzzy msgid "" "Enumerations are Python classes, and can have methods and special methods as " "usual. If we have this enumeration::" msgstr "" +"列舉是 Python 中的一種類別,可像慣例中的其他類別一樣,具有方法和特殊方法。若" +"我們定義以下列舉:" #: ../../howto/enum.rst:413 +#, fuzzy msgid "Then::" -msgstr "" +msgstr "接著是:" #: ../../howto/enum.rst:422 +#, fuzzy msgid "" "The rules for what is allowed are as follows: names that start and end with " "a single underscore are reserved by enum and cannot be used; all other " @@ -320,13 +419,20 @@ msgid "" "`__add__`, etc.), descriptors (methods are also descriptors), and variable " "names listed in :attr:`_ignore_`." msgstr "" +"下面是翻譯後的文字:定義枚舉(Enum)時,需注意以下規則:命名以一個底線開頭和" +"結尾的名稱保留給 enum ,不能使用;除了特殊方法(例如: :meth:`__str__`, :meth:" +"`__add__` 等)、描述符 (方法也是描述符) 以及在 :attr:`_ignore_` 中列出的變數" +"名之外,定義於枚舉內部的所有屬性都會成為此枚舉類別的成員。" #: ../../howto/enum.rst:429 +#, fuzzy msgid "" "Note: if your enumeration defines :meth:`__new__` and/or :meth:`__init__`, " "any value(s) given to the enum member will be passed into those methods. See " "`Planet`_ for an example." msgstr "" +"請注意:如果您的枚舉定義了 ``__new__`` 和/或 ``__init__`` 方法,則任何指定給" +"該枚舉成員的值都將傳遞到這些方法中。 請參考 `Planet`_ 的範例。" #: ../../howto/enum.rst:435 msgid "" @@ -338,32 +444,44 @@ msgstr "" #: ../../howto/enum.rst:442 msgid "Restricted Enum subclassing" -msgstr "" +msgstr "受限枚舉子類別化" #: ../../howto/enum.rst:444 +#, fuzzy msgid "" "A new :class:`Enum` class must have one base enum class, up to one concrete " "data type, and as many :class:`object`-based mixin classes as needed. The " "order of these base classes is::" msgstr "" +"一個新的 :class:`Enum` 類別必須擁有一個基礎列舉(enum)類別、不超過一種具體的資" +"料型別(data type),以及所需的任意數量使用 :class:`object` 為基礎(mixin) 的混" +"合類別。這些基礎類別(base classes)之間的順序為:" #: ../../howto/enum.rst:451 +#, fuzzy msgid "" "Also, subclassing an enumeration is allowed only if the enumeration does not " "define any members. So this is forbidden::" msgstr "" +"同時,只有在列舉型別未定義任何成員時才允許子類化enumeration。 因此,這是被禁" +"止的:" #: ../../howto/enum.rst:461 +#, fuzzy msgid "But this is allowed::" -msgstr "" +msgstr "但這是允許的:" #: ../../howto/enum.rst:472 +#, fuzzy msgid "" "Allowing subclassing of enums that define members would lead to a violation " "of some important invariants of types and instances. On the other hand, it " "makes sense to allow sharing some common behavior between a group of " "enumerations. (See `OrderedEnum`_ for an example.)" msgstr "" +"允許定義成員的列舉型別可以被繼承,但這也會違反一些重要的類型和實例不變數。然" +"而,在一組列舉中,允許共享某些通用的行為是有道理的。(例如參考 `OrderedEnum`_ " +"之範例) 。" #: ../../howto/enum.rst:481 msgid "Dataclass support" @@ -390,30 +508,46 @@ msgstr "" #: ../../howto/enum.rst:509 msgid "Pickling" msgstr "" +"Pickling又稱「序列化」,是指將 Python 對象轉為二進位串(byte stream)的過程," +"在物件長期保存、傳送或共享時非常有用。反向操作也可以通過 pickling 實現,即把" +"資料從二進位串中還原回來。Python 已經提供了官方支援的 \\`\\`pickle\\`\\` 模" +"組,以不同方式支援所有 Python 的內置物件類型與部分第三方擴展。若要 pickle 自" +"定義的類別、函式甚至整個模組等,只需要在對應物件上實作 \\_\\_pickle\\_\\_() " +"函數即可。使用好 pickle 往往意味著可以減少許多繁雜手續且增加更多效能!" #: ../../howto/enum.rst:511 +#, fuzzy msgid "Enumerations can be pickled and unpickled::" -msgstr "" +msgstr "列舉型別可以被 pickle 和 unpickle。::" #: ../../howto/enum.rst:518 +#, fuzzy msgid "" "The usual restrictions for pickling apply: picklable enums must be defined " "in the top level of a module, since unpickling requires them to be " "importable from that module." msgstr "" +"通常對於 pickling 有一些限制:可 pickle 的列舉型別必須在模組的最上層定義,因" +"為反序列化需要它們從該模組中 importable。" #: ../../howto/enum.rst:524 +#, fuzzy msgid "" "With pickle protocol version 4 it is possible to easily pickle enums nested " "in other classes." msgstr "" +"從 pickle 協議版本 4 開始,嵌套在其他類別內的 enums 可以方便地進行序列化" +"(pickle)。" #: ../../howto/enum.rst:527 +#, fuzzy msgid "" "It is possible to modify how enum members are pickled/unpickled by defining :" "meth:`__reduce_ex__` in the enumeration class. The default method is by-" "value, but enums with complicated values may want to use by-name::" msgstr "" +"可以透過在列舉類別中定義 :meth:`__reduce_ex__` 來修改枚舉成員的取捨(pickled/" +"unpickled)方式。" #: ../../howto/enum.rst:537 msgid "" @@ -423,20 +557,25 @@ msgstr "" #: ../../howto/enum.rst:542 msgid "Functional API" -msgstr "" +msgstr "功能性 API" #: ../../howto/enum.rst:544 +#, fuzzy msgid "" "The :class:`Enum` class is callable, providing the following functional API::" -msgstr "" +msgstr ":class:`Enum` 類別是可呼叫的,提供以下函數式 API:" #: ../../howto/enum.rst:554 +#, fuzzy msgid "" "The semantics of this API resemble :class:`~collections.namedtuple`. The " "first argument of the call to :class:`Enum` is the name of the enumeration." msgstr "" +"這個 API 的語義類似 :class:`~collections.namedtuple` 。 叫用 :class:`Enum` 的" +"第一個引數是列舉型別的名稱。" #: ../../howto/enum.rst:557 +#, fuzzy msgid "" "The second argument is the *source* of enumeration member names. It can be " "a whitespace-separated string of names, a sequence of names, a sequence of 2-" @@ -447,15 +586,24 @@ msgid "" "class derived from :class:`Enum` is returned. In other words, the above " "assignment to :class:`Animal` is equivalent to::" msgstr "" +"第二個參數是列舉成員名稱的「來源」。它可以是由空格分隔的字串、一系列名稱、具" +"有鍵值對的 2 元序列,或者映射(例如字典),其中包含了名稱和相應值。最後兩個選項" +"使得能夠將任意值指定給枚舉;其他則自動分配從 1 開始增加的整數 (使用 " +"``start`` 參數可指定不同的起始值) 。回傳一個衍生自 :class:`Enum` 的新類別。換" +"句話說,上面賦予 :class:`Animal` 的功能等價於:" #: ../../howto/enum.rst:573 +#, fuzzy msgid "" "The reason for defaulting to ``1`` as the starting number and not ``0`` is " "that ``0`` is ``False`` in a boolean sense, but by default enum members all " "evaluate to ``True``." msgstr "" +"預設將起始數字設為``1``而非``0``的原因是,布林運算中 ``0`` 為 ``False``, 但列" +"舉型別中成員的預設值皆為真(evaluate to True)。" #: ../../howto/enum.rst:577 +#, fuzzy msgid "" "Pickling enums created with the functional API can be tricky as frame stack " "implementation details are used to try and figure out which module the " @@ -463,47 +611,70 @@ msgid "" "function in a separate module, and also may not work on IronPython or " "Jython). The solution is to specify the module name explicitly as follows::" msgstr "" +"使用函數式 API 創建的枚舉型別可能會比較棘手,因為框架堆疊實現細節被用來嘗試找" +"出創建列舉型別的模組(例如,如果在另一個模組中使用實用工具函數則失敗,在 " +"IronPython 或 Jython 上也可能無法正常運作)。解決方案是明確指定模組名稱,如下" +"所示::: import enum class Color(enum.Enum): RED = 1 " +"GREEN = 2 BLUE = 3 # 將其存成二進位 Pickle 格式到開檔 with " +"open('file.pkl', 'wb') as f: pickle.dump(Color, f) # 從存储的 " +"Pickle 文件那裡重获原始对象,并验证它确实还拥有相同类型和值 with " +"open('file.pkl', 'rb') as f: LoadedColor = pickle.load(f) assert " +"str(LoadedColor.RED) == 'Color.RED'" #: ../../howto/enum.rst:587 +#, fuzzy msgid "" "If ``module`` is not supplied, and Enum cannot determine what it is, the new " "Enum members will not be unpicklable; to keep errors closer to the source, " "pickling will be disabled." msgstr "" +"如果未提供``module``,且Enum不能確定它是什麼,新的Enum成員將無法進行反序列" +"化; 為了讓錯誤更接近源頭, pickling會被禁用。" #: ../../howto/enum.rst:591 +#, fuzzy msgid "" "The new pickle protocol 4 also, in some circumstances, relies on :attr:" "`~definition.__qualname__` being set to the location where pickle will be " "able to find the class. For example, if the class was made available in " "class SomeData in the global scope::" msgstr "" +"新的 pickle 協議 4 在某些情況下還依賴於 :attr:``~definition.__qualname__`` 設" +"置為 pickle 能找到該類別位置的屬性。舉例而言,如果在全域範圍內創建了一個名為 " +"SomeData 的類別:" #: ../../howto/enum.rst:598 +#, fuzzy msgid "The complete signature is::" -msgstr "" +msgstr "完整的函式簽名如下::" #: ../../howto/enum.rst:610 msgid "*value*: What the new enum class will record as its name." msgstr "" #: ../../howto/enum.rst:612 +#, fuzzy msgid "" "*names*: The enum members. This can be a whitespace- or comma-separated " "string (values will start at 1 unless otherwise specified)::" msgstr "" +"*names*:列出 enum 的成員,這應為一個換行或以逗號分隔的字串(否則值將從 1 開" +"始)。:" #: ../../howto/enum.rst:617 +#, fuzzy msgid "or an iterator of names::" -msgstr "" +msgstr "或一個名稱的迭代器:" #: ../../howto/enum.rst:621 +#, fuzzy msgid "or an iterator of (name, value) pairs::" -msgstr "" +msgstr "或是一個 (名稱, 值) 配對的迭代器:" #: ../../howto/enum.rst:625 +#, fuzzy msgid "or a mapping::" -msgstr "" +msgstr "或是一個對應關係:" #: ../../howto/enum.rst:629 msgid "*module*: name of module where new enum class can be found." @@ -523,50 +694,60 @@ msgstr "" #: ../../howto/enum.rst:637 msgid "The *start* parameter was added." -msgstr "" +msgstr "新增了 *start* 參數。" #: ../../howto/enum.rst:642 msgid "Derived Enumerations" -msgstr "" +msgstr "衍生列舉" #: ../../howto/enum.rst:645 msgid "IntEnum" -msgstr "" +msgstr "IntEnum" #: ../../howto/enum.rst:647 +#, fuzzy msgid "" "The first variation of :class:`Enum` that is provided is also a subclass of :" "class:`int`. Members of an :class:`IntEnum` can be compared to integers; by " "extension, integer enumerations of different types can also be compared to " "each other::" msgstr "" +"提供的第一種:class:`Enum`變異體也是 :class:`int` 的子類別。:class:`IntEnum` " +"成員可與整數進行比較;由此,不同型別的整數列舉也可以相互比較:" #: ../../howto/enum.rst:668 +#, fuzzy msgid "" "However, they still can't be compared to standard :class:`Enum` " "enumerations::" -msgstr "" +msgstr "然而,它們仍無法與標準的 :class:`Enum` 型別相比較:" #: ../../howto/enum.rst:681 +#, fuzzy msgid "" ":class:`IntEnum` values behave like integers in other ways you'd expect::" -msgstr "" +msgstr ":class:`IntEnum` 型別的值在其他方面表現得像整數:" #: ../../howto/enum.rst:692 +#, fuzzy msgid "StrEnum" -msgstr "" +msgstr "`StrEnum`(字串列舉)" #: ../../howto/enum.rst:694 +#, fuzzy msgid "" "The second variation of :class:`Enum` that is provided is also a subclass " "of :class:`str`. Members of a :class:`StrEnum` can be compared to strings; " "by extension, string enumerations of different types can also be compared to " "each other." msgstr "" +"提供第二種 :class:`Enum` 變型的子類別 :class:`StrEnum` 。 :class:`StrEnum` 的" +"成員可與字串比較;因此,不同型態的字串列舉也可以彼此比較。" #: ../../howto/enum.rst:703 +#, fuzzy msgid "IntFlag" -msgstr "" +msgstr "整數旗標" #: ../../howto/enum.rst:705 msgid "" @@ -579,57 +760,77 @@ msgid "" msgstr "" #: ../../howto/enum.rst:713 +#, fuzzy msgid "" "Any operation on an :class:`IntFlag` member besides the bit-wise operations " "will lose the :class:`IntFlag` membership." msgstr "" +"除了以位元運算的方式操作 :class:`IntFlag` 成員之外,其他任何操作都會使這個成" +"員失去屬於 :class:`IntFlag` 的身份。" #: ../../howto/enum.rst:716 +#, fuzzy msgid "" "Bit-wise operations that result in invalid :class:`IntFlag` values will lose " "the :class:`IntFlag` membership. See :class:`FlagBoundary` for details." msgstr "" +"進行位元運算,若導致 :class:`IntFlag` 值無效,就會失去 :class:`IntFlag` 成員" +"資格。詳細資訊請參考 :class:`FlagBoundary`。" #: ../../howto/enum.rst:723 +#, fuzzy msgid "Sample :class:`IntFlag` class::" -msgstr "" +msgstr "範例 :class:`IntFlag` 類別:" #: ../../howto/enum.rst:739 +#, fuzzy msgid "It is also possible to name the combinations::" -msgstr "" +msgstr "可以將這些組合命名如下:" #: ../../howto/enum.rst:756 +#, fuzzy msgid "" "Named combinations are considered aliases. Aliases do not show up during " "iteration, but can be returned from by-value lookups." msgstr "" +"已翻譯如下: 已命名的組合被視為別名。 \"Aliases\" 在迭代時不會顯示,但可以" +"從按值查找中返回。" #: ../../howto/enum.rst:761 +#, fuzzy msgid "" "Another important difference between :class:`IntFlag` and :class:`Enum` is " "that if no flags are set (the value is 0), its boolean evaluation is :data:" "`False`::" msgstr "" +":class:`IntFlag` 和 :class:`Enum` 之間的另一個重要差異是,如果沒有設定標誌" +"(值為0),那麼它的布林估值就是 :data:`False`:。" #: ../../howto/enum.rst:769 +#, fuzzy msgid "" "Because :class:`IntFlag` members are also subclasses of :class:`int` they " "can be combined with them (but may lose :class:`IntFlag` membership::" msgstr "" +"由於 :class:`IntFlag` 成員也是 :class:`int` 的子類別,因此它們可以與這些數值" +"結合使用 (但其可能失去 :class:`IntFlag` 的成員身份) :" #: ../../howto/enum.rst:780 +#, fuzzy msgid "" "The negation operator, ``~``, always returns an :class:`IntFlag` member with " "a positive value::" -msgstr "" +msgstr "否定運算子 ``~`` ,總是會回傳一個 :class:`IntFlag` 成員,其值為正數。" #: ../../howto/enum.rst:786 +#, fuzzy msgid ":class:`IntFlag` members can also be iterated over::" -msgstr "" +msgstr ":class:`IntFlag` 的成員也可以進行迭代:" #: ../../howto/enum.rst:795 +#, fuzzy msgid "Flag" -msgstr "" +msgstr "Flag" #: ../../howto/enum.rst:797 msgid "" @@ -642,28 +843,35 @@ msgid "" msgstr "" #: ../../howto/enum.rst:806 +#, fuzzy msgid "" "Like :class:`IntFlag`, if a combination of :class:`Flag` members results in " "no flags being set, the boolean evaluation is :data:`False`::" msgstr "" +"類似 :class:`IntFlag` 的作法,如果一組 :class:`Flag` 類別的成員結果沒有任何旗" +"標被設定,那麼布林式評估會是 :data:`False`。" #: ../../howto/enum.rst:820 +#, fuzzy msgid "" "Individual flags should have values that are powers of two (1, 2, 4, " "8, ...), while combinations of flags will not::" -msgstr "" +msgstr "個別的標誌(flag) 應該具有2的次方數值(1, 2, 4, 8...),而標誌組合則不會:" #: ../../howto/enum.rst:832 +#, fuzzy msgid "" "Giving a name to the \"no flags set\" condition does not change its boolean " "value::" -msgstr "" +msgstr "將「無旗標設置」狀況命名並不會改變其布林值:" #: ../../howto/enum.rst:846 +#, fuzzy msgid ":class:`Flag` members can also be iterated over::" -msgstr "" +msgstr ":class:`Flag` 成員也可以被迭代:" #: ../../howto/enum.rst:856 +#, fuzzy msgid "" "For the majority of new code, :class:`Enum` and :class:`Flag` are strongly " "recommended, since :class:`IntEnum` and :class:`IntFlag` break some semantic " @@ -673,56 +881,81 @@ msgid "" "will not do; for example, when integer constants are replaced with " "enumerations, or for interoperability with other systems." msgstr "" +"對於大部分的新代碼,強烈建議使用 :class:`Enum` 和 :class:`Flag`, 因為 :class:" +"`IntEnum` 和 :class:`IntFlag` 違反了列舉型別的一些語意承諾(可比較整數,因此也" +"可適用於其他無關聯的列舉型別)。只有在不具備:class: `Enum`和:class: `Flag` 的" +"功能時方才應使用:class: ` IntEnum`與: class:``IntDate``;例如當整數常量被替" +"換成枚舉常量或需要互通性質上其他系統時。" #: ../../howto/enum.rst:866 +#, fuzzy msgid "Others" -msgstr "" +msgstr "抱歉,請提供需要翻譯的原始文字。" #: ../../howto/enum.rst:868 +#, fuzzy msgid "" "While :class:`IntEnum` is part of the :mod:`enum` module, it would be very " "simple to implement independently::" msgstr "" +"雖然 :class:`IntEnum` 是 :mod:`enum` 模組的一部分,但獨立實現也非常簡單:" #: ../../howto/enum.rst:874 +#, fuzzy msgid "" "This demonstrates how similar derived enumerations can be defined; for " "example a :class:`FloatEnum` that mixes in :class:`float` instead of :class:" "`int`." msgstr "" +"這段文字的翻譯:這展示了多種派生列舉定義相似之處;例如,一個以浮點型別" +"(``float``)混入代替整數(``int``)的 ``FloatEnum`` 類別。" #: ../../howto/enum.rst:877 +#, fuzzy msgid "Some rules:" -msgstr "" +msgstr "一些規則:" #: ../../howto/enum.rst:879 +#, fuzzy msgid "" "When subclassing :class:`Enum`, mix-in types must appear before :class:" "`Enum` itself in the sequence of bases, as in the :class:`IntEnum` example " "above." msgstr "" +"當子類別化 :class:`Enum` 時,mix-in 的型別必須在 :class:`Enum` 本身之前出現於" +"基礎序列中,就像先前舉的 :class:`IntEnum` 範例。" #: ../../howto/enum.rst:882 +#, fuzzy msgid "" "Mix-in types must be subclassable. For example, :class:`bool` and :class:" "`range` are not subclassable and will throw an error during Enum creation if " "used as the mix-in type." msgstr "" +"混入類別必須是可被繼承的。例如 :class:`bool` 和 :class:`range` 並非可被子類化" +"的,如果用於作為混入類型則會在建立枚舉時拋出錯誤訊息。" #: ../../howto/enum.rst:885 +#, fuzzy msgid "" "While :class:`Enum` can have members of any type, once you mix in an " "additional type, all the members must have values of that type, e.g. :class:" "`int` above. This restriction does not apply to mix-ins which only add " "methods and don't specify another type." msgstr "" +"雖然:class:`Enum`可以包含任何型別的成員,但是一旦混合了其他型別,所有的成員都" +"必須具有該型別的值,例如上面提到的 :class:`int`。這個限制不適用於僅添加方法而" +"未指定另一種類型的 mixin。" #: ../../howto/enum.rst:889 +#, fuzzy msgid "" "When another data type is mixed in, the :attr:`value` attribute is *not the " "same* as the enum member itself, although it is equivalent and will compare " "equal." msgstr "" +"當enum與其他型別混在一起時,即使他們是相等的且具有可比性,該列舉成員本身和屬" +"性 :attr:`value` *不會完全相同*。" #: ../../howto/enum.rst:892 msgid "" @@ -731,41 +964,58 @@ msgid "" msgstr "" #: ../../howto/enum.rst:894 +#, fuzzy msgid "" "%-style formatting: ``%s`` and ``%r`` call the :class:`Enum` class's :meth:" "`__str__` and :meth:`__repr__` respectively; other codes (such as ``%i`` or " "``%h`` for IntEnum) treat the enum member as its mixed-in type." msgstr "" +"`%-style formatting:` 使用 `%s` 和 `%r`,分別會呼叫 `Enum` 類別的 " +"`__str__()` 和 `__repr__()`;其它格式符號(例如:IntEnum 的 `%i` 或 `%h`)則" +"將列舉成員視為其混入型別。" #: ../../howto/enum.rst:897 +#, fuzzy msgid "" ":ref:`Formatted string literals `, :meth:`str.format`, and :func:" "`format` will use the enum's :meth:`__str__` method." msgstr "" +":ref:`格式化字串 `、:meth:`str.format` 和 :func:`format` 會使用列" +"舉型別的:meth:`__str__` 法。" #: ../../howto/enum.rst:902 +#, fuzzy msgid "" "Because :class:`IntEnum`, :class:`IntFlag`, and :class:`StrEnum` are " "designed to be drop-in replacements for existing constants, their :meth:" "`__str__` method has been reset to their data types' :meth:`__str__` method." msgstr "" +"由於:class:`IntEnum`、:class:`IntFlag`和 :class:`StrEnum`被設計為現有常數的即" +"插即用替代品,因此它們的:meth:`~__str__`方法已被重置為其資料類型的: meth:" +"`~__str__`方法。" #: ../../howto/enum.rst:910 +#, fuzzy msgid "When to use :meth:`__new__` vs. :meth:`__init__`" -msgstr "" +msgstr "何時使用 :meth:`__new__` 而不是 :meth:`__init__`" #: ../../howto/enum.rst:912 +#, fuzzy msgid "" ":meth:`__new__` must be used whenever you want to customize the actual value " "of the :class:`Enum` member. Any other modifications may go in either :meth:" "`__new__` or :meth:`__init__`, with :meth:`__init__` being preferred." msgstr "" +"當您想客製化 :class:`Enum` 成員的實際值時,必須使用:meth:`__new__`。任何其他" +"修改可以放在 :meth:`__new_` 或是 :meth:`__init__` 中,而優先選擇使用 :meth:" +"`__init__ `進行修改。" #: ../../howto/enum.rst:916 +#, fuzzy msgid "" "For example, if you want to pass several items to the constructor, but only " "want one of them to be the value::" -msgstr "" +msgstr "例如, 如果您想傳遞幾個項目給構造函數,但只想其中一個是值:" #: ../../howto/enum.rst:943 msgid "" @@ -774,75 +1024,101 @@ msgid "" msgstr "" #: ../../howto/enum.rst:948 +#, fuzzy msgid "Finer Points" -msgstr "" +msgstr "微妙之處" #: ../../howto/enum.rst:951 +#, fuzzy msgid "Supported ``__dunder__`` names" -msgstr "" +msgstr "支援的 ``__dunder_ _`` 命名" #: ../../howto/enum.rst:953 +#, fuzzy msgid "" ":attr:`__members__` is a read-only ordered mapping of ``member_name``:" "``member`` items. It is only available on the class." msgstr "" +":attr:`__members__` 是一個唯讀的有序映射,包含了 ``member_name``:``member`` " +"項目,在類別中才能使用。" #: ../../howto/enum.rst:956 +#, fuzzy msgid "" ":meth:`__new__`, if specified, must create and return the enum members; it " "is also a very good idea to set the member's :attr:`_value_` appropriately. " "Once all the members are created it is no longer used." msgstr "" +"如果指定了 `:meth:`__new__`` 則必須建立並返回列舉成員;同時,為其 :attr:" +"`_value_` 正確設值是一個非常好的選擇。當所有成員都建立完成後,此方法將不再使" +"用。" #: ../../howto/enum.rst:962 +#, fuzzy msgid "Supported ``_sunder_`` names" -msgstr "" +msgstr "支援 ``_sunder_`` 命名" #: ../../howto/enum.rst:964 +#, fuzzy msgid "``_name_`` -- name of the member" -msgstr "" +msgstr "``_name_`` -- 成員的名稱" #: ../../howto/enum.rst:965 +#, fuzzy msgid "" "``_value_`` -- value of the member; can be set / modified in ``__new__``" -msgstr "" +msgstr "``_value_`` -- 成員的值;可在 ``__new__`` 中設定/修改" #: ../../howto/enum.rst:967 +#, fuzzy msgid "" "``_missing_`` -- a lookup function used when a value is not found; may be " "overridden" -msgstr "" +msgstr "``_missing_`` -- 當找不到值時使用的查詢函式;可以被覆寫" #: ../../howto/enum.rst:969 +#, fuzzy msgid "" "``_ignore_`` -- a list of names, either as a :class:`list` or a :class:" "`str`, that will not be transformed into members, and will be removed from " "the final class" msgstr "" +"``_ignore_`` -- 包含不需要作為類別成員的名稱的列表,必須是 :class:`list` 或 :" +"class:`str` 類型,這些名稱將不會轉換為類別成員且將從最終所建立的類別中移除" #: ../../howto/enum.rst:972 +#, fuzzy msgid "" "``_order_`` -- used in Python 2/3 code to ensure member order is consistent " "(class attribute, removed during class creation)" msgstr "" +"``_order_`` -- 在 Python 2/3 代碼中使用以確保成員順序一致(類屬性,在類創建期" +"間刪除)" #: ../../howto/enum.rst:974 +#, fuzzy msgid "" "``_generate_next_value_`` -- used by the `Functional API`_ and by :class:" "`auto` to get an appropriate value for an enum member; may be overridden" msgstr "" +"``_generate_next_value_`` 是供 `Functional API`_ 和 :class:`auto` 使用的方" +"法,以便取得適當的列舉成員值;可被覆寫。" #: ../../howto/enum.rst:980 +#, fuzzy msgid "" "For standard :class:`Enum` classes the next value chosen is the last value " "seen incremented by one." -msgstr "" +msgstr "對於標準的 :class:`Enum` 類別,下一個選擇的值是最後看到的值加一。" #: ../../howto/enum.rst:983 +#, fuzzy msgid "" "For :class:`Flag` classes the next value chosen will be the next highest " "power-of-two, regardless of the last value seen." msgstr "" +"對於 :class:`Flag` 類別,下一個被選擇的值是接下來最高的二次冪,無論上一個值是" +"否見過。" #: ../../howto/enum.rst:986 msgid "``_missing_``, ``_order_``, ``_generate_next_value_``" @@ -853,33 +1129,45 @@ msgid "``_ignore_``" msgstr "``_ignore_``" #: ../../howto/enum.rst:989 +#, fuzzy msgid "" "To help keep Python 2 / Python 3 code in sync an :attr:`_order_` attribute " "can be provided. It will be checked against the actual order of the " "enumeration and raise an error if the two do not match::" msgstr "" +"為了協助保持 Python 2 / Python 3 的程式碼同步,可以提供一個 :attr:`_order_` " +"屬性。它將與列舉的實際順序進行檢查,如果不符則會引發錯誤::" #: ../../howto/enum.rst:1007 +#, fuzzy msgid "" "In Python 2 code the :attr:`_order_` attribute is necessary as definition " "order is lost before it can be recorded." msgstr "" +"在 Python 2 的程式中,:attr:`_order_` 屬性是必要的, 因為定義順序在記錄之前就" +"已遺失。" #: ../../howto/enum.rst:1012 +#, fuzzy msgid "_Private__names" -msgstr "" +msgstr "私有名稱" #: ../../howto/enum.rst:1014 +#, fuzzy msgid "" ":ref:`Private names ` are not converted to enum " "members, but remain normal attributes." msgstr "" +"私有名稱(即雙下劃線開頭的屬性或方法)不會轉換成列舉成員,而是保持正常屬性。" +"請參見 :ref:`Private names `。" #: ../../howto/enum.rst:1021 +#, fuzzy msgid "``Enum`` member type" -msgstr "" +msgstr "``成員型別(member type)``" #: ../../howto/enum.rst:1023 +#, fuzzy msgid "" "Enum members are instances of their enum class, and are normally accessed as " "``EnumClass.member``. In certain situations, such as writing custom enum " @@ -888,23 +1176,33 @@ msgid "" "names and attributes/methods from mixed-in classes, upper-case names are " "strongly recommended." msgstr "" +"列舉的成員是其列舉類別的實例,通常作法是透過 ``EnumClass.member`` 來存取。在 " +"Python 版本「3.5」到「3.10」中可以從其他成員存取,但不建議此用法,在版本" +"「3.11」之後:class:`Enum` 就不再允許該用法:" #: ../../howto/enum.rst:1034 +#, fuzzy msgid "Creating members that are mixed with other data types" -msgstr "" +msgstr "建立和其他資料型別混合的夾帶(mixed type)成員" #: ../../howto/enum.rst:1036 +#, fuzzy msgid "" "When subclassing other data types, such as :class:`int` or :class:`str`, " "with an :class:`Enum`, all values after the ``=`` are passed to that data " "type's constructor. For example::" msgstr "" +"當需要對其他數據類型(例如 :class:`int` 或 :class:`str`)進行父類別派生並使用" +"到:class:`Enum`時,屬於 `=` 之後的所有值都會傳遞給該數據類型的建構函式。例" +"如:" #: ../../howto/enum.rst:1048 +#, fuzzy msgid "Boolean value of ``Enum`` classes and members" -msgstr "" +msgstr "``Enum`` 類別和成員的布林值" #: ../../howto/enum.rst:1050 +#, fuzzy msgid "" "Enum classes that are mixed with non-:class:`Enum` types (such as :class:" "`int`, :class:`str`, etc.) are evaluated according to the mixed-in type's " @@ -912,138 +1210,185 @@ msgid "" "enum's boolean evaluation depend on the member's value add the following to " "your class::" msgstr "" +"混合了非 :class:`Enum` 型別(例如:class:`int`、:class:`str`等)的枚舉類型,會" +"依據混入型別所規定的規則進行評估;否則,所有成員均評估為:data: `True`. 要讓你" +"自己的enum布林求值取決於成員值時,在您的類中添加以下內容:" #: ../../howto/enum.rst:1059 +#, fuzzy msgid "Plain :class:`Enum` classes always evaluate as :data:`True`." -msgstr "" +msgstr "通常 :class:`Enum` 類別的值會被評估為 :data:`True`。" #: ../../howto/enum.rst:1063 +#, fuzzy msgid "``Enum`` classes with methods" -msgstr "" +msgstr "具有方法的「列舉」類別" #: ../../howto/enum.rst:1065 +#, fuzzy msgid "" "If you give your enum subclass extra methods, like the `Planet`_ class " "below, those methods will show up in a :func:`dir` of the member, but not of " "the class::" msgstr "" +"如果你給你的 enum 子類別新增了一些方法,就像下面這個 `Planet`_ 類別,那這些方" +"法會出現在成員(member)的 :func:`dir`,但不會出現在枚舉(enum)本身的 :func:" +"`dir` 裡:" #: ../../howto/enum.rst:1076 +#, fuzzy msgid "Combining members of ``Flag``" -msgstr "" +msgstr "合併``Flag``的成員" #: ../../howto/enum.rst:1078 +#, fuzzy msgid "" "Iterating over a combination of :class:`Flag` members will only return the " "members that are comprised of a single bit::" -msgstr "" +msgstr "迭代 :class:`Flag` 成員的組合將只會回傳由單一位元所構成的成員。" #: ../../howto/enum.rst:1096 +#, fuzzy msgid "``Flag`` and ``IntFlag`` minutia" -msgstr "" +msgstr "`Flag` 和 `IntFlag` 細節部份" #: ../../howto/enum.rst:1098 +#, fuzzy msgid "Using the following snippet for our examples::" msgstr "" +"以下我們將運用下列程式碼片段作為範例:```pythonname = 'John'age = 30if age > " +"18: print(f'{name} is an adult.')else: print(f'{name} is not yet an " +"adult.')```" #: ../../howto/enum.rst:1109 +#, fuzzy msgid "the following are true:" -msgstr "" +msgstr "以下是真的:" #: ../../howto/enum.rst:1111 +#, fuzzy msgid "single-bit flags are canonical" -msgstr "" +msgstr "單位欄位旗標是規範的" #: ../../howto/enum.rst:1112 +#, fuzzy msgid "multi-bit and zero-bit flags are aliases" -msgstr "" +msgstr "多位元標誌和零位元標誌是別名" #: ../../howto/enum.rst:1113 +#, fuzzy msgid "only canonical flags are returned during iteration::" -msgstr "" +msgstr "迭代時只返回正規旗標 (canonical flags)。" #: ../../howto/enum.rst:1118 +#, fuzzy msgid "" "negating a flag or flag set returns a new flag/flag set with the " "corresponding positive integer value::" -msgstr "" +msgstr "否定一個旗標或旗標集會傳回對應的正整數值。" #: ../../howto/enum.rst:1127 +#, fuzzy msgid "names of pseudo-flags are constructed from their members' names::" -msgstr "" +msgstr "偽旗標的名稱是由其成員名稱建構而成的:" #: ../../howto/enum.rst:1132 +#, fuzzy msgid "multi-bit flags, aka aliases, can be returned from operations::" -msgstr "" +msgstr "多位元旗標,也就是別名,可以從操作中返回:" #: ../../howto/enum.rst:1143 +#, fuzzy msgid "" "membership / containment checking: zero-valued flags are always considered " "to be contained::" -msgstr "" +msgstr "成員 / 包含測試:零值標誌始終被認為是包含的。" #: ../../howto/enum.rst:1149 +#, fuzzy msgid "" "otherwise, only if all bits of one flag are in the other flag will True be " "returned::" -msgstr "" +msgstr "否則,只有當一個標誌的所有位都在另一個標誌中時,才會返回 True:" #: ../../howto/enum.rst:1158 +#, fuzzy msgid "" "There is a new boundary mechanism that controls how out-of-range / invalid " "bits are handled: ``STRICT``, ``CONFORM``, ``EJECT``, and ``KEEP``:" msgstr "" +"有一個新的邊界機制控制超出範圍或無效位元的處理方式: ``STRICT``(嚴格)、" +"``CONFORM``(遵從)、``EJECT``(彈出)和 ``KEEP``(保留):" #: ../../howto/enum.rst:1161 +#, fuzzy msgid "STRICT --> raises an exception when presented with invalid values" -msgstr "" +msgstr "在遇到無效數值時,STRICT(嚴格模式)會引發一個異常。" #: ../../howto/enum.rst:1162 +#, fuzzy msgid "CONFORM --> discards any invalid bits" -msgstr "" +msgstr "CONFORM --> 丟棄任何無效的位元" #: ../../howto/enum.rst:1163 +#, fuzzy msgid "EJECT --> lose Flag status and become a normal int with the given value" -msgstr "" +msgstr "彈出(EJECT) --> 失去旗標狀態,並成為一個具有給定值的普通整數" #: ../../howto/enum.rst:1164 +#, fuzzy msgid "KEEP --> keep the extra bits" msgstr "" +"保留 -> KEEP,也就是保留那些額外的內容。請遵守翻譯規範和使用括號的方式,並確" +"保文本格式符號不受影響。每行的字元數應盡量控制在79個以內,如有超出必須換行。" #: ../../howto/enum.rst:1166 +#, fuzzy msgid "keeps Flag status and extra bits" -msgstr "" +msgstr "保留標誌位狀態和額外的位元" #: ../../howto/enum.rst:1167 +#, fuzzy msgid "extra bits do not show up in iteration" -msgstr "" +msgstr "額外的資料在遍歷時不會出現" #: ../../howto/enum.rst:1168 +#, fuzzy msgid "extra bits do show up in repr() and str()" -msgstr "" +msgstr "在 \\_\\_repr\\_\\_() 和 \\_\\_str\\_\\_() 的輸出中也會顯示額外的資訊" #: ../../howto/enum.rst:1170 +#, fuzzy msgid "" "The default for Flag is ``STRICT``, the default for ``IntFlag`` is " "``EJECT``, and the default for ``_convert_`` is ``KEEP`` (see ``ssl." "Options`` for an example of when ``KEEP`` is needed)." msgstr "" +"標記的預設值為「嚴格」(``STRICT``),IntFlag 的預設值為「拋出異常」" +"(``EJECT``),而 _convert_ 的預設值則是「保持現況」(``KEEP``) (請參考 ``ssl." +"Options`` 中需要使用 ``KEEP`` 的範例)。" #: ../../howto/enum.rst:1178 +#, fuzzy msgid "How are Enums and Flags different?" -msgstr "" +msgstr "列舉型別和旗標型別有何不同?" #: ../../howto/enum.rst:1180 +#, fuzzy msgid "" "Enums have a custom metaclass that affects many aspects of both derived :" "class:`Enum` classes and their instances (members)." msgstr "" +"限定枚舉 (Enums) 有一個獨特的元類,會影響到所有衍生 :class:`Enum` 類別及其實" +"例(成員)的許多屬性。" #: ../../howto/enum.rst:1185 +#, fuzzy msgid "Enum Classes" -msgstr "" +msgstr "列舉型別類 (Enum Classes)" #: ../../howto/enum.rst:1187 +#, fuzzy msgid "" "The :class:`EnumType` metaclass is responsible for providing the :meth:" "`__contains__`, :meth:`__dir__`, :meth:`__iter__` and other methods that " @@ -1053,12 +1398,20 @@ msgid "" "final :class:`Enum` class are correct (such as :meth:`__new__`, :meth:" "`__getnewargs__`, :meth:`__str__` and :meth:`__repr__`)." msgstr "" +":class:`EnumType`(列舉型別) 元類別(meta-class) 負責提供 `__contains__`、 " +"`__dir__`、 `__iter__` 以及其他方法,允許開發人員使用像是 ``list(Color)`` 或" +"是 ``some_enum_var in Color`` 的方式對一個 :class:`Enum`(列舉類別)進行操作," +"而通常類別則無法進行。:class:`EnumType`維護被命名的常數(names constants),例" +"如 :meth:`__new__.`, :meth:`__getnewargs__.`, :meth:`__str__.`, 和:meth: " +"'__repr__',並確保最終的:class'Enum'(列舉class)符合指定格式。" #: ../../howto/enum.rst:1196 +#, fuzzy msgid "Flag Classes" -msgstr "" +msgstr "旗標類別(Flag Classes)" #: ../../howto/enum.rst:1198 +#, fuzzy msgid "" "Flags have an expanded view of aliasing: to be canonical, the value of a " "flag needs to be a power-of-two value, and not a duplicate name. So, in " @@ -1066,12 +1419,17 @@ msgid "" "a. ``0``) or with more than one power-of-two value (e.g. ``3``) is " "considered an alias." msgstr "" +"標誌有一個更廣泛的別名觀點:為了符合規範,標誌的值需要是二次冪的值,而不是重" +"複的名稱。因此除了使用 :class:`Enum` 別名定義之外,沒有值(a.k.a. `0`)或多於" +"一個二次冪數(例如 `3`)的旗幟也被視為別名。" #: ../../howto/enum.rst:1204 +#, fuzzy msgid "Enum Members (aka instances)" -msgstr "" +msgstr "列舉成員(又稱為實例)" #: ../../howto/enum.rst:1206 +#, fuzzy msgid "" "The most interesting thing about enum members is that they are singletons. :" "class:`EnumType` creates them all while it is creating the enum class " @@ -1079,136 +1437,175 @@ msgid "" "new ones are ever instantiated by returning only the existing member " "instances." msgstr "" +"enum 成員最有趣的地方在於它們是單例(singletons)。當 :class:`EnumType` 建立" +"枚舉類別本身時,就會一併建立這些成員,並提供自訂的 :meth:`__new__` 方法來確" +"保只回傳現有的成員實例,而不會再被實體化出新的。" #: ../../howto/enum.rst:1212 +#, fuzzy msgid "Flag Members" -msgstr "" +msgstr "旗標成員" #: ../../howto/enum.rst:1214 +#, fuzzy msgid "" "Flag members can be iterated over just like the :class:`Flag` class, and " "only the canonical members will be returned. For example::" msgstr "" +"旗標成員可像 :class:`Flag` 類別一樣迭代,只有經典成員會被返回。舉例而言:" #: ../../howto/enum.rst:1220 +#, fuzzy msgid "(Note that ``BLACK``, ``PURPLE``, and ``WHITE`` do not show up.)" msgstr "" +"(請注意,``BLACK``, ``PURPLE`` 和 ``WHITE`` 不會出現)注意:本句中的單引號和" +"雙引號皆維持原文不變。" #: ../../howto/enum.rst:1222 +#, fuzzy msgid "" "Inverting a flag member returns the corresponding positive value, rather " "than a negative value --- for example::" -msgstr "" +msgstr "反轉旗標成員會返回相應的正值,而不是負值 --- 例如:" #: ../../howto/enum.rst:1228 +#, fuzzy msgid "" "Flag members have a length corresponding to the number of power-of-two " "values they contain. For example::" -msgstr "" +msgstr "旗標成員的長度與他們包含的二次冥值相對應。例如:" #: ../../howto/enum.rst:1238 +#, fuzzy msgid "Enum Cookbook" -msgstr "" +msgstr "列舉型別手冊" #: ../../howto/enum.rst:1241 +#, fuzzy msgid "" "While :class:`Enum`, :class:`IntEnum`, :class:`StrEnum`, :class:`Flag`, and :" "class:`IntFlag` are expected to cover the majority of use-cases, they cannot " "cover them all. Here are recipes for some different types of enumerations " "that can be used directly, or as examples for creating one's own." -msgstr "" +msgstr "雖然我們期望 :class:`Enum`、:cl" #: ../../howto/enum.rst:1248 +#, fuzzy msgid "Omitting values" -msgstr "" +msgstr "省略值" #: ../../howto/enum.rst:1250 +#, fuzzy msgid "" "In many use-cases, one doesn't care what the actual value of an enumeration " "is. There are several ways to define this type of simple enumeration:" msgstr "" +"很多時候,我們不在意列舉(enum)的實際值。定義這種簡單的列舉型別有幾種方式:" #: ../../howto/enum.rst:1253 +#, fuzzy msgid "use instances of :class:`auto` for the value" -msgstr "" +msgstr "使用 :class:`auto` 的實例當作值" #: ../../howto/enum.rst:1254 +#, fuzzy msgid "use instances of :class:`object` as the value" -msgstr "" +msgstr "使用 :class:`object` 的實例作為值。" #: ../../howto/enum.rst:1255 +#, fuzzy msgid "use a descriptive string as the value" -msgstr "" +msgstr "使用描述性的字串作為值" #: ../../howto/enum.rst:1256 +#, fuzzy msgid "" "use a tuple as the value and a custom :meth:`__new__` to replace the tuple " "with an :class:`int` value" msgstr "" +"使用 tuple 作為值,再加上自定義的 :meth:`__new__` 函式來將該 tuple 替換成 :" +"class:`int` 型別的值" #: ../../howto/enum.rst:1259 +#, fuzzy msgid "" "Using any of these methods signifies to the user that these values are not " "important, and also enables one to add, remove, or reorder members without " "having to renumber the remaining members." msgstr "" +"使用這些方法之一,向使用者傳達數值不重要的意涵,也能讓你加入、移除或重新排序" +"成員而不必重新編號其他成員。" #: ../../howto/enum.rst:1265 +#, fuzzy msgid "Using :class:`auto`" -msgstr "" +msgstr "使用 :class:`auto`" #: ../../howto/enum.rst:1267 +#, fuzzy msgid "Using :class:`auto` would look like::" -msgstr "" +msgstr "使用 `auto` 類別會像這樣:" #: ../../howto/enum.rst:1279 +#, fuzzy msgid "Using :class:`object`" -msgstr "" +msgstr "使用 `object` 類別" #: ../../howto/enum.rst:1281 +#, fuzzy msgid "Using :class:`object` would look like::" -msgstr "" +msgstr "使用 :class:`object` 會長得像這樣:" #: ../../howto/enum.rst:1291 +#, fuzzy msgid "" "This is also a good example of why you might want to write your own :meth:" "`__repr__`::" -msgstr "" +msgstr "這也是撰寫自己的 :meth:`__repr__` 的好範例。" #: ../../howto/enum.rst:1307 +#, fuzzy msgid "Using a descriptive string" -msgstr "" +msgstr "使用一個描述字串" #: ../../howto/enum.rst:1309 +#, fuzzy msgid "Using a string as the value would look like::" -msgstr "" +msgstr "將字串用作值的寫法如下所示::" #: ../../howto/enum.rst:1321 +#, fuzzy msgid "Using a custom :meth:`__new__`" -msgstr "" +msgstr "使用自訂的 :meth:`__new__` 方法" #: ../../howto/enum.rst:1323 +#, fuzzy msgid "Using an auto-numbering :meth:`__new__` would look like::" -msgstr "" +msgstr "使用自動編號的 :meth:`__new__` 方法會像這樣:" #: ../../howto/enum.rst:1340 +#, fuzzy msgid "" "To make a more general purpose ``AutoNumber``, add ``*args`` to the " "signature::" -msgstr "" +msgstr "為了讓``AutoNumber``成為更多用途的程式,請在函數签名中新增 ``*args``:" #: ../../howto/enum.rst:1350 +#, fuzzy msgid "" "Then when you inherit from ``AutoNumber`` you can write your own " "``__init__`` to handle any extra arguments::" msgstr "" +"當你從 ``AutoNumber`` 繼承時,可以撰寫自己的 `__init__` 來處理任何額外引數:" #: ../../howto/enum.rst:1369 +#, fuzzy msgid "" "The :meth:`__new__` method, if defined, is used during creation of the Enum " "members; it is then replaced by Enum's :meth:`__new__` which is used after " "class creation for lookup of existing members." msgstr "" +"如果已定義,:meth:`__new__` 方法會在創建 Enum 成員時使用;然後它會被 Enu" #: ../../howto/enum.rst:1375 msgid "" @@ -1218,57 +1615,76 @@ msgstr "" #: ../../howto/enum.rst:1382 msgid "OrderedEnum" -msgstr "" +msgstr "OrderedEnum" #: ../../howto/enum.rst:1384 +#, fuzzy msgid "" "An ordered enumeration that is not based on :class:`IntEnum` and so " "maintains the normal :class:`Enum` invariants (such as not being comparable " "to other enumerations)::" msgstr "" +"一個有序的列舉,它不依據 :class:`IntEnum` 類別而是遵循正常的 :class:`Enum` 不" +"變量 (例如不能與其他列舉進行比較):" #: ../../howto/enum.rst:1418 +#, fuzzy msgid "DuplicateFreeEnum" -msgstr "" +msgstr "重複值枚舉 (DuplicateFreeEnum)" #: ../../howto/enum.rst:1420 +#, fuzzy msgid "" "Raises an error if a duplicate member value is found instead of creating an " "alias::" -msgstr "" +msgstr "如果出現重複的成員值而不是建立別名,則會引發錯誤:" #: ../../howto/enum.rst:1445 +#, fuzzy msgid "" "This is a useful example for subclassing Enum to add or change other " "behaviors as well as disallowing aliases. If the only desired change is " "disallowing aliases, the :func:`unique` decorator can be used instead." msgstr "" +"這是一個有用的範例,可以通過繼承 Enum 來添加或修改其他行為以及禁止別名。如果" +"唯一需要的更改是禁止使用別名,那麼可以使用 :func:`unique` 裝飾器。" #: ../../howto/enum.rst:1451 +#, fuzzy msgid "Planet" -msgstr "" +msgstr "Planet" #: ../../howto/enum.rst:1453 +#, fuzzy msgid "" "If :meth:`__new__` or :meth:`__init__` is defined, the value of the enum " "member will be passed to those methods::" msgstr "" +"如果定義了 :meth:`__new__` 或是 :meth:`__init__`,將會把該枚舉成員的值傳遞給" +"這些方法。" #: ../../howto/enum.rst:1482 +#, fuzzy msgid "TimePeriod" -msgstr "" +msgstr "TimePeriod(時間期間)" #: ../../howto/enum.rst:1484 +#, fuzzy msgid "An example to show the :attr:`_ignore_` attribute in use::" -msgstr "" +msgstr "一個展示使用 :attr:`_ignore_` 屬性的範例:" #: ../../howto/enum.rst:1503 +#, fuzzy msgid "Subclassing EnumType" -msgstr "" +msgstr "子類別化 EnumType" #: ../../howto/enum.rst:1505 +#, fuzzy msgid "" "While most enum needs can be met by customizing :class:`Enum` subclasses, " "either with class decorators or custom functions, :class:`EnumType` can be " "subclassed to provide a different Enum experience." msgstr "" +"雖然大部分的枚舉需求可以透過客製化 :class:`Enum` subclasses,藉由使用類別裝飾" +"器或是自定義的函數來應付,在創造另一種不同的枚舉體驗時,可以建立 :class:" +"`EnumType` 子類別。" diff --git a/howto/functional.po b/howto/functional.po index 12ddb77d69..91011c2a00 100644 --- a/howto/functional.po +++ b/howto/functional.po @@ -8,9 +8,9 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-07-24 00:03+0000\n" -"PO-Revision-Date: 2018-05-23 14:36+0000\n" -"Last-Translator: Adrian Liaw \n" +"POT-Creation-Date: 2024-02-24 16:01+0800\n" +"PO-Revision-Date: 2023-03-26 18:25+0000\n" +"Last-Translator: CTHua \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" "Language: zh_TW\n" @@ -18,6 +18,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" +"X-Generator: Weblate 4.16.4\n" #: ../../howto/functional.rst:3 msgid "Functional Programming HOWTO" @@ -40,6 +41,7 @@ msgid "0.32" msgstr "0.32" #: ../../howto/functional.rst:8 +#, fuzzy msgid "" "In this document, we'll take a tour of Python's features suitable for " "implementing programs in a functional style. After an introduction to the " @@ -47,31 +49,43 @@ msgid "" "term:`iterator`\\s and :term:`generator`\\s and relevant library modules " "such as :mod:`itertools` and :mod:`functools`." msgstr "" +"在本文件中,我們將介紹適用於使用函數式風格實現程式的Python特性。在簡介函數式" +"編程概念後,我們將探討語言功能,例如「迭代器(iterator)」和「產生器" +"(generator)」以及相關的庫模塊如「itertools module(itertools函式庫) 」和" +"「functools module(functools函式庫)」。" #: ../../howto/functional.rst:16 msgid "Introduction" msgstr "簡介" #: ../../howto/functional.rst:18 +#, fuzzy msgid "" "This section explains the basic concept of functional programming; if you're " "just interested in learning about Python language features, skip to the next " "section on :ref:`functional-howto-iterators`." msgstr "" +"本節說明函數式程式設計的基本概念;如果你只想學習有關 Python 語言功能,請跳至" +"下一節 :ref:`functional-howto-iterators`。" #: ../../howto/functional.rst:22 +#, fuzzy msgid "" "Programming languages support decomposing problems in several different ways:" -msgstr "" +msgstr "程式語言支援多種不同的方式來拆解問題:" #: ../../howto/functional.rst:24 +#, fuzzy msgid "" "Most programming languages are **procedural**: programs are lists of " "instructions that tell the computer what to do with the program's input. C, " "Pascal, and even Unix shells are procedural languages." msgstr "" +"大部分的程式語言都是**程序式**的:程式是由一連串指令所組成,這些指令告訴電腦" +"要如何處理程式的輸入。C、Pascal 以及甚至 UNIX shells 都屬於程序式語言。" #: ../../howto/functional.rst:28 +#, fuzzy msgid "" "In **declarative** languages, you write a specification that describes the " "problem to be solved, and the language implementation figures out how to " @@ -80,8 +94,13 @@ msgid "" "to retrieve, and the SQL engine decides whether to scan tables or use " "indexes, which subclauses should be performed first, etc." msgstr "" +"在**宣告式(declarative)**語言中,你所撰寫的規範描述問題與計算目標,而程式會" +"決定如何高效地執行運算。SQL是你最可能熟悉的宣告式語言;一個 SQL 查詢描述了你" +"要檢索的數據集,然後由 SQL 引擎決定是否掃描表或使用索引、哪些副子句首先執行等" +"等。" #: ../../howto/functional.rst:35 +#, fuzzy msgid "" "**Object-oriented** programs manipulate collections of objects. Objects " "have internal state and support methods that query or modify this internal " @@ -89,8 +108,13 @@ msgid "" "and Python are languages that support object-oriented programming, but don't " "force the use of object-oriented features." msgstr "" +"面向物件編程(Object-oriented programming)是一種以對象作為基本單位進行運算的" +"程式設計方法。每個對象都擁有內在狀態,並支援可以查詢或修改此內部狀態的方法。" +"Smalltalk 和 Java 是面向物件語言,而C++和Python 雖然可以支援面向物件程式設計" +"但不強制使用這些特性。" #: ../../howto/functional.rst:41 +#, fuzzy msgid "" "**Functional** programming decomposes a problem into a set of functions. " "Ideally, functions only take inputs and produce outputs, and don't have any " @@ -98,8 +122,12 @@ msgid "" "known functional languages include the ML family (Standard ML, OCaml, and " "other variants) and Haskell." msgstr "" +"**函式程式設計(Functional programming)** 將問題分解為一組函數。理想情況下," +"函數只接收輸入並產生輸出,且不具備任何內部狀態會影響特定輸入的輸出結果。知名" +"的函示程式語言包括ML系列(Standard ML、OCaml, 和其他變種) 和 Haskell." #: ../../howto/functional.rst:47 +#, fuzzy msgid "" "The designers of some computer languages choose to emphasize one particular " "approach to programming. This often makes it difficult to write programs " @@ -111,8 +139,16 @@ msgid "" "GUI might be object-oriented while the processing logic is procedural or " "functional, for example." msgstr "" +"某些電腦語言的設計者選擇強調特定的程式編寫方法,因此製作不同編寫方式的程式會" +"變得很困難。其他則是支援數種程式編寫方法( multi-paradigm ) 的多范型 (multi-" +"paradigm) 語言。像是 Lisp、C++ 和 Python 都是多范型語言,你可以使用不同的編寫" +"方式在這幾種語言中撰寫主要以 procedural(程序化)、object-oriented(物件導向)" +"或 functional(函數導向)等三大方式;當你在開發一個龐大的程式時,不同區塊可能" +"採用不同的策略(e.g. GUI物件導向, 而logic部分則結合procedural 或 functional)," +"依情況而定。" #: ../../howto/functional.rst:58 +#, fuzzy msgid "" "In a functional program, input flows through a set of functions. Each " "function operates on its input and produces some output. Functional style " @@ -122,8 +158,13 @@ msgid "" "side effects means not using data structures that get updated as a program " "runs; every function's output must only depend on its input." msgstr "" +"在函數式程式中,輸入會流經一組函數。每個函數都會操作輸入並生成某些輸出。 函數" +"式風格不鼓勵有副作用的函數,即修改內部狀態或做出其他在返回值中看不到變化的更" +"改。 沒有任何副作用的功能稱為「純粹函數」。避免使用隨程序運行而更新的資料結" +"構,每個函數的輸出必須僅依賴其輸入。(79字元)" #: ../../howto/functional.rst:66 +#, fuzzy msgid "" "Some languages are very strict about purity and don't even have assignment " "statements such as ``a=3`` or ``c = a + b``, but it's difficult to avoid all " @@ -133,8 +174,13 @@ msgid "" "their side effects of sending some text to the screen or pausing execution " "for a second." msgstr "" +"一些程式語言非常嚴格,對於純淨度要求很高,甚至沒有賦值陳述句(例如 ``a=3`` " +"或 ``c = a + b``),但很難避免所有副作用,例如向螢幕列印或寫入磁碟檔案。另一" +"個例子是呼叫 :func:`print` 或 :func:`time.sleep` 函式,這兩者都沒有返回一個有" +"用的值。它們只會因其副作用而被調用,如向螢幕發送一些文字或暫停執行 1 秒鐘。" #: ../../howto/functional.rst:73 +#, fuzzy msgid "" "Python programs written in functional style usually won't go to the extreme " "of avoiding all I/O or all assignments; instead, they'll provide a " @@ -143,8 +189,12 @@ msgid "" "assignments to local variables, but won't modify global variables or have " "other side effects." msgstr "" +"用函數式風格撰寫的 Python 程序通常不會極端地避免所有 I/O 或所有賦值。相反,它" +"們會提供一個看起來很「函數式」 的介面, 但在內部仍然使用非函數式特性。例如, " +"函数的实现仍会使用賦值给本地變量,但不會修改全局变量或产生其它副作用。" #: ../../howto/functional.rst:79 +#, fuzzy msgid "" "Functional programming can be considered the opposite of object-oriented " "programming. Objects are little capsules containing some internal state " @@ -155,41 +205,56 @@ msgid "" "approaches by writing functions that take and return instances representing " "objects in your application (e-mail messages, transactions, etc.)." msgstr "" +"函數式編程可視為物件導向編程的相反。物件是一些小膠囊,含有內部狀態和一組方法" +"調用,讓您可以修改此狀態,而程序則包括進行正確的設定更改。函數式編程希望盡可" +"能避免更改硬體裝置,并且利用在函數之間流動的資料來進行運作。在Python中,您可" +"以通過撰寫接受並返回屬於應用程式中去展現物件(例如電子郵件消息、交易等) 的實" +"例 函數來結合這兩種方法。" #: ../../howto/functional.rst:88 +#, fuzzy msgid "" "Functional design may seem like an odd constraint to work under. Why should " "you avoid objects and side effects? There are theoretical and practical " "advantages to the functional style:" msgstr "" +"函數式設計看似是一個奇怪的限制,為什麼要避免使用物件和副作用呢?但實踐中與理" +"論上都有函數式風格的好處:" #: ../../howto/functional.rst:92 +#, fuzzy msgid "Formal provability." -msgstr "" +msgstr "正式可證明性。" #: ../../howto/functional.rst:93 +#, fuzzy msgid "Modularity." -msgstr "" +msgstr "模組化。" #: ../../howto/functional.rst:94 +#, fuzzy msgid "Composability." -msgstr "" +msgstr "組合性。" #: ../../howto/functional.rst:95 +#, fuzzy msgid "Ease of debugging and testing." -msgstr "" +msgstr "容易除錯和測試。" #: ../../howto/functional.rst:99 +#, fuzzy msgid "Formal provability" -msgstr "" +msgstr "正式可證明性" #: ../../howto/functional.rst:101 +#, fuzzy msgid "" "A theoretical benefit is that it's easier to construct a mathematical proof " "that a functional program is correct." -msgstr "" +msgstr "一個理論上的好處是,構建數學證明能更容易地確認函數式程式是否正確。" #: ../../howto/functional.rst:104 +#, fuzzy msgid "" "For a long time researchers have been interested in finding ways to " "mathematically prove programs correct. This is different from testing a " @@ -198,8 +263,13 @@ msgid "" "looks right; the goal is instead a rigorous proof that a program produces " "the right result for all possible inputs." msgstr "" +"許多研究者長期以來一直致力於找尋數學上證明程式正確性的方法。這和在大量輸入中" +"測試程式、並得出其輸出通常是正確的結果,或閱讀程式原始碼、並得出該代碼看起來" +"沒有問題不同;此處的目標是嚴格地證明:對於所有可能的輸入,該程序都能產生正確" +"的結果。" #: ../../howto/functional.rst:111 +#, fuzzy msgid "" "The technique used to prove programs correct is to write down " "**invariants**, properties of the input data and of the program's variables " @@ -209,16 +279,24 @@ msgid "" "This continues until you reach the end of the program, at which point the " "invariants should match the desired conditions on the program's output." msgstr "" +"證明程式的正確性技術是撰寫**不變式(invariants)**,這些不變式會反映輸入資料" +"和程式變數的屬性,而且總是成立。對於每行代碼,在執行之前如果有不變式X與Y成" +"立,就必須證明在執行後相似但略為不同的X'與Y'也需成立。重複此程序直到結束整段" +"程式碼時,應該符合期望產生程式的輸出條件。" #: ../../howto/functional.rst:119 +#, fuzzy msgid "" "Functional programming's avoidance of assignments arose because assignments " "are difficult to handle with this technique; assignments can break " "invariants that were true before the assignment without producing any new " "invariants that can be propagated onward." msgstr "" +"函數式編程不使用賦值是由於這種技術對處理賦值有困難。賦值會破壞先前的不變量," +"且未產生可往後傳遞的新不變量。" #: ../../howto/functional.rst:124 +#, fuzzy msgid "" "Unfortunately, proving programs correct is largely impractical and not " "relevant to Python software. Even trivial programs require proofs that are " @@ -229,12 +307,20 @@ msgid "" "the question of verifying the proof; maybe there's an error in it, and you " "wrongly believe you've proved the program correct." msgstr "" +"很不幸地,證明程式的正確性在實務上是不切實際且與 Python 軟體無關。即使是極簡" +"單的程式,其正確性的證明需要數頁才能完成;對於中等複雜度的程式來說,進行正確" +"性證明會非常龐大,在您日常使用的許多程式(例如 Python 直譯器、XML 解析器或瀏" +"覽器)當中只有少數甚至可能沒有被驗證過它們是否正確。 即使您擁有或產生了一份可" +"用作該程序之正確性證明文件,仍需考量如何驗証該文件本身;也許裡面存在錯誤、而" +"你卻錯誤地相信你已經成功地予以變更完畢並儲存該結果。" #: ../../howto/functional.rst:135 +#, fuzzy msgid "Modularity" -msgstr "" +msgstr "模組化" #: ../../howto/functional.rst:137 +#, fuzzy msgid "" "A more practical benefit of functional programming is that it forces you to " "break apart your problem into small pieces. Programs are more modular as a " @@ -242,16 +328,22 @@ msgid "" "thing than a large function that performs a complicated transformation. " "Small functions are also easier to read and to check for errors." msgstr "" +"函數式編程更切實的優點是它強迫你將問題分解成一個個小部分,程式因此變得更有模" +"組化。寫出只處理單一事項的小函示比較龐大且進行複雜轉換的大函數還要容易。另" +"外,促使拆分出多個小函示也可以增加易讀性和錯誤檢查效率。" #: ../../howto/functional.rst:145 +#, fuzzy msgid "Ease of debugging and testing" -msgstr "" +msgstr "調試和測試的便利性" #: ../../howto/functional.rst:147 +#, fuzzy msgid "Testing and debugging a functional-style program is easier." -msgstr "" +msgstr "測試和除錯功能風格的程式比較容易。" #: ../../howto/functional.rst:149 +#, fuzzy msgid "" "Debugging is simplified because functions are generally small and clearly " "specified. When a program doesn't work, each function is an interface point " @@ -259,20 +351,29 @@ msgid "" "intermediate inputs and outputs to quickly isolate the function that's " "responsible for a bug." msgstr "" +"偵錯變得簡單,因為函式通常都很小又清楚明確。當程式出現問題時,每個函式都是一" +"個介面點,在那裡您可以檢查資料是否正確。您可以查看中間輸入和輸出來迅速分離負" +"責漏洞的函式。" #: ../../howto/functional.rst:154 +#, fuzzy msgid "" "Testing is easier because each function is a potential subject for a unit " "test. Functions don't depend on system state that needs to be replicated " "before running a test; instead you only have to synthesize the right input " "and then check that the output matches expectations." msgstr "" +"測試更簡單了,因為每個函數都可以成為單元測試的潛在對象。函數不依賴於需要複製" +"系統狀態以運行測試的系統狀態;相反,您只需合成正確的輸入,然後檢查輸出是否符" +"合預期。" #: ../../howto/functional.rst:161 +#, fuzzy msgid "Composability" -msgstr "" +msgstr "組合性" #: ../../howto/functional.rst:163 +#, fuzzy msgid "" "As you work on a functional-style program, you'll write a number of " "functions with varying inputs and outputs. Some of these functions will be " @@ -282,25 +383,38 @@ msgid "" "that takes a filename and returns its contents, can be applied to many " "different situations." msgstr "" +"當您在開發函數風格的程式時,會撰寫多個具有不同輸入和輸出的函式。一些函 式會無" +"法避免地針對特定應用進行專門化設計,但其他部分則可以在許多不同類型 的程式中使" +"用。例如,一個接受目錄路徑並返回該目錄中所有XML文件的函數, 或者一個接受文件" +"名稱並返回其內容的函數,在許多不同情況下都是非常實 用的。" #: ../../howto/functional.rst:170 +#, fuzzy msgid "" "Over time you'll form a personal library of utilities. Often you'll " "assemble new programs by arranging existing functions in a new configuration " "and writing a few functions specialized for the current task." msgstr "" +"隨著時間過去,您會建立一個個人函式庫(library)。您常常只需要在排列現有的型別" +"(type)中加入一些引數(argument)與參數(parameter),並編寫幾個針對目前任務特化的" +"函式即可組裝新程式。" #: ../../howto/functional.rst:178 +#, fuzzy msgid "Iterators" -msgstr "" +msgstr "迭代器" #: ../../howto/functional.rst:180 +#, fuzzy msgid "" "I'll start by looking at a Python language feature that's an important " "foundation for writing functional-style programs: iterators." msgstr "" +"我會從一個對於撰寫函數式程式非常重要的 Python 語言特性開始,那就是「迭代" +"器」。" #: ../../howto/functional.rst:183 +#, fuzzy msgid "" "An iterator is an object representing a stream of data; this object returns " "the data one element at a time. A Python iterator must support a method " @@ -310,8 +424,14 @@ msgid "" "exception. Iterators don't have to be finite, though; it's perfectly " "reasonable to write an iterator that produces an infinite stream of data." msgstr "" +"一個迭代器 (iterator) 是代表資料串流的物件;這個物件會一次回傳一個元素。在 " +"Python 中,迭代器必須支援名為 :meth:`~iterator.__next__` 的方法,不需接受任何" +"引數且永遠回傳串流中下一個元素。若串流中沒有更多元素則 :meth:`~iterator." +"__next__` 必須引發 :exc:`StopIteration` 例外。雖然迭代器可以是非有限的,在程" +"式中撰寫可產生無限資料串流的迭代器也完全合理。" #: ../../howto/functional.rst:191 +#, fuzzy msgid "" "The built-in :func:`iter` function takes an arbitrary object and tries to " "return an iterator that will return the object's contents or elements, " @@ -320,32 +440,48 @@ msgid "" "lists and dictionaries. An object is called :term:`iterable` if you can get " "an iterator for it." msgstr "" +"內建的 :func:`iter` 函式可以接受任意物件,並嘗試回傳一個迭代器以返回物件的內" +"容或元素,若該物件不支援迭代則會產生 :exc:`TypeError`。Python 的多數內建資料" +"型別支援迭代,最常見的是串列和字典。當你能夠取得一個迭代器來處理某個物件時," +"這個物件就稱作是 :term:`可迭代的(iterable)`" #: ../../howto/functional.rst:198 +#, fuzzy msgid "You can experiment with the iteration interface manually:" -msgstr "" +msgstr "您可以手動嘗試迭代介面:" #: ../../howto/functional.rst:216 +#, fuzzy msgid "" "Python expects iterable objects in several different contexts, the most " "important being the :keyword:`for` statement. In the statement ``for X in " "Y``, Y must be an iterator or some object for which :func:`iter` can create " "an iterator. These two statements are equivalent::" msgstr "" +"Python 在多種情況下會期待可迭代物件(iterable),其中最主要的情況是在 :keyword:" +"`for` 陳述式中。在 ``for X in Y`` 的陳述式當中,變數 Y 必須為一個迭代器" +"(iterator)或可使用 :func:`iter` 建立出迭代器的物件(object)。以下兩個陳述式亦" +"具相同解釋:" #: ../../howto/functional.rst:228 +#, fuzzy msgid "" "Iterators can be materialized as lists or tuples by using the :func:`list` " "or :func:`tuple` constructor functions:" msgstr "" +"使用 :func:`list` 或 :func:`tuple` 建構函數可以將迭代器實現為列表或元組:" #: ../../howto/functional.rst:237 +#, fuzzy msgid "" "Sequence unpacking also supports iterators: if you know an iterator will " "return N elements, you can unpack them into an N-tuple:" msgstr "" +"序列展開(Sequence unpacking)也支援迭代器:如果你知道一個迭代器會回傳 N 個元" +"素,你可以將它們解壓縮成一個 N-tuple。" #: ../../howto/functional.rst:246 +#, fuzzy msgid "" "Built-in functions such as :func:`max` and :func:`min` can take a single " "iterator argument and will return the largest or smallest element. The " @@ -355,8 +491,14 @@ msgid "" "func:`min` will never return, and if the element X never appears in the " "stream, the ``\"in\"`` and ``\"not in\"`` operators won't return either." msgstr "" +"Python 中的內建函式(如::func:`max`、:func:`min`)可以接受單一的迭代器引數," +"並回傳該迭代器中最大或最小的元素。而「in」和「not in」運算子也支援迭代器:當X" +"在該迭代器返回之串流中被找到時,``X in iterator`` 會為真。如果所提供的迭代器" +"是無限大的,你將會遇到明顯問題; :func:`max` 或 :func:`min` 永不結束,若元素 " +"X 未出現於串流中, ``\"in\"`` 和 ``\"not in\"`` 運算子將同時失效。" #: ../../howto/functional.rst:254 +#, fuzzy msgid "" "Note that you can only go forward in an iterator; there's no way to get the " "previous element, reset the iterator, or make a copy of it. Iterator " @@ -366,63 +508,90 @@ msgid "" "need to do something different with the same stream, you'll have to create a " "new iterator." msgstr "" +"注意,您只能在迭代器中向前移動;沒有辦法獲取先前的元素、重置迭代器或複製它。 " +"迭代器物件可以選擇提供這些額外功能,但是迭代協議僅指定了:meth:`~iterator." +"__next__` 方法。 因此函式可能會消耗所有迭代器的輸出,如果您需要使用相同的流做" +"一些不同的事情,則必須創建新的迭代器。" #: ../../howto/functional.rst:264 +#, fuzzy msgid "Data Types That Support Iterators" -msgstr "" +msgstr "支援迭代器的資料型別" #: ../../howto/functional.rst:266 +#, fuzzy msgid "" "We've already seen how lists and tuples support iterators. In fact, any " "Python sequence type, such as strings, will automatically support creation " "of an iterator." msgstr "" +"我們已經看過了列表和元組如何支援迭代器。事實上,任何 Python 序列型別,例如字" +"串,都會自動支援建立迭代器。" #: ../../howto/functional.rst:270 +#, fuzzy msgid "" "Calling :func:`iter` on a dictionary returns an iterator that will loop over " "the dictionary's keys::" -msgstr "" +msgstr "呼叫 `iter` 函式處理字典會回傳一個迭代器,可循環取得該字典的鍵:" #: ../../howto/functional.rst:290 +#, fuzzy msgid "" "Note that starting with Python 3.7, dictionary iteration order is guaranteed " "to be the same as the insertion order. In earlier versions, the behaviour " "was unspecified and could vary between implementations." msgstr "" +"注意,在 Python 3.7 版本後,字典的迭代順序保證與插入順序相同。在較早的版本" +"中,此行為未定義且可能因實作而異。" #: ../../howto/functional.rst:294 +#, fuzzy msgid "" "Applying :func:`iter` to a dictionary always loops over the keys, but " "dictionaries have methods that return other iterators. If you want to " "iterate over values or key/value pairs, you can explicitly call the :meth:" "`~dict.values` or :meth:`~dict.items` methods to get an appropriate iterator." msgstr "" +"將 :func:`iter` 應用於字典時,會一直遍歷鍵(key),但是字典有其它回傳迭代器的方" +"法。因此如果你想要對值(value)或鍵/值 (key/value) 進行迭代操作,可以明確呼叫 :" +"meth:`~dict.values` 或 :meth:`~dict.items` 方法取得相應的迭代器。" #: ../../howto/functional.rst:300 +#, fuzzy msgid "" "The :func:`dict` constructor can accept an iterator that returns a finite " "stream of ``(key, value)`` tuples:" msgstr "" +":func:`dict` 建構式可接受回傳有限的 ``(key, value)`` tuple 流的迭代器:" #: ../../howto/functional.rst:307 +#, fuzzy msgid "" "Files also support iteration by calling the :meth:`~io.TextIOBase.readline` " "method until there are no more lines in the file. This means you can read " "each line of a file like this::" msgstr "" +"檔案還支援迭代,我們可以呼叫 :meth:`~io.TextIOBase.readline` 方法來逐行讀取檔" +"案,直到檔案結束為止。也就是說,我們可以透過以下方式來逐一讀取檔案中的每一" +"行:" #: ../../howto/functional.rst:315 +#, fuzzy msgid "" "Sets can take their contents from an iterable and let you iterate over the " "set's elements::" msgstr "" +"set 型別可以由 iterable 取得其內容,並且讓你能夠迭代(set iteration)所有的元" +"素:" #: ../../howto/functional.rst:331 +#, fuzzy msgid "Generator expressions and list comprehensions" -msgstr "" +msgstr "生成器表示式與串列生成式" #: ../../howto/functional.rst:333 +#, fuzzy msgid "" "Two common operations on an iterator's output are 1) performing some " "operation for every element, 2) selecting a subset of elements that meet " @@ -430,21 +599,30 @@ msgid "" "strip off trailing whitespace from each line or extract all the strings " "containing a given substring." msgstr "" +"迭代器的輸出上,兩個常見操作是:1)對每個元素進行某些操作,2)選擇符合特定條" +"件的元素子集。例如,給定一個字符串列表,您可能希望從每行中去除尾部空格或提取" +"所有包含特定子字符串的字符串。" #: ../../howto/functional.rst:339 +#, fuzzy msgid "" "List comprehensions and generator expressions (short form: \"listcomps\" and " "\"genexps\") are a concise notation for such operations, borrowed from the " "functional programming language Haskell (https://www.haskell.org/). You can " "strip all the whitespace from a stream of strings with the following code::" msgstr "" +"清單推導式和生成器表達式(簡稱「清單推導」和「生成器」)是一個簡潔的表示法," +"用於這樣的操作,並借用自函數式程式語言 Haskell (https://www.haskell.org/)。您" +"可以使用以下代碼從字符串流中刪除所有空格:" #: ../../howto/functional.rst:352 +#, fuzzy msgid "" "You can select only certain elements by adding an ``\"if\"`` condition::" -msgstr "" +msgstr "你可以透過加入 ``\"if\"`` 條件,只選擇特定的元素:" #: ../../howto/functional.rst:357 +#, fuzzy msgid "" "With a list comprehension, you get back a Python list; ``stripped_list`` is " "a list containing the resulting lines, not an iterator. Generator " @@ -454,36 +632,51 @@ msgid "" "infinite stream or a very large amount of data. Generator expressions are " "preferable in these situations." msgstr "" +"使用串列生成式,可以得到一個 Python 串列;`stripped_list` 是包含結果行的串" +"列,而不是迭代器。產生器表示式返回會根據需要計算值的迭代器,它不需要一次性將" +"所有值都實現。這意味著如果您正在使用返回無限流或大量數據的遠程執行引數時,列" +"表推導可能並不好用。在這些情況下更適合使用產生器表示式。" #: ../../howto/functional.rst:364 +#, fuzzy msgid "" "Generator expressions are surrounded by parentheses (\"()\") and list " "comprehensions are surrounded by square brackets (\"[]\"). Generator " "expressions have the form::" msgstr "" +"生成器表達式被圓括號(\"()\")包圍,而列表推導式則被方括號(\"[]\")包圍。生成" +"器表達式的形式為:" #: ../../howto/functional.rst:378 +#, fuzzy msgid "" "Again, for a list comprehension only the outside brackets are different " "(square brackets instead of parentheses)." -msgstr "" +msgstr "再次提醒,列表推導式只有外層括號不同(使用「中括號」而非「小括號」)。" #: ../../howto/functional.rst:381 +#, fuzzy msgid "" "The elements of the generated output will be the successive values of " "``expression``. The ``if`` clauses are all optional; if present, " "``expression`` is only evaluated and added to the result when ``condition`` " "is true." msgstr "" +"生成的輸出元素將是「expression」連續的值。如果有,「if」子句都是可選的;如果" +"存在,只有當條件為true時才會評估並添加「expression」到結果中。" #: ../../howto/functional.rst:385 +#, fuzzy msgid "" "Generator expressions always have to be written inside parentheses, but the " "parentheses signalling a function call also count. If you want to create an " "iterator that will be immediately passed to a function you can write::" msgstr "" +"生成器表達式總是必須寫在括號內,但用於函式呼叫的括號也被視為一種。如果想要創" +"建一個立即傳入函數中的迭代器,可寫成:" #: ../../howto/functional.rst:391 +#, fuzzy msgid "" "The ``for...in`` clauses contain the sequences to be iterated over. The " "sequences do not have to be the same length, because they are iterated over " @@ -492,40 +685,56 @@ msgid "" "looped over for each resulting pair of elements from ``sequence1`` and " "``sequence2``." msgstr "" +"``for...in`` 子句包含要迭代的序列。這些序列不必長度相同,因為它們從左到右遞" +"迴, **而非**並排運作。對於 ``sequence1`` 中的每個元素,都會从開始首元素在 " +"``sequence2`` 循环,在此基礎上再对组合出来的每对“元素”依次从头至尾地访问 " +"``sequence3``。" #: ../../howto/functional.rst:397 +#, fuzzy msgid "" "To put it another way, a list comprehension or generator expression is " "equivalent to the following Python code::" -msgstr "" +msgstr "換句話說,列表推導式或生成器表達式相當於以下 Python 代碼:" #: ../../howto/functional.rst:414 +#, fuzzy msgid "" "This means that when there are multiple ``for...in`` clauses but no ``if`` " "clauses, the length of the resulting output will be equal to the product of " "the lengths of all the sequences. If you have two lists of length 3, the " "output list is 9 elements long:" msgstr "" +"這表示當有多個 ``for ... in`` 子句但沒有 ``if`` 子句時,輸出的長度會等於所有" +"序列長度的乘積。如果你有兩個長度為3 的列表,則輸出列表將包含 9 個元素:" #: ../../howto/functional.rst:426 +#, fuzzy msgid "" "To avoid introducing an ambiguity into Python's grammar, if ``expression`` " "is creating a tuple, it must be surrounded with parentheses. The first list " "comprehension below is a syntax error, while the second one is correct::" msgstr "" +"為避免 Python 語法產生歧義,在建立元組時必須使用括號將「expression」包圍。第" +"一個列表理解式是語法錯誤,而第二個則正確:" #: ../../howto/functional.rst:437 +#, fuzzy msgid "Generators" -msgstr "" +msgstr "生成器" #: ../../howto/functional.rst:439 +#, fuzzy msgid "" "Generators are a special class of functions that simplify the task of " "writing iterators. Regular functions compute a value and return it, but " "generators return an iterator that returns a stream of values." msgstr "" +"產生器是一種特殊類型的函數,可簡化迭代器撰寫任務。通常函數運算並且回傳值,但" +"產生器回傳一個可提供一系列值得迭代物件。" #: ../../howto/functional.rst:443 +#, fuzzy msgid "" "You're doubtless familiar with how regular function calls work in Python or " "C. When you call a function, it gets a private namespace where its local " @@ -537,19 +746,29 @@ msgid "" "function where it left off? This is what generators provide; they can be " "thought of as resumable functions." msgstr "" +"你應該對在 Python 或 C 中如何呼叫常規函式很熟悉。當你呼叫一個函式時,它會得到" +"一個私用的命名空間以創建其本地變量。當函式達到`return`語句時,這些本地變量就" +"被銷毀並返回值給呼叫者。後續對相同函數的調用會創建新的私有命名空間和全新集合" +"的本地變數。但是,如果退出函数时不要丟棄本地变量怎么办? 如果您可以稍后恢复该" +"函数从离开点处继续执行会怎样?这就是生成器提供了什么;它们可以视为可恢复功能" #: ../../howto/functional.rst:452 +#, fuzzy msgid "Here's the simplest example of a generator function:" -msgstr "" +msgstr "以下是 generator function 最簡單的範例:" #: ../../howto/functional.rst:458 +#, fuzzy msgid "" "Any function containing a :keyword:`yield` keyword is a generator function; " "this is detected by Python's :term:`bytecode` compiler which compiles the " "function specially as a result." msgstr "" +"任何包含 :keyword:`yield` 關鍵字的函式都是生成器(generator) 函式;Python 的 " +"**字節碼(bytecode)** 編譯器會特別地編譯這種函式。" #: ../../howto/functional.rst:462 +#, fuzzy msgid "" "When you call a generator function, it doesn't return a single value; " "instead it returns a generator object that supports the iterator protocol. " @@ -560,26 +779,41 @@ msgid "" "preserved. On the next call to the generator's :meth:`~generator.__next__` " "method, the function will resume executing." msgstr "" +"當你調用 generator function 時,它並不會返回單一的值;而是回傳支援迭代器協定" +"(iterator protocol) 的 generator 物件。當執行到 ``yield`` 運算式時,程式將輸" +"出變數 i 的值,就像使用 ``return`` 陳述句時一樣。 ``yield`` 與 ``return`` 陳" +"述句間的差別在於,在達成「yield」後,generator 程序被暫停並保留當下本地變數" +"(local variables) 後往下執行。接下來再次呼叫該generator物件之 :meth:" +"`~generator.__next__` 方法時,此函示將繼續執行。" #: ../../howto/functional.rst:471 +#, fuzzy msgid "Here's a sample usage of the ``generate_ints()`` generator:" -msgstr "" +msgstr "以下是 ``generate_ints()`` 生成器的一個範例使用:" #: ../../howto/functional.rst:488 +#, fuzzy msgid "" "You could equally write ``for i in generate_ints(5)``, or ``a, b, c = " "generate_ints(3)``." msgstr "" +"你可以同樣寫為``for i in generate_ints(5)``,或者是``a, b, c = " +"generate_ints(3)``。" #: ../../howto/functional.rst:491 +#, fuzzy msgid "" "Inside a generator function, ``return value`` causes " "``StopIteration(value)`` to be raised from the :meth:`~generator.__next__` " "method. Once this happens, or the bottom of the function is reached, the " "procession of values ends and the generator cannot yield any further values." msgstr "" +"在產生器函數中,使用 ``return value`` 會導致從 :meth:`~generator.__next__` 方" +"法中拋出 ``StopIteration(value)``。一旦此情況發生或函數已執行到最深層,數值序" +"列即停止進展且該產生器函式無法再繼續或提供後續之值。" #: ../../howto/functional.rst:496 +#, fuzzy msgid "" "You could achieve the effect of generators manually by writing your own " "class and storing all the local variables of the generator as instance " @@ -588,16 +822,24 @@ msgid "" "method increment ``self.count`` and return it. However, for a moderately " "complicated generator, writing a corresponding class can be much messier." msgstr "" +"你可以透過手動編寫類別,並將所有產生器的區域變數存儲為實例變數感受到生成器效" +"果。例如,返回一個整數列表可通過設置「self.count」為0,並在: meth:`〜" +"iterator. __ next__`方法中增加「self.count」並返回它來完成。但是,對於中等程" +"度複雜的生成器 ,編寫相應的類可能會更加混亂。" #: ../../howto/functional.rst:504 +#, fuzzy msgid "" "The test suite included with Python's library, :source:`Lib/test/" "test_generators.py`, contains a number of more interesting examples. Here's " "one generator that implements an in-order traversal of a tree using " "generators recursively. ::" msgstr "" +"Python 函式庫內所附的測試套件 :source:`Lib/test/test_generators.py` 包含許多" +"有趣的範例。以下是一個使用遞迴單元生成器實作樹之中序走訪程式範例:" #: ../../howto/functional.rst:520 +#, fuzzy msgid "" "Two other examples in ``test_generators.py`` produce solutions for the N-" "Queens problem (placing N queens on an NxN chess board so that no queen " @@ -605,12 +847,17 @@ msgid "" "knight to every square of an NxN chessboard without visiting any square " "twice)." msgstr "" +"在 ``test_generators.py`` 中有另外兩個例子解決 N 皇后問題 (在 NxN 的象棋盤上" +"放置 N 個皇后,使得每個皇后不互相威脅)以及騎士巡迴(找到一條路徑讓騎士遍歷 " +"NxN 的棋盤且不重複造訪任何一格)。" #: ../../howto/functional.rst:528 +#, fuzzy msgid "Passing values into a generator" -msgstr "" +msgstr "傳遞參數至生成器" #: ../../howto/functional.rst:530 +#, fuzzy msgid "" "In Python 2.4 and earlier, generators only produced output. Once a " "generator's code was invoked to create an iterator, there was no way to pass " @@ -619,23 +866,33 @@ msgid "" "variable or by passing in some mutable object that callers then modify, but " "these approaches are messy." msgstr "" +"在 Python 2.4 及之前的版本中,生成器只能產生輸出。一旦啟動生成器代碼以創建迭" +"代器,當其執行被恢復時就無法傳遞任何新信息進入函式中。您可以通過使生成器查看" +"全局變數或傳入某些可變對象然後由調用者修改來編寫此功能,但這種方法很麻煩。" #: ../../howto/functional.rst:537 +#, fuzzy msgid "" "In Python 2.5 there's a simple way to pass values into a generator. :keyword:" "`yield` became an expression, returning a value that can be assigned to a " "variable or otherwise operated on::" msgstr "" +"在Python 2.5中,有一種簡單的方法可以將值傳遞給生成器。 ``yield`` 成為一個表達" +"式,會回傳可指派給變數或進行其他操作的值:" #: ../../howto/functional.rst:543 +#, fuzzy msgid "" "I recommend that you **always** put parentheses around a ``yield`` " "expression when you're doing something with the returned value, as in the " "above example. The parentheses aren't always necessary, but it's easier to " "always add them instead of having to remember when they're needed." msgstr "" +"建議當處理 ``yield`` 回傳的值時,請**總是**加上括號,如前述範例所示。這樣做並" +"非必要,但如果一直加上括號會比記得何時需要更容易。" #: ../../howto/functional.rst:548 +#, fuzzy msgid "" "(:pep:`342` explains the exact rules, which are that a ``yield``-expression " "must always be parenthesized except when it occurs at the top-level " @@ -643,47 +900,66 @@ msgid "" "write ``val = yield i`` but have to use parentheses when there's an " "operation, as in ``val = (yield i) + 12``.)" msgstr "" +"(:pep:`342` 指出準確的規則,即必須為 ``yield``-expression 加入括號,除非它出" +"現在指派運算式右側的最上層表達式。這意味著如果有運算,你需要使用括號,例如: " +"``val = (yield i) + 12`` ,但是你也可以直接寫成 ``val = yield i`` 避免使用括" +"號以作為最上層表達式。 )" #: ../../howto/functional.rst:554 +#, fuzzy msgid "" "Values are sent into a generator by calling its :meth:`send(value) " "` method. This method resumes the generator's code and the " "``yield`` expression returns the specified value. If the regular :meth:" "`~generator.__next__` method is called, the ``yield`` returns ``None``." msgstr "" +"透過呼叫生成器的 :meth:`send(value) ` 方法,可以將值送入生成" +"器。此方法重新啟動生成器執行程式並傳回指定的值,若是以一般 :meth:`~generator." +"__next__` 方法呼叫時,則會回傳 `None`。" #: ../../howto/functional.rst:559 +#, fuzzy msgid "" "Here's a simple counter that increments by 1 and allows changing the value " "of the internal counter." -msgstr "" +msgstr "這裡有一個簡單的計數器,可以使內部變數增量1並允許改變其值。" #: ../../howto/functional.rst:574 +#, fuzzy msgid "And here's an example of changing the counter:" -msgstr "" +msgstr "下面是更改計數器的範例:" #: ../../howto/functional.rst:591 +#, fuzzy msgid "" "Because ``yield`` will often be returning ``None``, you should always check " "for this case. Don't just use its value in expressions unless you're sure " "that the :meth:`~generator.send` method will be the only method used to " "resume your generator function." msgstr "" +"因為 ``yield`` 非常容易回傳成 `None`,所以你必須特別留意這種情況。除非你確定" +"只會使用 :meth:`~generator.send` 來重新啟動生成器函式,否則不要直接在運算中使" +"用其的回傳值。" #: ../../howto/functional.rst:596 +#, fuzzy msgid "" "In addition to :meth:`~generator.send`, there are two other methods on " "generators:" -msgstr "" +msgstr "除了 ``generator.send`` 方法之外,生成器還有另外兩種方法:" #: ../../howto/functional.rst:599 +#, fuzzy msgid "" ":meth:`throw(value) ` is used to raise an exception inside " "the generator; the exception is raised by the ``yield`` expression where the " "generator's execution is paused." msgstr "" +":meth:`throw(value) ` 用於在生成器內部引發異常;該異常由暫停" +"生成器執行的 ``yield`` 表達式引發。" #: ../../howto/functional.rst:603 +#, fuzzy msgid "" ":meth:`~generator.close` raises a :exc:`GeneratorExit` exception inside the " "generator to terminate the iteration. On receiving this exception, the " @@ -693,21 +969,32 @@ msgid "" "be called by Python's garbage collector when the generator is garbage-" "collected." msgstr "" +":meth:`~generator.close()` 會在產生器(iterator)中丟出 :exc:`GeneratorExit`例" +"外來終止迭代。當接收到此例外時,產生器的程式碼必須將其拋出 :exc:" +"`GeneratorExit` 或是 :exc:`StopIteration`; 接住這個例外做任何其他事情都是不合" +"法的,而且會觸發一個 :exc:`RuntimeError`。當垃圾回收系統處理釋放記憶體回收" +"(generator物件),也會呼叫:meth:`~generator.close()` 。" #: ../../howto/functional.rst:611 +#, fuzzy msgid "" "If you need to run cleanup code when a :exc:`GeneratorExit` occurs, I " "suggest using a ``try: ... finally:`` suite instead of catching :exc:" "`GeneratorExit`." msgstr "" +"如果你需要在 :exc:`GeneratorExit` 發生時運行清理程式,我建議使用 ``try: ... " +"finally:`` 套件區塊替代捕獲 :exc:`GeneratorExit`。" #: ../../howto/functional.rst:614 +#, fuzzy msgid "" "The cumulative effect of these changes is to turn generators from one-way " "producers of information into both producers and consumers." msgstr "" +"這些變更的累積效應是,將產生資訊的單向生成器轉換為同時兼具生成及消費能力。" #: ../../howto/functional.rst:617 +#, fuzzy msgid "" "Generators also become **coroutines**, a more generalized form of " "subroutines. Subroutines are entered at one point and exited at another " @@ -715,37 +1002,53 @@ msgid "" "can be entered, exited, and resumed at many different points (the ``yield`` " "statements)." msgstr "" +"產生器也可以變成 **協程(coroutine)** ,一種更廣義的子程序(subroutine)形式。子" +"程序從一個點進入並在另一個點退出(函數頂部和“return”語句),但是協程可以在許多" +"不同的點中被進入、退出和恢復(“yield”語句)。" #: ../../howto/functional.rst:624 +#, fuzzy msgid "Built-in functions" -msgstr "" +msgstr "內建函式" #: ../../howto/functional.rst:626 +#, fuzzy msgid "" "Let's look in more detail at built-in functions often used with iterators." -msgstr "" +msgstr "讓我們更詳細地看一下與迭代器經常使用的內建函式。" #: ../../howto/functional.rst:628 +#, fuzzy msgid "" "Two of Python's built-in functions, :func:`map` and :func:`filter` duplicate " "the features of generator expressions:" msgstr "" +"Python 內建函式 `map` 和 `filter` 兩個函式,都有生成器運算 expr 的功能。" #: ../../howto/functional.rst:640 +#, fuzzy msgid "" ":func:`map(f, iterA, iterB, ...) ` returns an iterator over the sequence" msgstr "" +":func:`map(f, iterA, iterB...) ` 回傳一個迭代器,此迭代器針對序列進行映" +"射。" #: ../../howto/functional.rst:632 +#, fuzzy msgid "" "``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ...``." msgstr "" +"``f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2])...``。" +"(註解:這個是用在 Python 中「將若干個 iterable 作為參數餵給一個函式」的快速" +"寫法,`zip` 在這種情況下表現得特別優秀。)" #: ../../howto/functional.rst:642 +#, fuzzy msgid "You can of course achieve the same effect with a list comprehension." -msgstr "" +msgstr "你當然可以使用串列生成式來達到相同的效果。" #: ../../howto/functional.rst:644 +#, fuzzy msgid "" ":func:`filter(predicate, iter) ` returns an iterator over all the " "sequence elements that meet a certain condition, and is similarly duplicated " @@ -753,140 +1056,201 @@ msgid "" "truth value of some condition; for use with :func:`filter`, the predicate " "must take a single value." msgstr "" +":func:`filter(predicate, iter) ` 會回傳一個迭代器,其中包含符合特定條" +"件的所有序列元素。此功能與串列生成式類似地運作。**predicate(謂詞)** 是一種" +"函數,用於返回某些情況下的真實值;對於 :func:`filter` 的使用,謂詞必須接受單" +"個值作為引數。" #: ../../howto/functional.rst:657 +#, fuzzy msgid "This can also be written as a list comprehension:" -msgstr "" +msgstr "這個也可以被形式化為串列生成式:" #: ../../howto/functional.rst:663 +#, fuzzy msgid "" ":func:`enumerate(iter, start=0) ` counts off the elements in the " "iterable returning 2-tuples containing the count (from *start*) and each " "element. ::" msgstr "" +":func:`enumerate(iter, start=0) ` 函数可用来枚举 iterable 中元素的" +"序号并返回一个二元组,包含了序号(从 *start* 开始)以及每个元素。 ::" #: ../../howto/functional.rst:673 +#, fuzzy msgid "" ":func:`enumerate` is often used when looping through a list and recording " "the indexes at which certain conditions are met::" -msgstr "" +msgstr "在遍歷列表並記錄符合特定條件的索引時,常使用 :func:`enumerate` 函數。" #: ../../howto/functional.rst:681 +#, fuzzy msgid "" ":func:`sorted(iterable, key=None, reverse=False) ` collects all the " "elements of the iterable into a list, sorts the list, and returns the sorted " "result. The *key* and *reverse* arguments are passed through to the " "constructed list's :meth:`~list.sort` method. ::" msgstr "" +":func:`sorted(iterable, key=None, reverse=False) <排序>` 會將可迭代的 " +"iterable 內容放到一個串列中,再依照順序進行排序並回傳。*key* 和 *reverse* 引" +"數也會被傳遞給建立的串列物件使用 :meth:`~list.sort` 方法。" #: ../../howto/functional.rst:696 +#, fuzzy msgid "" "(For a more detailed discussion of sorting, see the :ref:`sortinghowto`.)" -msgstr "" +msgstr "(關於排序的更詳細討論,請參閱 :ref:`sortinghowto`。)" #: ../../howto/functional.rst:699 +#, fuzzy msgid "" "The :func:`any(iter) ` and :func:`all(iter) ` built-ins look at " "the truth values of an iterable's contents. :func:`any` returns ``True`` if " "any element in the iterable is a true value, and :func:`all` returns " "``True`` if all of the elements are true values:" msgstr "" +"`:func:`any(iter) ` 與 :func:`all(iter) ` 是內建函式,用於檢查可迭" +"代物件的真實值。:func:`any` 會在可迭代物件中任意一個元素為真時回傳``True``;" +"而 :func:`all` 則會在所有元素都為真時回傳``True``:" #: ../../howto/functional.rst:718 +#, fuzzy msgid "" ":func:`zip(iterA, iterB, ...) ` takes one element from each iterable " "and returns them in a tuple::" msgstr "" +":func:`zip(iterA, iterB, ...) ` 針對每個可迭代物件取出一個元素,並以 " +"tuple 的形式回傳。" #: ../../howto/functional.rst:724 +#, fuzzy msgid "" "It doesn't construct an in-memory list and exhaust all the input iterators " "before returning; instead tuples are constructed and returned only if " "they're requested. (The technical term for this behaviour is `lazy " "evaluation `__.)" msgstr "" +"它並未構建一個記憶體串列而在回傳前耗盡所有輸入的迭代器;若要求時,它只有構建" +"元組才會回傳。(這種行為的技術名詞是「`惰性計算 `__。)" #: ../../howto/functional.rst:729 +#, fuzzy msgid "" "This iterator is intended to be used with iterables that are all of the same " "length. If the iterables are of different lengths, the resulting stream " "will be the same length as the shortest iterable. ::" msgstr "" +"此迭代器適用於長度相同的可迭代物件。如果這些可迭代物件的長度不同,則結果將與" +"最短可迭代物件一樣長。::(註:「此」可以視情況改成更貼切原文或上下文的詞彙;" +"「註」中的冒號為全形半形皆通用)" #: ../../howto/functional.rst:736 +#, fuzzy msgid "" "You should avoid doing this, though, because an element may be taken from " "the longer iterators and discarded. This means you can't go on to use the " "iterators further because you risk skipping a discarded element." msgstr "" +"你應避免這樣做,因為可能從較長的迭代器中取出元素並將其丟棄。這意味著你不能再" +"延續使用此迭代器,否則會有被跳過的舊元素風險。" #: ../../howto/functional.rst:742 +#, fuzzy msgid "The itertools module" -msgstr "" +msgstr "Python 模組 itertools" #: ../../howto/functional.rst:744 +#, fuzzy msgid "" "The :mod:`itertools` module contains a number of commonly used iterators as " "well as functions for combining several iterators. This section will " "introduce the module's contents by showing small examples." msgstr "" +"``itertools`` (迭代工具) 模組包含許多常用的迭代器,以及合併數個迭代器的函式。" +"本節會透過一些小例子來介紹模組內容。" #: ../../howto/functional.rst:748 +#, fuzzy msgid "The module's functions fall into a few broad classes:" -msgstr "" +msgstr "這個模組的函式可以分為幾個廣泛的類別:" #: ../../howto/functional.rst:750 +#, fuzzy msgid "Functions that create a new iterator based on an existing iterator." -msgstr "" +msgstr "基於現有的iterator建立新的遍歷器函數。" #: ../../howto/functional.rst:751 +#, fuzzy msgid "Functions for treating an iterator's elements as function arguments." -msgstr "" +msgstr "將迭代器的元素當作函式引數來處理的函式。" #: ../../howto/functional.rst:752 +#, fuzzy msgid "Functions for selecting portions of an iterator's output." -msgstr "" +msgstr "選取迭代器輸出的某些部分的函式。" #: ../../howto/functional.rst:753 +#, fuzzy msgid "A function for grouping an iterator's output." -msgstr "" +msgstr "一個用於將迭代器輸出分組的函式。" #: ../../howto/functional.rst:756 +#, fuzzy msgid "Creating new iterators" -msgstr "" +msgstr "建立新的迭代器" #: ../../howto/functional.rst:758 +#, fuzzy msgid "" ":func:`itertools.count(start, step) ` returns an infinite " "stream of evenly spaced values. You can optionally supply the starting " "number, which defaults to 0, and the interval between numbers, which " "defaults to 1::" msgstr "" +":func:`itertools.count(start, step) ` 回傳一個無限的序列,其" +"中包含均勻分佈的值。您可以選擇提供起始數字(預設為 0)和數字間隔時間(預設為 " +"1)::" #: ../../howto/functional.rst:769 +#, fuzzy msgid "" ":func:`itertools.cycle(iter) ` saves a copy of the contents " "of a provided iterable and returns a new iterator that returns its elements " "from first to last. The new iterator will repeat these elements " "infinitely. ::" msgstr "" +":func:`itertools.cycle(iter) ` 會儲存提供的可迭代物件之內容" +"並回傳一個新型別 `iterator` ,該 `iterator` 將從頭到尾依序回傳它所儲存的元" +"素。新的 `iterator` 會無限次數重複這些元素。::注意,上述英文原句有半形冒號符" +"號和 \"returns\" 單字(因為它是函式文件,使用命令式風格慣用語)。我也保留了" +"讓 python docutils 和 sphinx 自動辨識此格式需要的 rst 格式符號 (backticks, " +"angle-bracket-link-syntax) 。" #: ../../howto/functional.rst:776 +#, fuzzy msgid "" ":func:`itertools.repeat(elem, [n]) ` returns the provided " "element *n* times, or returns the element endlessly if *n* is not " "provided. ::" msgstr "" +":func:`itertools.repeat(elem, [n]) ` 回傳提供的元素 *n* " +"次,若未提供 *n* 值就無限次回傳此元素。::" #: ../../howto/functional.rst:784 +#, fuzzy msgid "" ":func:`itertools.chain(iterA, iterB, ...) ` takes an " "arbitrary number of iterables as input, and returns all the elements of the " "first iterator, then all the elements of the second, and so on, until all of " "the iterables have been exhausted. ::" msgstr "" +":func:`itertools.chain(iterA, iterB, ...) ` 會以多個迭代器為" +"輸入,返回第一個迭代器中的所有元素,然後是第二個迭代器的所有元素以此類推直到" +"每個迭代器都已被耗盡。" #: ../../howto/functional.rst:792 +#, fuzzy msgid "" ":func:`itertools.islice(iter, [start], stop, [step]) ` " "returns a stream that's a slice of the iterator. With a single *stop* " @@ -896,8 +1260,15 @@ msgid "" "and list slicing, you can't use negative values for *start*, *stop*, or " "*step*. ::" msgstr "" +":func:`itertools.islice(iter, [start], stop, [step]) ` 會回" +"傳一個迭代器的子序列 (slice)。只有一個 *stop* 參數時,會回傳前面的第 *stop* " +"個元素。如果您提供起始索引(start),會取得你想要的區間長度 *stop-start* 內元" +"素;同理,若您提供步距(step)參數值,將被省略指定元素格數後, 回傳接續該點剩下" +"满足长宽要求之子序列內容物件。請注意與 Python 的字串及清單$subscript$方式" +"(slicing)不同,在此無法使用負數來定義 $start$、$stop$ 或 $step$. ::" #: ../../howto/functional.rst:806 +#, fuzzy msgid "" ":func:`itertools.tee(iter, [n]) ` replicates an iterator; it " "returns *n* independent iterators that will all return the contents of the " @@ -906,12 +1277,20 @@ msgid "" "iterator, so this can consume significant memory if the iterator is large " "and one of the new iterators is consumed more than the others. ::" msgstr "" +":func:`itertools.tee(iter, [n]) ` 複製一個迭代器;它會回傳 " +"*n* 個互相獨立的迭代器,這些新的迭代器都會返回源頭(iter)的內容。如果你對於 " +"*n* 沒有給定值,那麼預設值就是2。 複製一個迭代器需要保存源頭(iter)部分內容," +"所以如果其中一個新的iterator被 more than the others 消耗掉比其他 iterators多" +"很多時,此操作可能會消耗大量記憶體。::(提示:本段文字來自Python官方文件中關" +"於 `itertools` 函式庫中 `tee()` 函数說明文檔)" #: ../../howto/functional.rst:825 +#, fuzzy msgid "Calling functions on elements" -msgstr "" +msgstr "對元素呼叫函式" #: ../../howto/functional.rst:827 +#, fuzzy msgid "" "The :mod:`operator` module contains a set of functions corresponding to " "Python's operators. Some examples are :func:`operator.add(a, b) ` " "(returns a callable that fetches the ``.id`` attribute)." msgstr "" +"``:mod:`operator` 模組包含一系列對應於 Python 運算子的函式。其中有些例子如" +"下:``- :func:`operator.add(a, b) `(加法)- :func:`operator." +"ne(a, b) `(與 ``a != b`` 相同)- :func:`operator." +"attrgetter('id') `(回傳取得 ``.id`` 屬性的可呼叫" +"(callable)物件)" #: ../../howto/functional.rst:833 +#, fuzzy msgid "" ":func:`itertools.starmap(func, iter) ` assumes that the " "iterable will return a stream of tuples, and calls *func* using these tuples " "as the arguments::" msgstr "" +":func:`itertools.starmap(func, iter) `假設可迭代物件會傳回" +"一串的元祖(tuple),並使用這些元祖當作引數呼叫*func*。" #: ../../howto/functional.rst:845 +#, fuzzy msgid "Selecting elements" -msgstr "" +msgstr "選取元素" #: ../../howto/functional.rst:847 +#, fuzzy msgid "" "Another group of functions chooses a subset of an iterator's elements based " "on a predicate." -msgstr "" +msgstr "另一組函式是利用預定條件篩選出迭代器中的元素子集。" #: ../../howto/functional.rst:850 +#, fuzzy msgid "" ":func:`itertools.filterfalse(predicate, iter) ` is " "the opposite of :func:`filter`, returning all elements for which the " "predicate returns false::" msgstr "" +":func:`itertools.filterfalse(predicate, iter) ` 是 :" +"func:`filter` 的相反,會傳回所有於述詞函式中回傳「False」的元素:" #: ../../howto/functional.rst:857 +#, fuzzy msgid "" ":func:`itertools.takewhile(predicate, iter) ` returns " "elements for as long as the predicate returns true. Once the predicate " "returns false, the iterator will signal the end of its results. ::" msgstr "" +"`itertools.takewhile(predicate, iter) `函式會回傳一直到" +"斷言條件返 回 False 為止的元素。 一旦是因為斷言條件返回了 False 而停止,該遍" +"歷器就會回報內容的結束。" #: ../../howto/functional.rst:870 +#, fuzzy msgid "" ":func:`itertools.dropwhile(predicate, iter) ` discards " "elements while the predicate returns true, and then returns the rest of the " "iterable's results. ::" msgstr "" +":func:`itertools.dropwhile(predicate, iter) ` 會在 " +"predicate 回傳 True 時,捨棄元素,一直到 predicate 回傳 False 為止再回傳 " +"iterable 中剩下的結果。" #: ../../howto/functional.rst:880 +#, fuzzy msgid "" ":func:`itertools.compress(data, selectors) ` takes two " "iterators and returns only those elements of *data* for which the " "corresponding element of *selectors* is true, stopping whenever either one " "is exhausted::" msgstr "" +":func:`itertools.compress(data, selectors) ` 函式需要兩個" +"迭代器,並回傳當 *selectors* 中對應的元素為 true 時 *data* 對應的元素。若其中" +"一個迭代器耗盡,該函式也會停止運行:" #: ../../howto/functional.rst:889 +#, fuzzy msgid "Combinatoric functions" -msgstr "" +msgstr "組合函數" #: ../../howto/functional.rst:891 +#, fuzzy msgid "" "The :func:`itertools.combinations(iterable, r) ` " "returns an iterator giving all possible *r*-tuple combinations of the " "elements contained in *iterable*. ::" msgstr "" +"`itertools.combinations(iterable, r) ` 函式會回傳一個" +"迭代器物件,其中包含從 *iterable* 物件中選擇出所有可能的長度為 *r* 的元素並組" +"合成 tuple 所形成之迭代器。" #: ../../howto/functional.rst:906 +#, fuzzy msgid "" "The elements within each tuple remain in the same order as *iterable* " "returned them. For example, the number 1 is always before 2, 3, 4, or 5 in " @@ -985,26 +1395,39 @@ msgid "" "permutations(iterable, r=None) `, removes this " "constraint on the order, returning all possible arrangements of length *r*::" msgstr "" +"每個 tuple 內的元素順序與 *iterable* 回傳時相同。例如,上述範例中數字 1 總是" +"在 2、3、4 或 5 的前面。一個類似的函式 :func:`itertools." +"permutations(iterable, r=None) ` 移除了此限制,並返回" +"所有長度為 *r* 的可能排列:" #: ../../howto/functional.rst:925 +#, fuzzy msgid "" "If you don't supply a value for *r* the length of the iterable is used, " "meaning that all the elements are permuted." msgstr "" +"如果你不提供 *r* 的值,將使用可迭代物件的長度來進行排列,也就是說所有元素都會" +"被重組。" #: ../../howto/functional.rst:928 +#, fuzzy msgid "" "Note that these functions produce all of the possible combinations by " "position and don't require that the contents of *iterable* are unique::" msgstr "" +"請注意,這些函式按位置產生所有可能的組合,不需要 *iterable* 的內容是唯一的。" #: ../../howto/functional.rst:935 +#, fuzzy msgid "" "The identical tuple ``('a', 'a', 'b')`` occurs twice, but the two 'a' " "strings came from different positions." msgstr "" +"這個相同的元組 ``('a', 'a', 'b')`` 出現了兩次,但是這裡的兩個字串 'a' 是來自" +"不同的位置。" #: ../../howto/functional.rst:938 +#, fuzzy msgid "" "The :func:`itertools.combinations_with_replacement(iterable, r) ` function relaxes a different constraint: " @@ -1012,12 +1435,18 @@ msgid "" "selected for the first position of each tuple and then is replaced before " "the second element is selected. ::" msgstr "" +"該函式 :func:`itertools.combinations_with_replacement(iterable, r) " +"` 可以放寬限制,讓相同的元素可以在同" +"一個 tuple 內重覆出現。此函式是先選擇第一個位置的元素,接著再更換該元素單打才" +"繼續選取下一個;如此類推,直到組合成完整的 tuple。" #: ../../howto/functional.rst:953 +#, fuzzy msgid "Grouping elements" -msgstr "" +msgstr "將元素分組" #: ../../howto/functional.rst:955 +#, fuzzy msgid "" "The last function I'll discuss, :func:`itertools.groupby(iter, " "key_func=None) `, is the most complicated. " @@ -1025,35 +1454,52 @@ msgid "" "element returned by the iterable. If you don't supply a key function, the " "key is simply each element itself." msgstr "" +"我可以幫你翻譯如下:最後一個要談的函式是 :func:`itertools.groupby(iter, " +"key_func=None) `。它是最複雜的之一,``key_func(elem)`` 是" +"用來計算可迭代物件中每個元素的關鍵值(key value) 的函式。如果沒有提供關鍵字 " +"(Key function),則以各個元素本身當作其鍵(key)。" #: ../../howto/functional.rst:960 +#, fuzzy msgid "" ":func:`~itertools.groupby` collects all the consecutive elements from the " "underlying iterable that have the same key value, and returns a stream of 2-" "tuples containing a key value and an iterator for the elements with that key." msgstr "" +":func:`~itertools.groupby` 會將具有相同鍵值的連續元素從底層可迭代物件中收集起" +"來,並返回包含鍵值和該鍵下元素的迭代器2個項目的流程。" #: ../../howto/functional.rst:988 +#, fuzzy msgid "" ":func:`~itertools.groupby` assumes that the underlying iterable's contents " "will already be sorted based on the key. Note that the returned iterators " "also use the underlying iterable, so you have to consume the results of " "iterator-1 before requesting iterator-2 and its corresponding key." msgstr "" +":func:`~itertools.groupby` 假設底層的可迭代物件已基於鍵值排序。需要注意的是," +"所返回之 iterators 也使用了原始的可迭代物件,因此必須先消耗掉 iterator-1 的結" +"果,再取用 iterator-2 及其對應鍵值。" #: ../../howto/functional.rst:995 +#, fuzzy msgid "The functools module" -msgstr "" +msgstr "`functools` 模組" #: ../../howto/functional.rst:997 +#, fuzzy msgid "" "The :mod:`functools` module contains some higher-order functions. A **higher-" "order function** takes one or more functions as input and returns a new " "function. The most useful tool in this module is the :func:`functools." "partial` function." msgstr "" +"Python 中 :mod:`functools` 模組包含了一些高階函式。**高階函式** 可以接受一個" +"或多個函式作為輸入並回傳新的函式。這個模組中最有用的工具是 :func:`functools." +"partial` 函數。" #: ../../howto/functional.rst:1002 +#, fuzzy msgid "" "For programs written in a functional style, you'll sometimes want to " "construct variants of existing functions that have some of the parameters " @@ -1062,14 +1508,22 @@ msgid "" "filling in a value for one of ``f()``'s parameters. This is called " "\"partial function application\"." msgstr "" +"對於以函數式風格撰寫的程式,有時您可能想要建立現有函數的變體,其中填入某些引" +"數。考慮一個 Python 函數 ``f(a, b, c)``;您可以希望創建一個新函數 ``g(b, " +"c)`` ,它等效於 ``f(1, b, c)``;這相當於為 ``f()`` 的一個參數填寫了值。這稱為" +"「部分函數應用」(\"partial function application\")。" #: ../../howto/functional.rst:1008 +#, fuzzy msgid "" "The constructor for :func:`~functools.partial` takes the arguments " "``(function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)``. The " "resulting object is callable, so you can just call it to invoke ``function`` " "with the filled-in arguments." msgstr "" +"`:func:`~functools.partial` 的建構子接受 ``(function, arg1, arg2, ..., " +"kwarg1=value1, kwarg2=value2)`` 引數。該物件是可呼叫的(callable),因此您只需" +"呼叫該物件以填入引數,然後調用 ``function``。" #: ../../howto/functional.rst:1013 msgid "Here's a small but realistic example::" @@ -1079,6 +1533,7 @@ msgstr "" "::" #: ../../howto/functional.rst:1025 +#, fuzzy msgid "" ":func:`functools.reduce(func, iter, [initial_value]) ` " "cumulatively performs an operation on all the iterable's elements and, " @@ -1092,88 +1547,125 @@ msgid "" "If the initial value is supplied, it's used as a starting point and " "``func(initial_value, A)`` is the first calculation. ::" msgstr "" +":func:`functools.reduce(func, iter, [initial_value]) ` 會對" +"可疊代的所有元素施行運算,因此無法應用在無限的可疊代物件上。*func* 必須是一個" +"接受兩個元素並回傳單一值的函式。 :func:`functools.reduce` 取得由疊代器返回的" +"第一個和第二個元素 A 和 B,並計算 ``func(A,B)`` 。 然後請求第三個元素 C ,計" +"算 ``func(func(A,B),C)`` ,結合此結果和傳回之第四個物件持續進行直到耗盡該疊代" +"器。 如果沒有任何返回值將引發 :exc:`TypeError` 錯誤。如果提供了起始值,則使用" +"它做為起點並進行「初始」處理,在首次遠算時會呼叫 `` func(initial_value,A)``::" #: ../../howto/functional.rst:1049 +#, fuzzy msgid "" "If you use :func:`operator.add` with :func:`functools.reduce`, you'll add up " "all the elements of the iterable. This case is so common that there's a " "special built-in called :func:`sum` to compute it:" msgstr "" +"如果您在 :func:`functools.reduce` 中使用 :func:`operator.add` ,它將會加總可" +"迭代物件中的所有元素。這個情況非常常見,因此 Python 已內建一個名為 :func:" +"`sum` 的函式用來計算:" #: ../../howto/functional.rst:1061 +#, fuzzy msgid "" "For many uses of :func:`functools.reduce`, though, it can be clearer to just " "write the obvious :keyword:`for` loop::" msgstr "" +"很多時候,在使用 :func:`functools.reduce` 時,只要寫一個明顯的 :keyword:" +"`for` 迴圈可能會更清楚易懂:" #: ../../howto/functional.rst:1073 +#, fuzzy msgid "" "A related function is :func:`itertools.accumulate(iterable, func=operator." "add) `. It performs the same calculation, but instead " "of returning only the final result, :func:`~itertools.accumulate` returns an " "iterator that also yields each partial result::" msgstr "" +"一個相關的函數是 :func:`itertools.accumulate(iterable, func=operator.add) " +"` 。它執行相同的計算,但不僅會回傳最終結果,也會產生每" +"個部分結果的迭代器:" #: ../../howto/functional.rst:1086 +#, fuzzy msgid "The operator module" -msgstr "" +msgstr "operator 模組" #: ../../howto/functional.rst:1088 +#, fuzzy msgid "" "The :mod:`operator` module was mentioned earlier. It contains a set of " "functions corresponding to Python's operators. These functions are often " "useful in functional-style code because they save you from writing trivial " "functions that perform a single operation." msgstr "" +"之前有提到 :mod:`operator` 模組,它包含一些對應 Python 運算子的函式。這些函式" +"常用於函數風格的程式碼中,因為可以省去寫只完成單一運算子操作的無用功。" #: ../../howto/functional.rst:1093 +#, fuzzy msgid "Some of the functions in this module are:" -msgstr "" +msgstr "本模組中的一些函式包括:" #: ../../howto/functional.rst:1095 +#, fuzzy msgid "" "Math operations: ``add()``, ``sub()``, ``mul()``, ``floordiv()``, " "``abs()``, ..." msgstr "" +"數學運算:``add()``(加法)、 ``sub()``(減法)、 ``mul()``(乘法)、 " +"``floordiv()``(整除)、「abs()」等等。" #: ../../howto/functional.rst:1096 +#, fuzzy msgid "Logical operations: ``not_()``, ``truth()``." -msgstr "" +msgstr "邏輯運算:``not_()``(非運算)、 ``truth()``(真值判斷)." #: ../../howto/functional.rst:1097 +#, fuzzy msgid "Bitwise operations: ``and_()``, ``or_()``, ``invert()``." -msgstr "" +msgstr "位元運算:``and_()``, ``or_()``, ``invert()``。" #: ../../howto/functional.rst:1098 +#, fuzzy msgid "" "Comparisons: ``eq()``, ``ne()``, ``lt()``, ``le()``, ``gt()``, and ``ge()``." -msgstr "" +msgstr "比較:``eq()``、``ne()``, ``lt()``, ``le()``, ``gt()`` 和 ``ge()``。" #: ../../howto/functional.rst:1099 +#, fuzzy msgid "Object identity: ``is_()``, ``is_not()``." -msgstr "" +msgstr "物件識別:``is_()``,``is_not()``。" #: ../../howto/functional.rst:1101 +#, fuzzy msgid "Consult the operator module's documentation for a complete list." -msgstr "" +msgstr "請參考 operator 模組的說明文件以取得完整清單" #: ../../howto/functional.rst:1105 +#, fuzzy msgid "Small functions and the lambda expression" -msgstr "" +msgstr "小型函式和 lambda 運算式" #: ../../howto/functional.rst:1107 +#, fuzzy msgid "" "When writing functional-style programs, you'll often need little functions " "that act as predicates or that combine elements in some way." msgstr "" +"在編寫函數式程式時,你通常會需要一些小型的函數來作為斷言(predicate)或以某種" +"方式組合元素之用。" #: ../../howto/functional.rst:1110 +#, fuzzy msgid "" "If there's a Python built-in or a module function that's suitable, you don't " "need to define a new function at all::" -msgstr "" +msgstr "如果有適合的 Python 內建函式或模組函式,您完全不需要定義新的函式:" #: ../../howto/functional.rst:1116 +#, fuzzy msgid "" "If the function you need doesn't exist, you need to write it. One way to " "write small functions is to use the :keyword:`lambda` expression. " @@ -1181,20 +1673,26 @@ msgid "" "parameters, and creates an anonymous function that returns the value of the " "expression::" msgstr "" +"如果你需要的函式不存在,就需要自行撰寫。編寫小型函式的其中一種方法是使用" +"「lambda」運算式。「lambda」接受任意數量的引數及結合這些引數的運算,並創建一" +"個匿名函式以回傳運算值:" #: ../../howto/functional.rst:1125 +#, fuzzy msgid "" "An alternative is to just use the ``def`` statement and define a function in " "the usual way::" -msgstr "" +msgstr "另一種方法是只需使用「def」陳述式,以通常的方式定義函式:" #: ../../howto/functional.rst:1134 +#, fuzzy msgid "" "Which alternative is preferable? That's a style question; my usual course " "is to avoid using ``lambda``." -msgstr "" +msgstr "哪種方式比較好呢?這是個風格問題。我通常不使用「lambda」函式庫。" #: ../../howto/functional.rst:1137 +#, fuzzy msgid "" "One reason for my preference is that ``lambda`` is quite limited in the " "functions it can define. The result has to be computable as a single " @@ -1203,108 +1701,145 @@ msgid "" "``lambda`` statement, you'll end up with an overly complicated expression " "that's hard to read. Quick, what's the following code doing? ::" msgstr "" +"我偏好使用lambda的原因之一是它定義函式時所能表達的範圍相對有限。lambda 的結果" +"必須以單一表達式計算,這意味著您無法使用多路 ``if... elif... else`` 比較或" +"``try…except`` 語句。如果您在 ``lambda`` 陳述式中嘗試完成太多工作,最終會得到" +"一個非常複雜且難以閱讀的 expression。你快速看懂以下 code 做些什麼了嗎?:: " +"[(x,y) for x in range(10) if x > 5 for y in range(x)]" #: ../../howto/functional.rst:1147 +#, fuzzy msgid "" "You can figure it out, but it takes time to disentangle the expression to " "figure out what's going on. Using a short nested ``def`` statements makes " "things a little bit better::" msgstr "" +"你可以自己解讀,但需要花點時間拆解這個表達式才能看懂。使用短的巢狀``def``陳述" +"句會使事情稍微好一些::" #: ../../howto/functional.rst:1157 +#, fuzzy msgid "But it would be best of all if I had simply used a ``for`` loop::" -msgstr "" +msgstr "但最好的方式還是我直接使用``for``迴圈:" #: ../../howto/functional.rst:1163 +#, fuzzy msgid "Or the :func:`sum` built-in and a generator expression::" -msgstr "" +msgstr "使用內建的 :func:`sum` 函式與生成器表示式:" #: ../../howto/functional.rst:1167 +#, fuzzy msgid "" "Many uses of :func:`functools.reduce` are clearer when written as ``for`` " "loops." msgstr "" +"很多使用 :func:`functools.reduce` 的情況,如果改以 ``for`` 迴圈來表達會更清" +"楚。" #: ../../howto/functional.rst:1169 +#, fuzzy msgid "" "Fredrik Lundh once suggested the following set of rules for refactoring uses " "of ``lambda``:" -msgstr "" +msgstr "Fredrik Lundh 曾建議以下重構 ``lambda`` 使用時應遵循的一些規則:" #: ../../howto/functional.rst:1172 +#, fuzzy msgid "Write a lambda function." -msgstr "" +msgstr "撰寫一個 lambda 函式。" #: ../../howto/functional.rst:1173 +#, fuzzy msgid "Write a comment explaining what the heck that lambda does." -msgstr "" +msgstr "寫一個註解來解釋這個 lambda 到底在幹嘛。" #: ../../howto/functional.rst:1174 +#, fuzzy msgid "" "Study the comment for a while, and think of a name that captures the essence " "of the comment." -msgstr "" +msgstr "仔細閱讀註解一會兒,思考出一個能夠涵蓋該註解核心的名稱。" #: ../../howto/functional.rst:1176 +#, fuzzy msgid "Convert the lambda to a def statement, using that name." -msgstr "" +msgstr "將 lambda 轉換成 def 陳述式,使用該名稱。" #: ../../howto/functional.rst:1177 +#, fuzzy msgid "Remove the comment." -msgstr "" +msgstr "移除註解。" #: ../../howto/functional.rst:1179 +#, fuzzy msgid "" "I really like these rules, but you're free to disagree about whether this " "lambda-free style is better." -msgstr "" +msgstr "我真的很喜歡這些規則,但如果你贊成不使用 lambda 的風格也可以。" #: ../../howto/functional.rst:1184 +#, fuzzy msgid "Revision History and Acknowledgements" -msgstr "" +msgstr "修訂歷史與致謝" #: ../../howto/functional.rst:1186 +#, fuzzy msgid "" "The author would like to thank the following people for offering " "suggestions, corrections and assistance with various drafts of this article: " "Ian Bicking, Nick Coghlan, Nick Efford, Raymond Hettinger, Jim Jewett, Mike " "Krell, Leandro Lameiro, Jussi Salmela, Collin Winter, Blake Winton." msgstr "" +"作者想感謝以下人士對本文各版本提供意見、修正和協助,包括:Ian Bicking、Nick " +"Coghlan、Nick Efford、Raymond Hettinger、Jim Jewett、Mike Krell、Leandro " +"Lameiro、Jussi Salmela Collin Winter 和 Blake Winton。" #: ../../howto/functional.rst:1191 +#, fuzzy msgid "Version 0.1: posted June 30 2006." -msgstr "" +msgstr "版本0.1:於 2006 年 6 月 30 日張貼。" #: ../../howto/functional.rst:1193 +#, fuzzy msgid "Version 0.11: posted July 1 2006. Typo fixes." -msgstr "" +msgstr "0.11版本:於2006年7月1日發佈。修正打字錯誤。" #: ../../howto/functional.rst:1195 +#, fuzzy msgid "" "Version 0.2: posted July 10 2006. Merged genexp and listcomp sections into " "one. Typo fixes." msgstr "" +"版本 0.2:於 2006 年 7 月 10 日張貼。將生成器表達式和列表解析的部分合併為一個" +"部分。修正了錯誤拼字。" #: ../../howto/functional.rst:1198 +#, fuzzy msgid "" "Version 0.21: Added more references suggested on the tutor mailing list." -msgstr "" +msgstr "版本0.21:新增了郵件列表tutor建議的更多參考資料。" #: ../../howto/functional.rst:1200 +#, fuzzy msgid "" "Version 0.30: Adds a section on the ``functional`` module written by Collin " "Winter; adds short section on the operator module; a few other edits." msgstr "" +"版本 0.30:新增了一個由 Collin Winter 編寫的「函數」模組部分;加入了有關" +"「operator」模組的簡短介紹;做了幾處其他修訂。" #: ../../howto/functional.rst:1205 +#, fuzzy msgid "References" -msgstr "" +msgstr "參考文獻" #: ../../howto/functional.rst:1208 +#, fuzzy msgid "General" -msgstr "" +msgstr "總則" #: ../../howto/functional.rst:1210 +#, fuzzy msgid "" "**Structure and Interpretation of Computer Programs**, by Harold Abelson and " "Gerald Jay Sussman with Julie Sussman. The book can be found at https://" @@ -1314,19 +1849,30 @@ msgid "" "of the design approaches described in these chapters are applicable to " "functional-style Python code." msgstr "" +"《計算機程序的結構和解釋》(Structure and Interpretation of Computer " +"Programs)是 Harold Abelson 和 Gerald Jay Sussman 與 Julie Sussman 的經典電腦" +"科學教材。該書第 2 章和第 3 章探討了使用序列和流在程式中組織資料流的方法。該" +"書範例以 Scheme 程式語言呈現,但這些章節描述的設計方法對於函數式風格 Python " +"的程式碼也很有適用性。完整文本可以在 https://mitpress.mit.edu/sicp/ 找到。" #: ../../howto/functional.rst:1218 +#, fuzzy msgid "" "https://www.defmacro.org/ramblings/fp.html: A general introduction to " "functional programming that uses Java examples and has a lengthy historical " "introduction." msgstr "" +"這是一篇介紹函數式程式設計的文章,以 Java 程式碼作為示範,並附上相當冗長的歷" +"史背景介紹。原文連結為 https://www.defmacro.org/ramblings/fp.html" #: ../../howto/functional.rst:1221 +#, fuzzy msgid "" "https://en.wikipedia.org/wiki/Functional_programming: General Wikipedia " "entry describing functional programming." msgstr "" +"https://zh.wikipedia.org/wiki/%E5%87%BD%E6%95%B8%E7%A8%8B%E5%BC%8F這是維基百" +"科上介紹函數程式設計的條目。" #: ../../howto/functional.rst:1224 msgid "https://en.wikipedia.org/wiki/Coroutine: Entry for coroutines." @@ -1347,23 +1893,31 @@ msgstr "https://en.wikipedia.org/wiki/Currying: currying 概念的條目。" #: ../../howto/functional.rst:1231 msgid "Python-specific" -msgstr "" +msgstr "Python特有" #: ../../howto/functional.rst:1233 +#, fuzzy msgid "" "https://gnosis.cx/TPiP/: The first chapter of David Mertz's book :title-" "reference:`Text Processing in Python` discusses functional programming for " "text processing, in the section titled \"Utilizing Higher-Order Functions in " "Text Processing\"." msgstr "" +"https://gnosis.cx/TPiP/: David Mertz 的書 :title-reference:`Python 文字處理` " +"第一章,討論利用高階函式在文字處理上的功能編程。" #: ../../howto/functional.rst:1238 +#, fuzzy msgid "" "Mertz also wrote a 3-part series of articles on functional programming for " "IBM's DeveloperWorks site; see `part 1 `__, `part 2 `__, and " "`part 3 `__," msgstr "" +"Mertz 在 IBM 的 DeveloperWorks 站台上撰寫了一系列三篇有關函數程式設計的文章;" +"請查閱 `第一部分 `__、`第二部分 " +"`__ 和 `第三部分 `__。" #: ../../howto/functional.rst:1246 msgid "Python documentation" @@ -1390,3 +1944,5 @@ msgid "" ":pep:`342`: \"Coroutines via Enhanced Generators\" describes the new " "generator features in Python 2.5." msgstr "" +"「PEP 342:利用強化型產生器達成協同程式設計」描述的是 Python 2.5 中新增的生成" +"器功能。" diff --git a/howto/logging.po b/howto/logging.po index a8a3738ef3..fb7f525607 100644 --- a/howto/logging.po +++ b/howto/logging.po @@ -8,16 +8,17 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-02-02 00:03+0000\n" -"PO-Revision-Date: 2018-05-23 14:36+0000\n" -"Last-Translator: Adrian Liaw \n" -"Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" -"tw)\n" +"POT-Creation-Date: 2024-02-24 16:01+0800\n" +"PO-Revision-Date: 2023-03-26 18:25+0000\n" +"Last-Translator: CTHua \n" +"Language-Team: logging (generated) \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: Weblate 4.16.4\n" #: ../../howto/logging.rst:3 msgid "Logging HOWTO" @@ -67,19 +68,22 @@ msgid "" "func:`critical`. To determine when to use logging, see the table below, " "which states, for each of a set of common tasks, the best tool to use for it." msgstr "" +"日誌(Logging)提供方便使用的一組簡單紀錄函式。這些函式包括::func:`debug`、:" +"func:`info`、:func:`warning`、:func:`error` 和 :func:`critical`。如需確定何時" +"使用紀錄,請參見以下表格,其中列出每個常見工作的最佳工具。" #: ../../howto/logging.rst:34 msgid "Task you want to perform" -msgstr "" +msgstr "你想要執行的任務" #: ../../howto/logging.rst:34 msgid "The best tool for the task" -msgstr "" +msgstr "此任務最好的工具" #: ../../howto/logging.rst:36 msgid "" "Display console output for ordinary usage of a command line script or program" -msgstr "" +msgstr "對於命令行或程式的普通用法,結果會顯示在控制台" #: ../../howto/logging.rst:36 msgid ":func:`print`" @@ -89,49 +93,57 @@ msgstr ":func:`print`" msgid "" "Report events that occur during normal operation of a program (e.g. for " "status monitoring or fault investigation)" -msgstr "" +msgstr "報告在程式正常操作期間發生的事件(例如用於狀態監視或故障調查)" #: ../../howto/logging.rst:40 msgid "" ":func:`logging.info` (or :func:`logging.debug` for very detailed output for " "diagnostic purposes)" msgstr "" +":func:`logging.info` (或者在需要診斷用的詳細輸出時使用 :func:`logging." +"debug` )" #: ../../howto/logging.rst:45 msgid "Issue a warning regarding a particular runtime event" -msgstr "" +msgstr "針對特定的運行時事件發出警告" #: ../../howto/logging.rst:45 msgid "" ":func:`warnings.warn` in library code if the issue is avoidable and the " "client application should be modified to eliminate the warning" msgstr "" +"如果問題是可以避免的,那麼客戶端應該修改以消除警告,請在庫代碼中使用 :func:" +"`warnings.warn` 警告" #: ../../howto/logging.rst:50 msgid "" ":func:`logging.warning` if there is nothing the client application can do " "about the situation, but the event should still be noted" msgstr "" +"如果客戶端應用程式無法處理該情況,但該事件仍應注意,可以使用 :func:`logging." +"warning`" #: ../../howto/logging.rst:55 msgid "Report an error regarding a particular runtime event" -msgstr "" +msgstr "回報與特定執行階段事件相關的錯誤" #: ../../howto/logging.rst:55 msgid "Raise an exception" -msgstr "" +msgstr "引發例外" #: ../../howto/logging.rst:58 msgid "" "Report suppression of an error without raising an exception (e.g. error " "handler in a long-running server process)" -msgstr "" +msgstr "報告錯誤但不引發異常(如長時間執行的伺服器進程中的錯誤處理程序)" #: ../../howto/logging.rst:58 msgid "" ":func:`logging.error`, :func:`logging.exception` or :func:`logging.critical` " "as appropriate for the specific error and application domain" msgstr "" +"根據錯誤及應用領域的特定需要,使用 :func:`logging.error`、:func:`logging." +"exception` 或 :func:`logging.critical`" #: ../../howto/logging.rst:65 msgid "" @@ -139,14 +151,16 @@ msgid "" "they are used to track. The standard levels and their applicability are " "described below (in increasing order of severity):" msgstr "" +"日誌紀錄函數的命名方式是根據它們所用於追蹤的事件等級或嚴重程度。以下是標準等" +"級及其可應用的範圍說明(按嚴重程度遞增排序):" #: ../../howto/logging.rst:72 ../../howto/logging.rst:866 msgid "Level" -msgstr "" +msgstr "等級" #: ../../howto/logging.rst:72 msgid "When it's used" -msgstr "" +msgstr "何時使用" #: ../../howto/logging.rst:74 ../../howto/logging.rst:876 msgid "``DEBUG``" @@ -155,7 +169,7 @@ msgstr "``DEBUG``" #: ../../howto/logging.rst:74 msgid "" "Detailed information, typically of interest only when diagnosing problems." -msgstr "" +msgstr "詳細的資訊,僅在診斷問題時才會有用。" #: ../../howto/logging.rst:77 ../../howto/logging.rst:874 msgid "``INFO``" @@ -163,7 +177,7 @@ msgstr "``INFO``" #: ../../howto/logging.rst:77 msgid "Confirmation that things are working as expected." -msgstr "" +msgstr "確認一切都按預期運作。" #: ../../howto/logging.rst:80 ../../howto/logging.rst:872 msgid "``WARNING``" @@ -175,6 +189,8 @@ msgid "" "problem in the near future (e.g. 'disk space low'). The software is still " "working as expected." msgstr "" +"一種意外發生的徵兆,或是暗示著未來可能出現問題(例如:「磁碟空間不足」),程" +"式還在按照預期運作。" #: ../../howto/logging.rst:85 ../../howto/logging.rst:870 msgid "``ERROR``" @@ -184,7 +200,7 @@ msgstr "``ERROR``" msgid "" "Due to a more serious problem, the software has not been able to perform " "some function." -msgstr "" +msgstr "因為有更嚴重的問題,軟體無法執行某些功能。" #: ../../howto/logging.rst:88 ../../howto/logging.rst:868 msgid "``CRITICAL``" @@ -194,7 +210,7 @@ msgstr "``CRITICAL``" msgid "" "A serious error, indicating that the program itself may be unable to " "continue running." -msgstr "" +msgstr "嚴重錯誤,表示此程式本身可能無法繼續執行。" #: ../../howto/logging.rst:92 msgid "" @@ -202,6 +218,8 @@ msgid "" "and above will be tracked, unless the logging package is configured to do " "otherwise." msgstr "" +"預設的級別是「警告」(``WARNING``),這意味著除非配置記錄器套件進行其他設定,否" +"則只會追蹤這個級別以上的事件。" #: ../../howto/logging.rst:96 msgid "" @@ -209,6 +227,8 @@ msgid "" "of handling tracked events is to print them to the console. Another common " "way is to write them to a disk file." msgstr "" +"被追蹤的事件可以採取不同的處理方式。處理追蹤事件最簡單的方法是將它們輸出到控" +"制台。另一個常見的方法是將它們寫入磁盤文件。" #: ../../howto/logging.rst:104 msgid "A simple example" @@ -216,14 +236,11 @@ msgstr "一個簡單範例" #: ../../howto/logging.rst:106 msgid "A very simple example is::" -msgstr "" -"一個非常簡單的例子是:\n" -"\n" -"::" +msgstr "在這裡舉一個簡單的例子:::" #: ../../howto/logging.rst:112 msgid "If you type these lines into a script and run it, you'll see:" -msgstr "" +msgstr "若您將這幾行程式碼加入腳本中並且運行,你就能看到:::" #: ../../howto/logging.rst:118 msgid "" @@ -234,10 +251,14 @@ msgid "" "explained later. The actual output can be formatted quite flexibly if you " "need that; formatting options will also be explained later." msgstr "" +"這段訊息顯示在控制台上。由於預設級別為 ``WARNING``,所以 ``INFO`` 訊息並未出" +"現。印出的訊息包含級別以及在 logging 呼叫中提供的事件說明,例如 'Watch " +"out!'。目前不必擔心 'root' 部分,稍後將會說明。如果您需要的話,可以彈性地格式" +"化實際輸出;相關選項也將在稍後說明。" #: ../../howto/logging.rst:127 msgid "Logging to a file" -msgstr "" +msgstr "將日誌寫入檔案" #: ../../howto/logging.rst:129 msgid "" @@ -246,6 +267,10 @@ msgid "" "Python interpreter, and don't just continue from the session described " "above::" msgstr "" +"一個非常普遍的情況是將日誌事件記錄在文件中,因此讓我們接著看看這個情況。請務" +"必在新啟動的 Python 直譯器中嘗試以下操作,而不是繼續以上描述的會話:\n" +"\n" +"::" #: ../../howto/logging.rst:140 msgid "" @@ -255,12 +280,15 @@ msgid "" "passed, which determines how encoding errors are handled. For available " "values and the default, see the documentation for :func:`open`." msgstr "" +"新增了 *encoding* 引數。在早期的 Python 版本中,如果未指定,將使用 :func:" +"`open` 的預設編碼。儘管在上面的範例中沒有顯示,現在也可以傳遞 *errors* 引數," +"以決定如何處理編碼錯誤。可用的值與其預設值,請參閱 :func:`open` 的文件。" #: ../../howto/logging.rst:147 msgid "" "And now if we open the file and look at what we have, we should find the log " "messages:" -msgstr "" +msgstr "現在,如果我們打開文件並查看其內容,我們應該能夠找到日誌訊息:" #: ../../howto/logging.rst:157 msgid "" @@ -268,17 +296,21 @@ msgid "" "threshold for tracking. In this case, because we set the threshold to " "``DEBUG``, all of the messages were printed." msgstr "" +"這個範例同時也展示了如何設定日誌紀錄等級作為追蹤的門檻。在這個例子中,因為門" +"檻被設為 ``DEBUG``,所以所有的訊息都會被印出來。" #: ../../howto/logging.rst:161 msgid "" "If you want to set the logging level from a command-line option such as:" -msgstr "" +msgstr "如果您想要從命令列選項設置日誌級別,例如:" #: ../../howto/logging.rst:167 msgid "" "and you have the value of the parameter passed for ``--log`` in some " "variable *loglevel*, you can use::" msgstr "" +"如果你已經將 ``--log`` 引數的值存儲在某個變數 *loglevel* 中,你可以使用以下方" +"式:" #: ../../howto/logging.rst:172 msgid "" @@ -286,6 +318,10 @@ msgid "" "argument. You may want to error check any user input value, perhaps as in " "the following example::" msgstr "" +"要取得值以傳遞給 :func:`basicConfig` 的 *level* 引數,你可能會想檢查任何使用" +"者輸入的值,如下面的示例所示:\n" +"\n" +"::" #: ../../howto/logging.rst:184 msgid "" @@ -295,6 +331,10 @@ msgid "" "off simple configuration facility, only the first call will actually do " "anything: subsequent calls are effectively no-ops." msgstr "" +":func:`basicConfig` 的呼叫應該放在任何關於 :func:`debug` 、 :func:`info` 等的" +"呼叫 *之前* ,否則這些功能就會以預設選項自動呼叫 :func:`basicConfig` 。由於它" +"旨在作為一次性的簡單配置工具,只有第一個呼叫才能真正執行:後續的呼叫基本上是" +"無操作(no-ops)。" #: ../../howto/logging.rst:190 msgid "" @@ -303,26 +343,30 @@ msgid "" "afresh, not remembering the messages from earlier runs, you can specify the " "*filemode* argument, by changing the call in the above example to::" msgstr "" +"如果你多次運行上面的腳本,就會將每次成功執行時的訊息添加到名為 *example.log* " +"檔案中。如果你希望每次都從頭開始,不記住以前運行時的訊息,則可以指定 " +"*filemode* 引數,在上面範例中更改呼叫方式如下所示:" #: ../../howto/logging.rst:197 msgid "" "The output will be the same as before, but the log file is no longer " "appended to, so the messages from earlier runs are lost." msgstr "" +"輸出會和之前一樣,但不再新增在舊的紀錄檔內,因此之前執行的訊息都遺失了。" #: ../../howto/logging.rst:202 msgid "Logging from multiple modules" -msgstr "" +msgstr "紀錄多個模組" #: ../../howto/logging.rst:204 msgid "" "If your program consists of multiple modules, here's an example of how you " "could organize logging in it::" -msgstr "" +msgstr "如果你的程式包含多個模組,以下是一個有關如何組織日誌紀錄的範例:::" #: ../../howto/logging.rst:228 msgid "If you run *myapp.py*, you should see this in *myapp.log*:" -msgstr "" +msgstr "當你執行 *myapp.py* ,你會看到以下訊息在 *myapp.log* :::" #: ../../howto/logging.rst:236 msgid "" @@ -334,20 +378,26 @@ msgid "" "to refer to the documentation beyond the tutorial level -- see :ref:`logging-" "advanced-tutorial`." msgstr "" +"是你應該看到的。你可以將此應用到多個模組,使用 *mylib.py* 中的模式。請注意," +"在這個簡單的使用模式下,除非查看事件描述,不然你無法透過查看日誌檔案得知訊息" +"來自於應用程式的哪個位置。若你要追蹤訊息的位置,請參考進階教學的文件--參閱 :" +"ref:`logging-advanced-tutorial`。" #: ../../howto/logging.rst:246 msgid "Logging variable data" -msgstr "" +msgstr "紀錄變數數據" #: ../../howto/logging.rst:248 msgid "" "To log variable data, use a format string for the event description message " "and append the variable data as arguments. For example::" msgstr "" +"為了紀錄變數資料,請使用格式字串作為事件描述訊息,並將變數資料逐一附加為引" +"數。例如:::" #: ../../howto/logging.rst:254 msgid "will display:" -msgstr "" +msgstr "會顯示:" #: ../../howto/logging.rst:260 msgid "" @@ -358,20 +408,25 @@ msgid "" "options *are* supported, but exploring them is outside the scope of this " "tutorial: see :ref:`formatting-styles` for more information." msgstr "" +"正如您所看到的,將變量數據合併到事件描述消息中使用的是舊的 `%` 樣式的字符串格" +"式化方式。這是出於向後兼容,logging 包的發布日期先於新的格式化選項,如 :meth:" +"`str.format` 方法和 :class:`string.Template` 類別。雖然支援這些新的格式化選" +"項,但是探索這些選項超出了本教程的範圍:請參閱 :ref:`formatting-styles` 獲取" +"更多信息。" #: ../../howto/logging.rst:269 msgid "Changing the format of displayed messages" -msgstr "" +msgstr "變更顯示訊息的格式" #: ../../howto/logging.rst:271 msgid "" "To change the format which is used to display messages, you need to specify " "the format you want to use::" -msgstr "" +msgstr "如果你要變更顯示訊息使用的格式,需要指定你想使用的格式,如下:" #: ../../howto/logging.rst:280 msgid "which would print:" -msgstr "" +msgstr "將會印出:" #: ../../howto/logging.rst:288 msgid "" @@ -382,20 +437,30 @@ msgid "" "including variable data) and perhaps to display when the event occurred. " "This is described in the next section." msgstr "" +"需要注意的是,前面範例中出現的'root'已經消失。對於可以出現在格式字串中的完整" +"內容,您可以參閱 :ref:`logrecord-attributes` 的文件,簡單的使用方法是,只使" +"用 *levelname* (嚴重性)、*message* (事件描述,包括變數的值)和可能的顯示事" +"件發生時間。下一個段落會解釋這個。" #: ../../howto/logging.rst:297 msgid "Displaying the date/time in messages" -msgstr "" +msgstr "在訊息中顯示日期/時間" #: ../../howto/logging.rst:299 msgid "" "To display the date and time of an event, you would place '%(asctime)s' in " "your format string::" msgstr "" +"如果想要顯示事件的日期和時間,你可以在你的格式化字串中放置 '%(asctime)s':\n" +"\n" +"::" #: ../../howto/logging.rst:306 msgid "which should print something like this:" msgstr "" +"會顯示以下訊息:\n" +"\n" +"::" #: ../../howto/logging.rst:312 msgid "" @@ -403,20 +468,27 @@ msgid "" "rfc:`3339`. If you need more control over the formatting of the date/time, " "provide a *datefmt* argument to ``basicConfig``, as in this example::" msgstr "" +"預設的時間/日期格式遵照 ISO8601 或 :rfc:`3339` 。如果你想要有更多的時間/日期" +"自訂性,可以在 ``basicConfig`` 傳入 *datefmt* 的引數,如以下範例:\n" +"\n" +"::" #: ../../howto/logging.rst:320 msgid "which would display something like this:" msgstr "" +"會顯示以下訊息:\n" +"\n" +"::" #: ../../howto/logging.rst:326 msgid "" "The format of the *datefmt* argument is the same as supported by :func:`time." "strftime`." -msgstr "" +msgstr "*datefmt* 引數的格式與 :func:`time.strftime` 支援的格式相同。" #: ../../howto/logging.rst:331 msgid "Next Steps" -msgstr "" +msgstr "下一步" #: ../../howto/logging.rst:333 msgid "" @@ -426,6 +498,9 @@ msgid "" "time in reading the following sections. If you're ready for that, grab some " "of your favourite beverage and carry on." msgstr "" +"這個基礎教學就到這裡了。它應該足以讓您開始使用記錄功能。日誌套件還提供了更多" +"功能,但如果想要充分利用它,您需要再花些時間閱讀以下部分。如果您準備好了,就" +"拿起您喜愛的飲料繼續閱讀吧。" #: ../../howto/logging.rst:339 msgid "" @@ -435,6 +510,10 @@ msgid "" "group (available at https://groups.google.com/g/comp.lang.python) and you " "should receive help before too long." msgstr "" +"如果你需要一個簡單的日誌記錄的功能,可以參考上面的例子將它添加到你自己的腳本" +"中。如果你遇到問題或不理解某些內容,可以在comp.lang.python Usenet 群組(網" +"址:https://groups.google.com/forum/#!forum/comp.lang.python)上發問,你會很" +"快得到幫助。" #: ../../howto/logging.rst:345 msgid "" @@ -442,42 +521,50 @@ msgid "" "slightly more advanced/in-depth tutorial than the basic one above. After " "that, you can take a look at the :ref:`logging-cookbook`." msgstr "" +"還在嗎?你可以繼續閱讀下面幾節,這比上面的基礎教學稍微進階/深入一些。之後,你" +"可以看看 :ref:`logging-cookbook` 。" #: ../../howto/logging.rst:353 msgid "Advanced Logging Tutorial" -msgstr "" +msgstr "進階日誌記錄教學" #: ../../howto/logging.rst:355 msgid "" "The logging library takes a modular approach and offers several categories " "of components: loggers, handlers, filters, and formatters." msgstr "" +"日誌(logging)庫採用模組化(modular)設計,提供多個類別組件,包括記錄器" +"(loggers)、處理器(handlers)、過濾器(filters)和格式化器(formatters)。" #: ../../howto/logging.rst:358 msgid "Loggers expose the interface that application code directly uses." -msgstr "" +msgstr "記錄器(logger)公開應用程式可以直接使用的介面。" #: ../../howto/logging.rst:359 msgid "" "Handlers send the log records (created by loggers) to the appropriate " "destination." msgstr "" +"處理器 (handlers)將由記錄器 (loggers)建立的日誌紀錄 (log records) 傳送" +"到適當的目的地。" #: ../../howto/logging.rst:361 msgid "" "Filters provide a finer grained facility for determining which log records " "to output." -msgstr "" +msgstr "過濾器 (filters) 提供更精細的機制來決定輸出哪些日誌記錄。" #: ../../howto/logging.rst:363 msgid "Formatters specify the layout of log records in the final output." -msgstr "" +msgstr "格式化(formatters)器指定最終輸出中日誌記錄的佈局。" #: ../../howto/logging.rst:365 msgid "" "Log event information is passed between loggers, handlers, filters and " "formatters in a :class:`LogRecord` instance." msgstr "" +"日誌事件(log event)資訊在記錄器、處理器、 過濾器和格式化器之間通過 :class:" +"`LogRecord` 實例傳遞。" #: ../../howto/logging.rst:368 msgid "" @@ -489,18 +576,28 @@ msgid "" "want, and indicate the area of an application in which a logged message " "originates." msgstr "" +"紀錄是透過呼叫 :class:`Logger` 類別的實例方法(以下稱為 :dfn:`loggers` )來執" +"行。每個實例都有一個名稱,並藉由用點(句號)作為分隔符號的命名空間層次來進行" +"概念化排列。例如,名為「scan」的 logger 父級是記錄器(logger)「scan.text」、" +"「scan.html」及「scan.pdf」。記錄器(logger)名稱可以取任何你想要的,並且表示" +"一個應用程式中產生紀錄訊息的區域。" #: ../../howto/logging.rst:375 msgid "" "A good convention to use when naming loggers is to use a module-level " "logger, in each module which uses logging, named as follows::" msgstr "" +"命名記錄器的一個良好慣例是在每個使用記錄的模組中,使用模組級別的記錄器,命名" +"如下:\n" +"\n" +"::" #: ../../howto/logging.rst:380 msgid "" "This means that logger names track the package/module hierarchy, and it's " "intuitively obvious where events are logged just from the logger name." msgstr "" +"這意味著日誌名稱追踪套件/模組階層,因此從日誌名稱可以直觀明瞭事件的記錄位置。" #: ../../howto/logging.rst:383 msgid "" @@ -510,6 +607,10 @@ msgid "" "the root logger. The functions and the methods have the same signatures. The " "root logger's name is printed as 'root' in the logged output." msgstr "" +"記錄器等級中階層的根稱為根記錄器。這是由函數 :func:`debug`、:func:`info`、:" +"func:`warning`、:func:`error` 和 :func:`critical` 使用的記錄器,這些函數僅調" +"用根記錄器的同名方法。這些函數和方法具有相同的簽章(signature)。根記錄器的名" +"稱在記錄輸出中顯示為 'root'。" #: ../../howto/logging.rst:389 msgid "" @@ -521,6 +622,11 @@ msgid "" "destination class if you have special requirements not met by any of the " "built-in handler classes." msgstr "" +"當然,以不同的目的地記錄訊息是可能的。該套件支援將日誌訊息寫入檔案、HTTP GET/" +"POST 位置、透過 SMTP 的電子郵件、通用的 sockets、隊列、或像 syslog 或 " +"Windows NT 事件日誌等作業系統特定的日誌機制。這些目的地由 :dfn:`handler` 類別" +"提供服務。如果您有特殊需求,而內建的 handler 類別無法滿足,則可以創建自己的日" +"誌目的地類別。" #: ../../howto/logging.rst:396 msgid "" @@ -533,10 +639,15 @@ msgid "" "displayed message before delegating to the root logger to do the actual " "message output." msgstr "" +"預設情況下,對於所有的日誌訊息都沒有設定目的地。您可以使用教程範例中的 :func:" +"`basicConfig` 指定目的地(例如控制台或檔案)。如果您呼叫 :func:`debug`、:" +"func:`info`、:func:`warning`、:func:`error` 和 :func:`critical` 函數,它們會" +"檢查是否設定目的地,如果沒有,它們會設置目的地為控制台(``sys.stderr``)以及" +"顯示信息的默認格式,然後委派給根記錄器來進行實際的訊息輸出。" #: ../../howto/logging.rst:404 msgid "The default format set by :func:`basicConfig` for messages is:" -msgstr "" +msgstr ":func:`basicConfig` 預設設定的訊息格式為:" #: ../../howto/logging.rst:410 msgid "" @@ -544,20 +655,22 @@ msgid "" "the *format* keyword argument. For all options regarding how a format string " "is constructed, see :ref:`formatter-objects`." msgstr "" +"您可以透過傳遞格式字符串到 :func:`basicConfig` 的 *format* 關鍵字引數來進行更" +"改。有關格式字符串構建的所有選項,請參閱 :ref:`formatter-objects` 。" #: ../../howto/logging.rst:415 msgid "Logging Flow" -msgstr "" +msgstr "記錄流程" #: ../../howto/logging.rst:417 msgid "" "The flow of log event information in loggers and handlers is illustrated in " "the following diagram." -msgstr "" +msgstr "下面的圖表說明了記錄器(logger)和處理器(handler)中事件資訊的流動。" #: ../../howto/logging.rst:424 msgid "Loggers" -msgstr "" +msgstr "記錄器(Loggers)" #: ../../howto/logging.rst:426 msgid "" @@ -568,16 +681,20 @@ msgid "" "Third, logger objects pass along relevant log messages to all interested log " "handlers." msgstr "" +":class:`Logger` (紀錄器)物件有三個工作。首先,它們向應用程式代碼公開多個方" +"法,讓應用程式可以在運行時記錄訊息。其次,紀錄器物件基於嚴重性(預設過濾方" +"式)或過濾器物件來決定要處理哪些日誌訊息。第三,紀錄器物件將相關的日誌訊息傳" +"遞給所有感興趣的日誌處理程序。" #: ../../howto/logging.rst:432 msgid "" "The most widely used methods on logger objects fall into two categories: " "configuration and message sending." -msgstr "" +msgstr "記錄器物件上最常使用的方法可分為兩類:配置和傳送訊息。" #: ../../howto/logging.rst:435 msgid "These are the most common configuration methods:" -msgstr "" +msgstr "以下是最常見的設定方法:" #: ../../howto/logging.rst:437 msgid "" @@ -587,6 +704,10 @@ msgid "" "INFO, the logger will handle only INFO, WARNING, ERROR, and CRITICAL " "messages and will ignore DEBUG messages." msgstr "" +":meth:`Logger.setLevel` 指定了一個 logger 所處理的最低嚴重性信息,其中 debug " +"是最低內建的嚴重性等級,而 critical 是最高內建的嚴重性等級。例如,如果嚴重性" +"等級是 INFO,logger 只處理 INFO、WARNING、ERROR 和 CRITICAL 訊息,而會忽略 " +"DEBUG 訊息。" #: ../../howto/logging.rst:443 msgid "" @@ -594,6 +715,9 @@ msgid "" "handler objects from the logger object. Handlers are covered in more detail " "in :ref:`handler-basic`." msgstr "" +":meth:`Logger.addHandler` 和 :meth:`Logger.removeHandler` 可以新增或移除處理" +"器物件到當前的記錄器物件(logger object)。有關處理器的詳細說明,請參閱 :ref:" +"`handler-basic` 。" #: ../../howto/logging.rst:447 msgid "" @@ -601,17 +725,19 @@ msgid "" "filter objects from the logger object. Filters are covered in more detail " "in :ref:`filter`." msgstr "" +":meth:`Logger.addFilter` 和 :meth:`Logger.removeFilter` 分別是在記錄器對象中" +"新增和移除過濾器物件。更多關於過濾器的細節可以參閱 :ref:`filter`。" #: ../../howto/logging.rst:451 msgid "" "You don't need to always call these methods on every logger you create. See " "the last two paragraphs in this section." -msgstr "" +msgstr "你並不需要在每個你建立的記錄器上都呼叫這些方法,請參考此節的最後兩段。" #: ../../howto/logging.rst:454 msgid "" "With the logger object configured, the following methods create log messages:" -msgstr "" +msgstr "有了已配置的記錄器物件之後,下列這些方法可以產生日誌訊息:" #: ../../howto/logging.rst:456 msgid "" @@ -625,6 +751,12 @@ msgid "" "about a keyword of ``exc_info`` and use it to determine whether to log " "exception information." msgstr "" +":meth:`Logger.debug` 、 :meth:`Logger.info` 、 :meth:`Logger.warning` 、 :" +"meth:`Logger.error` 和 :meth:`Logger.critical` 建立一個包含訊息和相應名稱的日" +"誌紀錄。 訊息實際上是格式化字串,可以包含 ``%s`` 、 ``%d`` 、 ``%f`` 等的標準" +"字符串替換語法。 其餘引數都是對應於訊息中的替換欄位的物件列表。 對於 " +"``**kwargs`` ,日誌記錄函數只關心參數中是否有 ``exc_info`` 的關鍵字,並使用它" +"來確定是否記錄異常訊息。" #: ../../howto/logging.rst:466 msgid "" @@ -632,6 +764,9 @@ msgid "" "error`. The difference is that :meth:`Logger.exception` dumps a stack trace " "along with it. Call this method only from an exception handler." msgstr "" +":meth:`Logger.exception` 這個方法建立的日誌訊息與 :meth:`Logger.error` 相近。" +"差別在於 :meth:`Logger.exception` 會附上一個程式執行時的堆疊(traceback)。請只" +"將此方法用於例外處理(exception handlers)。" #: ../../howto/logging.rst:470 msgid "" @@ -640,6 +775,8 @@ msgid "" "convenience methods listed above, but this is how to log at custom log " "levels." msgstr "" +":meth:`Logger.log` 需要額外的日誌級別當作明確引數。使用這種方式記錄日誌訊息會" +"比上述方法冗長一些,不過這是使用自定義日誌級別時如何記錄日誌的方式。" #: ../../howto/logging.rst:474 msgid "" @@ -652,6 +789,12 @@ msgid "" "loggers with names of ``foo.bar``, ``foo.bar.baz``, and ``foo.bam`` are all " "descendants of ``foo``." msgstr "" +":func:`getLogger` 會回傳一個指定名稱的日誌物件實例的參照,如果沒有指定名稱," +"則使用 ``root`` 當作其名稱。這些名稱是由點號分隔的層次結構。使用相同名字多次" +"呼叫 :func:`getLogger` 將返回對相同日誌物件的參考。在層級 list(串列)中層級" +"更低的日誌物件是高級別日誌物件的子物件。例如,對於名稱為 ``foo`` 的日誌物件," +"名稱為 ``foo.bar``、``foo.bar.baz`` 和 ``foo.bam`` 的日誌物件都只是 ``foo`` " +"的子物件。" #: ../../howto/logging.rst:482 msgid "" @@ -664,6 +807,11 @@ msgid "" "the logger is used to determine whether the event is passed to the logger's " "handlers." msgstr "" +"記錄器有一個 *有效級別* 的概念。如果沒有在記錄器上明確設置級別,則使用其父級" +"別作為其有效級別。如果父級沒有設置明確的級別,則檢查 *它* 的父級別,依此類推 " +"- 搜索所有祖先(ancestors),直到找到明確設置的級別。根記錄器始終設置了明確的" +"級別(默認為 ``WARNING`` )。在決定是否處理事件時,記錄器的有效級別用於確定事" +"件是否傳遞給記錄器的處理器。" #: ../../howto/logging.rst:490 msgid "" @@ -674,10 +822,14 @@ msgid "" "needed. (You can, however, turn off propagation by setting the *propagate* " "attribute of a logger to ``False``.)" msgstr "" +"子記錄器會將訊息在其祖先(ancestor)記錄器所關聯的處理程式傳播,因此並不需要" +"為應用程式使用的所有記錄器定義和配置處理器。因此只需要為最高級別的記錄器配置" +"處理器,然後根據需要創建子記錄器即可。(您可以透過將記事本的 *propagate* 屬性" +"設置為 ``False`` 來關閉傳播)" #: ../../howto/logging.rst:501 msgid "Handlers" -msgstr "" +msgstr "處理器" #: ../../howto/logging.rst:503 msgid "" @@ -691,6 +843,12 @@ msgid "" "individual handlers where each handler is responsible for sending messages " "of a specific severity to a specific location." msgstr "" +":class:`~logging.Handler` 物件負責將適當的日誌訊息分派到處理器指定的目的地" +"(根據日誌訊息的嚴重性)。 :class:`Logger` (日誌物件)可以使用 :meth:" +"`~Logger.addHandler` 方法向自己添加零個或多個處理器物件。作為一個範例場景,一" +"個應用程式可能想要將所有日誌訊息發送到一個日誌檔案、將所有錯誤或更高級別的日" +"誌訊息發送到 stdout、並將所有嚴重的日誌訊息發送到一個電子郵件地址。這種場景需" +"要三個獨立的處理器,其中每個處理器負責將特定嚴重性的訊息發送到特定位置。" #: ../../howto/logging.rst:513 msgid "" @@ -698,6 +856,8 @@ msgid "" "handlers`); the tutorials use mainly :class:`StreamHandler` and :class:" "`FileHandler` in its examples." msgstr "" +"標準函式庫中包含了相當多的處理器型別(請見 :ref:`useful-handlers`),本教學範" +"例主要會使用 :class:`StreamHandler` 與 :class:`FileHandler`。" #: ../../howto/logging.rst:517 msgid "" @@ -706,6 +866,8 @@ msgid "" "application developers who are using the built-in handler objects (that is, " "not creating custom handlers) are the following configuration methods:" msgstr "" +"對於應用程式開發人員而言,與處理器相關的方法非常少。當使用內置的處理程序物件 " +"(即未創建自定義處理程序) 時,似乎只有以下設置方法對應用程式開發人員有關:" #: ../../howto/logging.rst:522 msgid "" @@ -716,18 +878,25 @@ msgid "" "The level set in each handler determines which messages that handler will " "send on." msgstr "" +":meth:`~Handler.setLevel` 方法在日誌物件中同樣存在,它指定了將會被傳遞至目的" +"地的最低記錄等級。爲什麼有兩個 :func:`setLevel` 方法呢?記錄器中設置的等級決" +"定哪些消息程度會通過該日誌物件傳遞到其處理程序。每個處理器中設置的日誌等級則" +"決定該處理器需要發送哪些訊息。" #: ../../howto/logging.rst:528 msgid "" ":meth:`~Handler.setFormatter` selects a Formatter object for this handler to " "use." msgstr "" +":meth:`~Handler.setFormatter` 方法可為此處理器選擇一個格式化器物件使用。" #: ../../howto/logging.rst:531 msgid "" ":meth:`~Handler.addFilter` and :meth:`~Handler.removeFilter` respectively " "configure and deconfigure filter objects on handlers." msgstr "" +":meth:`~Handler.addFilter` 方法和 :meth:`~Handler.removeFilter` 方法可以分別" +"在處理器中設定或移除過濾物件。" #: ../../howto/logging.rst:534 msgid "" @@ -736,10 +905,13 @@ msgid "" "the interface that all handlers should have and establishes some default " "behavior that child classes can use (or override)." msgstr "" +"應用程式程式碼不應直接實例化和使用 :class:`Handler` 的實例。相反的,:class:" +"`Handler` 類別是一個基礎類別,它定義了所有處理器應具備的介面(interface),並" +"建立了某些子類別可以使用(或可以覆寫)的預設行為。" #: ../../howto/logging.rst:541 msgid "Formatters" -msgstr "" +msgstr "格式化器 (Formatters)" #: ../../howto/logging.rst:543 msgid "" @@ -750,12 +922,18 @@ msgid "" "takes three optional arguments -- a message format string, a date format " "string and a style indicator." msgstr "" +"格式化器對象(Formatter objects)設置記錄訊息的最終順序、結構和內容。和基礎" +"的 :class:`logging.Handler` 類別不同,應用程式程式碼可以實例化格式化器類別," +"不過如果應用程式需要特殊行為,則可能需要子類別繼承格式化器。構造函數" +"(condtructor)有三個可選引數 -- 訊息格式字串、日期格式字串和樣式指示器。" #: ../../howto/logging.rst:552 msgid "" "If there is no message format string, the default is to use the raw " "message. If there is no date format string, the default date format is:" msgstr "" +"如果沒有訊息格式字串,預設使用原始訊息。如果沒有日期格式字串,預設的日期格式" +"為:::" #: ../../howto/logging.rst:559 msgid "" @@ -763,6 +941,8 @@ msgid "" "``'{'``, or ``'$'``. If one of these is not specified, then ``'%'`` will be " "used." msgstr "" +"在結尾處附加了毫秒, ``style`` 是 ``'%'`` , ``'{'`` 和 ``'$'`` 的其中一個。" +"如果未指定其中任一個,則使用 ``'%'`` 。" #: ../../howto/logging.rst:562 msgid "" @@ -773,10 +953,15 @@ msgid "" "arguments), while if the style is ``'$'`` then the message format string " "should conform to what is expected by :meth:`string.Template.substitute`." msgstr "" +"如果 ``style`` 是 ``'%'``,則訊息格式字串使用 ``%()s`` 的樣式" +"字串替換;可能的鍵名已在 :ref:`logrecord-attributes` 中文件化。如果樣式是 " +"``'{'``,則假設訊息格式字串與 :meth:`str.format` 相容(使用關鍵字引數),而如" +"果樣式是 ``'$'``,則訊息格式字串應符合 :meth:`string.Template.substitute` 預" +"期的格式。" #: ../../howto/logging.rst:569 msgid "Added the ``style`` parameter." -msgstr "新增 ``style`` 參數。" +msgstr "新增 ``style`` 引數。" #: ../../howto/logging.rst:572 msgid "" @@ -784,6 +969,8 @@ msgid "" "format, the severity of the message, and the contents of the message, in " "that order::" msgstr "" +"下列訊息格式字串將以人類可讀的方式記錄訊息時間、訊息嚴重性和訊息內容,按照這" +"個順序:::" #: ../../howto/logging.rst:578 msgid "" @@ -795,32 +982,40 @@ msgid "" "want all logging times to be shown in GMT, set the ``converter`` attribute " "in the Formatter class (to ``time.gmtime`` for GMT display)." msgstr "" +"格式化器使用可自訂的函式,將紀錄的建立時間轉換為元組。預設使用 :func:`time." +"localtime`;如果要針對特定格式化器實例更改此設定,則將該實例的 ``converter`` " +"屬性設置為一個與 :func:`time.localtime` 或 :func:`time.gmtime` 等具有相同簽名" +"的函式。如果要為所有格式化器更改此設定,像是要在所有記錄時間顯示為 GMT 時間," +"可以在格式化器類別中設置 ``converter`` 屬性(以 ``time.gmtime`` 為 GMT 顯" +"示)。" #: ../../howto/logging.rst:588 msgid "Configuring Logging" -msgstr "" +msgstr "設定日誌(Logging)" #: ../../howto/logging.rst:592 msgid "Programmers can configure logging in three ways:" -msgstr "" +msgstr "程序員有三種設定日誌(Logging)的方式:" #: ../../howto/logging.rst:594 msgid "" "Creating loggers, handlers, and formatters explicitly using Python code that " "calls the configuration methods listed above." msgstr "" +"以 Python 程式碼方式呼叫上述設定方法來明確的生成紀錄器 (loggers)、處理器 " +"(handlers), 以及格式器 (formatters)。" #: ../../howto/logging.rst:596 msgid "" "Creating a logging config file and reading it using the :func:`fileConfig` " "function." -msgstr "" +msgstr "使用 :func:`fileConfig` 函式來生成一個日誌設定檔並讀取它。" #: ../../howto/logging.rst:598 msgid "" "Creating a dictionary of configuration information and passing it to the :" "func:`dictConfig` function." -msgstr "" +msgstr "創建一個包含設定訊息的字典,並將其傳送給 :func:`dictConfig` 函數。" #: ../../howto/logging.rst:601 msgid "" @@ -828,11 +1023,13 @@ msgid "" "config-api`. The following example configures a very simple logger, a " "console handler, and a simple formatter using Python code::" msgstr "" +"請參閱 :ref:`logging-config-api` 來了解關於最後兩個選項的參考文件。以下示範設" +"定一個非常簡單的紀錄器、控制台處理器和一個使用Python代碼的簡單格式化器:" #: ../../howto/logging.rst:631 msgid "" "Running this module from the command line produces the following output:" -msgstr "" +msgstr "在命令行執行這個模組將會產生下列輸出:" #: ../../howto/logging.rst:642 msgid "" @@ -840,15 +1037,17 @@ msgid "" "identical to those in the example listed above, with the only difference " "being the names of the objects::" msgstr "" +"以下Python模組建立了一個幾乎與上面列表中相同的記錄器(logger)、處理器" +"(handler),和格式化器(formatter),唯一不同之處在於對象名稱的改變 :" #: ../../howto/logging.rst:661 msgid "Here is the logging.conf file:" -msgstr "" +msgstr "這是 logging.conf 檔案:" #: ../../howto/logging.rst:693 msgid "" "The output is nearly identical to that of the non-config-file-based example:" -msgstr "" +msgstr "輸出結果和非設定文件範例的幾乎完全相同:" #: ../../howto/logging.rst:704 msgid "" @@ -856,6 +1055,8 @@ msgid "" "Python code approach, mainly separation of configuration and code and the " "ability of noncoders to easily modify the logging properties." msgstr "" +"可以看出使用設定檔的方法相較於使用 Python 程式碼具有一些優點。主要包括分離設" +"定和程式碼、以及使得非開發人員能夠簡單地修改日誌屬性。" #: ../../howto/logging.rst:708 msgid "" @@ -867,6 +1068,11 @@ msgid "" "configuration. Please refer to the reference documentation for more " "information, and specify ``False`` for this parameter if you wish." msgstr "" +":func:`fileConfig` 函式帶有一個預設參數:``disable_existing_loggers``,其因向" +"後相容的考量而默認值為 ``True``。這可能不符合您的要求,除非它們或祖先" +"(ancestor)明確包含於設定當中,不然會因為它導致在呼叫 :func:`fileConfig` 前" +"已存在的非根記錄器(loggers) 被停用。如需更多資訊請查看參考文件並在本參數中指" +"定值 ``False``。" #: ../../howto/logging.rst:716 msgid "" @@ -876,6 +1082,10 @@ msgid "" "the logger-disabling behaviour described above, which may not be what you " "want - in which case, provide the key explicitly with a value of ``False``." msgstr "" +"傳遞給 :func:`dictConfig` 的字典也可以使用 ``disable_existing_loggers`` 作為" +"鍵的布林值,如果該項在字典中沒有明確指定,則預設會解釋為 ``True``。這導致了上" +"述關閉 Logger 的行為,可能不是您想要的行為;若是如此,請提供顯式的鍵並以值 " +"\"False\" 提供。" #: ../../howto/logging.rst:726 msgid "" @@ -887,6 +1097,11 @@ msgid "" "module ``mymodule``, where ``mypackage`` is available on the Python import " "path)." msgstr "" +"注意,設定檔中引用的類別名稱必須相對於日誌模組,或是可以透過正常匯入方式解析" +"的絕對值。因此你可以使用 :class:`~logging.handlers.WatchedFileHandler` (相對" +"於日誌模組),或者 ``mypackage.mymodule.MyHandler``\\ (表示在套件 " +"``mypackage`` 的模組 ``mymodule`` 中定義一個類別,並確認該套件已在 Python 匯" +"入路徑上)" #: ../../howto/logging.rst:734 msgid "" @@ -902,22 +1117,29 @@ msgid "" "can construct the dictionary in Python code, receive it in pickled form over " "a socket, or use whatever approach makes sense for your application." msgstr "" +"在 Python 3.2 中,引入了一種使用字典來保存設定資訊的新方法。這提供了上述配置" +"文件方法所提供功能的超集,是推薦用於新應用程式和部署的配置方法。因為 Python " +"字典被用來保存配置資訊, 你可以使用不同的方式來填充字典, 所以你有更多的選項" +"來進行配置。例如, 你可以使用 JSON 格式的配置文件填充配置字典, 或者如果你能" +"夠存取到 YAML 處理功能, 則可以使用 YAML 格式的文件來填充配置字典。或者當然你" +"可以在 Python 程式碼中建立字典, 或通過 socket 以序列化形式接收它,或使用任何" +"對於應用程式合理的方法。" #: ../../howto/logging.rst:746 msgid "" "Here's an example of the same configuration as above, in YAML format for the " "new dictionary-based approach:" -msgstr "" +msgstr "以下是與上述相同的設定範例,採用基於新字典的 YAML 格式:" #: ../../howto/logging.rst:770 msgid "" "For more information about logging using a dictionary, see :ref:`logging-" "config-api`." -msgstr "" +msgstr "欲了解如何使用字典進行紀錄,請參考 :ref:`logging-config-api`。" #: ../../howto/logging.rst:774 msgid "What happens if no configuration is provided" -msgstr "" +msgstr "如果未提供設定,會發生什麼" #: ../../howto/logging.rst:776 msgid "" @@ -937,10 +1159,16 @@ msgid "" "handler's level is set to ``WARNING``, so all events at this and greater " "severities will be output." msgstr "" +"本事件使用 'handler of last resort' 輸出,該處理程序存儲在 :data:" +"`lastResort` 中。這個內部處理程序不與任何日誌記錄器相關聯,作用類似於 :class:" +"`~logging.StreamHandler`,將事件描述信息寫入當前值 ``sys.stderr``\\ (因此會" +"尊重任何已生效的重定向)。對消息不進行任何格式化處理,僅打印出基本的事件描述" +"信息。處理程序的級別設置為 ``WARNING`` ,因此將輸出該級別及更高級別的所有事" +"件。" #: ../../howto/logging.rst:791 msgid "For versions of Python prior to 3.2, the behaviour is as follows:" -msgstr "" +msgstr "在 Python 3.2 版本之前的版本,其行為如下:" #: ../../howto/logging.rst:793 msgid "" @@ -961,7 +1189,7 @@ msgstr "" #: ../../howto/logging.rst:805 msgid "Configuring Logging for a Library" -msgstr "" +msgstr "為函式庫設定日誌" #: ../../howto/logging.rst:807 msgid "" @@ -973,6 +1201,10 @@ msgid "" "of severity ``WARNING`` and greater will be printed to ``sys.stderr``. This " "is regarded as the best default behaviour." msgstr "" +"開發使用 logging 的函式庫時,應該注意函式庫如何使用 logging ,例如日誌記錄器" +"的名稱。也需要考慮到 logging 設定的配置方式。如果應用程式未使用日誌記錄且函式" +"庫程式碼有做出日誌記錄呼叫(如前一節所述),則會將所有大於或等於 ``WARNING`` " +"嚴重性事件列印至 ``sys.stderr`` 中。這被認為是最佳預設行為。" #: ../../howto/logging.rst:815 msgid "" @@ -985,6 +1217,11 @@ msgid "" "suitably configured then logging calls made in library code will send output " "to those handlers, as normal." msgstr "" +"如果因為某些原因,你 *不希望* 在缺乏任何記錄配置的情況下打印這些訊息,你可以" +"將一個什麼也不做的處理器附加到你函式庫的頂層記錄器。這樣可以避免打印訊息,因" +"為函式庫的事件都會找到對應的處理器:它只是不會產生任何輸出。如果函式庫使用者" +"為應用使用配置日誌,那麼該配置可能會添加一些處理器,如果級別被適當配置,那麼" +"在函式庫程式碼中進行的 logging 呼叫將像平常一樣將輸出發送到這些處理器中。" #: ../../howto/logging.rst:824 msgid "" @@ -996,6 +1233,11 @@ msgid "" "library *foo* is done using loggers with names matching 'foo.x', 'foo.x.y', " "etc. then the code::" msgstr "" +"logging 套件中包含了一個什麼也不做的處理器 (do-nothing handler): :class:" +"`~logging.NullHandler`\\ (自 Python 3.1 起)。你可以將此處理器的實例新增至被" +"函式庫使用的 logging 命名空間的頂層記錄器中,( *如果* 你希望在缺少日誌配置的" +"情況下防止函式庫產生的日誌事件被輸出至 ``sys.stderr``\\ )。 如果函式庫 " +"*foo* 使用的所有記錄器都符合名為 'foo.x','foo.x.y' 等的規則,則程式碼: ::" #: ../../howto/logging.rst:835 msgid "" @@ -1003,6 +1245,8 @@ msgid "" "libraries, then the logger name specified can be 'orgname.foo' rather than " "just 'foo'." msgstr "" +"應該要有預期的結果。如果一個組織生產了多個函式庫,則指定的記錄器名稱會是 " +"'orgname.foo' ,而不只是 'foo' 。" #: ../../howto/logging.rst:839 msgid "" @@ -1013,6 +1257,9 @@ msgid "" "application developer to configure the logging verbosity or handlers of your " "library as they wish." msgstr "" +"強烈建議你 *不要記錄到根記錄器* 。而是使用一個具有獨特並易於識別的名稱的記錄" +"器,例如你函式庫的頂層套件或模組的 ``__name__`` 。如果記錄到根日誌,應用程序" +"開發者將很難或無法在你的函式庫設置他們想要的logging詳細程度和處理器。" #: ../../howto/logging.rst:846 msgid "" @@ -1024,10 +1271,14 @@ msgid "" "handlers 'under the hood', you might well interfere with their ability to " "carry out unit tests and deliver logs which suit their requirements." msgstr "" +"非常建議你 *不要在你的處理器中添加除* :class:`~logging.NullHandler` *之外的記" +"錄器到你的函式庫*。這是因為處理器的配置取決於使用你函式庫的應用程式開發人員," +"他們會知道目標受眾以及適合使用那些處理器: 如果在 '幕後' 添加處理器,你可能會" +"干擾他們進行單元測試的能力,並且妨礙他們生成符合自己要求的日誌。" #: ../../howto/logging.rst:857 msgid "Logging Levels" -msgstr "" +msgstr "日誌層級" #: ../../howto/logging.rst:859 msgid "" @@ -1037,10 +1288,13 @@ msgid "" "define a level with the same numeric value, it overwrites the predefined " "value; the predefined name is lost." msgstr "" +"日誌等級的數值定義如下表所示。如果你想要定義自己的等級,並且需要使它們相對於" +"預定義的等級具有特定的值,這些內容會對你非常有幫助。如果你定義一個與預設數值" +"相同的等級,它會覆寫原先對應的預設值;原本預設的名稱也會消失。" #: ../../howto/logging.rst:866 msgid "Numeric value" -msgstr "" +msgstr "數值" #: ../../howto/logging.rst:868 msgid "50" @@ -1079,6 +1333,10 @@ msgid "" "the method call's, no logging message is actually generated. This is the " "basic mechanism controlling the verbosity of logging output." msgstr "" +"級別 (Levels) 也可以與記錄器相關聯,被開發人員設置或通過加載保存的日誌配置。" +"當在記錄器上調用logging方法時,該記錄器將其自身級別和與方法調用相關聯的級別進" +"行比較。如果記錄器的級別高於方法調用的級別,則實際上不會生成任何記錄消息。這" +"是控制日誌輸出冗長程度的基本機制。" #: ../../howto/logging.rst:888 msgid "" @@ -1086,6 +1344,8 @@ msgid "" "class. When a logger decides to actually log an event, a :class:`~logging." "LogRecord` instance is created from the logging message." msgstr "" +"記錄訊息被編碼成 :class:`~logging.LogRecord` 類別的實例。當記錄器決定要真正地" +"記錄事件時,會從日誌訊息創建 :class:`~logging.LogRecord` 的實例。" #: ../../howto/logging.rst:892 msgid "" @@ -1103,6 +1363,14 @@ msgid "" "message (unless the *propagate* flag for a logger is set to a false value, " "at which point the passing to ancestor handlers stops)." msgstr "" +"記錄訊息通過 :dfn:`handlers`\\ (它們是 :class:`Handler` 類別的子類別實例)的" +"分派機制進行處理。Handlers 負責確保一個已記錄的訊息(以 :class:`LogRecord` 的" +"形式)最終會出現在特定位置(或多個位置),該訊息對於目標受眾(如終端用戶、支" +"援人員、系統管理員、開發人員)非常有用。Handlers 負責處理特定目的地的 :class:" +"`LogRecord` 實例。每個記錄器可以有零個、一個或多個處理器與之關聯(透過 :" +"class:`Logger` 的 :meth:`~Logger.addHandler` 方法)。除了與記錄器直接關聯的任" +"何處理器,*調用所有祖先的處理器* 都會被調用以分派此訊息(除非某個處理器的 " +"*propagate* 旗標設置為 False,在這種情況下,傳遞到祖先的處理器就會停止)." #: ../../howto/logging.rst:906 msgid "" @@ -1113,10 +1381,14 @@ msgid "" "defined subclasses of :class:`Handler` will need to override this :meth:" "`~Handler.emit`." msgstr "" +"與記錄器一樣,處理器也可以有與其相關的層級。處理器的層級起到過濾器的作用,就" +"像記錄器的層級一樣。如果一個處理器決定實際分發事件,則使用 :meth:`~Handler." +"emit` 方法將訊息發送至目的地。大部分自定義的 :class:`Handler` 子類別都需要覆" +"蓋此 :meth:`~Handler.emit` 。" #: ../../howto/logging.rst:915 msgid "Custom Levels" -msgstr "" +msgstr "自訂等級" #: ../../howto/logging.rst:917 msgid "" @@ -1130,26 +1402,32 @@ msgid "" "difficult for the using developer to control and/or interpret, because a " "given numeric value might mean different things for different libraries." msgstr "" +"定義自己的層級是可行的,但通常不是必要的,因為現有層級是根據實際經驗選擇的。" +"然而,如果您確實需要自定義層級,則在這樣做時應該非常小心,如果您正在開發函式" +"庫,則*自定義層級可能是一個非常糟糕的想法*。這是因為如果多個函式庫作者都定義" +"自己的自定義層級,那麼在一起使用這些函式庫時,日誌輸出可能會對使用開發人員的" +"控制或解釋造成困難,因為給定的數值可能對於不同的函式庫而言意味著不同的事情。" #: ../../howto/logging.rst:930 msgid "Useful Handlers" -msgstr "" +msgstr "有用的處理器" #: ../../howto/logging.rst:932 msgid "" "In addition to the base :class:`Handler` class, many useful subclasses are " "provided:" -msgstr "" +msgstr "除了基礎的 :class:`Handler` 類別,還提供了許多有用的子類別:" #: ../../howto/logging.rst:935 msgid "" ":class:`StreamHandler` instances send messages to streams (file-like " "objects)." msgstr "" +":class:`StreamHandler` 實例會將訊息傳送到資料流(與檔案相似的物件)中。" #: ../../howto/logging.rst:938 msgid ":class:`FileHandler` instances send messages to disk files." -msgstr "" +msgstr ":class:`FileHandler` 實例會將訊息傳送至磁碟檔案。" #: ../../howto/logging.rst:940 msgid "" @@ -1158,60 +1436,79 @@ msgid "" "directly. Instead, use :class:`~handlers.RotatingFileHandler` or :class:" "`~handlers.TimedRotatingFileHandler`." msgstr "" +":class:`~handlers.BaseRotatingHandler` 是處理紀錄檔案輪替的基礎類別。不建議直" +"接實例化,而是使用 :class:`~handlers.RotatingFileHandler` 或是 :class:" +"`~handlers.TimedRotatingFileHandler`。" #: ../../howto/logging.rst:945 msgid "" ":class:`~handlers.RotatingFileHandler` instances send messages to disk " "files, with support for maximum log file sizes and log file rotation." msgstr "" +":class:`~handlers.RotatingFileHandler` 的實例可以將訊息記錄到磁碟檔案中,支援" +"最大日誌檔大小和日誌檔輪替。" #: ../../howto/logging.rst:948 msgid "" ":class:`~handlers.TimedRotatingFileHandler` instances send messages to disk " "files, rotating the log file at certain timed intervals." msgstr "" +":class:`~handlers.TimedRotatingFileHandler` 實例會在特定時間間隔輪替日誌文件" +"並將訊息寫到磁碟檔案中。" #: ../../howto/logging.rst:951 msgid "" ":class:`~handlers.SocketHandler` instances send messages to TCP/IP sockets. " "Since 3.4, Unix domain sockets are also supported." msgstr "" +":class:`~handlers.SocketHandler` 實例會將訊息發送到 TCP/IP sockets 上。從 " +"3.4 開始,Unix domain sockets 也受支援。" #: ../../howto/logging.rst:954 msgid "" ":class:`~handlers.DatagramHandler` instances send messages to UDP sockets. " "Since 3.4, Unix domain sockets are also supported." msgstr "" +":class:`~handlers.DatagramHandler` 實例會將訊息發送到 UDP sockets。自 3.4 " +"起,也支援 Unix domain sockets。" #: ../../howto/logging.rst:957 msgid "" ":class:`~handlers.SMTPHandler` instances send messages to a designated email " "address." -msgstr "" +msgstr ":class:`~handlers.SMTPHandler` 實例會發送訊息到指定的電子郵件地址。" #: ../../howto/logging.rst:960 msgid "" ":class:`~handlers.SysLogHandler` instances send messages to a Unix syslog " "daemon, possibly on a remote machine." msgstr "" +":class:`~handlers.SysLogHandler` 實例可以將訊息發送至 Unix syslog daemon,其" +"中可能包含遠端機器的資訊。" #: ../../howto/logging.rst:963 msgid "" ":class:`~handlers.NTEventLogHandler` instances send messages to a Windows " "NT/2000/XP event log." msgstr "" +":class:`~handlers.NTEventLogHandler` 實例將訊息傳送至 Windows NT/2000/XP 事件" +"記錄。" #: ../../howto/logging.rst:966 msgid "" ":class:`~handlers.MemoryHandler` instances send messages to a buffer in " "memory, which is flushed whenever specific criteria are met." msgstr "" +":class:`~handlers.MemoryHandler` 實例將訊息儲存到記憶體緩衝區,在符合特定條件" +"時進行刷新。" #: ../../howto/logging.rst:969 msgid "" ":class:`~handlers.HTTPHandler` instances send messages to an HTTP server " "using either ``GET`` or ``POST`` semantics." msgstr "" +":class:`~handlers.HTTPHandler` 實例使用 ``GET`` 或 ``POST`` 來語意地向 HTTP " +"伺服器發送訊息。" #: ../../howto/logging.rst:972 msgid "" @@ -1220,12 +1517,17 @@ msgid "" "name. This handler is only useful on Unix-like systems; Windows does not " "support the underlying mechanism used." msgstr "" +":class:`~handlers.WatchedFileHandler` 實例會監看其所記錄的檔案。若該檔案變" +"更,則將關閉並使用新的文件名重新開啟。此處理程序僅在類Unix 系統上才有用,因" +"為 Windows 不支援所使用的底層機制。" #: ../../howto/logging.rst:977 msgid "" ":class:`~handlers.QueueHandler` instances send messages to a queue, such as " "those implemented in the :mod:`queue` or :mod:`multiprocessing` modules." msgstr "" +":class:`~handlers.QueueHandler` 實例將訊息發送到佇列中,例如在 :mod:`queue` " +"或 :mod:`multiprocessing` 模組中實作的那些佇列。" #: ../../howto/logging.rst:980 msgid "" @@ -1235,14 +1537,18 @@ msgid "" "the library user has not configured logging. See :ref:`library-config` for " "more information." msgstr "" +":class:`NullHandler` (空處理器) 的實例對錯誤訊息沒有作用。這些被程式庫開發者" +"使用,他們想要使用日誌紀錄(logging),但是希望避免當程式庫的使用者未配置日誌紀" +"錄時所顯示的 \"找不到與XXX相關聯之處理器No handlers could be found for " +"logger\" 訊息。參閱 :ref:`library-config` 以瞭解更多資訊。" #: ../../howto/logging.rst:986 msgid "The :class:`NullHandler` class." -msgstr "" +msgstr ":class:`NullHandler` 類別。" #: ../../howto/logging.rst:989 msgid "The :class:`~handlers.QueueHandler` class." -msgstr "" +msgstr ":class:`~handlers.QueueHandler` 類別。" #: ../../howto/logging.rst:992 msgid "" @@ -1251,6 +1557,9 @@ msgid "" "defined in a sub-module, :mod:`logging.handlers`. (There is also another sub-" "module, :mod:`logging.config`, for configuration functionality.)" msgstr "" +":class:`NullHandler`、:class:`StreamHandler` 和 :class:`FileHandler` 這三個類" +"別是在核心日誌套件中定義的。其他處理器則是在 :mod:`logging.handlers` 子模組中" +"定義(也有另一個名為 :mod:`logging.config` 的子模組,負責配置功能)。" #: ../../howto/logging.rst:997 msgid "" @@ -1258,6 +1567,8 @@ msgid "" "class:`Formatter` class. They are initialized with a format string suitable " "for use with the % operator and a dictionary." msgstr "" +"記錄訊息被格式化以便透過 :class:`Formatter` 類別的實例呈現。它們適用於 % 運算" +"子和字典的格式字串來進行初始化。" #: ../../howto/logging.rst:1001 msgid "" @@ -1266,6 +1577,8 @@ msgid "" "applied to each message in the batch), there is provision for header and " "trailer format strings." msgstr "" +"若要在一批訊息中套用格式,可使用 :class:`~handlers.BufferingFormatter` 實例。" +"除了針對批次中每個訊息所套用的格式字串外,還有提供標題和尾串格式字串。" #: ../../howto/logging.rst:1006 msgid "" @@ -1276,6 +1589,10 @@ msgid "" "consult all their filters for permission. If any filter returns a false " "value, the message is not processed further." msgstr "" +"當只使用日誌器或處理器的層級篩選時不足以滿足需求的時候,可以向 :class:" +"`Logger` 和 :class:`Handler` 實例中添加 :class:`Filter`\\ (透過其方法 :meth:" +"`~Handler.addFilter` )。在決定如何進一步處理訊息之前,日誌器和處理器會查詢所" +"有篩選程式碼是否允許。如果任何篩選程式返回 false 值,則不再繼續處理此訊息。" #: ../../howto/logging.rst:1013 msgid "" @@ -1283,18 +1600,24 @@ msgid "" "name. If this feature is used, messages sent to the named logger and its " "children are allowed through the filter, and all others dropped." msgstr "" +":class:`Filter` 基本功能是可以按照特定記錄器名稱進行過濾。如果使用此功能,傳" +"送到已命名的記錄器及其子物件的訊息將通過篩檢程序,而所有其他訊息則會被放棄。" #: ../../howto/logging.rst:1021 msgid "Exceptions raised during logging" -msgstr "" +msgstr "記錄期間引發的異常" #: ../../howto/logging.rst:1023 +#, fuzzy msgid "" "The logging package is designed to swallow exceptions which occur while " "logging in production. This is so that errors which occur while handling " "logging events - such as logging misconfiguration, network or other similar " "errors - do not cause the application using logging to terminate prematurely." msgstr "" +"logging 套件的設計目標是在生產上運作時會忽略發生於記錄(log)中的例外狀況" +"(exception)。因此,在處理 logging 事件時,例如日誌配置不正確、網路或其他類似" +"型別的錯誤 - 不會導致使用 logging 的應用程式提前終止。" #: ../../howto/logging.rst:1028 msgid "" @@ -1303,14 +1626,21 @@ msgid "" "method of a :class:`Handler` subclass are passed to its :meth:`~Handler." "handleError` method." msgstr "" +":class:`SystemExit` 和 :class:`KeyboardInterrupt` 例外情況絕不會被忽略,其他" +"在 :class:`Handler` 的子類別中的 :meth:`~Handler.emit` 方法發生的例外狀況將會" +"傳遞到它的 :meth:`~Handler.handleError` 方法。" #: ../../howto/logging.rst:1033 +#, fuzzy msgid "" "The default implementation of :meth:`~Handler.handleError` in :class:" "`Handler` checks to see if a module-level variable, :data:`raiseExceptions`, " "is set. If set, a traceback is printed to :data:`sys.stderr`. If not set, " "the exception is swallowed." msgstr "" +":class:`Handler` 中的 :meth:`~Handler.handleError`預設實作會檢查模組層級變" +"數 :data:`raiseExceptions` 是否被設置。如有設置,臨界值將會輸出至寫入器 :" +"data:`sys.stderr` 而未被處理。" #: ../../howto/logging.rst:1039 msgid "" @@ -1319,10 +1649,13 @@ msgid "" "occur. It's advised that you set :data:`raiseExceptions` to ``False`` for " "production usage." msgstr "" +":data:`raiseExceptions` 的預設值為「True」。這是因為在開發時,通常希望能夠得" +"知任何例外情況的通知。建議你在產品使用時將 :data:`raiseExceptions` 設置為" +"「False」。" #: ../../howto/logging.rst:1049 msgid "Using arbitrary objects as messages" -msgstr "" +msgstr "將任意物件當作訊息" #: ../../howto/logging.rst:1051 msgid "" @@ -1335,10 +1668,15 @@ msgid "" "`~handlers.SocketHandler` emits an event by pickling it and sending it over " "the wire." msgstr "" +"在前幾個小節和範例中,都是使用字串做為處理事件的訊息(message)。但這不是唯一" +"的選擇,你可以傳遞任意物件當作訊息,當日誌系統需要將其轉換成字串表示式時會呼" +"叫它的 :meth:`~object.__str__` 方法。事實上,如果想要的話,甚至可以完全不計算" +"字串的表示 - 例如 :class:`~handlers.SocketHandler` 可以透過序列化(pickling)" +"事件並通過網路去發布它。" #: ../../howto/logging.rst:1062 msgid "Optimization" -msgstr "" +msgstr "最佳化" #: ../../howto/logging.rst:1064 msgid "" @@ -1350,12 +1688,20 @@ msgid "" "event would be created by the Logger for that level of call. You can write " "code like this::" msgstr "" +"訊息引數的格式化會被延遲到無法避免時才執行。即便如此,計算傳給日誌方法的引數" +"也是費時的,如果日誌會忽略事件並丟棄記錄,就應該要盡量避免計算它們。為了做出" +"決定,您可以呼叫 :meth:`~Logger.isEnabledFor` 方法(需要 level 引數)並返回是" +"否記錄器會根據該呼叫等級建立事件。下面是範例程式碼:\n" +"\n" +"::" #: ../../howto/logging.rst:1076 msgid "" "so that if the logger's threshold is set above ``DEBUG``, the calls to " "``expensive_func1`` and ``expensive_func2`` are never made." msgstr "" +"若記錄器的閾值設定高於 ``DEBUG``,則不會呼叫 ``expensive_func1`` 和 " +"``expensive_func2``。" #: ../../howto/logging.rst:1079 msgid "" @@ -1368,26 +1714,34 @@ msgid "" "need to be recomputed when the logging configuration changes dynamically " "while the application is running (which is not all that common)." msgstr "" +"在某些情況下, :meth:`~Logger.isEnabledFor` 本身可能比你想像的更昂貴(例如對" +"於僅在較高層級的記錄器中設置明確級別的深度記錄器中)。 在這種情況下(或者如果" +"您想要避免在緊密循環中呼叫方法),您可以在局部變數或實例變數中快取對 :meth:" +"`~Logger.isEnabledFor` 的呼叫結果,並使用它來代替每次呼叫該方法。 這樣的快取" +"僅需要在應用程式運行時動態更改日誌配置時重新計算(這並不是很常見)。" #: ../../howto/logging.rst:1088 +#, fuzzy msgid "" "There are other optimizations which can be made for specific applications " "which need more precise control over what logging information is collected. " "Here's a list of things you can do to avoid processing during logging which " "you don't need:" msgstr "" +"有些情況需要更精確地控制所收集的日誌資訊,可以進行其他特定應用程式的最佳化。" +"以下是一些可避免處理不必要記錄期間的操作列表:" #: ../../howto/logging.rst:1094 msgid "What you don't want to collect" -msgstr "" +msgstr "你不想收集的東西" #: ../../howto/logging.rst:1094 msgid "How to avoid collecting it" -msgstr "" +msgstr "如何避免收集它" #: ../../howto/logging.rst:1096 msgid "Information about where calls were made from." -msgstr "" +msgstr "有關從哪裡呼叫的資訊。" #: ../../howto/logging.rst:1096 msgid "" @@ -1395,32 +1749,35 @@ msgid "" "_getframe`, which may help to speed up your code in environments like PyPy " "(which can't speed up code that uses :func:`sys._getframe`)." msgstr "" +"將 ``logging._srcfile`` 設為 ``None``。可避免呼叫 :func:`sys._getframe` ,這" +"可加快程式的速度,特別是像 PyPy 環境等無法使用 :func:`sys._getframe` 加速的情" +"況。" #: ../../howto/logging.rst:1102 msgid "Threading information." -msgstr "" +msgstr "執行緒資訊。" #: ../../howto/logging.rst:1102 msgid "Set ``logging.logThreads`` to ``False``." -msgstr "" +msgstr "將 ``logging.logThreads`` 設為 ``False``。" #: ../../howto/logging.rst:1104 msgid "Current process ID (:func:`os.getpid`)" -msgstr "" +msgstr "目前處理行程的 ID (:func:`os.getpid`\\ )" #: ../../howto/logging.rst:1104 msgid "Set ``logging.logProcesses`` to ``False``." -msgstr "" +msgstr "將 ``logging.logProcesses`` 設定為 ``False``。" #: ../../howto/logging.rst:1106 msgid "" "Current process name when using ``multiprocessing`` to manage multiple " "processes." -msgstr "" +msgstr "使用 ``multiprocessing`` 來管理多個程序時目前的程序名稱。" #: ../../howto/logging.rst:1106 msgid "Set ``logging.logMultiprocessing`` to ``False``." -msgstr "" +msgstr "將 ``logging.logMultiprocessing`` 設為 ``False`` 。" #: ../../howto/logging.rst:1109 msgid "Current :class:`asyncio.Task` name when using ``asyncio``." @@ -1436,6 +1793,8 @@ msgid "" "you don't import :mod:`logging.handlers` and :mod:`logging.config`, they " "won't take up any memory." msgstr "" +"請注意,核心日誌模組僅包含基本處理器(handler)。如果您不 import :mod:" +"`logging.handlers` 和 :mod:`logging.config` 的話,將不會佔用任何記憶體。" #: ../../howto/logging.rst:1120 msgid "Other resources" @@ -1447,7 +1806,7 @@ msgstr ":mod:`logging` 模組" #: ../../howto/logging.rst:1125 msgid "API reference for the logging module." -msgstr "" +msgstr "日誌模組的 API 參考文件。" #: ../../howto/logging.rst:1128 msgid "Module :mod:`logging.config`" @@ -1455,7 +1814,7 @@ msgstr ":mod:`logging.config` 模組" #: ../../howto/logging.rst:1128 msgid "Configuration API for the logging module." -msgstr "" +msgstr "日誌模組的設定 API。" #: ../../howto/logging.rst:1131 msgid "Module :mod:`logging.handlers`" @@ -1463,8 +1822,8 @@ msgstr ":mod:`logging.handlers` 模組" #: ../../howto/logging.rst:1131 msgid "Useful handlers included with the logging module." -msgstr "" +msgstr "日誌模組內含有許多實用的處理器。" #: ../../howto/logging.rst:1133 msgid ":ref:`A logging cookbook `" -msgstr "" +msgstr ":ref:`A logging cookbook `" diff --git a/howto/regex.po b/howto/regex.po index 79836d436a..32aab74b62 100644 --- a/howto/regex.po +++ b/howto/regex.po @@ -9,9 +9,9 @@ msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-10-24 00:03+0000\n" -"PO-Revision-Date: 2018-05-23 14:37+0000\n" -"Last-Translator: Adrian Liaw \n" +"POT-Creation-Date: 2024-02-24 16:01+0800\n" +"PO-Revision-Date: 2023-03-26 18:25+0000\n" +"Last-Translator: CTHua \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" "Language: zh_TW\n" @@ -19,6 +19,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" +"X-Generator: Weblate 4.16.4\n" #: ../../howto/regex.rst:5 msgid "Regular Expression HOWTO" @@ -68,6 +69,7 @@ msgstr "" "是否有任一處匹配這個規則?」。除此之外,你也可以使用 REs 來修改或是切割字串。" #: ../../howto/regex.rst:35 +#, fuzzy msgid "" "Regular expression patterns are compiled into a series of bytecodes which " "are then executed by a matching engine written in C. For advanced use, it " @@ -77,8 +79,14 @@ msgid "" "requires that you have a good understanding of the matching engine's " "internals." msgstr "" +"正規表示式(regular expression)模式會被編譯成一系列 C 語言的匹配引擎可執行之" +"位元碼,進而由該引擎來進行匹配。若想要針對高階使用方式做最佳化,則需仔細考量" +"引擎如何處理特定正規表示式,在其產生能更快執行之 bytecode 的前提下編寫正規表" +"示式。本文件並未涉及有關最佳化方面的內容,因為它需要您對匹配引擎具有深入了" +"解。" #: ../../howto/regex.rst:42 +#, fuzzy msgid "" "The regular expression language is relatively small and restricted, so not " "all possible string processing tasks can be done using regular expressions. " @@ -88,38 +96,55 @@ msgid "" "be slower than an elaborate regular expression, it will also probably be " "more understandable." msgstr "" +"正規表示式 (regular expression) 語言相對小而受限,因此並非所有可能的字串處理" +"任務都可以使用正規表示式完成。有些任務 *可以* 使用正規表示式完成,但表達會變" +"得非常複雜。在這些情況下,您最好撰寫 Python 代碼來處理;雖然 Python 代碼比精" +"心設計的正則表達式要慢,但也更容易被理解。" #: ../../howto/regex.rst:51 +#, fuzzy msgid "Simple Patterns" -msgstr "" +msgstr "簡單模式" #: ../../howto/regex.rst:53 +#, fuzzy msgid "" "We'll start by learning about the simplest possible regular expressions. " "Since regular expressions are used to operate on strings, we'll begin with " "the most common task: matching characters." msgstr "" +"讓我們從學習最簡單的正規表示式開始。由於正規表示式是用來操作字串的,因此我們" +"先從最常見的任務開始:匹配字符。" #: ../../howto/regex.rst:57 +#, fuzzy msgid "" "For a detailed explanation of the computer science underlying regular " "expressions (deterministic and non-deterministic finite automata), you can " "refer to almost any textbook on writing compilers." msgstr "" +"關於正規表示式底層的計算機科學原理(如有限狀態自動機之決定性及非決定性),您" +"可以參考幾乎任何編寫編譯器的教科書以得到詳細解釋。" #: ../../howto/regex.rst:63 +#, fuzzy msgid "Matching Characters" -msgstr "" +msgstr "匹配字元" #: ../../howto/regex.rst:65 +#, fuzzy msgid "" "Most letters and characters will simply match themselves. For example, the " "regular expression ``test`` will match the string ``test`` exactly. (You " "can enable a case-insensitive mode that would let this RE match ``Test`` or " "``TEST`` as well; more about this later.)" msgstr "" +"大部分的字母和符號只會跟它們自己配對。例如,正規表示式「test」只會精確地匹配" +"到字串「test」。(你可以啟用不分大小寫模式來讓這個 RE 匹配 「Test」或" +"「TEST」;等等再詳細說明。)" #: ../../howto/regex.rst:70 +#, fuzzy msgid "" "There are exceptions to this rule; some characters are special :dfn:" "`metacharacters`, and don't match themselves. Instead, they signal that " @@ -128,14 +153,20 @@ msgid "" "this document is devoted to discussing various metacharacters and what they " "do." msgstr "" +"有例外情形,其中一些字元是特殊的 dfn:`metacharacters` (元字元),並非匹配它" +"們自己。相反的,這表示某些需要匹配不尋常事物,或通過重複其他部分或改變其含義" +"來影響 RE 的其他部分。本文件的大部分內容都是討論各種 \"metacharacters\" 及其" +"用法。" #: ../../howto/regex.rst:76 +#, fuzzy msgid "" "Here's a complete list of the metacharacters; their meanings will be " "discussed in the rest of this HOWTO." -msgstr "" +msgstr "下面是元字符的完整清單;它們的含義將在本 HOWTO 的其餘部分中討論。" #: ../../howto/regex.rst:83 +#, fuzzy msgid "" "The first metacharacters we'll look at are ``[`` and ``]``. They're used for " "specifying a character class, which is a set of characters that you wish to " @@ -146,16 +177,26 @@ msgid "" "characters. If you wanted to match only lowercase letters, your RE would be " "``[a-z]``." msgstr "" +"我們首先會看到的是「[」和「]」這些元字符。它們用於指定一個字元類別" +"(character class),即您希望匹配的一組字元。 字符可以逐個列出,也可以通過給" +"兩個字符並用「'-'」分開來表示一定範圍的字符。例如,「[abc]」將匹配任何一個字" +"符為「a」「b」或者是 「c」中的內容;同樣地使用範圍表達相同集合則寫成 「[a-" +"c] 」。「[a-z]」代表僅希望匹配小寫字母。" #: ../../howto/regex.rst:92 +#, fuzzy msgid "" "Metacharacters (except ``\\``) are not active inside classes. For example, " "``[akm$]`` will match any of the characters ``'a'``, ``'k'``, ``'m'``, or " "``'$'``; ``'$'`` is usually a metacharacter, but inside a character class " "it's stripped of its special nature." msgstr "" +"元字符(除了``\\``)在字符類中不起作用。例如,「[akm $]」將與任何一個字元" +"「'a'」、「'k'」、「 'm '」或者「 '$' 」匹配; 「 '$' 」通常是一個元字符,但在" +"字符集合裡就被剝離了特殊的本質。" #: ../../howto/regex.rst:97 +#, fuzzy msgid "" "You can match the characters not listed within the class by :dfn:" "`complementing` the set. This is indicated by including a ``'^'`` as the " @@ -164,8 +205,11 @@ msgid "" "does not have special meaning. For example: ``[5^]`` will match either a " "``'5'`` or a ``'^'``." msgstr "" +"你可以通過“補集”匹配在類中未列出的字符。這是通過將``'^'``作為該類的第一個字符" +"來指示的。例如,``[^5]`` 將匹配除了“5”之外的任何字符。 如果插入符號(caret)" #: ../../howto/regex.rst:103 +#, fuzzy msgid "" "Perhaps the most important metacharacter is the backslash, ``\\``. As in " "Python string literals, the backslash can be followed by various characters " @@ -174,15 +218,23 @@ msgid "" "need to match a ``[`` or ``\\``, you can precede them with a backslash to " "remove their special meaning: ``\\[`` or ``\\\\``." msgstr "" +"也許最重要的是反斜線「\\」這個元字符。如同 Python 字串文字,反斜線後面可以跟" +"著各種字元來表示不同的特殊序列。它也被用來轉義所有的元字符,所以你仍然可以在" +"模式中匹配它們;例如,如果你需要匹配 \"[\" 或 \"\\\" ,則可以在它們前面加上一" +"條反斜線使其失去特殊意義:\"\\[\" 或者 \"\\\\\\\\\"." #: ../../howto/regex.rst:110 +#, fuzzy msgid "" "Some of the special sequences beginning with ``'\\'`` represent predefined " "sets of characters that are often useful, such as the set of digits, the set " "of letters, or the set of anything that isn't whitespace." msgstr "" +"有些以``'\\'``開頭的特殊序列代表已事先定義好,並常用到的一組字元,例如數字、" +"字母或空白之外的任何東西。" #: ../../howto/regex.rst:115 +#, fuzzy msgid "" "Let's take an example: ``\\w`` matches any alphanumeric character. If the " "regex pattern is expressed in bytes, this is equivalent to the class ``[a-zA-" @@ -192,8 +244,14 @@ msgid "" "in a string pattern by supplying the :const:`re.ASCII` flag when compiling " "the regular expression." msgstr "" +"讓我們舉一個例子:``\\w`` 匹配任何字母或數字字符。 如果正則表達式模式採用位元" +"組表示,這等同於類別 ``[a-zA-Z0-9_]`` 。如果正則表達式模式是一個字符串,那麼 " +"``\\w`` 將匹配由 :mod:`unicodedata` 模塊提供的 Unicode 資料庫中標記為文字的所" +"有字符。您可以在編譯正則表達式時提供 :const:`re.ASCII` 旗幟來使用更受限定義下" +"的 ``\\w`` 字符串模式。" #: ../../howto/regex.rst:123 +#, fuzzy msgid "" "The following list of special sequences isn't complete. For a complete list " "of sequences and expanded class definitions for Unicode string patterns, see " @@ -201,84 +259,102 @@ msgid "" "Standard Library reference. In general, the Unicode versions match any " "character that's in the appropriate category in the Unicode database." msgstr "" +"下列特殊序列的列表不是完整的,要查看Unicode字串模式的所有定義和擴展類別,請參" +"考標準函式庫參考資料中 :ref:`Regular Expression Syntax ` 的最後部" +"分。一般而言,Unicode 版本會將該合適種類在 Unicode 資料庫內任何字元配對。" #: ../../howto/regex.rst:131 msgid "``\\d``" msgstr "``\\d``" #: ../../howto/regex.rst:131 +#, fuzzy msgid "Matches any decimal digit; this is equivalent to the class ``[0-9]``." -msgstr "" +msgstr "匹配所有十進位數字;等價於類別 ``[0-9]``。" #: ../../howto/regex.rst:134 msgid "``\\D``" msgstr "``\\D``" #: ../../howto/regex.rst:134 +#, fuzzy msgid "" "Matches any non-digit character; this is equivalent to the class ``[^0-9]``." -msgstr "" +msgstr "匹配任何非數字字符;這等同於類別 ``[^0-9]``。" #: ../../howto/regex.rst:138 msgid "``\\s``" msgstr "``\\s``" #: ../../howto/regex.rst:137 +#, fuzzy msgid "" "Matches any whitespace character; this is equivalent to the class " "``[ \\t\\n\\r\\f\\v]``." -msgstr "" +msgstr "符合所有空格字符,相當於類別 ``[ \\t\\n\\r\\f\\v]``。" #: ../../howto/regex.rst:142 msgid "``\\S``" msgstr "``\\S``" #: ../../howto/regex.rst:141 +#, fuzzy msgid "" "Matches any non-whitespace character; this is equivalent to the class ``[^ " "\\t\\n\\r\\f\\v]``." -msgstr "" +msgstr "符合任何非空白字元,這相當於目錄 ``[^ \\t\\n\\r\\f\\v]``。" #: ../../howto/regex.rst:146 msgid "``\\w``" msgstr "``\\w``" #: ../../howto/regex.rst:145 +#, fuzzy msgid "" "Matches any alphanumeric character; this is equivalent to the class ``[a-zA-" "Z0-9_]``." -msgstr "" +msgstr "匹配任何字母、數字的字符,這等價於類別 ``[a-zA-Z0-9_]``。" #: ../../howto/regex.rst:150 msgid "``\\W``" msgstr "``\\W``" #: ../../howto/regex.rst:149 +#, fuzzy msgid "" "Matches any non-alphanumeric character; this is equivalent to the class " "``[^a-zA-Z0-9_]``." -msgstr "" +msgstr "匹配任何一個非字母或數字的字符;這相當於類別``[^a-zA-Z0-9_]``。" #: ../../howto/regex.rst:152 +#, fuzzy msgid "" "These sequences can be included inside a character class. For example, " "``[\\s,.]`` is a character class that will match any whitespace character, " "or ``','`` or ``'.'``." msgstr "" +"這些字元序列可包含在字元類別內。例如,``[\\s,.]``是一個字元類別,可以匹配任何" +"空白字元、``','``或``'.'``。" #: ../../howto/regex.rst:156 +#, fuzzy msgid "" "The final metacharacter in this section is ``.``. It matches anything " "except a newline character, and there's an alternate mode (:const:`re." "DOTALL`) where it will match even a newline. ``.`` is often used where you " "want to match \"any character\"." msgstr "" +"本節的最後一個元字符是``.``。它匹配除了換行符號以外的任何字元,並且有另一種模" +"式 (:const:`re.DOTALL`) 可以匹配包含企業符號在內的所有字元。 ``.`` 常用於想要" +"匹配\"任意字元\" 的情況下。" #: ../../howto/regex.rst:163 +#, fuzzy msgid "Repeating Things" -msgstr "" +msgstr "重複的事情" #: ../../howto/regex.rst:165 +#, fuzzy msgid "" "Being able to match varying sets of characters is the first thing regular " "expressions can do that isn't already possible with the methods available on " @@ -286,48 +362,68 @@ msgid "" "they wouldn't be much of an advance. Another capability is that you can " "specify that portions of the RE must be repeated a certain number of times." msgstr "" +"正規表示式的第一個優點是,能夠使用不同字符集合匹配。這在字串方法中並不可行。" +"然而,如果只有此能力,在正則表達式上的進展就不會太大。另一個技巧包括:您可以" +"指定 RE 的某些部分必須重複幾次。" #: ../../howto/regex.rst:171 +#, fuzzy msgid "" "The first metacharacter for repeating things that we'll look at is ``*``. " "``*`` doesn't match the literal character ``'*'``; instead, it specifies " "that the previous character can be matched zero or more times, instead of " "exactly once." msgstr "" +"我們要介紹的第一個用於重複事物的元字符是「*」。星號不代表字面上的星號;它代表" +"先前出現過的 字元可以匹配零次或多次,而非只能出現一次。" #: ../../howto/regex.rst:175 +#, fuzzy msgid "" "For example, ``ca*t`` will match ``'ct'`` (0 ``'a'`` characters), ``'cat'`` " "(1 ``'a'``), ``'caaat'`` (3 ``'a'`` characters), and so forth." msgstr "" +"例如,「ca*t」會符合 「ct」(0個「a」字元)、「cat」(1個「a」字元)、 " +"「caaat」(3 個「a」字元)等。" #: ../../howto/regex.rst:178 +#, fuzzy msgid "" "Repetitions such as ``*`` are :dfn:`greedy`; when repeating a RE, the " "matching engine will try to repeat it as many times as possible. If later " "portions of the pattern don't match, the matching engine will then back up " "and try again with fewer repetitions." msgstr "" +"重複使用如 ``*`` 的正規表示式(RE)稱為 :dfn:`貪婪`,即當 RE 重複時,匹配引擎" +"會試圖盡可能多次地重複。 如果模式的後續部分不符合,則匹配引擎將往回跳並嘗試減" +"少重複次數。" #: ../../howto/regex.rst:183 +#, fuzzy msgid "" "A step-by-step example will make this more obvious. Let's consider the " "expression ``a[bcd]*b``. This matches the letter ``'a'``, zero or more " "letters from the class ``[bcd]``, and finally ends with a ``'b'``. Now " "imagine matching this RE against the string ``'abcbd'``." msgstr "" +"一個逐步解釋的範例會更加清晰。讓我們考慮表達式``a[bcd]*b``,它可以匹配字母 " +"'a',來自類別 ``[bcd]`` 中的零或多個字母,最後以 'b' 結尾。現在想像一下將此正" +"規表示式與字符串 \"abcbd\" 進行匹配的情況。" #: ../../howto/regex.rst:189 +#, fuzzy msgid "Step" -msgstr "" +msgstr "您好,以下是翻譯結果:步驟" #: ../../howto/regex.rst:189 +#, fuzzy msgid "Matched" -msgstr "" +msgstr "符合" #: ../../howto/regex.rst:189 +#, fuzzy msgid "Explanation" -msgstr "" +msgstr "解釋" #: ../../howto/regex.rst:191 msgid "1" @@ -338,8 +434,9 @@ msgid "``a``" msgstr "``a``" #: ../../howto/regex.rst:191 +#, fuzzy msgid "The ``a`` in the RE matches." -msgstr "" +msgstr "RE 中的 ``a`` 符合。" #: ../../howto/regex.rst:193 msgid "2" @@ -350,24 +447,27 @@ msgid "``abcbd``" msgstr "``abcbd``" #: ../../howto/regex.rst:193 +#, fuzzy msgid "" "The engine matches ``[bcd]*``, going as far as it can, which is to the end " "of the string." -msgstr "" +msgstr "引擎會以盡可能長的方式比對``[bcd]*``,一直到字串的結束。" #: ../../howto/regex.rst:197 msgid "3" msgstr "3" #: ../../howto/regex.rst:197 ../../howto/regex.rst:205 +#, fuzzy msgid "*Failure*" -msgstr "" +msgstr "*失敗*" #: ../../howto/regex.rst:197 +#, fuzzy msgid "" "The engine tries to match ``b``, but the current position is at the end of " "the string, so it fails." -msgstr "" +msgstr "引擎試圖配對「b」,但目前位置在字串結束處,因此失敗。" #: ../../howto/regex.rst:202 msgid "4" @@ -378,18 +478,21 @@ msgid "``abcb``" msgstr "``abcb``" #: ../../howto/regex.rst:202 +#, fuzzy msgid "Back up, so that ``[bcd]*`` matches one less character." -msgstr "" +msgstr "倒退一個字符,這樣 ``[bcd]*`` 能夠少匹配一個字元。" #: ../../howto/regex.rst:205 msgid "5" msgstr "5" #: ../../howto/regex.rst:205 +#, fuzzy msgid "" "Try ``b`` again, but the current position is at the last character, which is " "a ``'d'``." msgstr "" +"嘗試再次執行 ``b``,但目前的位置已經在最後一個字元上,而這個字元是一個「d」。" #: ../../howto/regex.rst:209 ../../howto/regex.rst:213 msgid "6" @@ -400,16 +503,19 @@ msgid "``abc``" msgstr "``abc``" #: ../../howto/regex.rst:209 +#, fuzzy msgid "Back up again, so that ``[bcd]*`` is only matching ``bc``." -msgstr "" +msgstr "再次進行備份,這樣 ``[bcd]*`` 將只匹配到 ``bc``。" #: ../../howto/regex.rst:213 +#, fuzzy msgid "" "Try ``b`` again. This time the character at the current position is " "``'b'``, so it succeeds." -msgstr "" +msgstr "請再試一次 ``b``。這次目前位置的字元是``'b'``,所以它成功了" #: ../../howto/regex.rst:219 +#, fuzzy msgid "" "The end of the RE has now been reached, and it has matched ``'abcb'``. This " "demonstrates how the matching engine goes as far as it can at first, and if " @@ -418,8 +524,13 @@ msgid "" "``[bcd]*``, and if that subsequently fails, the engine will conclude that " "the string doesn't match the RE at all." msgstr "" +"RE(正規表示式)已經到達結尾,並匹配了「abcb」。這演示了匹配引擎一開始盡可能" +"地前行,如果沒有找到任何匹配項目則會回溯並不斷再試驗 RE 其餘部分。它會一直倒" +"退,直到都嘗試完「[bcd]*」的零次數匹配;如果之後仍然失敗,引擎就會得出結論:" +"這個字串根本不符合這個 RE。" #: ../../howto/regex.rst:226 +#, fuzzy msgid "" "Another repeating metacharacter is ``+``, which matches one or more times. " "Pay careful attention to the difference between ``*`` and ``+``; ``*`` " @@ -428,16 +539,25 @@ msgid "" "similar example, ``ca+t`` will match ``'cat'`` (1 ``'a'``), ``'caaat'`` (3 " "``'a'``\\ s), but won't match ``'ct'``." msgstr "" +"另一個重複元字符是「+」,它可配對出現一或多次。要特別留意「*」和「+」之間的差" +"異。「*」可以匹配*零個或多個次數*,所以被重複配對的字元可能完全不會出現;相反" +"地,用了「+」就必須至少出現 * 一次。舉例來說,像 ``ca+t`` 就能配對到 " +"``'cat'``(其中只有1個\"a\")、``'caaat'``(其中有3 個 \"a\"),但無法匹配到 " +"``'ct'``。" #: ../../howto/regex.rst:233 +#, fuzzy msgid "" "There are two more repeating operators or quantifiers. The question mark " "character, ``?``, matches either once or zero times; you can think of it as " "marking something as being optional. For example, ``home-?brew`` matches " "either ``'homebrew'`` or ``'home-brew'``." msgstr "" +"有兩個更多的重複運算子或量詞。問號字元「?」會匹配一次或零次;你可以將它視為可" +"選項目的標記。例如,「home-?brew」匹配「'homebrew'」或者是「'home-brew'」。" #: ../../howto/regex.rst:238 +#, fuzzy msgid "" "The most complicated quantifier is ``{m,n}``, where *m* and *n* are decimal " "integers. This quantifier means there must be at least *m* repetitions, and " @@ -445,13 +565,20 @@ msgid "" "and ``'a///b'``. It won't match ``'ab'``, which has no slashes, or ``'a////" "b'``, which has four." msgstr "" +"最複雜的量詞是 ``{m,n}``,其中 *m* 和 *n* 為十進制整數。 這個量詞意味著必須至" +"少重複 *m* 次,最多為 *n*。例如,``a/{1,3}b`` 將匹配 ``'a/b'``, ``'a//b'`` " +"和 ``'a///b'``。它不會匹配沒有斜杠的 ``'ab'` `,也不會匹配有四個斜杠的 `,' " +"a//// b '`." #: ../../howto/regex.rst:244 +#, fuzzy msgid "" "You can omit either *m* or *n*; in that case, a reasonable value is assumed " "for the missing value. Omitting *m* is interpreted as a lower limit of 0, " "while omitting *n* results in an upper bound of infinity." msgstr "" +"你可以省略 *m* 或 *n* 的其中一項,這種情況下將會自動指定一個合理的值。如果省" +"略 *m* 則被解釋為最小值是 0,而如果忽略了 `n` 就視為上限無窮大。" #: ../../howto/regex.rst:248 msgid "" @@ -467,38 +594,54 @@ msgid "" "better to use ``*``, ``+``, or ``?`` when you can, simply because they're " "shorter and easier to read." msgstr "" +"若有節制觀念的讀者可能會注意到,其餘三種量詞都可以用這種表示法表達。``{0,}`` " +"同於 ``*``, ``{1,}`` 相當於 ``+``, 而 ``{0,1}`` 與 ``?`` 相同。如果可以,建議" +"使用簡潔易讀的 `*`, `+`, 或 `?`" #: ../../howto/regex.rst:259 +#, fuzzy msgid "Using Regular Expressions" -msgstr "" +msgstr "使用正規表示式" #: ../../howto/regex.rst:261 +#, fuzzy msgid "" "Now that we've looked at some simple regular expressions, how do we actually " "use them in Python? The :mod:`re` module provides an interface to the " "regular expression engine, allowing you to compile REs into objects and then " "perform matches with them." msgstr "" +"現在我們已經看過了一些簡單的正規表示式,那麼我們該如何在Python中實際使用它們" +"呢? :mod:`re`模組提供了與正規表示引擎的介面,允許您將 RE 編譯成物件,然後使" +"用它們執行匹配。" #: ../../howto/regex.rst:268 +#, fuzzy msgid "Compiling Regular Expressions" -msgstr "" +msgstr "編譯正規表示式 (Regular Expression)" #: ../../howto/regex.rst:270 +#, fuzzy msgid "" "Regular expressions are compiled into pattern objects, which have methods " "for various operations such as searching for pattern matches or performing " "string substitutions. ::" msgstr "" +"正規表示式 (regular expression) 會被編譯成模式物件(pattern object),這些物件" +"擁有各種方法可以進行多種操作,例如尋找符合該模式的字串或執行字串替換。 ::" #: ../../howto/regex.rst:279 +#, fuzzy msgid "" ":func:`re.compile` also accepts an optional *flags* argument, used to enable " "various special features and syntax variations. We'll go over the available " "settings later, but for now a single example will do::" msgstr "" +":func:`re.compile` 函式也可以接受一個選擇性的 *flags* 引數,用於啟用各種特殊" +"功能和語法變化。我們稍後將介紹可用的設置,但現在單獨舉一個例子即可:" #: ../../howto/regex.rst:285 +#, fuzzy msgid "" "The RE is passed to :func:`re.compile` as a string. REs are handled as " "strings because regular expressions aren't part of the core Python language, " @@ -508,26 +651,37 @@ msgid "" "simply a C extension module included with Python, just like the :mod:" "`socket` or :mod:`zlib` modules." msgstr "" +"正規表示式(RE)被當作字串傳遞給 :func:`re.compile`。因為正規表示式並不是" +"Python核心語言的一部分,也沒有特殊的語法來表達它們。有些應用程式根本不需要使" +"用正規表示式,所以無需將其包含在語言规範中。相反地,:mod:`re` 模塊只是 " +"Python 的 C 擴展模塊之一,就像 :mod:`socket` 或 :mod:`zlib` 模块一樣。" #: ../../howto/regex.rst:292 +#, fuzzy msgid "" "Putting REs in strings keeps the Python language simpler, but has one " "disadvantage which is the topic of the next section." msgstr "" +"將正規表示式放入字串中可使 Python 語言保持簡潔,但有一個缺點──下一節的主題。" #: ../../howto/regex.rst:299 +#, fuzzy msgid "The Backslash Plague" -msgstr "" +msgstr "反斜線之患" #: ../../howto/regex.rst:301 +#, fuzzy msgid "" "As stated earlier, regular expressions use the backslash character " "(``'\\'``) to indicate special forms or to allow special characters to be " "used without invoking their special meaning. This conflicts with Python's " "usage of the same character for the same purpose in string literals." msgstr "" +"如前所述,正規表示式使用反斜線字元(``'\\'``)來指示特殊形式或允許使用特殊字" +"符而不引起其特殊意義。這與Python在字符串常量中使用相同字符的目的產生了衝突。" #: ../../howto/regex.rst:306 +#, fuzzy msgid "" "Let's say you want to write a RE that matches the string ``\\section``, " "which might be found in a LaTeX file. To figure out what to write in the " @@ -538,40 +692,57 @@ msgid "" "to express this as a Python string literal, both backslashes must be escaped " "*again*." msgstr "" +"假設你要寫一個正規表示式,以匹配 LaTeX 文件中可能出現的字串 ``\\section`` 。" +"為了找出在程序代碼中應該怎麼寫,必須從需要匹配的字串開始。接下來,您必須使用" +"反斜線來轉義任何反斜線和其他元字符 ,這導致輸入 \\\\section 字符串。最後傳遞" +"給 :func:`re.compile` 的字符串必須是 '\\\\\\\\section'。但是,在 Python 字符" +"串文字中表達此值時,兩個反斜杠 *再次* 必須被轉義。" #: ../../howto/regex.rst:315 +#, fuzzy msgid "Characters" msgstr "" +":這是一個測試文本。請確保你的翻譯遵守了所有規範,包括使用全形標點符號、插入" +"空白以及保留專有名詞等。此外,要記得適時加上括號注釋或直接保留英文原文,並且" +"不超過79字元寬度。謝謝您的合作!" #: ../../howto/regex.rst:315 +#, fuzzy msgid "Stage" msgstr "" +"\"Stage\" 可以根據不同的情境有不同的中文翻譯,以下提供幾種可能:- 舞台- 階" +"段- 演出場次- (賽車等)比賽階段或路段如果能給予更多上下文的訊息,我可以針對" +"情境作出更准確的翻譯。" #: ../../howto/regex.rst:317 msgid "``\\section``" msgstr "``\\section``" #: ../../howto/regex.rst:317 +#, fuzzy msgid "Text string to be matched" -msgstr "" +msgstr "要比對的文字串" #: ../../howto/regex.rst:319 msgid "``\\\\section``" msgstr "``\\\\section``" #: ../../howto/regex.rst:319 +#, fuzzy msgid "Escaped backslash for :func:`re.compile`" -msgstr "" +msgstr "在 :func:`re.compile` 中使用反斜線需加上跳脫符號" #: ../../howto/regex.rst:321 ../../howto/regex.rst:348 msgid "``\"\\\\\\\\section\"``" msgstr "``\"\\\\\\\\section\"``" #: ../../howto/regex.rst:321 +#, fuzzy msgid "Escaped backslashes for a string literal" -msgstr "" +msgstr "對於字串常數請使用跳脫字元反斜線。" #: ../../howto/regex.rst:324 +#, fuzzy msgid "" "In short, to match a literal backslash, one has to write ``'\\\\\\\\'`` as " "the RE string, because the regular expression must be ``\\\\``, and each " @@ -579,8 +750,13 @@ msgid "" "literal. In REs that feature backslashes repeatedly, this leads to lots of " "repeated backslashes and makes the resulting strings difficult to understand." msgstr "" +"簡而言之,若要匹配反斜線字元本身,在正規表達式中要寫成 ``'\\\\\\\\'``。這是因" +"為正規表達式必須寫成 ``\\\\``,而每個反斜線又需要以在 Python 字串常值內表示的" +"方式寫成 ``\\\\``,所以在多次出現反斜線的正規表達式中會有很多重複使用的反斜" +"線,讓結果字串難以理解。" #: ../../howto/regex.rst:330 +#, fuzzy msgid "" "The solution is to use Python's raw string notation for regular expressions; " "backslashes are not handled in any special way in a string literal prefixed " @@ -589,8 +765,13 @@ msgid "" "newline. Regular expressions will often be written in Python code using this " "raw string notation." msgstr "" +"解決方法是使用 Python 的原始字串記號來表達正規表示式;在以 ``'r'`` 為前綴的字" +"串文字中,反斜線不會有任何特別的處理方式,因此 ``r\"\\n\"`` 是一個包含兩個字" +"符 ' \\' 和 ' n ' 的字串, 而 ``\"\\n\"`` 則是一個包含換行符的單個字符。在撰" +"寫 Python 代碼時,通常會採用這種原始字符串形式來撰寫正規表示式。" #: ../../howto/regex.rst:336 +#, fuzzy msgid "" "In addition, special escape sequences that are valid in regular expressions, " "but not valid as Python string literals, now result in a :exc:" @@ -598,14 +779,19 @@ msgid "" "means the sequences will be invalid if raw string notation or escaping the " "backslashes isn't used." msgstr "" +"此外,現在正規表示式中有效但不是 Python 字串實體所用的特殊 Escape sequence," +"會產生 :exc:`DeprecationWarning` 警告且最終可能會變成 :exc:`SyntaxError`, 也" +"就是說如果沒有使用 raw string 標記或反斜線進行字符轉義,該序列將無效。" #: ../../howto/regex.rst:344 +#, fuzzy msgid "Regular String" -msgstr "" +msgstr "正規字串" #: ../../howto/regex.rst:344 +#, fuzzy msgid "Raw string" -msgstr "" +msgstr "原始字串" #: ../../howto/regex.rst:346 msgid "``\"ab*\"``" @@ -628,82 +814,106 @@ msgid "``r\"\\w+\\s+\\1\"``" msgstr "``r\"\\w+\\s+\\1\"``" #: ../../howto/regex.rst:355 +#, fuzzy msgid "Performing Matches" -msgstr "" +msgstr "進行比對" #: ../../howto/regex.rst:357 +#, fuzzy msgid "" "Once you have an object representing a compiled regular expression, what do " "you do with it? Pattern objects have several methods and attributes. Only " "the most significant ones will be covered here; consult the :mod:`re` docs " "for a complete listing." msgstr "" +"翻譯: 編譯正規表示式後,您需要對其進行操作。模式物件有多個方法和屬性,這裡" +"只介紹最重要的幾個;請參考 :mod:`re` 文件以獲取完整列表。" #: ../../howto/regex.rst:363 ../../howto/regex.rst:417 #: ../../howto/regex.rst:1065 +#, fuzzy msgid "Method/Attribute" -msgstr "" +msgstr "方法/屬性" #: ../../howto/regex.rst:363 ../../howto/regex.rst:417 #: ../../howto/regex.rst:1065 +#, fuzzy msgid "Purpose" -msgstr "" +msgstr "目的" #: ../../howto/regex.rst:365 msgid "``match()``" msgstr "``match()``" #: ../../howto/regex.rst:365 +#, fuzzy msgid "Determine if the RE matches at the beginning of the string." -msgstr "" +msgstr "判斷正規表示式是否在字串開頭符合。" #: ../../howto/regex.rst:368 msgid "``search()``" msgstr "``search()``" #: ../../howto/regex.rst:368 +#, fuzzy msgid "Scan through a string, looking for any location where this RE matches." -msgstr "" +msgstr "掃描字串,尋找任何符合正規表示式的位置。" #: ../../howto/regex.rst:371 msgid "``findall()``" msgstr "``findall()``" #: ../../howto/regex.rst:371 +#, fuzzy msgid "Find all substrings where the RE matches, and returns them as a list." -msgstr "" +msgstr "尋找所有符合正規表示式的子字串,並將其以清單形式返回。" #: ../../howto/regex.rst:374 msgid "``finditer()``" msgstr "``finditer()``" #: ../../howto/regex.rst:374 +#, fuzzy msgid "" "Find all substrings where the RE matches, and returns them as an :term:" "`iterator`." -msgstr "" +msgstr "尋找所有符合正規表示式的子字串,並以 :term:`iterator` 形式回傳它們。" #: ../../howto/regex.rst:378 +#, fuzzy msgid "" ":meth:`~re.Pattern.match` and :meth:`~re.Pattern.search` return ``None`` if " "no match can be found. If they're successful, a :ref:`match object ` instance is returned, containing information about the match: " "where it starts and ends, the substring it matched, and more." msgstr "" +":meth:`~re.Pattern.match` 和 :meth:`~re.Pattern.search` 如果沒有找到符合的條" +"件,會傳回 ``None``。如果成功了,將返回一個 :ref:`match object ` 物件實例,包含有關匹配的資訊: 匹配開始和結束位置、它所匹配的子字串" +"等等。" #: ../../howto/regex.rst:383 +#, fuzzy msgid "" "You can learn about this by interactively experimenting with the :mod:`re` " "module." msgstr "" +"你可以透過互動式的方式在 `re` 模組中實驗學習。如果你有可用的 `tkinter`,也可" +"以參考 Python 發行版附帶的演示程式 :source:`Tools/demo/redemo.py` 。它允許你" +"輸入正規表示式以及字串,並顯示正規表示式是否符合或失敗。當嘗試除錯複雜的正則" +"表達式時,:file:`redemo.py` 可能非常實用。" #: ../../howto/regex.rst:386 +#, fuzzy msgid "" "This HOWTO uses the standard Python interpreter for its examples. First, run " "the Python interpreter, import the :mod:`re` module, and compile a RE::" msgstr "" +"這份 HOWTO 使用Python標準解譯器進行範例演示。首先,啟動Python解譯器,匯入 " +"``re`` 模組,並編譯一個正規表示式 (RE):" #: ../../howto/regex.rst:394 +#, fuzzy msgid "" "Now, you can try matching various strings against the RE ``[a-z]+``. An " "empty string shouldn't match at all, since ``+`` means 'one or more " @@ -711,58 +921,77 @@ msgid "" "which will cause the interpreter to print no output. You can explicitly " "print the result of :meth:`!match` to make this clear. ::" msgstr "" +"現在,你可以嘗試將各種字串與正規表示式 ``[a-z]+`` 進行匹配。由於 ``+`` 的含義" +"是「出現一次或多次」,因此空字串應該根本沒有匹配。這種情況下,:meth:`~re." +"Pattern.match` 應返回 ``None`` ,進解譯器不會輸出任何內容。您可以明確地打印 :" +"meth:`!match` 的結果以使其清楚明白。.. code-block:: python import re " +"pattern = re.compile(r'[a-z]+') match_object = pattern.match('') # The " +"output of `print(match_object)` is None" #: ../../howto/regex.rst:404 +#, fuzzy msgid "" "Now, let's try it on a string that it should match, such as ``tempo``. In " "this case, :meth:`~re.Pattern.match` will return a :ref:`match object `, so you should store the result in a variable for later use. ::" msgstr "" +"現在讓我們把它應用到一個應該符合的字串,例如 \"tempo\"。在這種情況下, :meth:" +"`~re.Pattern.match` 會回傳一個 :ref:`match object `,所以您應" +"當將結果存入變數中以供後續使用。::" #: ../../howto/regex.rst:412 +#, fuzzy msgid "" "Now you can query the :ref:`match object ` for information " "about the matching string. Match object instances also have several methods " "and attributes; the most important ones are:" msgstr "" +"現在你可以查詢 :ref:`match object ` 來獲取有關匹配字串的資訊," +"而且 Match object 實例還具有多種方法和屬性;其中最重要的是:" #: ../../howto/regex.rst:419 msgid "``group()``" msgstr "``group()``" #: ../../howto/regex.rst:419 +#, fuzzy msgid "Return the string matched by the RE" -msgstr "" +msgstr "回傳正規表示式 (RE) 所匹配的字串。" #: ../../howto/regex.rst:421 msgid "``start()``" msgstr "``start()``" #: ../../howto/regex.rst:421 +#, fuzzy msgid "Return the starting position of the match" -msgstr "" +msgstr "傳回符合項目的起始位置" #: ../../howto/regex.rst:423 msgid "``end()``" msgstr "``end()``" #: ../../howto/regex.rst:423 +#, fuzzy msgid "Return the ending position of the match" -msgstr "" +msgstr "傳回匹配項目結束的位置" #: ../../howto/regex.rst:425 msgid "``span()``" msgstr "``span()``" #: ../../howto/regex.rst:425 +#, fuzzy msgid "Return a tuple containing the (start, end) positions of the match" -msgstr "" +msgstr "回傳一個包含匹配位置 (起始,結束) 的元組" #: ../../howto/regex.rst:429 +#, fuzzy msgid "Trying these methods will soon clarify their meaning::" -msgstr "" +msgstr "嘗試這些方法很快就會澄清它們的意思:" #: ../../howto/regex.rst:438 +#, fuzzy msgid "" ":meth:`~re.Match.group` returns the substring that was matched by the RE. :" "meth:`~re.Match.start` and :meth:`~re.Match.end` return the starting and " @@ -773,21 +1002,34 @@ msgid "" "scans through the string, so the match may not start at zero in that " "case. ::" msgstr "" +":meth:`~re.Match.group` 方法會返回由正則表達式匹配的子字串。 :meth:`~re." +"Match.start` 和 :meth:`~re.Match.end` 方法分別返回匹配開始和結束的索引,而 :" +"meth:`~re.Match.span` 會一次性返回起始和結束位置。 因為:meth:`~re.Pattern." +"match`方法只是檢查 RE 是否從字符串開頭進行匹配,所以:meth:`!start` 總是等於" +"零。 但是,在模式使用的:meth:`~re.Pattern.search`方法中,掃描整個字符串時匹配" +"可能不從零位置開始。 ::" #: ../../howto/regex.rst:455 +#, fuzzy msgid "" "In actual programs, the most common style is to store the :ref:`match object " "` in a variable, and then check if it was ``None``. This " "usually looks like::" msgstr "" +"在實際的程式中,最常見的樣式是將 :ref:`match object ` 存儲在一" +"個變量中,然後檢查它是否為 ``None``。通常看起來像這樣:" #: ../../howto/regex.rst:466 +#, fuzzy msgid "" "Two pattern methods return all of the matches for a pattern. :meth:`~re." "Pattern.findall` returns a list of matching strings::" msgstr "" +"兩種模式方法可返回符合樣式的所有結果。 :meth:`~re.Pattern.findall` 返回匹配字" +"串列表:" #: ../../howto/regex.rst:473 +#, fuzzy msgid "" "The ``r`` prefix, making the literal a raw string literal, is needed in this " "example because escape sequences in a normal \"cooked\" string literal that " @@ -795,20 +1037,30 @@ msgid "" "in a :exc:`DeprecationWarning` and will eventually become a :exc:" "`SyntaxError`. See :ref:`the-backslash-plague`." msgstr "" +"在此例子中,必須使用``r``前綴來將字面值變成一個原始字串文字(r raw string " +"literal),因為在正常的 \"cooked\" 字串文字中轉義字元如果不被Python識別(相較" +"於正則表達式),現在會產生 :exc:`DeprecationWarning` 錯誤訊息並最終成為 :exc:" +"`SyntaxError`. 觀看 :ref:`the-backslash-plague`." #: ../../howto/regex.rst:479 +#, fuzzy msgid "" ":meth:`~re.Pattern.findall` has to create the entire list before it can be " "returned as the result. The :meth:`~re.Pattern.finditer` method returns a " "sequence of :ref:`match object ` instances as an :term:" "`iterator`::" msgstr "" +":meth:`~re.Pattern.findall` 方法必須在返回結果之前先創建整個列表。 :meth:" +"`~re.Pattern.finditer`方法以迭代器的形式返回一系列:ref:`match object `實例:" #: ../../howto/regex.rst:495 +#, fuzzy msgid "Module-Level Functions" -msgstr "" +msgstr "模組級函式" #: ../../howto/regex.rst:497 +#, fuzzy msgid "" "You don't have to create a pattern object and call its methods; the :mod:" "`re` module also provides top-level functions called :func:`~re.match`, :" @@ -817,28 +1069,40 @@ msgid "" "with the RE string added as the first argument, and still return either " "``None`` or a :ref:`match object ` instance. ::" msgstr "" +"你不必創建一個模式對象並調用其方法;相應的 :mod:`re` 模塊還提供了頂級函數," +"如::func:`~re.match`, :func:`~re.search`, :func:`~re.findall`, :func:`~re." +"sub`。這些函數與相應的模式方法具有相同的引數(以 RE 字串作為第一個引數),仍" +"然返回 ``None`` 或者是匹配對象 (:ref:`match-objects`) 實例。 ::" #: ../../howto/regex.rst:509 +#, fuzzy msgid "" "Under the hood, these functions simply create a pattern object for you and " "call the appropriate method on it. They also store the compiled object in a " "cache, so future calls using the same RE won't need to parse the pattern " "again and again." msgstr "" +"在幕後,這些函式只是為您建立一個模式物件,並調用其適當的方法。它們還會將已編" +"譯的物件儲存在快取中,因此未來使用相同的正規表示式時不需要再次解析模式。" #: ../../howto/regex.rst:514 +#, fuzzy msgid "" "Should you use these module-level functions, or should you get the pattern " "and call its methods yourself? If you're accessing a regex within a loop, " "pre-compiling it will save a few function calls. Outside of loops, there's " "not much difference thanks to the internal cache." msgstr "" +"如果您正在迴圈中存取正規表示式,事前編譯可以減少一些函數呼叫。在迴圈外部由於" +"內部快取的原因,使用這些模組級別函數和直接調用它的方法沒有太大區別。" #: ../../howto/regex.rst:522 +#, fuzzy msgid "Compilation Flags" -msgstr "" +msgstr "編譯標誌" #: ../../howto/regex.rst:526 +#, fuzzy msgid "" "Compilation flags let you modify some aspects of how regular expressions " "work. Flags are available in the :mod:`re` module under two names, a long " @@ -849,73 +1113,91 @@ msgid "" "them; ``re.I | re.M`` sets both the :const:`I` and :const:`M` flags, for " "example." msgstr "" +"編譯旗標可以讓你修改常規表達式的某些功能。在 :mod:`re` 模組中提供了兩個名稱形" +"式:一個是長名稱,如 :const:`IGNORECASE`,另一個是短名稱,如 :const:`I`(如果" +"您熟悉Perl的模式修飾符,在Python裡也有相同的字母;例如: re.VERBOSE 的簡写是 " +"「re.X」)。可以通過按位 OR () 來指定多个旗帜; 例如,``re.I | re.M`` 同時設置" +"了 :const:`I ` 和: const`:M ` 旗標" #: ../../howto/regex.rst:534 +#, fuzzy msgid "" "Here's a table of the available flags, followed by a more detailed " "explanation of each one." -msgstr "" +msgstr "下表列出了可用的標誌,接下來是每個標誌的更詳細說明。" #: ../../howto/regex.rst:538 +#, fuzzy msgid "Flag" -msgstr "" +msgstr "旗標" #: ../../howto/regex.rst:538 +#, fuzzy msgid "Meaning" -msgstr "" +msgstr "意思,含義" #: ../../howto/regex.rst:540 msgid ":const:`ASCII`, :const:`A`" msgstr ":const:`ASCII`, :const:`A`" #: ../../howto/regex.rst:540 +#, fuzzy msgid "" "Makes several escapes like ``\\w``, ``\\b``, ``\\s`` and ``\\d`` match only " "on ASCII characters with the respective property." msgstr "" +"會使``\\w``、``\\b``、``\\s`` 和 ``\\d``這幾種跳脫字元,只能匹配ASCII字符上該" +"屬性的對應值。" #: ../../howto/regex.rst:544 msgid ":const:`DOTALL`, :const:`S`" msgstr ":const:`DOTALL`, :const:`S`" #: ../../howto/regex.rst:544 +#, fuzzy msgid "Make ``.`` match any character, including newlines." -msgstr "" +msgstr "讓「.」匹配任何字元,包括換行符號。" #: ../../howto/regex.rst:547 msgid ":const:`IGNORECASE`, :const:`I`" msgstr ":const:`IGNORECASE`, :const:`I`" #: ../../howto/regex.rst:547 +#, fuzzy msgid "Do case-insensitive matches." -msgstr "" +msgstr "進行不分大小寫的比對。" #: ../../howto/regex.rst:549 msgid ":const:`LOCALE`, :const:`L`" msgstr ":const:`LOCALE`, :const:`L`" #: ../../howto/regex.rst:549 +#, fuzzy msgid "Do a locale-aware match." -msgstr "" +msgstr "進行區域敏感字串比對。" #: ../../howto/regex.rst:551 msgid ":const:`MULTILINE`, :const:`M`" msgstr ":const:`MULTILINE`, :const:`M`" #: ../../howto/regex.rst:551 +#, fuzzy msgid "Multi-line matching, affecting ``^`` and ``$``." -msgstr "" +msgstr "多行匹配,會影響到 ``^`` 和 ``$``。" #: ../../howto/regex.rst:554 +#, fuzzy msgid ":const:`VERBOSE`, :const:`X` (for 'extended')" -msgstr "" +msgstr "`:const:`VERBOSE``, ``:const:`X``(表示「擴展」)" #: ../../howto/regex.rst:554 +#, fuzzy msgid "" "Enable verbose REs, which can be organized more cleanly and understandably." -msgstr "" +msgstr "啟用語意清晰且易於整理的冗長正規表示式(verbose REs)。" #: ../../howto/regex.rst:563 +#, fuzzy msgid "" "Perform case-insensitive matching; character class and literal strings will " "match letters by ignoring case. For example, ``[A-Z]`` will match lowercase " @@ -930,14 +1212,25 @@ msgid "" "lowercasing doesn't take the current locale into account; it will if you " "also set the :const:`LOCALE` flag." msgstr "" +"執行不分大小寫比對;字元類別和直接字串會忽略大小寫來比對,舉例來說,``[A-Z]``" +"可以找到小寫字母。如果沒使用 :const:`ASCII` 旗標禁用非 ASCII 匹配的話,完整" +"的 Unicode 比對也是有效的。而當 Unicode 的模式 ``[a-z]`` 或 ``[A-Z]`` 與 :" +"const:`IGNORECASE` 旗標一起使用時,它們會匹配52個 ASCII 字母以及另外4個非 " +"ASCII 字母: 'İ' (U+0130, 土耳其大寫點I),'ı' (U+0131, 土耳其小寫無點" +"i),'ſ' (U+017F, Latin small letter long s) 和 'K' (U+212A, Kelvin " +"sign)。\"Spam\" 可以被匹配為 ``'Spam'``, ``" #: ../../howto/regex.rst:581 +#, fuzzy msgid "" "Make ``\\w``, ``\\W``, ``\\b``, ``\\B`` and case-insensitive matching " "dependent on the current locale instead of the Unicode database." msgstr "" +"將 `\\w`、`\\W`、`\\b`、`\\B` 和不區分大小寫的比對方式改為依賴於當前語系" +"(local)而非 Unicode 資料庫。" #: ../../howto/regex.rst:584 +#, fuzzy msgid "" "Locales are a feature of the C library intended to help in writing programs " "that take account of language differences. For example, if you're " @@ -955,14 +1248,26 @@ msgid "" "matching is already enabled by default in Python 3 for Unicode (str) " "patterns, and it is able to handle different locales/languages." msgstr "" +"locale 是 C 函式庫的一個特色,旨在幫助撰寫具有考慮語言差異的程式。例如,如果" +"要處理編碼成法文的文字,您可以使用 ``\\w+`` 來符合單字,但是在位元組(bytes)" +"模式中只會符合字符類 [A-Za-z] 的 ``\\w``;它不會匹配對應於 é 或 ç 的位元組。" +"如果你的系統已正確配置並選定了法國區域設定(locale),某些 C 函數將告訴程序应将" +"对于é相应的字节也视为字母。建議在編譯正則表示時设置 :const:`LOCALE` 標志這将" +"使得所生成編譯物件使用这些 C 函数来处理 ``\\w``;雖然較慢但能如預期般支持用於" +"匹配法文單詞之錶達方式: \\w+。Python3 不鼓勵使用此标识, 因为区域机制非常不可" +"靠,而且一次仅处理一个“文化”,并且仅适用于8位本地语环境. Unicode 照常 (默认) " +"支援 Python3 序列(str)形式供Unicode使用, 能夠處理不同的Locale/ Language。" #: ../../howto/regex.rst:606 +#, fuzzy msgid "" "(``^`` and ``$`` haven't been explained yet; they'll be introduced in " "section :ref:`more-metacharacters`.)" msgstr "" +"(``^`` 和 ``$`` 尚未解釋,接下來將在 :ref:`more-metacharacters` 章節中介紹。)" #: ../../howto/regex.rst:609 +#, fuzzy msgid "" "Usually ``^`` matches only at the beginning of the string, and ``$`` matches " "only at the end of the string and immediately before the newline (if any) at " @@ -972,21 +1277,33 @@ msgid "" "matches either at the end of the string and at the end of each line " "(immediately preceding each newline)." msgstr "" +"通常 ``^`` 會只比對字串一開始出現的位置,而 ``$`` 則會只比對字串結束和行末 " +"(如果有) 的位置。 若指定此旗標時, ``^`` 不僅僅會比對字串開頭,也會在每個換行" +"符號之後立即與下一行的內容做比對。同理地,元字符 ``$`` 已可匹配到字符串或每行" +"(BOL 或 EOL) 的结尾(EOL前一个任意且必须是换行符)。" #: ../../howto/regex.rst:622 +#, fuzzy msgid "" "Makes the ``'.'`` special character match any character at all, including a " "newline; without this flag, ``'.'`` will match anything *except* a newline." msgstr "" +"開啟 ``'.'`` 特殊字元,可匹配任何字符,包括換行;否則,``'.'`` 會匹配除了換行" +"之外的其 它所有字符。" #: ../../howto/regex.rst:630 +#, fuzzy msgid "" "Make ``\\w``, ``\\W``, ``\\b``, ``\\B``, ``\\s`` and ``\\S`` perform ASCII-" "only matching instead of full Unicode matching. This is only meaningful for " "Unicode patterns, and is ignored for byte patterns." msgstr "" +"讓 ``\\w``, ``\\W``, ``\\b``, ``\\B``, ``\\s`` 和 ``\\S`` 僅進行 ASCII 字元的" +"比對,而非全字串 Unicode 範圍比對。此設置只在使用 Unicode 的情況下有效,且不" +"用於位元組型別式。" #: ../../howto/regex.rst:639 +#, fuzzy msgid "" "This flag allows you to write regular expressions that are more readable by " "granting you more flexibility in how you can format them. When this flag " @@ -997,46 +1314,65 @@ msgid "" "comments are marked by a ``'#'`` that's neither in a character class or " "preceded by an unescaped backslash." msgstr "" +"這個旗標讓您更自由地編排和縮排正規表示式,使其更易讀。當設定此旗標後,除非空" +"格在字元類別(class)中或是沒有轉移符(escape character)時否則都會被忽略。這能夠" +"幫助您更清晰地整理和縮進正規表示式的內容。同時,它也允許您在 RE 中添加被引擎" +"忽略的註解;只要以 ``'#'`` 開頭即可(不包括那些位於字元類別或前面已脫逸過" +"的)。" #: ../../howto/regex.rst:648 +#, fuzzy msgid "" "For example, here's a RE that uses :const:`re.VERBOSE`; see how much easier " "it is to read? ::" msgstr "" +"舉例來說,這是一個使用 :const:`re.VERBOSE` 的正規表示式;看起來容易閱讀許多了" +"吧?::" #: ../../howto/regex.rst:661 +#, fuzzy msgid "Without the verbose setting, the RE would look like this::" -msgstr "" +msgstr "如果不啟用冗長模式,正規表示式會長這樣::" #: ../../howto/regex.rst:667 +#, fuzzy msgid "" "In the above example, Python's automatic concatenation of string literals " "has been used to break up the RE into smaller pieces, but it's still more " "difficult to understand than the version using :const:`re.VERBOSE`." msgstr "" +"在上述例子中,Python 的自動字串連接功能被用來將正規表示式 (RE) 拆成較小的片" +"段,但仍比使用 :const:`re.VERBOSE` 更難理解。" #: ../../howto/regex.rst:673 +#, fuzzy msgid "More Pattern Power" -msgstr "" +msgstr "更強大的模式綜合利用" #: ../../howto/regex.rst:675 +#, fuzzy msgid "" "So far we've only covered a part of the features of regular expressions. In " "this section, we'll cover some new metacharacters, and how to use groups to " "retrieve portions of the text that was matched." msgstr "" +"迄今為止,我們只涵蓋了正規表達式部分功能。在本節中,我們將介紹一些新的元字符" +"和如何使用群組檢索匹配文本的部分。" #: ../../howto/regex.rst:683 +#, fuzzy msgid "More Metacharacters" -msgstr "" +msgstr "更多元字符" #: ../../howto/regex.rst:685 +#, fuzzy msgid "" "There are some metacharacters that we haven't covered yet. Most of them " "will be covered in this section." -msgstr "" +msgstr "本節將介紹一些我們還沒有涉及的元字符。其中大部分會在本節中詳加說明。" #: ../../howto/regex.rst:688 +#, fuzzy msgid "" "Some of the remaining metacharacters to be discussed are :dfn:`zero-width " "assertions`. They don't cause the engine to advance through the string; " @@ -1047,12 +1383,17 @@ msgid "" "once at a given location, they can obviously be matched an infinite number " "of times." msgstr "" +"還有一些其它的元字符待討論,稱為「零寬斷言(zero-width assertions)」。它們並不" +"會使引擎在字串中前進;取而代之的是,它們根本沒有消耗任何字元,僅是成功或失" +"敗。例如,“\\b”表示當前位置位於單詞邊界上;該位置完全未被“\\b”所改變。因此," +"絕不能重複使用零寬斷言,因為如果在某個位置匹配一次,顯然可以無限次地匹配。" #: ../../howto/regex.rst:704 msgid "``|``" msgstr "``|``" #: ../../howto/regex.rst:697 +#, fuzzy msgid "" "Alternation, or the \"or\" operator. If *A* and *B* are regular " "expressions, ``A|B`` will match any string that matches either *A* or *B*. " @@ -1061,56 +1402,78 @@ msgid "" "``'Crow'`` or ``'Servo'``, not ``'Cro'``, a ``'w'`` or an ``'S'``, and " "``'ervo'``." msgstr "" +"交替運算,或者「或」的操作符。如果 *A* 和 *B* 是正規表示式, ``A|B`` 會匹配所" +"有同時符合*A* 或 *B* 的字符組成之字串。為了讓多個字符的交替工作得更理性化," +"``|`` 符號極低優先級。因此 ``Crow|Servo`` 可以匹配到=>'Crow' 或 'Servo' ,而" +"不是 'Cro'、'w'、大寫'S'、及 'ervo\"。" #: ../../howto/regex.rst:703 +#, fuzzy msgid "" "To match a literal ``'|'``, use ``\\|``, or enclose it inside a character " "class, as in ``[|]``." msgstr "" +"要匹配文字中的「|」,可以使用反斜線加上符號\"\\|\",或是將它放入字元組中,例" +"如\"[|]\"。" #: ../../howto/regex.rst:719 msgid "``^``" msgstr "``^``" #: ../../howto/regex.rst:707 +#, fuzzy msgid "" "Matches at the beginning of lines. Unless the :const:`MULTILINE` flag has " "been set, this will only match at the beginning of the string. In :const:" "`MULTILINE` mode, this also matches immediately after each newline within " "the string." msgstr "" +"從每一行的開頭開始找尋符合的內容。除非設定了 :const:`MULTILINE` 標誌,否則只" +"會在字串開頭進行匹配。在 :const:`MULTILINE` 狀態下,也會立即經過字串中每個換" +"行符號後進行匹配。" #: ../../howto/regex.rst:711 +#, fuzzy msgid "" "For example, if you wish to match the word ``From`` only at the beginning of " "a line, the RE to use is ``^From``. ::" msgstr "" +"例如,如果你希望只在行首匹配單詞「`From`」,要使用的正則表達式是" +"「``^From``」。::" #: ../../howto/regex.rst:719 +#, fuzzy msgid "To match a literal ``'^'``, use ``\\^``." msgstr "" +"為了符合文字字面上的「^」,使用反斜線「\\」來表示,因此表達成 「\\^」。" #: ../../howto/regex.rst:733 msgid "``$``" msgstr "``$``" #: ../../howto/regex.rst:722 +#, fuzzy msgid "" "Matches at the end of a line, which is defined as either the end of the " "string, or any location followed by a newline character. ::" msgstr "" +"與一行的結尾匹配,其定義為字串的結尾或是接在換行符號之後的任何位置。::(注意:" +"句中 :: 符號應保留rst格式)" #: ../../howto/regex.rst:732 +#, fuzzy msgid "" "To match a literal ``'$'``, use ``\\$`` or enclose it inside a character " "class, as in ``[$]``." msgstr "" +"為了匹配字面上的 ``'$'``,請使用 ``\\$`` 或將其包含在字符類中,例如 ``[$]``。" #: ../../howto/regex.rst:739 msgid "``\\A``" msgstr "``\\A``" #: ../../howto/regex.rst:736 +#, fuzzy msgid "" "Matches only at the start of the string. When not in :const:`MULTILINE` " "mode, ``\\A`` and ``^`` are effectively the same. In :const:`MULTILINE` " @@ -1118,34 +1481,46 @@ msgid "" "string, but ``^`` may match at any location inside the string that follows a " "newline character." msgstr "" +"僅能在字串的開頭進行匹配。若未啟用 :const:`MULTILINE` 模式,``\\A`` 與 ``^`` " +"其實表示相同意義;然而,在 :const:`MULTILINE` 模式下,它們有所不同:``\\A`` " +"仍舊只會匹配到字串的起始位置,但是 ``^`` 則可以通過換行符後面的任何位置進行匹" +"配。" #: ../../howto/regex.rst:742 msgid "``\\Z``" msgstr "``\\Z``" #: ../../howto/regex.rst:742 +#, fuzzy msgid "Matches only at the end of the string." -msgstr "" +msgstr "僅在字串結尾進行匹配。" #: ../../howto/regex.rst:777 msgid "``\\b``" msgstr "``\\b``" #: ../../howto/regex.rst:745 +#, fuzzy msgid "" "Word boundary. This is a zero-width assertion that matches only at the " "beginning or end of a word. A word is defined as a sequence of alphanumeric " "characters, so the end of a word is indicated by whitespace or a non-" "alphanumeric character." msgstr "" +"單字邊界,是一個零寬度的斷言,只會在單詞的開頭或結尾符合。 單字指由英文字母和" +"阿拉伯數字所構成的序列,因此單詞的結尾必定以空格或非英數字符號來表示。" #: ../../howto/regex.rst:750 +#, fuzzy msgid "" "The following example matches ``class`` only when it's a complete word; it " "won't match when it's contained inside another word. ::" msgstr "" +"以下範例只會比對「``class``」是否為一個完整的單字,當它包含在其他單字中時則不" +"會比對到。 ::" #: ../../howto/regex.rst:761 +#, fuzzy msgid "" "There are two subtleties you should remember when using this special " "sequence. First, this is the worst collision between Python's string " @@ -1155,29 +1530,41 @@ msgid "" "won't match as you expect it to. The following example looks the same as our " "previous RE, but omits the ``'r'`` in front of the RE string. ::" msgstr "" +"當您使用這個特殊序列時,請注意兩點細節。首先,這是 Python 字符串字面值和正則" +"表達式序列之間最嚴重的衝突。在 Python 字符串字面值中,“\\b” 是回退字符" +"(ASCII 值為 8)。如果您沒有使用原始字符串,那麼 Python 會將“\\b”轉換為回退" +"鍵,而您的正則表達式不會按預期匹配。以下實例看起來與我們之前的 RE 相同,但省" +"略了 RE 字符串前面的“r”。::" #: ../../howto/regex.rst:775 +#, fuzzy msgid "" "Second, inside a character class, where there's no use for this assertion, " "``\\b`` represents the backspace character, for compatibility with Python's " "string literals." msgstr "" +"第二點,在字元類別中,如果不需要這個斷言,``\\b`` 代表的是消除符號" +"(backspace),為了與 Python 的字串表示法相容。" #: ../../howto/regex.rst:782 msgid "``\\B``" msgstr "``\\B``" #: ../../howto/regex.rst:780 +#, fuzzy msgid "" "Another zero-width assertion, this is the opposite of ``\\b``, only matching " "when the current position is not at a word boundary." msgstr "" +"另一個零寬斷言,這是 ``\\b`` 的相反,僅在當前位置不位於單字邊界時匹配。" #: ../../howto/regex.rst:785 +#, fuzzy msgid "Grouping" -msgstr "" +msgstr "群組化" #: ../../howto/regex.rst:787 +#, fuzzy msgid "" "Frequently you need to obtain more information than just whether the RE " "matched or not. Regular expressions are often used to dissect strings by " @@ -1185,15 +1572,22 @@ msgid "" "of interest. For example, an RFC-822 header line is divided into a header " "name and a value, separated by a ``':'``, like this:" msgstr "" +"常常需要取得比僅僅判斷正規表達式是否符合的更多資訊。經常用來剖析字串的方式是" +"撰寫一個正規表示式,其中包含許多子組,可以匹配各種不同的所需元素。例如," +"RFC-822標頭行可分成一個標頭名稱和一個值,由冒號「:」分隔開來︰" #: ../../howto/regex.rst:800 +#, fuzzy msgid "" "This can be handled by writing a regular expression which matches an entire " "header line, and has one group which matches the header name, and another " "group which matches the header's value." msgstr "" +"這可以透過匹配整個標頭行的正規表示式來處理,其中有一個群組匹配標頭名稱,另一" +"個群組匹配標頭值。" #: ../../howto/regex.rst:804 +#, fuzzy msgid "" "Groups are marked by the ``'('``, ``')'`` metacharacters. ``'('`` and " "``')'`` have much the same meaning as they do in mathematical expressions; " @@ -1202,8 +1596,12 @@ msgid "" "``, or ``{m,n}``. For example, ``(ab)*`` will match zero or more " "repetitions of ``ab``. ::" msgstr "" +"群組由「(」和「)」 元字符標記。 在數學表達式中,「(」和 「)」具有大致相同的含" +"義; 它們將其中包含的表達式分組在一起,可以使用量詞(如*、+、?或{m,n})重複" +"群組內容。 例如,“(ab)*”可以匹配零個或多個“ ab”。::" #: ../../howto/regex.rst:815 +#, fuzzy msgid "" "Groups indicated with ``'('``, ``')'`` also capture the starting and ending " "index of the text that they match; this can be retrieved by passing an " @@ -1214,28 +1612,45 @@ msgid "" "we'll see how to express groups that don't capture the span of text that " "they match. ::" msgstr "" +"以符合守則的方式翻譯如下:有括弧主分組 ``'('``, ``')'`` 捕捉了匹配文本的開始" +"與結束索引,可透過向 :meth:`~re.Match.group`、:meth:`~re.Match.start`、:meth:" +"`~re.Match.end` 和:meth:`~re.Match.span` 中傳入參數獲取此訊息。 主分組以 0 開" +"始編號,因此第一個是 0,代表整個正規表示式(RE),所以當透過 :ref:`match " +"object ` 方法時預設值皆會回傳主分組 0。 後續我們也會看到如何表" +"示不包含其匹配範圍但需要群聚化的案例. ::" #: ../../howto/regex.rst:831 +#, fuzzy msgid "" "Subgroups are numbered from left to right, from 1 upward. Groups can be " "nested; to determine the number, just count the opening parenthesis " "characters, going from left to right. ::" msgstr "" +"子群組自左而右,由1開始進行編號。此外,群組還可以嵌套;想要確定嵌套數量時,只" +"需要從左至右計算開啟括號的個數即可。::(註:原文中的冒號符號是保留rst格式用" +"法,在之前加上了反斜槓表示轉譯)" #: ../../howto/regex.rst:844 +#, fuzzy msgid "" ":meth:`~re.Match.group` can be passed multiple group numbers at a time, in " "which case it will return a tuple containing the corresponding values for " "those groups. ::" msgstr "" +":meth:`~re.Match.group` 可以同時傳入多個群組編號,這樣它會回傳包含這些群組對" +"應數值的元组。" #: ../../howto/regex.rst:850 +#, fuzzy msgid "" "The :meth:`~re.Match.groups` method returns a tuple containing the strings " "for all the subgroups, from 1 up to however many there are. ::" msgstr "" +":meth:`~re.Match.groups` 方法會傳回一個元組(tuple),裡面包含所有子群組" +"(subgroups)所對應的字串,從 1 到數量上限不等。" #: ../../howto/regex.rst:856 +#, fuzzy msgid "" "Backreferences in a pattern allow you to specify that the contents of an " "earlier capturing group must also be found at the current location in the " @@ -1245,24 +1660,38 @@ msgid "" "including arbitrary characters in a string, so be sure to use a raw string " "when incorporating backreferences in a RE." msgstr "" +"在模式中使用回溯參照可讓你指定早期捕獲組的內容必須在字串的當前位置也存在。例" +"如,若精確內容與第一個群組相符合,則「\\1」將會成功;反之失敗。請記得 Python " +"字串面值也使用反斜槓後接數字來包含任意字元,因此在正規表示式 (RE) 中嵌入回溯" +"參照時要用原始字串表達法(raw string)。" #: ../../howto/regex.rst:864 +#, fuzzy msgid "For example, the following RE detects doubled words in a string. ::" msgstr "" +"例如,以下的正規表示式可以在一個字串中偵測出重複的單詞。:: import re text " +"= 'Hello world world' pattern = r'\\b(\\w+)\\s+\\1\\b' match = re." +"search(pattern, text) if match: print('Found duplicated word:', match." +"group(1))" #: ../../howto/regex.rst:870 +#, fuzzy msgid "" "Backreferences like this aren't often useful for just searching through a " "string --- there are few text formats which repeat data in this way --- but " "you'll soon find out that they're *very* useful when performing string " "substitutions." msgstr "" +"像這樣的反向參照通常對於只是搜尋字串來說並不常用 --- 很少有文字格式會以此方式" +"重複數據 --- 但當你執行字串取代時,就很快發現它們*非常*有用。" #: ../../howto/regex.rst:876 +#, fuzzy msgid "Non-capturing and Named Groups" -msgstr "" +msgstr "非捕獲和命名群組" #: ../../howto/regex.rst:878 +#, fuzzy msgid "" "Elaborate REs may use many groups, both to capture substrings of interest, " "and to group and structure the RE itself. In complex REs, it becomes " @@ -1270,8 +1699,12 @@ msgid "" "help with this problem. Both of them use a common syntax for regular " "expression extensions, so we'll look at that first." msgstr "" +"複雜的正規表示式(RE)可能會使用許多群組,既用於捕獲感興趣的子串,又用於分組" +"和結構化 RE 本身。在複雜的 RE 中,很難跟踪群號碼。有兩個功能可以幫助解決這個" +"問題。它們都使用常見的正則表達式擴展語法來實現,因此我們首先看一下這種形式。" #: ../../howto/regex.rst:884 +#, fuzzy msgid "" "Perl 5 is well known for its powerful additions to standard regular " "expressions. For these new features the Perl developers couldn't choose new " @@ -1281,8 +1714,14 @@ msgid "" "expressions would be assuming that ``&`` was a regular character and " "wouldn't have escaped it by writing ``\\&`` or ``[&]``." msgstr "" +"Perl 5 因其對於常規表示式(regular expressions)的強大增強而聞名。為了支援這" +"些新特性,Perl 的開發者不能選擇使用單一的按鍵元字符或以「\\」開頭的新特殊序" +"列,否則會使 Perl 的正規表示式與標準 REs 形成混亂不同之處。例如他們若選擇 & " +"作為一個新元字符,老表達式就可能誤以為 & 是正常字元並沒有通過組" +"合“\\&”或“[&]”來跳脫它。" #: ../../howto/regex.rst:891 +#, fuzzy msgid "" "The solution chosen by the Perl developers was to use ``(?...)`` as the " "extension syntax. ``?`` immediately after a parenthesis was a syntax error " @@ -1292,29 +1731,44 @@ msgid "" "positive lookahead assertion) and ``(?:foo)`` is something else (a non-" "capturing group containing the subexpression ``foo``)." msgstr "" +"Perl 開發人員所選擇的方法是使用 ``(?...)`` 作為擴展語法,直接在括號後面加上 " +"``?`` 是語法錯誤,因為 ``?`` 無事可做,故此不會引入任何相容性問題。而 ``?`` " +"後面的字元指出正使用何種擴充功能,因此 ``(?=foo)`` 是一種東西(肯定先行斷" +"言),而 ``(?:foo)`` 則是另一種東西(非捕獲式群組中包含子表達式「foo」)。" #: ../../howto/regex.rst:899 +#, fuzzy msgid "" "Python supports several of Perl's extensions and adds an extension syntax to " "Perl's extension syntax. If the first character after the question mark is " "a ``P``, you know that it's an extension that's specific to Python." msgstr "" +"Python 支援 Perl 的數個擴展,並在其基礎上加入了一種撰寫擴展的語法。如果問號後" +"的首字為「P」,表示它是 Python 專屬的擴展。" #: ../../howto/regex.rst:904 +#, fuzzy msgid "" "Now that we've looked at the general extension syntax, we can return to the " "features that simplify working with groups in complex REs." msgstr "" +"現在我們已經看過一般擴展語法,接著可以回到簡化處理包含多個群組的正規表達式所" +"需特性。" #: ../../howto/regex.rst:907 +#, fuzzy msgid "" "Sometimes you'll want to use a group to denote a part of a regular " "expression, but aren't interested in retrieving the group's contents. You " "can make this fact explicit by using a non-capturing group: ``(?:...)``, " "where you can replace the ``...`` with any other regular expression. ::" msgstr "" +"有時你會想用群組來表達正規表示式的一部分,但卻不需要加以擷取。你可以使用非捕" +"獲性群組 ``(?:...)``,在其中可填上任何其他的正規表示式。::備註:原文中最後有" +"兩個冒號(::),這是reStructuredText格式符號,需保留" #: ../../howto/regex.rst:919 +#, fuzzy msgid "" "Except for the fact that you can't retrieve the contents of what the group " "matched, a non-capturing group behaves exactly the same as a capturing " @@ -1326,14 +1780,24 @@ msgid "" "performance difference in searching between capturing and non-capturing " "groups; neither form is any faster than the other." msgstr "" +"一個非捕獲式群組的行為與捕獲式群組完全相同,唯一的不同就是不能檢索該群組所匹" +"配內容。你可以在非捕獲式群組中放置任何東西、重複使用``*``等重複元字符,並將其" +"嵌套在其他(捕獲式或非捕獲)的群組中。當修改現有模式時, ``(?:...)`` 就特別實" +"用了,因為您可以添加新的分組而無需更改所有其他分組的數字。值得一提的是,在搜" +"索時指定具名子模式和隨後反覆參考它們之間存在明顯速度差異;但不存在在包含是否" +"被括號所夾上這個形成闭合结构区别这个层面能帶來性能上的差异。" #: ../../howto/regex.rst:928 +#, fuzzy msgid "" "A more significant feature is named groups: instead of referring to them by " "numbers, groups can be referenced by a name." msgstr "" +"一個更重要的功能是 \"命名group\":而不是用數字來引用它們,可以通過名稱來引用" +"群組。" #: ../../howto/regex.rst:931 +#, fuzzy msgid "" "The syntax for a named group is one of the Python-specific extensions: ``(?" "P...)``. *name* is, obviously, the name of the group. Named groups " @@ -1344,27 +1808,40 @@ msgid "" "still given numbers, so you can retrieve information about a group in two " "ways::" msgstr "" +"命名群組的語法是 Python 特定的擴充功能:``(?P...)``。*name* 顯然是群組" +"的名稱。命名群組表現與捕獲群組相同,此外還能將一個名字賦予該群組作為其識別。" +"處理有關捕獲群組的 :ref:`match object ` 方法接受由數字引用之表" +"示欲選取特定群组或以含所需之特定命名之字符串當作輸入替代的方式去透過這些方法" +"操作命名控制算式咧!雖然每个给定单元只会有一个编号但它也可以由名称调用从而看" +"到编号。" #: ../../howto/regex.rst:946 +#, fuzzy msgid "" "Additionally, you can retrieve named groups as a dictionary with :meth:`~re." "Match.groupdict`::" msgstr "" +"此外,您可以使用 :meth:`~re.Match.groupdict` 方法將命名群組作為字典檢索:" #: ../../howto/regex.rst:953 +#, fuzzy msgid "" "Named groups are handy because they let you use easily remembered names, " "instead of having to remember numbers. Here's an example RE from the :mod:" "`imaplib` module::" msgstr "" +"命名群組是很方便的,因為它們讓你使用容易記住的名稱,而不是要記住數字。這裡有" +"一個 :mod:`imaplib` 模組中的正則表示式範例:" #: ../../howto/regex.rst:964 +#, fuzzy msgid "" "It's obviously much easier to retrieve ``m.group('zonem')``, instead of " "having to remember to retrieve group 9." -msgstr "" +msgstr "很明顯,取回 ``m.group('zonem')`` 比記住要取得第 9 組方便多了。" #: ../../howto/regex.rst:967 +#, fuzzy msgid "" "The syntax for backreferences in an expression such as ``(...)\\1`` refers " "to the number of the group. There's naturally a variant that uses the group " @@ -1374,23 +1851,33 @@ msgid "" "words, ``\\b(\\w+)\\s+\\1\\b`` can also be written as ``\\b(?" "P\\w+)\\s+(?P=word)\\b``::" msgstr "" +"在 ``(...)\\1`` 這樣的表達中,用於反向引用的數字代表著此群組(group)所在位" +"置;同樣地也有另一種使用名稱而非數字來表示群組的 Python 擴展:``(?P=...)``。" +"當運算式掃描到此時,等號右邊被指定名稱的群組其內容就會重新被掃描和比對。利用 " +"``\\b(\\w+)\\s+\\1\\b`` 可以找出重複單詞,這個正規表示式亦可改寫成 ``\\b(?" +"P\\w+)\\s+(?P=word)\\b`` 。" #: ../../howto/regex.rst:980 +#, fuzzy msgid "Lookahead Assertions" -msgstr "" +msgstr "先行斷言" #: ../../howto/regex.rst:982 +#, fuzzy msgid "" "Another zero-width assertion is the lookahead assertion. Lookahead " "assertions are available in both positive and negative form, and look like " "this:" msgstr "" +"另一種零寬度斷言是「先行斷言」(lookahead assertion)。先行斷言分為肯定和否定" +"兩種形式,符號如下:" #: ../../howto/regex.rst:990 msgid "``(?=...)``" msgstr "``(?=...)``" #: ../../howto/regex.rst:986 +#, fuzzy msgid "" "Positive lookahead assertion. This succeeds if the contained regular " "expression, represented here by ``...``, successfully matches at the current " @@ -1398,35 +1885,47 @@ msgid "" "tried, the matching engine doesn't advance at all; the rest of the pattern " "is tried right where the assertion started." msgstr "" +"正向先行斷言。如果包含在此處的正規表示式被成功匹配到當前位置,則此項目 " +"succeeds;否則失敗。但一旦已嘗試了所包含的表達式,配對引擎就不再往下移動,在" +"斷言開始的地方嘗試模式中其餘部分。" #: ../../howto/regex.rst:995 msgid "``(?!...)``" msgstr "``(?!...)``" #: ../../howto/regex.rst:993 +#, fuzzy msgid "" "Negative lookahead assertion. This is the opposite of the positive " "assertion; it succeeds if the contained expression *doesn't* match at the " "current position in the string." msgstr "" +"負向先行斷言。這與肯定先行斷言相反;它在當前位置的字串不符合包含表達式時才會" +"成功。" #: ../../howto/regex.rst:997 +#, fuzzy msgid "" "To make this concrete, let's look at a case where a lookahead is useful. " "Consider a simple pattern to match a filename and split it apart into a base " "name and an extension, separated by a ``.``. For example, in ``news.rc``, " "``news`` is the base name, and ``rc`` is the filename's extension." msgstr "" +"為了讓這個概念更明確,讓我們來看一個使用向前搜尋的案例。考慮一個簡單的模式來" +"匹配文件名並將其拆分成基本名稱和擴展名,以「.」隔開。例如,在「news.rc」中," +"「news」是基本名稱,而「rc」是文件的副檔名。" #: ../../howto/regex.rst:1002 +#, fuzzy msgid "The pattern to match this is quite simple:" -msgstr "" +msgstr "符合此模式的正則表示式相當簡單:" #: ../../howto/regex.rst:1004 msgid "``.*[.].*$``" msgstr "``.*[.].*$``" #: ../../howto/regex.rst:1006 +#, fuzzy msgid "" "Notice that the ``.`` needs to be treated specially because it's a " "metacharacter, so it's inside a character class to only match that specific " @@ -1435,25 +1934,36 @@ msgid "" "expression matches ``foo.bar`` and ``autoexec.bat`` and ``sendmail.cf`` and " "``printers.conf``." msgstr "" +"請注意 ``.`` 需要特殊處理,因為它是一個元字符,所以必須在字元類中使用才能匹配" +"該特定字符。同時請注意尾隨的 `$` 符號; 這是為了確保擴展名中包含所有其餘部分。" +"此正規表達式匹配 `foo.bar`、 `autoexec.bat`、 `sendmail.cf`和 `printers." +"conf` 等檔案名稱。" #: ../../howto/regex.rst:1013 +#, fuzzy msgid "" "Now, consider complicating the problem a bit; what if you want to match " "filenames where the extension is not ``bat``? Some incorrect attempts:" msgstr "" +"現在,考慮再複雜化問題;如果你想要匹配副檔名不是「bat」的文件名呢?以下是一些" +"錯誤嘗試:" #: ../../howto/regex.rst:1016 +#, fuzzy msgid "" "``.*[.][^b].*$`` The first attempt above tries to exclude ``bat`` by " "requiring that the first character of the extension is not a ``b``. This is " "wrong, because the pattern also doesn't match ``foo.bar``." msgstr "" +"``.*[.][^b].*$`` 上述的第一個嘗試是試圖通過要求擴展名的第一個字符不是 ``b``," +"來排除 ``bat``。這是錯誤的,因為此模式也無法匹配“foo.bar”。" #: ../../howto/regex.rst:1020 msgid "``.*[.]([^b]..|.[^a].|..[^t])$``" msgstr "``.*[.]([^b]..|.[^a].|..[^t])$``" #: ../../howto/regex.rst:1022 +#, fuzzy msgid "" "The expression gets messier when you try to patch up the first solution by " "requiring one of the following cases to match: the first character of the " @@ -1463,31 +1973,45 @@ msgid "" "with a two-letter extension such as ``sendmail.cf``. We'll complicate the " "pattern again in an effort to fix it." msgstr "" +"當我們嘗試透過要求以下情況中的一種匹配以修補第一個解決方案時,表達式變得更加" +"混亂:擴展名的第一個字元不是「b」 ; 第二个字符不是「a」; 或者第三个字符不是" +"「t」。这將接受 \"foo.bar\" 和拒絕 \"autoexec.bat\" ,但它需要一个三字母擴展" +"名,并且将无法接受具有两字母擴展名(例如“sendmail.cf”)的文件名。我们將再次混" +"合模式,试图进行修复。" #: ../../howto/regex.rst:1030 msgid "``.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$``" msgstr "``.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$``" #: ../../howto/regex.rst:1032 +#, fuzzy msgid "" "In the third attempt, the second and third letters are all made optional in " "order to allow matching extensions shorter than three characters, such as " "``sendmail.cf``." msgstr "" +"在第三次嘗試中,第二個和第三個字母均為可選項以便匹配短於三個字符的擴展名,例" +"如“sendmail.cf”。" #: ../../howto/regex.rst:1036 +#, fuzzy msgid "" "The pattern's getting really complicated now, which makes it hard to read " "and understand. Worse, if the problem changes and you want to exclude both " "``bat`` and ``exe`` as extensions, the pattern would get even more " "complicated and confusing." msgstr "" +"現在這個樣式變得非常複雜,這使得它很難閱讀和理解。更糟的是,如果問題發生改" +"變,並且您要排除``bat``和 ``exe`` 作為副檔名,那麼該模式就會變得更加複雜和混" +"亂。" #: ../../howto/regex.rst:1041 +#, fuzzy msgid "A negative lookahead cuts through all this confusion:" -msgstr "" +msgstr "一個負向前瞻可以解決所有這些困惑:" #: ../../howto/regex.rst:1043 +#, fuzzy msgid "" "``.*[.](?!bat$)[^.]*$`` The negative lookahead means: if the expression " "``bat`` doesn't match at this point, try the rest of the pattern; if " @@ -1496,62 +2020,79 @@ msgid "" "only starts with ``bat``, will be allowed. The ``[^.]*`` makes sure that " "the pattern works when there are multiple dots in the filename." msgstr "" +"``.*[.](?!bat$)[^.]*$``這個表達式中負向前瞻的意思是:如果現在此處的表達式" +"「bat」不符合,則嘗試模式其餘部分;如果「bat$」能夠匹配成功,則整個模式將失" +"敗。必須使用結尾的「$」來確保類似於`sample.batch` 的文件名(其中擴展名僅以 " +"`bat` 開頭)也可以被接受。「[^.] *」則確保了當文件名中存在多個點時,該模式仍然" +"有效。" #: ../../howto/regex.rst:1050 +#, fuzzy msgid "" "Excluding another filename extension is now easy; simply add it as an " "alternative inside the assertion. The following pattern excludes filenames " "that end in either ``bat`` or ``exe``:" msgstr "" +"排除其他檔案副檔名現在很容易;只要在斷言中加入它作為替代方案即可。下列模式可" +"以排除以``bat``或``exe``結尾的檔案:" #: ../../howto/regex.rst:1054 msgid "``.*[.](?!bat$|exe$)[^.]*$``" msgstr "``.*[.](?!bat$|exe$)[^.]*$``" #: ../../howto/regex.rst:1058 +#, fuzzy msgid "Modifying Strings" -msgstr "" +msgstr "修改字串" #: ../../howto/regex.rst:1060 +#, fuzzy msgid "" "Up to this point, we've simply performed searches against a static string. " "Regular expressions are also commonly used to modify strings in various " "ways, using the following pattern methods:" msgstr "" +"到目前為止,我們僅對一個靜態字符串執行了搜尋。正規表示式也常被用來以不同方式" +"修改字串,使用下列模式方法:" #: ../../howto/regex.rst:1067 msgid "``split()``" msgstr "``split()``" #: ../../howto/regex.rst:1067 +#, fuzzy msgid "Split the string into a list, splitting it wherever the RE matches" -msgstr "" +msgstr "將字串拆分成一個列表,在與正規表示式(RE)相符處進行拆分" #: ../../howto/regex.rst:1070 msgid "``sub()``" msgstr "``sub()``" #: ../../howto/regex.rst:1070 +#, fuzzy msgid "" "Find all substrings where the RE matches, and replace them with a different " "string" -msgstr "" +msgstr "尋找所有正規表示式所匹配到的子字串,並將其替換為不同的字串。" #: ../../howto/regex.rst:1073 msgid "``subn()``" msgstr "``subn()``" #: ../../howto/regex.rst:1073 +#, fuzzy msgid "" "Does the same thing as :meth:`!sub`, but returns the new string and the " "number of replacements" -msgstr "" +msgstr "跟 `sub()` 方法一樣,但回傳值是所產生的新字串以及替換發生的次數" #: ../../howto/regex.rst:1080 +#, fuzzy msgid "Splitting Strings" -msgstr "" +msgstr "分割字串" #: ../../howto/regex.rst:1082 +#, fuzzy msgid "" "The :meth:`~re.Pattern.split` method of a pattern splits a string apart " "wherever the RE matches, returning a list of the pieces. It's similar to " @@ -1560,16 +2101,24 @@ msgid "" "splitting by whitespace or by a fixed string. As you'd expect, there's a " "module-level :func:`re.split` function, too." msgstr "" +"模式的 :meth:`~re.Pattern.split` 方法將字符串拆分為正則表達式匹配到的位置,返" +"回一個列表。它類似於字串的 :meth:`!split` 方法,但在可分隔符號方面提供更多通" +"用性;字串 :meth:`!split` 只支援按空格或固定字元進行拆分 。預期中也有模組級別" +"的函數::func:`re.split`。" #: ../../howto/regex.rst:1093 +#, fuzzy msgid "" "Split *string* by the matches of the regular expression. If capturing " "parentheses are used in the RE, then their contents will also be returned as " "part of the resulting list. If *maxsplit* is nonzero, at most *maxsplit* " "splits are performed." msgstr "" +"拆分*string*,以正則表達式作匹配。若RE使用捕獲括號,將其內容也一同返回為結果" +"列表的元素。當*maxsplit*非零時,最多執行 *maxsplit* 次拆分。" #: ../../howto/regex.rst:1098 +#, fuzzy msgid "" "You can limit the number of splits made, by passing a value for *maxsplit*. " "When *maxsplit* is nonzero, at most *maxsplit* splits will be made, and the " @@ -1577,67 +2126,94 @@ msgid "" "the following example, the delimiter is any sequence of non-alphanumeric " "characters. ::" msgstr "" +"你可以透過設定 *maxsplit* 參數來限制分割次數,當 *maxsplit* 值不是零時,最多" +"分割此數量並將字串剩餘部份作為清單的最終元素回傳。在下面的範例中,以非英文字" +"母和數字為區隔符號。 ::" #: ../../howto/regex.rst:1110 +#, fuzzy msgid "" "Sometimes you're not only interested in what the text between delimiters is, " "but also need to know what the delimiter was. If capturing parentheses are " "used in the RE, then their values are also returned as part of the list. " "Compare the following calls::" msgstr "" +"有時你不僅對分隔符之間的文本有興趣,也需要知道該分隔符是什麼。 如果在正規表示" +"式(RE)中使用捕獲括號,那麼它們的值也作為列表的一部分返回。 比較以下調用:" #: ../../howto/regex.rst:1122 +#, fuzzy msgid "" "The module-level function :func:`re.split` adds the RE to be used as the " "first argument, but is otherwise the same. ::" msgstr "" +"模組層級函式 :func:`re.split` 新增一個參數,作為被用來作為第一個引數的正規表" +"示式,但除此之外功能和原本相同。" #: ../../howto/regex.rst:1134 +#, fuzzy msgid "Search and Replace" -msgstr "" +msgstr "搜尋和取代" #: ../../howto/regex.rst:1136 +#, fuzzy msgid "" "Another common task is to find all the matches for a pattern, and replace " "them with a different string. The :meth:`~re.Pattern.sub` method takes a " "replacement value, which can be either a string or a function, and the " "string to be processed." msgstr "" +"另一個常見的任務是找到符合某個模式的所有匹配項,並用不同的字串取代它們。 :" +"meth:`~re.Pattern.sub` 方法需要傳入替換值,可以是字串或函式,以及要處理的字" +"串。" #: ../../howto/regex.rst:1143 +#, fuzzy msgid "" "Returns the string obtained by replacing the leftmost non-overlapping " "occurrences of the RE in *string* by the replacement *replacement*. If the " "pattern isn't found, *string* is returned unchanged." msgstr "" +"回傳字串,此字串經取代來源字串 *string* 中最左邊且沒重複的區段符合所提供的正" +"規表示式 RE 所產生之符合 REPLACEMENT 的內容。若未找到該模式,則回傳原始 " +"*string* 字串。" #: ../../howto/regex.rst:1147 +#, fuzzy msgid "" "The optional argument *count* is the maximum number of pattern occurrences " "to be replaced; *count* must be a non-negative integer. The default value " "of 0 means to replace all occurrences." msgstr "" +"選擇性的參數 *count* 是最大替換次數;*count* 必須是一個非負整數。預設值為0," +"表示取代所有出現次數。" #: ../../howto/regex.rst:1151 +#, fuzzy msgid "" "Here's a simple example of using the :meth:`~re.Pattern.sub` method. It " "replaces colour names with the word ``colour``::" -msgstr "" +msgstr "以下是使用 ``sub`` 方法的一個簡單範例。它會將顏色名稱替換為「color」:" #: ../../howto/regex.rst:1160 +#, fuzzy msgid "" "The :meth:`~re.Pattern.subn` method does the same work, but returns a 2-" "tuple containing the new string value and the number of replacements that " "were performed::" msgstr "" +":meth:`~re.Pattern.subn` 方法與 :meth:`~re.SubPattern.sub` 相同,但其回傳值為" +"一個包含新字串和替換次數共兩個元素的二元組(tuple)。" #: ../../howto/regex.rst:1169 +#, fuzzy msgid "" "Empty matches are replaced only when they're not adjacent to a previous " "empty match. ::" -msgstr "" +msgstr "只有當一個空匹配不相鄰於先前的空匹配時,才會替換空匹配。 ::" #: ../../howto/regex.rst:1176 +#, fuzzy msgid "" "If *replacement* is a string, any backslash escapes in it are processed. " "That is, ``\\n`` is converted to a single newline character, ``\\r`` is " @@ -1647,14 +2223,22 @@ msgid "" "incorporate portions of the original text in the resulting replacement " "string." msgstr "" +"如果 *replacement* 是一個字串,其中的反斜線會被處理。也就是 `\\n` 會轉換成新" +"行符號,`\\r` 會轉換成回車鍵等等。未知的轉義符如 `\\&` 則不變留作原樣。可替代" +"參照(backreferences),例如 `\\6`,將會被正則表達式中相對應組別所匹配到的子" +"字串取代掉,因此可以在替換字串中加上原始訊息文字部分" #: ../../howto/regex.rst:1183 +#, fuzzy msgid "" "This example matches the word ``section`` followed by a string enclosed in " "``{``, ``}``, and changes ``section`` to ``subsection``::" msgstr "" +"本例尋找單字「section」後,接著一串以大括號包覆的字詞,並將「section」替換為" +"「subsection」。即:" #: ../../howto/regex.rst:1190 +#, fuzzy msgid "" "There's also a syntax for referring to named groups as defined by the ``(?" "P...)`` syntax. ``\\g`` will use the substring matched by the " @@ -1665,8 +2249,15 @@ msgid "" "literal character ``'0'``.) The following substitutions are all equivalent, " "but use all three variations of the replacement string. ::" msgstr "" +"``(?P...)`` 的語法可以用來引用以 ``name`` 為名的群組,``\\g`` 將" +"會使用所對應到的子字串在命名群組中帶有這個名稱。 一定也可以使用对应的群组编号" +"来替换(即 \\g)。因此,\\g<2> 相當于 \\2,在替換字符串中沒什麼歧义。" +"但是如果连接了其它字符时,则不然:例如“\\g<2>0”操作代表第二个分组加上一个0. " +"而没有被转义为包含两个数字的分组引用\"20\". 下面三种情况等价而且展示三种形式" +"的替换字符串:." #: ../../howto/regex.rst:1207 +#, fuzzy msgid "" "*replacement* can also be a function, which gives you even more control. If " "*replacement* is a function, the function is called for every non-" @@ -1674,14 +2265,20 @@ msgid "" "a :ref:`match object ` argument for the match and can use " "this information to compute the desired replacement string and return it." msgstr "" +"*replacement* (替換物)也可以是一個函數,這樣就能讓你得到更多的控制。如果 " +"*replacement* 是一個函數,則在每次非重疊出現 *pattern* 的地方都會呼叫該函" +"式。 在每次呼叫中,將傳遞給該函式 :ref:`match object ` 參數以" +"供與匹配相關的替換操作並將其返回。" #: ../../howto/regex.rst:1213 +#, fuzzy msgid "" "In the following example, the replacement function translates decimals into " "hexadecimal::" -msgstr "" +msgstr "以下是範例,此替換函數可將十進制轉譯成十六進制:" #: ../../howto/regex.rst:1225 +#, fuzzy msgid "" "When using the module-level :func:`re.sub` function, the pattern is passed " "as the first argument. The pattern may be provided as an object or as a " @@ -1690,24 +2287,34 @@ msgid "" "pattern string, e.g. ``sub(\"(?i)b+\", \"x\", \"bbbb BBBB\")`` returns ``'x " "x'``." msgstr "" +"當使用模組層級的 :func:`re.sub` 函式時,模式會傳遞為第一個引數。 模式可以提供" +"為物件或字串; 如果您需要指定正規表示式旗標,必須將模式物件用作第一個參數,或" +"在模式字串中使用內置修改器,例如 ``sub(\"(?i)b+\", \"x\", \"bbbb BBBB\")`` 就" +"會返回 ``'x x'``" #: ../../howto/regex.rst:1233 +#, fuzzy msgid "Common Problems" -msgstr "" +msgstr "常見問題" #: ../../howto/regex.rst:1235 +#, fuzzy msgid "" "Regular expressions are a powerful tool for some applications, but in some " "ways their behaviour isn't intuitive and at times they don't behave the way " "you may expect them to. This section will point out some of the most common " "pitfalls." msgstr "" +"正規表示式 (regular expression) 是某些情況下強大的工具,但有時其行為並不直" +"觀,而且偶爾會表現出你未曾預期的結果。本節將指出其中一些最常見的陷阱。" #: ../../howto/regex.rst:1241 +#, fuzzy msgid "Use String Methods" -msgstr "" +msgstr "使用字串方法" #: ../../howto/regex.rst:1243 +#, fuzzy msgid "" "Sometimes using the :mod:`re` module is a mistake. If you're matching a " "fixed string, or a single character class, and you're not using any :mod:" @@ -1718,8 +2325,13 @@ msgid "" "for the purpose, instead of the large, more generalized regular expression " "engine." msgstr "" +"有時候使用 :mod:`re` 模組會是一個錯誤。如果您只是匹配固定字串或單一字符類,並" +"且不使用任何: const:`~re.IGNORECASE` 等模块功能,则可能不需要完整的正規表達式" +"的功能。相對地, 字符串有好幾種方法來執行操作以查找固定字符串,而這些方法通常" +"更快,因為其實現是一個被優化、小型 C loop 而已,而非大型、更廣泛的正則引擎" #: ../../howto/regex.rst:1251 +#, fuzzy msgid "" "One example might be replacing a single fixed string with another one; for " "example, you might replace ``word`` with ``deed``. :func:`re.sub` seems " @@ -1731,8 +2343,15 @@ msgid "" "``word`` have a word boundary on either side. This takes the job beyond :" "meth:`!replace`'s abilities.)" msgstr "" +"一個例子可能是將一個固定字符串替換為另一個,例如您可以將“word” 替换为 " +"“deed”。使用 :func:`re.sub` 函数似乎是最佳選擇,但要考虑到 :meth:`~str." +"replace` 方法。请注意,:meth:`!replace` 也會替换單詞内的“word”," +"将“swordfish”变成了\"sdeedfish\",但单纯RE \"word\" 也会这样做。 (为了避免在" +"单词的部分执行替换,模式必须是`\\bword\\b' ,以要求 `word`` 在任何一侧都有一" +"个字边界。 这将超出 :meth:! replace 的能力范围)" #: ../../howto/regex.rst:1260 +#, fuzzy msgid "" "Another common task is deleting every occurrence of a single character from " "a string or replacing it with another single character. You might do this " @@ -1740,18 +2359,26 @@ msgid "" "capable of doing both tasks and will be faster than any regular expression " "operation can be." msgstr "" +"另一個常見的任務是從字串中刪除某個特定字元的所有出現,或以另一個單一字元取代" +"它。你可能會透過類似這樣的指令實現:``re.sub('\\n', ' ', S)``,但 :meth:" +"`~str.translate` 可同時做到這兩件事情且速度比正規表示式操作更快。" #: ../../howto/regex.rst:1266 +#, fuzzy msgid "" "In short, before turning to the :mod:`re` module, consider whether your " "problem can be solved with a faster and simpler string method." msgstr "" +"簡而言之,在使用 :mod:`re` 模組之前,請考慮是否可以使用更快且更簡單的字串方法" +"來解決您的問題。" #: ../../howto/regex.rst:1271 +#, fuzzy msgid "match() versus search()" -msgstr "" +msgstr "match() 與 search() 的差別" #: ../../howto/regex.rst:1273 +#, fuzzy msgid "" "The :func:`~re.match` function only checks if the RE matches at the " "beginning of the string while :func:`~re.search` will scan forward through " @@ -1760,14 +2387,20 @@ msgid "" "start at 0; if the match wouldn't start at zero, :func:`!match` will *not* " "report it. ::" msgstr "" +":func:`~re.match` 函式只會檢查 RE 是否與字串開頭匹配,而 :func:`~re.search` " +"則會透過掃描字串尋找匹配。重要的是要注意這個差別,請記得:只有當一個匹配成功" +"且從 0 開始才會報告 :func:`!match` 的掃描結果;否則就不會報告該結果。例如:" #: ../../howto/regex.rst:1284 +#, fuzzy msgid "" "On the other hand, :func:`~re.search` will scan forward through the string, " "reporting the first match it finds. ::" msgstr "" +"另一方面,:func:`~re.search` 會從字符串前向掃描,報告它發現的第一個匹配。 ::" #: ../../howto/regex.rst:1292 +#, fuzzy msgid "" "Sometimes you'll be tempted to keep using :func:`re.match`, and just add ``." "*`` to the front of your RE. Resist this temptation and use :func:`re." @@ -1778,19 +2411,29 @@ msgid "" "analysis lets the engine quickly scan through the string looking for the " "starting character, only trying the full match if a ``'C'`` is found." msgstr "" +"有時你會想繼續使用 :func:`re.match`,只需在正則式上方加上 ``.*`` 。但最好抵制" +"這種誘惑,改用 :func:`re.search`。因為正則表達式的編譯器會分析它以加速查找匹" +"配的流程。其中一項分析是確定匹配首字元;例如,以 \"Crow\" 開始的模式必須與" +"「C」開頭相符。此分析讓引擎快速掃描字串尋找起始字符,在發現「C」時才嘗試完整" +"匹配。" #: ../../howto/regex.rst:1301 +#, fuzzy msgid "" "Adding ``.*`` defeats this optimization, requiring scanning to the end of " "the string and then backtracking to find a match for the rest of the RE. " "Use :func:`re.search` instead." msgstr "" +"將 ``.*`` 加入正規表示式會使此優化失效,需要掃描整個字串,接著回溯尋找 RE 的" +"其他匹配項。建議使用 :func:`re.search` 代替。" #: ../../howto/regex.rst:1307 +#, fuzzy msgid "Greedy versus Non-Greedy" -msgstr "" +msgstr "貪婪模式與非貪婪模式" #: ../../howto/regex.rst:1309 +#, fuzzy msgid "" "When repeating a regular expression, as in ``a*``, the resulting action is " "to consume as much of the pattern as possible. This fact often bites you " @@ -1798,8 +2441,13 @@ msgid "" "brackets surrounding an HTML tag. The naive pattern for matching a single " "HTML tag doesn't work because of the greedy nature of ``.*``. ::" msgstr "" +"當重複一個正規表示式,例如 ``a*`` ,結果會儘可能多的消耗範圍內的模式。當您試" +"圖匹配成對的平衡分隔符號時,這個特性經常會出現問題,例如用於 HTML 標記周圍小" +"於與大於括號(angle brackets)。由於 ``.*`` 貪心的本質, 難以使用單一HTML標籤" +"匹配模式解決困境。::" #: ../../howto/regex.rst:1323 +#, fuzzy msgid "" "The RE matches the ``'<'`` in ``''``, and the ``.*`` consumes the rest " "of the string. There's still more left in the RE, though, and the ``>`` " @@ -1808,8 +2456,13 @@ msgid "" "The final match extends from the ``'<'`` in ``''`` to the ``'>'`` in " "``''``, which isn't what you want." msgstr "" +"RE(正規表示式)匹配 ``''`` 中的 ``'<'``, 然後使用「.*」消耗掉其餘部分" +"的字串。雖然 RE 仍有更多內容,但是 '>'無法在字符串末尾匹配,因此正規表達式引" +"擎必須逐字符回溯直到找到 ' > ' 的匹配為止。 最終匹配從 `` '< html> '`` 中的“ " +"<”一直延伸到 `` ''``的“>”,這不是您想要的。" #: ../../howto/regex.rst:1330 +#, fuzzy msgid "" "In this case, the solution is to use the non-greedy quantifiers ``*?``, ``+?" "``, ``??``, or ``{m,n}?``, which match as *little* text as possible. In the " @@ -1817,8 +2470,13 @@ msgid "" "matches, and when it fails, the engine advances a character at a time, " "retrying the ``'>'`` at every step. This produces just the right result::" msgstr "" +"在這個情況下,解決方法是使用 non-greedy quantifiers ``*?``, ``+?``, ``??``, " +"或 ``{m,n}?``。它會匹配最少的文字。在上面的範例中,當第一個``'<'``被匹配後就" +"立即嘗試``'>'``,如果失敗則引擎每次向前移動一個字符並重新嘗試匹配到此為止。這" +"樣可以得到正確的結果:" #: ../../howto/regex.rst:1339 +#, fuzzy msgid "" "(Note that parsing HTML or XML with regular expressions is painful. Quick-" "and-dirty patterns will handle common cases, but HTML and XML have special " @@ -1827,27 +2485,43 @@ msgid "" "patterns will be *very* complicated. Use an HTML or XML parser module for " "such tasks.)" msgstr "" +"(注意,使用正規表示式解析 HTML 或 XML 是非常困難的。簡單而粗略的樣式能處理常" +"見情況,但是 HTML 和 XML 有特別情況會打壞明顯正規表達示;當你寫下一個講解所有" +"可能性的原則時,樣式將變得*非常*複雜。對於這些任務使用 HTML 或XML 解析器模" +"組)" #: ../../howto/regex.rst:1347 +#, fuzzy msgid "Using re.VERBOSE" msgstr "" +"使用 re.VERBOSE在使用 Python 的正規表示式來進行字串處理時,re 模組中的 " +"VERBOSE 設定是很好用的一個功能。它讓你可以將想要搜尋/替換的模式寫成一個更容易" +"閱讀且較為完整的格式,適合於需要撰寫複雜正則表達式的程式設計師使用。" #: ../../howto/regex.rst:1349 +#, fuzzy msgid "" "By now you've probably noticed that regular expressions are a very compact " "notation, but they're not terribly readable. REs of moderate complexity can " "become lengthy collections of backslashes, parentheses, and metacharacters, " "making them difficult to read and understand." msgstr "" +"現在你可能已經注意到正規表示式(regular expression)是一種非常緊湊的符號,但" +"並不容易閱讀。中等複雜度的正規表達式可能會變成裝滿斜線、括號和元字符的長字符" +"串,使其很難理解和閱讀。" #: ../../howto/regex.rst:1354 +#, fuzzy msgid "" "For such REs, specifying the :const:`re.VERBOSE` flag when compiling the " "regular expression can be helpful, because it allows you to format the " "regular expression more clearly." msgstr "" +"對於這樣的正規表示式(REs),在編譯時指定:const:`re.VERBOSE` 標誌可以很有幫" +"助,因為它允許您更清晰地格式化正則表達式。" #: ../../howto/regex.rst:1358 +#, fuzzy msgid "" "The ``re.VERBOSE`` flag has several effects. Whitespace in the regular " "expression that *isn't* inside a character class is ignored. This means " @@ -1857,24 +2531,38 @@ msgid "" "extend from a ``#`` character to the next newline. When used with triple-" "quoted strings, this enables REs to be formatted more neatly::" msgstr "" +"``re.VERBOSE`` 標誌有數個效果。正規表達式中的空白字符(這**不在**字符類中)將" +"被忽略。這意味著像 ``dog | cat`` 這樣子的表示也等同於比較難讀的寫法: " +"\\```dog|cat\\``` ,但是 ``[a b]`` 仍然可以匹配到'`a`', `'b'`或一個空格。此" +"外,您也可以將註釋放入正則表達式內部;註釋從其中一個“#” 字符開始,直到下一行" +"新行為止長度和位置沒有限制,可以自由嵌套和換行使用。\\ 如舉例:`\\\\\\" +"\\```pythonpattern = re.compile(r''' (\\d+) # capture one or more digits " +"[-/+]? # an optional minus or plus sign (\\d+) # capture one or more " +"digits''', re.VERBOSE)``當用於三引號字符串時,它使得 REs 可以更整齊地排版:" #: ../../howto/regex.rst:1375 +#, fuzzy msgid "This is far more readable than::" -msgstr "" +msgstr "這樣閱讀起來會輕鬆許多::: 這遠比以下方式容易閱讀:" #: ../../howto/regex.rst:1381 +#, fuzzy msgid "Feedback" -msgstr "" +msgstr "回饋" #: ../../howto/regex.rst:1383 +#, fuzzy msgid "" "Regular expressions are a complicated topic. Did this document help you " "understand them? Were there parts that were unclear, or Problems you " "encountered that weren't covered here? If so, please send suggestions for " "improvements to the author." msgstr "" +"正規表示式是一個複雜的主題。 這份文件有幫助您了解嗎? 是否有部分不清楚或未涵" +"蓋到問題? 如果有,請將改進建議發送給作者。" #: ../../howto/regex.rst:1388 +#, fuzzy msgid "" "The most complete book on regular expressions is almost certainly Jeffrey " "Friedl's Mastering Regular Expressions, published by O'Reilly. " @@ -1884,3 +2572,8 @@ msgid "" "edition covered Python's now-removed :mod:`!regex` module, which won't help " "you much.) Consider checking it out from your library." msgstr "" +"關於正規表示式,最完整的參考書籍很可能是 Jeffrey Friedl 的《Mastering " +"Regular Expressions》,由 O'Reilly 出版。但很遺憾地,它只著重在Perl和Java的正" +"規表示式版本上,沒有包含任何Python內容,因此對於使用Python編程來說甚至可能不" +"太實用。(第一版涵蓋了現今已移除的 :mod:`!regex` 模組)建議您從圖書館借閱該" +"書。"