Skip to content

Commit 21eb799

Browse files
committed
13.11小节完成
1 parent a1075de commit 21eb799

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Python内置的 ``fileinput`` 模块让这个变得简单。如果你有一个
2727
那么你就能以前面提到的所有方式来为此脚本提供输入。假设你将此脚本保存为 ``filein.py`` 并将其变为可执行文件,
2828
那么你可以像下面这样调用它,得到期望的输出:
2929

30-
.. code-block:: python
30+
.. code-block:: bash
3131
3232
$ ls | ./filein.py # Prints a directory listing to stdout.
3333
$ ./filein.py /etc/passwd # Reads /etc/passwd to stdout.

source/c13/p10_read_configuration_files.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
----------
1515
``configparser`` 模块能被用来读取配置文件。例如,假设你有如下的配置文件:
1616

17+
::
18+
1719
; config.ini
1820
; Sample configuration file
1921

@@ -72,6 +74,9 @@
7274
>>> cfg.set('debug','log_errors','False')
7375
>>> import sys
7476
>>> cfg.write(sys.stdout)
77+
78+
::
79+
7580
[installation]
7681
library = %(prefix)s/lib
7782
include = %(prefix)s/include
@@ -105,11 +110,15 @@
105110
对于可实现同样功能的配置文件和Python源文件是有很大的不同的。
106111
首先,配置文件的语法要更自由些,下面的赋值语句是等效的:
107112

113+
::
114+
108115
prefix=/usr/local
109116
prefix: /usr/local
110117

111118
配置文件中的名字是不区分大小写的。例如:
112119

120+
::
121+
113122
>>> cfg.get('installation','PREFIX')
114123
'/usr/local'
115124
>>> cfg.get('installation','prefix')
@@ -118,6 +127,8 @@
118127

119128
在解析值的时候,``getboolean()`` 方法查找任何可行的值。例如下面都是等价的:
120129

130+
::
131+
121132
log_errors = true
122133
log_errors = TRUE
123134
log_errors = Yes
@@ -127,6 +138,8 @@
127138
文件是安装一个整体被读取的。如果碰到了变量替换,它实际上已经被替换完成了。
128139
例如,在下面这个配置中,``prefix`` 变量在使用它的变量之前后之后定义都是可以的:
129140

141+
::
142+
130143
[installation]
131144
library=%(prefix)s/lib
132145
include=%(prefix)s/include
@@ -136,6 +149,8 @@
136149
``ConfigParser`` 有个容易被忽视的特性是它能一次读取多个配置文件然后合并成一个配置。
137150
例如,假设一个用户像下面这样构造了他们的配置文件:
138151

152+
::
153+
139154
; ~/.config.ini
140155
[installation]
141156
prefix=/Users/beazley/test
@@ -145,6 +160,8 @@
145160

146161
读取这个文件,它就能跟之前的配置合并起来。如:
147162

163+
.. code-block:: python
164+
148165
>>> # Previously read configuration
149166
>>> cfg.get('installation', 'prefix')
150167
'/usr/local'
@@ -166,6 +183,8 @@
166183
产生这种结果的原因是变量的改写采取的是后发制人策略,以最后一个为准。
167184
你可以像下面这样做试验:
168185

186+
.. code-block:: python
187+
169188
>>> cfg.get('installation','library')
170189
'/Users/beazley/test/lib'
171190
>>> cfg.set('installation','prefix','/tmp/dir')

source/c13/p11_add_logging_to_simple_scripts.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ example:
5151

5252
运行这个程序后,在文件 ``app.log`` 中的内容应该是下面这样:
5353

54+
::
55+
5456
CRITICAL:root:Host www.python.org unknown
5557
ERROR:root:Could not find 'spam'
5658

@@ -67,6 +69,8 @@ the basicConfig() call. For example:
6769
6870
最后输出变成如下:
6971

72+
::
73+
7074
CRITICAL:2012-11-20 12:27:13,595:Host www.python.org unknown
7175
ERROR:2012-11-20 12:27:13,595:Could not find 'spam'
7276
WARNING:2012-11-20 12:27:13,595:Feature is deprecated
@@ -86,6 +90,8 @@ the basicConfig() call. For example:
8690
8791
创建一个下面这样的文件,名字叫 ``logconfig.ini`` :
8892

93+
::
94+
8995
[loggers]
9096
keys=root
9197

0 commit comments

Comments
 (0)