You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 11.-brief-tour-of-the-standard-library-part-ii.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
# 11. Brief Tour of the Standard Library — Part II
1
+
# 11. Khám phá thư viện chuẩn — phần II
2
2
3
3
4
4
5
-
This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts.
5
+
Chuyến đi thứ hai này bao hàm những module nâng cao hơn sẽ hỗ trợ cho các nhu cầu lập trình chuyên nghiệp. Những module này hiếm khi xuất hiện trong các đoạn mã nhỏ.
6
6
7
-
### 11.1. Output Formatting
7
+
### 11.1. Định dạng ngõ ra (Output Formatting)
8
8
9
-
The[`reprlib`](https://docs.python.org/3/library/reprlib.html#module-reprlib)module provides a version of [`repr()`](https://docs.python.org/3/library/functions.html#repr)customized for abbreviated displays of large or deeply nested containers:>>>
9
+
Module[`reprlib`](https://docs.python.org/3/library/reprlib.html#module-reprlib)cung cấp một phiên bản tùy chỉnh của [`repr()`](https://docs.python.org/3/library/functions.html#repr)cho những hiện thị rút gọn của những containers lớn:>>>
The[`pprint`](https://docs.python.org/3/library/pprint.html#module-pprint)module offers more sophisticated control over printing both built-in and user defined objects in a way that is readable by the interpreter. When the result is longer than one line, the “pretty printer” adds line breaks and indentation to more clearly reveal data structure:>>>
17
+
Module[`pprint`](https://docs.python.org/3/library/pprint.html#module-pprint)hỗ trợ kiểm soát tinh vi hơn qua việc xuất cả những đối tượng có sẵn và được người dùng định nghĩa. Khi kết quả dài hơn một dòng, “pretty printer” thêm vào dòng trống và căn lề để cấu trúc dữ liệu hiển thị rõ ràng hơn:>>>
18
18
19
19
```text
20
20
>>> import pprint
@@ -29,7 +29,7 @@ The [`pprint`](https://docs.python.org/3/library/pprint.html#module-pprint) modu
29
29
'blue']]]
30
30
```
31
31
32
-
The[`textwrap`](https://docs.python.org/3/library/textwrap.html#module-textwrap)module formats paragraphs of text to fit a given screen width:>>>
32
+
Module[`textwrap`](https://docs.python.org/3/library/textwrap.html#module-textwrap)định dạng đoạn văn bản cho phù hợp với chiều rộng màn hình hiển thị cho trước:>>>
33
33
34
34
```text
35
35
>>> import textwrap
@@ -44,7 +44,7 @@ instead of one big string with newlines
44
44
to separate the wrapped lines.
45
45
```
46
46
47
-
The[`locale`](https://docs.python.org/3/library/locale.html#module-locale)module accesses a database of culture specific data formats. The grouping attribute of locale’s format function provides a direct way of formatting numbers with group separators:>>>
47
+
Module[`locale`](https://docs.python.org/3/library/locale.html#module-locale)truy cập vào một cơ sở dữ liệu của các định dạng dữ liệu cụ thể theo văn hóa. Thuộc tính nhóm của hàm định dạng của locale cung cấp một cách thức trực tiếp cho việc định dạng số với các phân cách nhóm:>>>
48
48
49
49
```text
50
50
>>> import locale
@@ -59,11 +59,11 @@ The [`locale`](https://docs.python.org/3/library/locale.html#module-locale) modu
59
59
'$1,234,567.80'
60
60
```
61
61
62
-
### 11.2. Templating
62
+
### 11.2. Khuôn mẫu (Templating)
63
63
64
-
The[`string`](https://docs.python.org/3/library/string.html#module-string)module includes a versatile [`Template`](https://docs.python.org/3/library/string.html#string.Template)class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application.
64
+
Module[`string`](https://docs.python.org/3/library/string.html#module-string)bao gồm một lớp (class) đa năng [`Template`](https://docs.python.org/3/library/string.html#string.Template)với cú pháp được đơn giản hóa phù hợp cho việc chỉnh sửa của người dùng cuối(end-user). Việc này cho phép người dùng tùy chỉnh các ứng dụng mà không cần phải thay thế.
65
65
66
-
The format uses placeholder names formed by`$`with valid Python identifiers \(alphanumeric characters and underscores\). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing `$$`creates a single escaped `$`:>>>
66
+
Định dạng sử dụng các tên giữ chỗ(placeholder) biểu thị bởi`$`với các định danh hợp lệ của Python \(chữ số và dấu gạch dưới\). Sử dụng dấu ngoặc nhọn cho placeholder cho phép nhiều kí tự chữ số theo sau nó mà không cần có dấu cách. Việc sử dụng `$$`trả về `$`, tức là bỏ qua chức năng của `$` trong cú pháp, hiểu như là một ký tự (tương tự escape `\` trong Linux shell) :>>>
67
67
68
68
```text
69
69
>>> from string import Template
@@ -72,7 +72,7 @@ The format uses placeholder names formed by `$` with valid Python identifiers \(
72
72
'Nottinghamfolk send $10 to the ditch fund.'
73
73
```
74
74
75
-
The [`substitute()`](https://docs.python.org/3/library/string.html#string.Template.substitute)method raises a[`KeyError`](https://docs.python.org/3/library/exceptions.html#KeyError)when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the [`safe_substitute()`](https://docs.python.org/3/library/string.html#string.Template.safe_substitute)method may be more appropriate — it will leave placeholders unchanged if data is missing:>>>
75
+
Phương pháp [`substitute()`](https://docs.python.org/3/library/string.html#string.Template.substitute)tạo ra một[`KeyError`](https://docs.python.org/3/library/exceptions.html#KeyError)khi một placeholder không được hỗ trợ trong từ điển hoặc đối số từ khóa. Cho những ứng dụng kiểu như tổng hợp mail, dữ liệu được cung cấp có thể chưa hoàn chỉnh và phương pháp [`safe_substitute()`](https://docs.python.org/3/library/string.html#string.Template.safe_substitute) có thể phù hợp hơn — nó sẽ giữ nguyên placeholders nếu dữ liệu bị thiếu:>>>
76
76
77
77
```text
78
78
>>> t = Template('Return the $item to $owner.')
@@ -85,7 +85,7 @@ KeyError: 'owner'
85
85
'Return the unladen swallow to $owner.'
86
86
```
87
87
88
-
Template subclasses can specify a custom delimiter. For example, a batch renaming utility for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or file format:>>>
88
+
Các lớp phụ của Template có thể chỉ rõ dấu phân cách tùy chỉnh. Ví dụ, một tiện ích đổi tên hàng loạt cho một trình ảnh có thể chọn sử dụng dấu phần trăm (%) cho placeholder như ngày hiện tại, số thứ tự ảnh, hoặc định dạng file:>>>
Another application for templating is separating program logic from the details of multiple output formats. This makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports.
110
+
Ứng dụng khác cho khuôn mẫu là phân chia logic chương trình từ những chi tiết của các định dạng đa ngõ ra. Điều này khiến nó khả dụng cho các khuôn mẫu tùy chỉnh thay thế ở các files XML, báo cáo văn bản, và báo cáo web HTML.
111
111
112
-
### 11.3. Working with Binary Data Record Layouts
112
+
### 11.3. Làm việc với những cách bố trí bản ghi dữ liệu nhị phân (Binary Data Record Layouts)
113
113
114
-
The[`struct`](https://docs.python.org/3/library/struct.html#module-struct)module provides [`pack()`](https://docs.python.org/3/library/struct.html#struct.pack)and[`unpack()`](https://docs.python.org/3/library/struct.html#struct.unpack)functions for working with variable length binary record formats. The following example shows how to loop through header information in a ZIP file without using the[`zipfile`](https://docs.python.org/3/library/zipfile.html#module-zipfile)module. Pack codes `"H"`and`"I"`represent two and four byte unsigned numbers respectively. The `"<"`indicates that they are standard size and in little-endian byte order:
114
+
Module[`struct`](https://docs.python.org/3/library/struct.html#module-struct)cung cấp các hàm [`pack()`](https://docs.python.org/3/library/struct.html#struct.pack)và[`unpack()`](https://docs.python.org/3/library/struct.html#struct.unpack)cho quá trình làm việc với các định dạng bản ghi nhị phân có chiều dài thay đổi được. Ví dụ tiếp theo chỉ ra cách lặp thông qua thông tin đầu (header information) trong một file ZIP mà không sử dụng module [`zipfile`](https://docs.python.org/3/library/zipfile.html#module-zipfile). Các mã đóng gói `"H"`và`"I"`đại diện cho 2 và 4 byte số không dấu. Kí hiệu `"<"` mô tả nó đang ở kích thước tiêu chuẩn và theo thứ tự byte là little-endian:
115
115
116
116
```text
117
117
import struct
@@ -120,7 +120,7 @@ with open('myfile.zip', 'rb') as f:
120
120
data = f.read()
121
121
122
122
start = 0
123
-
for i in range(3): # show the first 3 file headers
123
+
for i in range(3): # chỉ đến 3 header đầu tiên của file
start += extra_size + comp_size # skip to the next header
134
+
start += extra_size + comp_size # bỏ qua cho đến header tiếp theo
135
135
```
136
136
137
-
### 11.4. Multi-threading
137
+
### 11.4. Đa luồng (Multi-threading)
138
138
139
139
Threading is a technique for decoupling tasks which are not sequentially dependent. Threads can be used to improve the responsiveness of applications that accept user input while other tasks run in the background. A related use case is running I/O in parallel with computations in another thread.
0 commit comments