@@ -1596,21 +1596,26 @@ msgstr ""
15961596
15971597#: ../../library/pickle.rst:782
15981598msgid "Dispatch Tables"
1599- msgstr "分派表 "
1599+ msgstr "調度表 "
16001600
16011601#: ../../library/pickle.rst:784
16021602msgid ""
16031603"If one wants to customize pickling of some classes without disturbing any "
16041604"other code which depends on pickling, then one can create a pickler with a "
16051605"private dispatch table."
16061606msgstr ""
1607+ "如果你希望在不干擾其他物件正常封裝的前提下建立一個針對特定物件的封裝器,你可"
1608+ "建立一個有私密調度表的封裝器。"
16071609
16081610#: ../../library/pickle.rst:788
16091611msgid ""
16101612"The global dispatch table managed by the :mod:`copyreg` module is available "
16111613"as :data:`!copyreg.dispatch_table`. Therefore, one may choose to use a "
16121614"modified copy of :data:`!copyreg.dispatch_table` as a private dispatch table."
16131615msgstr ""
1616+ "由 :mod:`copyreg` 模組管理的全域調度表可以 :data:`!copyreg.dispatch_table` 呼"
1617+ "叫。你可以透過這個方式來基於原始 :data:`!copyreg.dispatch_table` 創建一個修改"
1618+ "過的版本,作為你的專屬用途的調度表。"
16141619
16151620#: ../../library/pickle.rst:793
16161621msgid "For example ::"
@@ -1633,6 +1638,8 @@ msgid ""
16331638"creates an instance of :class:`pickle.Pickler` with a private dispatch table "
16341639"which handles the ``SomeClass`` class specially. Alternatively, the code ::"
16351640msgstr ""
1641+ "建立了一個 :class:`pickle.Pickler`,其中含有專門處裡 ``SomeClass`` 類別的專屬"
1642+ "調度表。此外,你也可以寫作:::"
16361643
16371644#: ../../library/pickle.rst:804
16381645msgid ""
@@ -1653,6 +1660,8 @@ msgid ""
16531660"does the same but all instances of ``MyPickler`` will by default share the "
16541661"private dispatch table. On the other hand, the code ::"
16551662msgstr ""
1663+ "這樣可產生相似的結果,唯一不同的是往後所有 ``MyPickler`` 預設都會使用這個專屬"
1664+ "調度表。最後,如果將程式寫為:::"
16561665
16571666#: ../../library/pickle.rst:813
16581667msgid ""
@@ -1668,11 +1677,11 @@ msgstr ""
16681677msgid ""
16691678"modifies the global dispatch table shared by all users of the :mod:`copyreg` "
16701679"module."
1671- msgstr ""
1680+ msgstr "則會改變 :mod:`copyreg` 模組內建、所有使用者共通的調度表。 "
16721681
16731682#: ../../library/pickle.rst:822
16741683msgid "Handling Stateful Objects"
1675- msgstr ""
1684+ msgstr "處裡紀錄大量狀態的物件 "
16761685
16771686#: ../../library/pickle.rst:828
16781687msgid ""
@@ -1685,6 +1694,12 @@ msgid ""
16851694"__setstate__` and :meth:`!__getstate__` methods are used to implement this "
16861695"behavior. ::"
16871696msgstr ""
1697+ "以下的範例展示了如何修改針對特定類別封裝時的行為。下面的 :class:`!"
1698+ "TextReader` 類別會開啟一個文字檔案,並在每次呼叫其 :meth:`!readline` 方法時返"
1699+ "回當前行編號與該行內容。如果 :class:`!TextReader` 實例被封裝,所有*除了檔案物"
1700+ "件之外*的屬性成員都會被保存。在該實例被拆封時,檔案將被重新開啟,並從上次的位"
1701+ "置繼續讀取。這個行為的達成是透過 :meth:`!__setstate__` 和 :meth:`!"
1702+ "__getstate__` 方法來實作的。::"
16881703
16891704#: ../../library/pickle.rst:836
16901705msgid ""
@@ -1725,10 +1740,45 @@ msgid ""
17251740" # Finally, save the file.\n"
17261741" self.file = file"
17271742msgstr ""
1743+ "class TextReader:\n"
1744+ " \"\"\" 列出文字檔案中的行並對其進行編號。\"\"\" \n"
1745+ "\n"
1746+ " def __init__(self, filename):\n"
1747+ " self.filename = filename\n"
1748+ " self.file = open(filename)\n"
1749+ " self.lineno = 0\n"
1750+ "\n"
1751+ " def readline(self):\n"
1752+ " self.lineno += 1\n"
1753+ " line = self.file.readline()\n"
1754+ " if not line:\n"
1755+ " return None\n"
1756+ " if line.endswith('\\ n'):\n"
1757+ " line = line[:-1]\n"
1758+ " return \" %i: %s\" % (self.lineno, line)\n"
1759+ "\n"
1760+ " def __getstate__(self):\n"
1761+ " # 從 self.__dict__ 中複製物件的狀態。包含了所有的實例屬性。\n"
1762+ " # 使用 dict.copy() 方法以避免修改原始狀態。\n"
1763+ " state = self.__dict__.copy()\n"
1764+ " # 移除不可封裝的項目。\n"
1765+ " del state['file']\n"
1766+ " return state\n"
1767+ "\n"
1768+ " def __setstate__(self, state):\n"
1769+ " # 恢復實例屬性(即 filename 和 lineno)。\n"
1770+ " self.__dict__.update(state)\n"
1771+ " # 恢復到先前開啟了檔案的狀態。為此,我們需要重新開啟它並一直讀取到行"
1772+ "數編號相同。\n"
1773+ " file = open(self.filename)\n"
1774+ " for _ in range(self.lineno):\n"
1775+ " file.readline()\n"
1776+ " # 存檔。\n"
1777+ " self.file = file"
17281778
17291779#: ../../library/pickle.rst:874
17301780msgid "A sample usage might be something like this::"
1731- msgstr ""
1781+ msgstr "可以這樣實際使用::: "
17321782
17331783#: ../../library/pickle.rst:876
17341784msgid ""
@@ -1752,7 +1802,7 @@ msgstr ""
17521802
17531803#: ../../library/pickle.rst:888
17541804msgid "Custom Reduction for Types, Functions, and Other Objects"
1755- msgstr ""
1805+ msgstr "針對型別、函數或特定物件定製縮減函數 "
17561806
17571807#: ../../library/pickle.rst:892
17581808msgid ""
@@ -1761,6 +1811,9 @@ msgid ""
17611811"the object's type, or we may want to customize the pickling of functions and "
17621812"classes."
17631813msgstr ""
1814+ "有時候,:attr:`~Pickler.dispatch_table` 的彈性空間可能不夠。尤其當我們想要使"
1815+ "用型別以外的方式來判斷如何使用自訂封裝、或者我們想要自訂特定函式和類別的封裝"
1816+ "方法時。"
17641817
17651818#: ../../library/pickle.rst:897
17661819msgid ""
@@ -1770,13 +1823,18 @@ msgid ""
17701823"alternatively return :data:`NotImplemented` to fallback to the traditional "
17711824"behavior."
17721825msgstr ""
1826+ "如果是這樣的話,可以繼承 :class:`Pickler` 類別並實作一個 :meth:`~Pickler."
1827+ "reducer_override` 方法。此方法可以回傳任意的縮減元組(參閱 :meth:`~object."
1828+ "__reduce__`)、也可以回傳 :data:`NotImplemented` 以回退至原始的行為。"
17731829
17741830#: ../../library/pickle.rst:902
17751831msgid ""
17761832"If both the :attr:`~Pickler.dispatch_table` and :meth:`~Pickler."
17771833"reducer_override` are defined, then :meth:`~Pickler.reducer_override` method "
17781834"takes priority."
17791835msgstr ""
1836+ "如果 :attr:`~Pickler.dispatch_table` 和 :meth:`~Pickler.reducer_override` 都"
1837+ "被定義了的話,:meth:`~Pickler.reducer_override` 的優先度較高。"
17801838
17811839#: ../../library/pickle.rst:907
17821840msgid ""
@@ -1786,12 +1844,16 @@ msgid ""
17861844"class:`dict`, :class:`set`, :class:`frozenset`, :class:`list` and :class:"
17871845"`tuple`."
17881846msgstr ""
1847+ "出於效能考量,處裡以下物件可能不會呼叫 :meth:`~Pickler.reducer_override`:"
1848+ "``None``、``True``、``False``,以及 :class:`int`、:class:`float`、:class:"
1849+ "`bytes`、:class:`str`、:class:`dict`、:class:`set`、:class:`frozenset`、:"
1850+ "class:`list` 和 :class:`tuple` 的實例。"
17891851
17901852#: ../../library/pickle.rst:913
17911853msgid ""
17921854"Here is a simple example where we allow pickling and reconstructing a given "
17931855"class::"
1794- msgstr ""
1856+ msgstr "以下是一個簡單的例子,我們示範如何允許封裝和重建給定的類別::: "
17951857
17961858#: ../../library/pickle.rst:916
17971859msgid ""
@@ -1823,10 +1885,37 @@ msgid ""
18231885"assert unpickled_class.__name__ == \" MyClass\" \n"
18241886"assert unpickled_class.my_attribute == 1"
18251887msgstr ""
1888+ "import io\n"
1889+ "import pickle\n"
1890+ "\n"
1891+ "class MyClass:\n"
1892+ " my_attribute = 1\n"
1893+ "\n"
1894+ "class MyPickler(pickle.Pickler):\n"
1895+ " def reducer_override(self, obj):\n"
1896+ " \"\"\" MyClass 的自訂縮減函數。\"\"\" \n"
1897+ " if getattr(obj, \" __name__\" , None) == \" MyClass\" :\n"
1898+ " return type, (obj.__name__, obj.__bases__,\n"
1899+ " {'my_attribute': obj.my_attribute})\n"
1900+ " else:\n"
1901+ " # 遭遇其他物件,則使用一般的縮減方式\n"
1902+ " return NotImplemented\n"
1903+ "\n"
1904+ "f = io.BytesIO()\n"
1905+ "p = MyPickler(f)\n"
1906+ "p.dump(MyClass)\n"
1907+ "\n"
1908+ "del MyClass\n"
1909+ "\n"
1910+ "unpickled_class = pickle.loads(f.getvalue())\n"
1911+ "\n"
1912+ "assert isinstance(unpickled_class, type)\n"
1913+ "assert unpickled_class.__name__ == \" MyClass\" \n"
1914+ "assert unpickled_class.my_attribute == 1\n"
18261915
18271916#: ../../library/pickle.rst:948
18281917msgid "Out-of-band Buffers"
1829- msgstr ""
1918+ msgstr "帶外(Out-of-band)資料緩衝區 "
18301919
18311920#: ../../library/pickle.rst:952
18321921msgid ""
@@ -1837,6 +1926,10 @@ msgid ""
18371926"structure of objects into a sequential stream of bytes, intrinsically "
18381927"involves copying data to and from the pickle stream."
18391928msgstr ""
1929+ ":mod:`pickle` 模組會被用於用於傳輸龐大的資料。此時,將複製記憶體的次數降到最"
1930+ "低以保持效能變得很重要。然而,:mod:`pickle` 模組的正常操作過程中,當它將物件"
1931+ "的圖狀結構(graph-like structure)轉換為連續的位元組串流時,本質上就涉及將資"
1932+ "料複製到封裝流以及從封裝流複製資料。"
18401933
18411934#: ../../library/pickle.rst:959
18421935msgid ""
@@ -1845,10 +1938,12 @@ msgid ""
18451938"implementation of the communications system) support the out-of-band "
18461939"transfer facilities provided by pickle protocol 5 and higher."
18471940msgstr ""
1941+ "如果*供給者*(被傳遞物件的型別的實作)與*消費者*(資訊交換系統的實作)都支援"
1942+ "由 pickle 協定 5 或更高版本提供的帶外傳輸功能,則可以避免此一先天限制。"
18481943
18491944#: ../../library/pickle.rst:965
18501945msgid "Provider API"
1851- msgstr ""
1946+ msgstr "供給者 API "
18521947
18531948#: ../../library/pickle.rst:967
18541949msgid ""
@@ -1857,6 +1952,9 @@ msgid ""
18571952"a :class:`PickleBuffer` instance (instead of e.g. a :class:`bytes` object) "
18581953"for any large data."
18591954msgstr ""
1955+ "要封裝的大型資料物件,則必須實作一個針對 5 版協定及以上的 :meth:`~object."
1956+ "__reduce_ex__` 方法,該方法應返回一個 :class:`PickleBuffer` 實例來處理任何大"
1957+ "型資料(而非返回如 :class:`bytes` 物件)。"
18601958
18611959#: ../../library/pickle.rst:972
18621960msgid ""
@@ -1866,16 +1964,21 @@ msgid ""
18661964"opt-in to tell :mod:`pickle` that they will handle those buffers by "
18671965"themselves."
18681966msgstr ""
1967+ "一個 :class:`PickleBuffer` 物件*指示*了當下底層的緩衝區狀態適合進行帶外資料傳"
1968+ "輸。這些物件仍然相容 :mod:`pickle` 模組的一般使用方式。消費者程式也可以選擇介"
1969+ "入,指示 :mod:`pickle` 他們將自行處理這些緩衝區。"
18691970
18701971#: ../../library/pickle.rst:979
18711972msgid "Consumer API"
1872- msgstr ""
1973+ msgstr "消費者 API "
18731974
18741975#: ../../library/pickle.rst:981
18751976msgid ""
18761977"A communications system can enable custom handling of the :class:"
18771978"`PickleBuffer` objects generated when serializing an object graph."
18781979msgstr ""
1980+ "一個資訊交換系統可以決定要自行處裡序列化物件圖時產生的 :class:`PickleBuffer` "
1981+ "物件。"
18791982
18801983#: ../../library/pickle.rst:984
18811984msgid ""
0 commit comments