@@ -1596,21 +1596,26 @@ msgstr ""
1596
1596
1597
1597
#: ../../library/pickle.rst:782
1598
1598
msgid "Dispatch Tables"
1599
- msgstr "分派表 "
1599
+ msgstr "調度表 "
1600
1600
1601
1601
#: ../../library/pickle.rst:784
1602
1602
msgid ""
1603
1603
"If one wants to customize pickling of some classes without disturbing any "
1604
1604
"other code which depends on pickling, then one can create a pickler with a "
1605
1605
"private dispatch table."
1606
1606
msgstr ""
1607
+ "如果你希望在不干擾其他物件正常封裝的前提下建立一個針對特定物件的封裝器,你可"
1608
+ "建立一個有私密調度表的封裝器。"
1607
1609
1608
1610
#: ../../library/pickle.rst:788
1609
1611
msgid ""
1610
1612
"The global dispatch table managed by the :mod:`copyreg` module is available "
1611
1613
"as :data:`!copyreg.dispatch_table`. Therefore, one may choose to use a "
1612
1614
"modified copy of :data:`!copyreg.dispatch_table` as a private dispatch table."
1613
1615
msgstr ""
1616
+ "由 :mod:`copyreg` 模組管理的全域調度表可以 :data:`!copyreg.dispatch_table` 呼"
1617
+ "叫。你可以透過這個方式來基於原始 :data:`!copyreg.dispatch_table` 創建一個修改"
1618
+ "過的版本,作為你的專屬用途的調度表。"
1614
1619
1615
1620
#: ../../library/pickle.rst:793
1616
1621
msgid "For example ::"
@@ -1633,6 +1638,8 @@ msgid ""
1633
1638
"creates an instance of :class:`pickle.Pickler` with a private dispatch table "
1634
1639
"which handles the ``SomeClass`` class specially. Alternatively, the code ::"
1635
1640
msgstr ""
1641
+ "建立了一個 :class:`pickle.Pickler`,其中含有專門處裡 ``SomeClass`` 類別的專屬"
1642
+ "調度表。此外,你也可以寫作:::"
1636
1643
1637
1644
#: ../../library/pickle.rst:804
1638
1645
msgid ""
@@ -1653,6 +1660,8 @@ msgid ""
1653
1660
"does the same but all instances of ``MyPickler`` will by default share the "
1654
1661
"private dispatch table. On the other hand, the code ::"
1655
1662
msgstr ""
1663
+ "這樣可產生相似的結果,唯一不同的是往後所有 ``MyPickler`` 預設都會使用這個專屬"
1664
+ "調度表。最後,如果將程式寫為:::"
1656
1665
1657
1666
#: ../../library/pickle.rst:813
1658
1667
msgid ""
@@ -1668,11 +1677,11 @@ msgstr ""
1668
1677
msgid ""
1669
1678
"modifies the global dispatch table shared by all users of the :mod:`copyreg` "
1670
1679
"module."
1671
- msgstr ""
1680
+ msgstr "則會改變 :mod:`copyreg` 模組內建、所有使用者共通的調度表。 "
1672
1681
1673
1682
#: ../../library/pickle.rst:822
1674
1683
msgid "Handling Stateful Objects"
1675
- msgstr ""
1684
+ msgstr "處裡紀錄大量狀態的物件 "
1676
1685
1677
1686
#: ../../library/pickle.rst:828
1678
1687
msgid ""
@@ -1685,6 +1694,12 @@ msgid ""
1685
1694
"__setstate__` and :meth:`!__getstate__` methods are used to implement this "
1686
1695
"behavior. ::"
1687
1696
msgstr ""
1697
+ "以下的範例展示了如何修改針對特定類別封裝時的行為。下面的 :class:`!"
1698
+ "TextReader` 類別會開啟一個文字檔案,並在每次呼叫其 :meth:`!readline` 方法時返"
1699
+ "回當前行編號與該行內容。如果 :class:`!TextReader` 實例被封裝,所有*除了檔案物"
1700
+ "件之外*的屬性成員都會被保存。在該實例被拆封時,檔案將被重新開啟,並從上次的位"
1701
+ "置繼續讀取。這個行為的達成是透過 :meth:`!__setstate__` 和 :meth:`!"
1702
+ "__getstate__` 方法來實作的。::"
1688
1703
1689
1704
#: ../../library/pickle.rst:836
1690
1705
msgid ""
@@ -1725,10 +1740,45 @@ msgid ""
1725
1740
" # Finally, save the file.\n"
1726
1741
" self.file = file"
1727
1742
msgstr ""
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"
1728
1778
1729
1779
#: ../../library/pickle.rst:874
1730
1780
msgid "A sample usage might be something like this::"
1731
- msgstr ""
1781
+ msgstr "可以這樣實際使用::: "
1732
1782
1733
1783
#: ../../library/pickle.rst:876
1734
1784
msgid ""
@@ -1752,7 +1802,7 @@ msgstr ""
1752
1802
1753
1803
#: ../../library/pickle.rst:888
1754
1804
msgid "Custom Reduction for Types, Functions, and Other Objects"
1755
- msgstr ""
1805
+ msgstr "針對型別、函數或特定物件定製縮減函數 "
1756
1806
1757
1807
#: ../../library/pickle.rst:892
1758
1808
msgid ""
@@ -1761,6 +1811,9 @@ msgid ""
1761
1811
"the object's type, or we may want to customize the pickling of functions and "
1762
1812
"classes."
1763
1813
msgstr ""
1814
+ "有時候,:attr:`~Pickler.dispatch_table` 的彈性空間可能不夠。尤其當我們想要使"
1815
+ "用型別以外的方式來判斷如何使用自訂封裝、或者我們想要自訂特定函式和類別的封裝"
1816
+ "方法時。"
1764
1817
1765
1818
#: ../../library/pickle.rst:897
1766
1819
msgid ""
@@ -1770,13 +1823,18 @@ msgid ""
1770
1823
"alternatively return :data:`NotImplemented` to fallback to the traditional "
1771
1824
"behavior."
1772
1825
msgstr ""
1826
+ "如果是這樣的話,可以繼承 :class:`Pickler` 類別並實作一個 :meth:`~Pickler."
1827
+ "reducer_override` 方法。此方法可以回傳任意的縮減元組(參閱 :meth:`~object."
1828
+ "__reduce__`)、也可以回傳 :data:`NotImplemented` 以回退至原始的行為。"
1773
1829
1774
1830
#: ../../library/pickle.rst:902
1775
1831
msgid ""
1776
1832
"If both the :attr:`~Pickler.dispatch_table` and :meth:`~Pickler."
1777
1833
"reducer_override` are defined, then :meth:`~Pickler.reducer_override` method "
1778
1834
"takes priority."
1779
1835
msgstr ""
1836
+ "如果 :attr:`~Pickler.dispatch_table` 和 :meth:`~Pickler.reducer_override` 都"
1837
+ "被定義了的話,:meth:`~Pickler.reducer_override` 的優先度較高。"
1780
1838
1781
1839
#: ../../library/pickle.rst:907
1782
1840
msgid ""
@@ -1786,12 +1844,16 @@ msgid ""
1786
1844
"class:`dict`, :class:`set`, :class:`frozenset`, :class:`list` and :class:"
1787
1845
"`tuple`."
1788
1846
msgstr ""
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` 的實例。"
1789
1851
1790
1852
#: ../../library/pickle.rst:913
1791
1853
msgid ""
1792
1854
"Here is a simple example where we allow pickling and reconstructing a given "
1793
1855
"class::"
1794
- msgstr ""
1856
+ msgstr "以下是一個簡單的例子,我們示範如何允許封裝和重建給定的類別::: "
1795
1857
1796
1858
#: ../../library/pickle.rst:916
1797
1859
msgid ""
@@ -1823,10 +1885,37 @@ msgid ""
1823
1885
"assert unpickled_class.__name__ == \" MyClass\" \n"
1824
1886
"assert unpickled_class.my_attribute == 1"
1825
1887
msgstr ""
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"
1826
1915
1827
1916
#: ../../library/pickle.rst:948
1828
1917
msgid "Out-of-band Buffers"
1829
- msgstr ""
1918
+ msgstr "帶外(Out-of-band)資料緩衝區 "
1830
1919
1831
1920
#: ../../library/pickle.rst:952
1832
1921
msgid ""
@@ -1837,6 +1926,10 @@ msgid ""
1837
1926
"structure of objects into a sequential stream of bytes, intrinsically "
1838
1927
"involves copying data to and from the pickle stream."
1839
1928
msgstr ""
1929
+ ":mod:`pickle` 模組會被用於用於傳輸龐大的資料。此時,將複製記憶體的次數降到最"
1930
+ "低以保持效能變得很重要。然而,:mod:`pickle` 模組的正常操作過程中,當它將物件"
1931
+ "的圖狀結構(graph-like structure)轉換為連續的位元組串流時,本質上就涉及將資"
1932
+ "料複製到封裝流以及從封裝流複製資料。"
1840
1933
1841
1934
#: ../../library/pickle.rst:959
1842
1935
msgid ""
@@ -1845,10 +1938,12 @@ msgid ""
1845
1938
"implementation of the communications system) support the out-of-band "
1846
1939
"transfer facilities provided by pickle protocol 5 and higher."
1847
1940
msgstr ""
1941
+ "如果*供給者*(被傳遞物件的型別的實作)與*消費者*(資訊交換系統的實作)都支援"
1942
+ "由 pickle 協定 5 或更高版本提供的帶外傳輸功能,則可以避免此一先天限制。"
1848
1943
1849
1944
#: ../../library/pickle.rst:965
1850
1945
msgid "Provider API"
1851
- msgstr ""
1946
+ msgstr "供給者 API "
1852
1947
1853
1948
#: ../../library/pickle.rst:967
1854
1949
msgid ""
@@ -1857,6 +1952,9 @@ msgid ""
1857
1952
"a :class:`PickleBuffer` instance (instead of e.g. a :class:`bytes` object) "
1858
1953
"for any large data."
1859
1954
msgstr ""
1955
+ "要封裝的大型資料物件,則必須實作一個針對 5 版協定及以上的 :meth:`~object."
1956
+ "__reduce_ex__` 方法,該方法應返回一個 :class:`PickleBuffer` 實例來處理任何大"
1957
+ "型資料(而非返回如 :class:`bytes` 物件)。"
1860
1958
1861
1959
#: ../../library/pickle.rst:972
1862
1960
msgid ""
@@ -1866,16 +1964,21 @@ msgid ""
1866
1964
"opt-in to tell :mod:`pickle` that they will handle those buffers by "
1867
1965
"themselves."
1868
1966
msgstr ""
1967
+ "一個 :class:`PickleBuffer` 物件*指示*了當下底層的緩衝區狀態適合進行帶外資料傳"
1968
+ "輸。這些物件仍然相容 :mod:`pickle` 模組的一般使用方式。消費者程式也可以選擇介"
1969
+ "入,指示 :mod:`pickle` 他們將自行處理這些緩衝區。"
1869
1970
1870
1971
#: ../../library/pickle.rst:979
1871
1972
msgid "Consumer API"
1872
- msgstr ""
1973
+ msgstr "消費者 API "
1873
1974
1874
1975
#: ../../library/pickle.rst:981
1875
1976
msgid ""
1876
1977
"A communications system can enable custom handling of the :class:"
1877
1978
"`PickleBuffer` objects generated when serializing an object graph."
1878
1979
msgstr ""
1980
+ "一個資訊交換系統可以決定要自行處裡序列化物件圖時產生的 :class:`PickleBuffer` "
1981
+ "物件。"
1879
1982
1880
1983
#: ../../library/pickle.rst:984
1881
1984
msgid ""
0 commit comments