diff --git a/source/c01/p01_unpack_sequence_into_separate_variables.rst b/source/c01/p01_unpack_sequence_into_separate_variables.rst index f0a26d9..fabaed0 100644 --- a/source/c01/p01_unpack_sequence_into_separate_variables.rst +++ b/source/c01/p01_unpack_sequence_into_separate_variables.rst @@ -7,8 +7,6 @@ ---------- 现在有一个包含N个元素的元组或者是序列,怎样将它里面的值解压后同时赋值给N个变量? -| - ---------- 解决方案 ---------- @@ -56,8 +54,6 @@ ValueError: need more than 2 values to unpack >>> -| - ---------- 讨论 ---------- diff --git a/source/c01/p02_unpack_elements_from_iterables.rst b/source/c01/p02_unpack_elements_from_iterables.rst index 739873d..2b4d780 100644 --- a/source/c01/p02_unpack_elements_from_iterables.rst +++ b/source/c01/p02_unpack_elements_from_iterables.rst @@ -8,8 +8,6 @@ 如果一个可迭代对象的元素个数超过变量个数时,会抛出一个 ``ValueError`` 。 那么怎样才能从这个可迭代对象中解压出N个元素出来? -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ Python的星号表达式可以用来解决这个问题。比如,你在学习 >>> current 3 -| - ---------- 讨论 ---------- diff --git a/source/c01/p03_keep_last_n_items.rst b/source/c01/p03_keep_last_n_items.rst index 2b6b8ae..90e8901 100644 --- a/source/c01/p03_keep_last_n_items.rst +++ b/source/c01/p03_keep_last_n_items.rst @@ -7,8 +7,6 @@ ---------- 在迭代操作或者其他操作的时候,怎样只保留最后有限几个元素的历史记录? -| - ---------- 解决方案 ---------- @@ -36,8 +34,6 @@ print(line, end='') print('-' * 20) -| - ---------- 讨论 ---------- diff --git a/source/c01/p04_find_largest_or_smallest_n_items.rst b/source/c01/p04_find_largest_or_smallest_n_items.rst index b3eac20..dce4617 100644 --- a/source/c01/p04_find_largest_or_smallest_n_items.rst +++ b/source/c01/p04_find_largest_or_smallest_n_items.rst @@ -7,8 +7,6 @@ ---------- 怎样从一个集合中获得最大或者最小的N个元素列表? -| - ---------- 解决方案 ---------- @@ -38,8 +36,6 @@ heapq模块有两个函数:``nlargest()`` 和 ``nsmallest()`` 可以完美解 译者注:上面代码在对每个元素进行对比的时候,会以 ``price`` 的值进行比较。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p05_implement_a_priority_queue.rst b/source/c01/p05_implement_a_priority_queue.rst index f8183b5..68fe840 100644 --- a/source/c01/p05_implement_a_priority_queue.rst +++ b/source/c01/p05_implement_a_priority_queue.rst @@ -7,8 +7,6 @@ ---------- 怎样实现一个按优先级排序的队列? 并且在这个队列上面每次pop操作总是返回优先级最高的那个元素 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ 仔细观察可以发现,第一个 ``pop()`` 操作返回优先级最高的元素。 另外注意到如果两个有着相同优先级的元素( ``foo`` 和 ``grok`` ),pop操作按照它们被插入到队列的顺序返回的。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p06_map_keys_to_multiple_values_in_dict.rst b/source/c01/p06_map_keys_to_multiple_values_in_dict.rst index d5df2a4..55a1f91 100644 --- a/source/c01/p06_map_keys_to_multiple_values_in_dict.rst +++ b/source/c01/p06_map_keys_to_multiple_values_in_dict.rst @@ -7,8 +7,6 @@ ---------- 怎样实现一个键对应多个值的字典(也叫 ``multidict`` )? -| - ---------- 解决方案 ---------- @@ -58,8 +56,6 @@ 但是很多程序员觉得 ``setdefault()`` 用起来有点别扭。因为每次调用都得创建一个新的初始值的实例(例子程序中的空列表[])。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p07_keep_dict_in_order.rst b/source/c01/p07_keep_dict_in_order.rst index b3b00e6..902821a 100644 --- a/source/c01/p07_keep_dict_in_order.rst +++ b/source/c01/p07_keep_dict_in_order.rst @@ -7,8 +7,6 @@ ---------- 你想创建一个字典,并且在迭代或序列化这个字典的时候能够控制元素的顺序。 -| - ---------- 解决方案 ---------- @@ -38,8 +36,6 @@ '{"foo": 1, "bar": 2, "spam": 3, "grok": 4}' >>> -| - ---------- 讨论 ---------- diff --git a/source/c01/p08_calculating_with_dict.rst b/source/c01/p08_calculating_with_dict.rst index 57561ad..80f590d 100644 --- a/source/c01/p08_calculating_with_dict.rst +++ b/source/c01/p08_calculating_with_dict.rst @@ -7,8 +7,6 @@ ---------- 怎样在数据字典中执行一些计算操作(比如求最小值、最大值、排序等等)? -| - ---------- 解决方案 ---------- @@ -52,8 +50,6 @@ print(min(prices_and_names)) # OK print(max(prices_and_names)) # ValueError: max() arg is an empty sequence -| - ---------- 讨论 ---------- diff --git a/source/c01/p09_find_commonalities_in_dicts.rst b/source/c01/p09_find_commonalities_in_dicts.rst index 5e93a25..8c8326e 100644 --- a/source/c01/p09_find_commonalities_in_dicts.rst +++ b/source/c01/p09_find_commonalities_in_dicts.rst @@ -7,8 +7,6 @@ ---------- 怎样在两个字典中寻寻找相同点(比如相同的键、相同的值等等)? -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ c = {key:a[key] for key in a.keys() - {'z', 'w'}} # c is {'x': 1, 'y': 2} -| - ---------- 讨论 ---------- diff --git a/source/c01/p10_remove_duplicates_from_seq_order.rst b/source/c01/p10_remove_duplicates_from_seq_order.rst index 0c36eb1..168da12 100644 --- a/source/c01/p10_remove_duplicates_from_seq_order.rst +++ b/source/c01/p10_remove_duplicates_from_seq_order.rst @@ -7,8 +7,6 @@ ---------- 怎样在一个序列上面保持元素顺序的同时消除重复的值? -| - ---------- 解决方案 ---------- @@ -56,8 +54,6 @@ 如果你想基于单个字段、属性或者某个更大的数据结构来消除重复元素,第二种方案同样可以胜任。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p11_naming_slice.rst b/source/c01/p11_naming_slice.rst index 7986a88..f3c10c6 100644 --- a/source/c01/p11_naming_slice.rst +++ b/source/c01/p11_naming_slice.rst @@ -7,8 +7,6 @@ ---------- 你的程序已经出现一大堆已无法直视的硬编码切片下标,然后你想清理下代码。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ 第二种版本中,你避免了大量无法理解的硬编码下标,使得你的代码更加清晰可读了。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p12_determine_most_freqently_items_in_seq.rst b/source/c01/p12_determine_most_freqently_items_in_seq.rst index 82a3d67..e4b69ba 100644 --- a/source/c01/p12_determine_most_freqently_items_in_seq.rst +++ b/source/c01/p12_determine_most_freqently_items_in_seq.rst @@ -7,8 +7,6 @@ ---------- 怎样找出一个序列中出现次数最多的元素呢? -| - ---------- 解决方案 ---------- @@ -32,8 +30,6 @@ print(top_three) # Outputs [('eyes', 8), ('the', 5), ('look', 4)] -| - ---------- 讨论 ---------- diff --git a/source/c01/p13_sort_list_of_dicts_by_key.rst b/source/c01/p13_sort_list_of_dicts_by_key.rst index b682582..a5a158c 100644 --- a/source/c01/p13_sort_list_of_dicts_by_key.rst +++ b/source/c01/p13_sort_list_of_dicts_by_key.rst @@ -7,8 +7,6 @@ ---------- 你有一个字典列表,你想根据某个或某几个字典字段来排序这个列表。 -| - ---------- 解决方案 ---------- @@ -63,8 +61,6 @@ {'fname': 'Big', 'uid': 1004, 'lname': 'Jones'}, {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}] -| - ---------- 讨论 ---------- diff --git a/source/c01/p14_sort_objects_without_compare_support.rst b/source/c01/p14_sort_objects_without_compare_support.rst index 96a9d40..b388444 100644 --- a/source/c01/p14_sort_objects_without_compare_support.rst +++ b/source/c01/p14_sort_objects_without_compare_support.rst @@ -7,8 +7,6 @@ ---------- 你想排序类型相同的对象,但是他们不支持原生的比较操作。 -| - ---------- 解决方案 ---------- @@ -41,8 +39,6 @@ [User(3), User(23), User(99)] >>> -| - ---------- 讨论 ---------- diff --git a/source/c01/p15_group_records_based_on_field.rst b/source/c01/p15_group_records_based_on_field.rst index 2372fbe..8fd3786 100644 --- a/source/c01/p15_group_records_based_on_field.rst +++ b/source/c01/p15_group_records_based_on_field.rst @@ -7,8 +7,6 @@ ---------- 你有一个字典或者实例的序列,然后你想根据某个特定的字段比如 ``date`` 来分组迭代访问。 -| - ---------- 解决方案 ---------- @@ -61,8 +59,6 @@ {'date': '07/04/2012', 'address': '5148 N CLARK'} {'date': '07/04/2012', 'address': '1039 W GRANVILLE'} -| - ---------- 讨论 ---------- diff --git a/source/c01/p16_filter_sequence_elements.rst b/source/c01/p16_filter_sequence_elements.rst index bf73da3..dea8a93 100644 --- a/source/c01/p16_filter_sequence_elements.rst +++ b/source/c01/p16_filter_sequence_elements.rst @@ -7,8 +7,6 @@ ---------- 你有一个数据序列,想利用一些规则从中提取出需要的值或者是缩短序列 -| - ---------- 解决方案 ---------- @@ -58,8 +56,6 @@ # Outputs ['1', '2', '-3', '4', '5'] ``filter()`` 函数创建了一个迭代器,因此如果你想得到一个列表的话,就得像示例那样使用 ``list()`` 去转换。 -| - ---------- 讨论 ---------- diff --git a/source/c01/p17_extract_subset_of_dict.rst b/source/c01/p17_extract_subset_of_dict.rst index 475e874..27026c3 100644 --- a/source/c01/p17_extract_subset_of_dict.rst +++ b/source/c01/p17_extract_subset_of_dict.rst @@ -7,8 +7,6 @@ ---------- 你想构造一个字典,它是另外一个字典的子集。 -| - ---------- 解决方案 ---------- @@ -29,8 +27,6 @@ tech_names = {'AAPL', 'IBM', 'HPQ', 'MSFT'} p2 = {key: value for key, value in prices.items() if key in tech_names} -| - ---------- 讨论 ---------- diff --git a/source/c01/p18_map_names_to_sequence_elements.rst b/source/c01/p18_map_names_to_sequence_elements.rst index a711eb3..39b59b8 100644 --- a/source/c01/p18_map_names_to_sequence_elements.rst +++ b/source/c01/p18_map_names_to_sequence_elements.rst @@ -8,8 +8,6 @@ 你有一段通过下标访问列表或者元组中元素的代码,但是这样有时候会使得你的代码难以阅读, 于是你想通过名称来访问元素。 -| - ---------- 解决方案 ---------- @@ -74,8 +72,6 @@ total += s.shares * s.price return total -| - ---------- 讨论 ---------- diff --git a/source/c01/p19_transform_and_reduce_data_same_time.rst b/source/c01/p19_transform_and_reduce_data_same_time.rst index 4f689f5..ce1a958 100644 --- a/source/c01/p19_transform_and_reduce_data_same_time.rst +++ b/source/c01/p19_transform_and_reduce_data_same_time.rst @@ -8,8 +8,6 @@ 你需要在数据序列上执行聚集函数(比如 ``sum()`` , ``min()`` , ``max()`` ), 但是首先你需要先转换或者过滤数据 -| - ---------- 解决方案 ---------- @@ -44,8 +42,6 @@ ] min_shares = min(s['shares'] for s in portfolio) -| - ---------- 讨论 ---------- diff --git a/source/c01/p20_combine_multiple_map_to_single_map.rst b/source/c01/p20_combine_multiple_map_to_single_map.rst index 8fb1359..16f9f3e 100644 --- a/source/c01/p20_combine_multiple_map_to_single_map.rst +++ b/source/c01/p20_combine_multiple_map_to_single_map.rst @@ -8,8 +8,6 @@ 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在。 -| - ---------- 解决方案 ---------- @@ -31,8 +29,6 @@ print(c['y']) # Outputs 2 (from b) print(c['z']) # Outputs 3 (from a) -| - ---------- 讨论 ---------- diff --git a/source/c02/p01_split_string_on_multiple_delimiters.rst b/source/c02/p01_split_string_on_multiple_delimiters.rst index 64ed4c1..09bcad4 100644 --- a/source/c02/p01_split_string_on_multiple_delimiters.rst +++ b/source/c02/p01_split_string_on_multiple_delimiters.rst @@ -7,8 +7,6 @@ ---------- 你需要将一个字符串分割为多个字段,但是分隔符(还有周围的空格)并不是固定的。 -| - ---------- 解决方案 ---------- @@ -23,8 +21,6 @@ >>> re.split(r'[;,\s]\s*', line) ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo'] -| - ---------- 讨论 ---------- diff --git a/source/c02/p02_match_text_at_start_end.rst b/source/c02/p02_match_text_at_start_end.rst index 448dec7..d1c191b 100644 --- a/source/c02/p02_match_text_at_start_end.rst +++ b/source/c02/p02_match_text_at_start_end.rst @@ -7,8 +7,6 @@ ---------- 你需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URL Scheme等等。 -| - ---------- 解决方案 ---------- @@ -70,8 +68,6 @@ True >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p03_match_strings_with_shell_wildcard.rst b/source/c02/p03_match_strings_with_shell_wildcard.rst index c6db5a6..9c9448b 100644 --- a/source/c02/p03_match_strings_with_shell_wildcard.rst +++ b/source/c02/p03_match_strings_with_shell_wildcard.rst @@ -7,8 +7,6 @@ ---------- 你想使用 **Unix Shell** 中常用的通配符(比如 ``*.py`` , ``Dat[0-9]*.csv`` 等)去匹配文本字符串 -| - ---------- 解决方案 ---------- @@ -72,8 +70,6 @@ ['5412 N CLARK ST'] >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p04_match_and_search_text.rst b/source/c02/p04_match_and_search_text.rst index b6f14dc..d9025be 100644 --- a/source/c02/p04_match_and_search_text.rst +++ b/source/c02/p04_match_and_search_text.rst @@ -7,8 +7,6 @@ ---------- 你想匹配或者搜索特定模式的文本 -| - ---------- 解决方案 ---------- @@ -135,8 +133,6 @@ ('3', '13', '2013') >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p05_search_and_replace_text.rst b/source/c02/p05_search_and_replace_text.rst index f101f5f..dc73462 100644 --- a/source/c02/p05_search_and_replace_text.rst +++ b/source/c02/p05_search_and_replace_text.rst @@ -7,8 +7,6 @@ ---------- 你想在字符串中搜索和匹配指定的文本模式 -| - ---------- 解决方案 ---------- @@ -71,8 +69,6 @@ 2 >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p06_search_replace_case_insensitive.rst b/source/c02/p06_search_replace_case_insensitive.rst index 407bdef..a6e3b9d 100644 --- a/source/c02/p06_search_replace_case_insensitive.rst +++ b/source/c02/p06_search_replace_case_insensitive.rst @@ -7,8 +7,6 @@ ---------- 你需要以忽略大小写的方式搜索与替换文本字符串 -| - ---------- 解决方案 ---------- @@ -52,8 +50,6 @@ 译者注: ``matchcase('snake')`` 返回了一个回调函数(参数必须是 ``match`` 对象),前面一节一节提到过, ``sub()`` 函数除了接受替换字符串外,还能接受一个回调函数。 -| - ---------- 讨论 ---------- diff --git a/source/c02/p07_specify_regexp_for_shortest_match.rst b/source/c02/p07_specify_regexp_for_shortest_match.rst index 04959e6..4560197 100644 --- a/source/c02/p07_specify_regexp_for_shortest_match.rst +++ b/source/c02/p07_specify_regexp_for_shortest_match.rst @@ -8,8 +8,6 @@ 你正在试着用正则表达式匹配某个文本模式,但是它找到的是模式的最长可能匹配。 而你想修改它变成查找最短的可能匹配。 -| - ---------- 解决方案 ---------- @@ -42,8 +40,6 @@ 这样就使得匹配变成非贪婪模式,从而得到最短的匹配,也就是我们想要的结果。 -| - ---------- 讨论 ---------- diff --git a/source/c02/p08_regexp_for_multiline_partterns.rst b/source/c02/p08_regexp_for_multiline_partterns.rst index 103d03d..9614347 100644 --- a/source/c02/p08_regexp_for_multiline_partterns.rst +++ b/source/c02/p08_regexp_for_multiline_partterns.rst @@ -7,8 +7,6 @@ ---------- 你正在试着使用正则表达式去匹配一大块的文本,而你需要跨越多行去匹配。 -| - ---------- 解决方案 ---------- @@ -41,8 +39,6 @@ 在这个模式中, ``(?:.|\n)`` 指定了一个非捕获组 (也就是它定义了一个仅仅用来做匹配,而不能通过单独捕获或者编号的组)。 -| - ---------- 讨论 ---------- diff --git a/source/c02/p09_normalize_unicode_text_to_regexp.rst b/source/c02/p09_normalize_unicode_text_to_regexp.rst index b5fefa8..b3177ad 100644 --- a/source/c02/p09_normalize_unicode_text_to_regexp.rst +++ b/source/c02/p09_normalize_unicode_text_to_regexp.rst @@ -7,8 +7,6 @@ ---------- 你正在处理Unicode字符串,需要确保所有字符串在底层有相同的表示。 -| - ---------- 解决方案 ---------- @@ -72,8 +70,6 @@ Python同样支持扩展的标准化形式NFKC和NFKD,它们在处理某些字 'fi' >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p10_work_with_unicode_in_regexp.rst b/source/c02/p10_work_with_unicode_in_regexp.rst index 2920faf..4758aae 100644 --- a/source/c02/p10_work_with_unicode_in_regexp.rst +++ b/source/c02/p10_work_with_unicode_in_regexp.rst @@ -7,8 +7,6 @@ ---------- 你正在使用正则表达式处理文本,但是关注的是Unicode字符处理。 -| - ---------- 解决方案 ---------- diff --git a/source/c02/p11_strip_unwanted_characters.rst b/source/c02/p11_strip_unwanted_characters.rst index e331b5c..89be1d9 100644 --- a/source/c02/p11_strip_unwanted_characters.rst +++ b/source/c02/p11_strip_unwanted_characters.rst @@ -7,8 +7,6 @@ ---------- 你想去掉文本字符串开头,结尾或者中间不想要的字符,比如空白。 -| - ---------- 解决方案 ---------- @@ -34,8 +32,6 @@ 'hello' >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p12_sanitizing_clean_up_text.rst b/source/c02/p12_sanitizing_clean_up_text.rst index fd6e9ac..937f864 100644 --- a/source/c02/p12_sanitizing_clean_up_text.rst +++ b/source/c02/p12_sanitizing_clean_up_text.rst @@ -7,8 +7,6 @@ ---------- 一些无聊的幼稚黑客在你的网站页面表单中输入文本"pýtĥöñ",然后你想将这些字符清理掉。 -| - ---------- 解决方案 ---------- @@ -97,8 +95,6 @@ 这里的标准化操作将原来的文本分解为单独的和音符。接下来的ASCII编码/解码只是简单的一下子丢弃掉那些字符。 当然,这种方法仅仅只在最后的目标就是获取到文本对应ACSII表示的时候生效。 -| - ---------- 讨论 ---------- diff --git a/source/c02/p13_aligning_text_strings.rst b/source/c02/p13_aligning_text_strings.rst index c13143f..bc91fec 100644 --- a/source/c02/p13_aligning_text_strings.rst +++ b/source/c02/p13_aligning_text_strings.rst @@ -7,8 +7,6 @@ ---------- 你想通过某种对齐方式来格式化字符串 -| - ---------- 解决方案 ---------- @@ -77,8 +75,6 @@ ' 1.23 ' >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p14_combine_and_concatenate_strings.rst b/source/c02/p14_combine_and_concatenate_strings.rst index ca0db93..cc2803a 100644 --- a/source/c02/p14_combine_and_concatenate_strings.rst +++ b/source/c02/p14_combine_and_concatenate_strings.rst @@ -7,8 +7,6 @@ ---------- 你想将几个小的字符串合并为一个大的字符串 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ 'HelloWorld' >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p15_interpolating_variables_in_strings.rst b/source/c02/p15_interpolating_variables_in_strings.rst index 03f9eed..b479fbe 100644 --- a/source/c02/p15_interpolating_variables_in_strings.rst +++ b/source/c02/p15_interpolating_variables_in_strings.rst @@ -7,8 +7,6 @@ ---------- 你想创建一个内嵌变量的字符串,变量被它的值所表示的字符串替换掉。 -| - ---------- 解决方案 ---------- @@ -98,8 +96,6 @@ Python并没有对在字符串中简单替换变量值提供直接的支持。 Your favorite color is {color} >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p16_reformat_text_to_fixed_number_columns.rst b/source/c02/p16_reformat_text_to_fixed_number_columns.rst index bfb25e0..760d8d0 100644 --- a/source/c02/p16_reformat_text_to_fixed_number_columns.rst +++ b/source/c02/p16_reformat_text_to_fixed_number_columns.rst @@ -7,8 +7,6 @@ ---------- 你有一些长字符串,想以指定的列宽将它们重新格式化。 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ the eyes, look into my eyes, you're under. -| - ---------- 讨论 ---------- diff --git a/source/c02/p17_handle_html_xml_in_text.rst b/source/c02/p17_handle_html_xml_in_text.rst index c81d59c..141a8fc 100644 --- a/source/c02/p17_handle_html_xml_in_text.rst +++ b/source/c02/p17_handle_html_xml_in_text.rst @@ -8,8 +8,6 @@ 你想将HTML或者XML实体如 ``&entity;`` 或 ``&#code;`` 替换为对应的文本。 再者,你需要转换文本中特定的字符(比如<, >, 或 &)。 -| - ---------- 解决方案 ---------- @@ -60,8 +58,6 @@ 'The prompt is >>>' >>> -| - ---------- 讨论 ---------- diff --git a/source/c02/p18_tokenizing_text.rst b/source/c02/p18_tokenizing_text.rst index 90f4cc8..9958586 100644 --- a/source/c02/p18_tokenizing_text.rst +++ b/source/c02/p18_tokenizing_text.rst @@ -7,8 +7,6 @@ ---------- 你有一个字符串,想从左至右将其解析为一个令牌流。 -| - ---------- 解决方案 ---------- @@ -103,8 +101,6 @@ for tok in tokens: print(tok) -| - ---------- 讨论 ---------- diff --git a/source/c02/p19_writing_recursive_descent_parser.rst b/source/c02/p19_writing_recursive_descent_parser.rst index cd6fd5c..ff557cb 100644 --- a/source/c02/p19_writing_recursive_descent_parser.rst +++ b/source/c02/p19_writing_recursive_descent_parser.rst @@ -8,8 +8,6 @@ 你想根据一组语法规则解析文本并执行命令,或者构造一个代表输入的抽象语法树。 如果语法非常简单,你可以自己写这个解析器,而不是使用一些框架。 -| - ---------- 解决方案 ---------- @@ -213,8 +211,6 @@ if __name__ == '__main__': descent_parser() -| - ---------- 讨论 ---------- diff --git a/source/c02/p20_perform_text_operations_on_byte_string.rst b/source/c02/p20_perform_text_operations_on_byte_string.rst index 52d9310..c41e64c 100644 --- a/source/c02/p20_perform_text_operations_on_byte_string.rst +++ b/source/c02/p20_perform_text_operations_on_byte_string.rst @@ -7,8 +7,6 @@ ---------- 你想在字节字符串上执行普通的文本操作(比如移除,搜索和替换)。 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ [b'FOO', b'BAR', b'SPAM'] >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p01_round_number.rst b/source/c03/p01_round_number.rst index b53f2ad..c386b22 100644 --- a/source/c03/p01_round_number.rst +++ b/source/c03/p01_round_number.rst @@ -7,8 +7,6 @@ ---------- 你想对浮点数执行指定精度的舍入运算。 -| - ---------- 解决方案 ---------- @@ -43,8 +41,6 @@ 1628000 >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p02_accurate_decimal_calculations.rst b/source/c03/p02_accurate_decimal_calculations.rst index df236df..56d9512 100644 --- a/source/c03/p02_accurate_decimal_calculations.rst +++ b/source/c03/p02_accurate_decimal_calculations.rst @@ -7,8 +7,6 @@ ---------- 你需要对浮点数执行精确的计算操作,并且不希望有任何小误差的出现。 -| - ---------- 解决方案 ---------- @@ -68,8 +66,6 @@ 0.76470588235294117647058823529411764705882352941176 >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p03_format_numbers_for_output.rst b/source/c03/p03_format_numbers_for_output.rst index 9fb8f7f..f286192 100644 --- a/source/c03/p03_format_numbers_for_output.rst +++ b/source/c03/p03_format_numbers_for_output.rst @@ -7,8 +7,6 @@ ---------- 你需要将数字格式化后输出,并控制数字的位数、对齐、千位分隔符和其他的细节。 -| - ---------- 解决方案 ---------- @@ -61,8 +59,6 @@ 'The value is 1,234.57' >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p04_binary_octal_hexadecimal_int.rst b/source/c03/p04_binary_octal_hexadecimal_int.rst index ebd7b25..bd73d16 100644 --- a/source/c03/p04_binary_octal_hexadecimal_int.rst +++ b/source/c03/p04_binary_octal_hexadecimal_int.rst @@ -7,8 +7,6 @@ ---------- 你需要转换或者输出使用二进制,八进制或十六进制表示的整数。 -| - ---------- 解决方案 ---------- @@ -70,8 +68,6 @@ 1234 >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p05_pack_unpack_large_int_from_bytes.rst b/source/c03/p05_pack_unpack_large_int_from_bytes.rst index 95213a5..01b76b3 100644 --- a/source/c03/p05_pack_unpack_large_int_from_bytes.rst +++ b/source/c03/p05_pack_unpack_large_int_from_bytes.rst @@ -7,8 +7,6 @@ ---------- 你有一个字节字符串并想将它解压成一个整数。或者,你需要将一个大整数转换为一个字节字符串。 -| - ---------- 解决方案 ---------- @@ -41,8 +39,6 @@ b'4\x00#\x00\x01\xef\xcd\x00\xab\x90x\x00V4\x12\x00' >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p06_complex_math.rst b/source/c03/p06_complex_math.rst index 60b2cd0..e2421b3 100644 --- a/source/c03/p06_complex_math.rst +++ b/source/c03/p06_complex_math.rst @@ -8,8 +8,6 @@ 你写的最新的网络认证方案代码遇到了一个难题,并且你唯一的解决办法就是使用复数空间。 再或者是你仅仅需要使用复数来执行一些计算操作。 -| - ---------- 解决方案 ---------- @@ -64,8 +62,6 @@ (-4.829809383269385-5.5920560936409816j) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p07_infinity_and_nan.rst b/source/c03/p07_infinity_and_nan.rst index 8222204..0d23e02 100644 --- a/source/c03/p07_infinity_and_nan.rst +++ b/source/c03/p07_infinity_and_nan.rst @@ -7,8 +7,6 @@ ---------- 你想创建或测试正无穷、负无穷或NaN(非数字)的浮点数。 -| - ---------- 解决方案 ---------- @@ -37,8 +35,6 @@ Python并没有特殊的语法来表示这些特殊的浮点值,但是可以 True >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p08_calculating_with_fractions.rst b/source/c03/p08_calculating_with_fractions.rst index deeeaf0..cfe20f5 100644 --- a/source/c03/p08_calculating_with_fractions.rst +++ b/source/c03/p08_calculating_with_fractions.rst @@ -8,8 +8,6 @@ 你进入时间机器,突然发现你正在做小学家庭作业,并涉及到分数计算问题。 或者你可能需要写代码去计算在你的木工工厂中的测量值。 -| - ---------- 解决方案 ---------- @@ -47,8 +45,6 @@ Fraction(15, 4) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p09_calculating_with_large_num_arrays.rst b/source/c03/p09_calculating_with_large_num_arrays.rst index 4dae3ea..a01b661 100644 --- a/source/c03/p09_calculating_with_large_num_arrays.rst +++ b/source/c03/p09_calculating_with_large_num_arrays.rst @@ -7,8 +7,6 @@ ---------- 你需要在大数据集(比如数组或网格)上面执行计算。 -| - ---------- 解决方案 ---------- @@ -166,8 +164,6 @@ [ 9, 10, 10, 10]]) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p10_matrix_and_linear_algebra_calculation.rst b/source/c03/p10_matrix_and_linear_algebra_calculation.rst index 424f441..fd5fbcb 100644 --- a/source/c03/p10_matrix_and_linear_algebra_calculation.rst +++ b/source/c03/p10_matrix_and_linear_algebra_calculation.rst @@ -7,8 +7,6 @@ ---------- 你需要执行矩阵和线性代数运算,比如矩阵乘法、寻找行列式、求解线性方程组等等。 -| - ---------- 解决方案 ---------- @@ -78,8 +76,6 @@ [4]]) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p11_pick_things_at_random.rst b/source/c03/p11_pick_things_at_random.rst index a083ba6..75f03a7 100644 --- a/source/c03/p11_pick_things_at_random.rst +++ b/source/c03/p11_pick_things_at_random.rst @@ -7,8 +7,6 @@ ---------- 你想从一个序列中随机抽取若干元素,或者想生成几个随机数。 -| - ---------- 解决方案 ---------- @@ -95,8 +93,6 @@ 335837000776573622800628485064121869519521710558559406913275 >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p12_convert_days_to_seconds_and_others.rst b/source/c03/p12_convert_days_to_seconds_and_others.rst index f15547d..0847870 100644 --- a/source/c03/p12_convert_days_to_seconds_and_others.rst +++ b/source/c03/p12_convert_days_to_seconds_and_others.rst @@ -7,8 +7,6 @@ ---------- 你需要执行简单的时间转换,比如天到秒,小时到分钟等的转换。 -| - ---------- 解决方案 ---------- @@ -67,8 +65,6 @@ 1 >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p13_determine_last_friday_date.rst b/source/c03/p13_determine_last_friday_date.rst index 3fd6b77..446140e 100644 --- a/source/c03/p13_determine_last_friday_date.rst +++ b/source/c03/p13_determine_last_friday_date.rst @@ -7,8 +7,6 @@ ---------- 你需要查找星期中某一天最后出现的日期,比如星期五。 -| - ---------- 解决方案 ---------- @@ -62,8 +60,6 @@ Python的 ``datetime`` 模块中有工具函数和类可以帮助你执行这样 datetime.datetime(2012, 12, 16, 0, 0) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p14_date_range_for_current_month.rst b/source/c03/p14_date_range_for_current_month.rst index 952335b..12df3fb 100644 --- a/source/c03/p14_date_range_for_current_month.rst +++ b/source/c03/p14_date_range_for_current_month.rst @@ -7,8 +7,6 @@ ---------- 你的代码需要在当前月份中循环每一天,想找到一个计算这个日期范围的高效方法。 -| - ---------- 解决方案 ---------- @@ -51,8 +49,6 @@ 2012-08-09 #... and so on... -| - ---------- 讨论 ---------- diff --git a/source/c03/p15_convert_strings_into_datetimes.rst b/source/c03/p15_convert_strings_into_datetimes.rst index 2cae0c6..5d01f28 100644 --- a/source/c03/p15_convert_strings_into_datetimes.rst +++ b/source/c03/p15_convert_strings_into_datetimes.rst @@ -7,8 +7,6 @@ ---------- 你的应用程序接受字符串格式的输入,但是你想将它们转换为 ``datetime`` 对象以便在上面执行非字符串操作。 -| - ---------- 解决方案 ---------- @@ -25,8 +23,6 @@ datetime.timedelta(3, 77824, 177393) >>> -| - ---------- 讨论 ---------- diff --git a/source/c03/p16_manipulate_dates_involving_timezone.rst b/source/c03/p16_manipulate_dates_involving_timezone.rst index b8b17f5..3279cf9 100644 --- a/source/c03/p16_manipulate_dates_involving_timezone.rst +++ b/source/c03/p16_manipulate_dates_involving_timezone.rst @@ -71,8 +71,6 @@ 2013-03-10 03:15:00-05:00 >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p01_manually_consuming_iterator.rst b/source/c04/p01_manually_consuming_iterator.rst index 16c2fe4..35e1904 100644 --- a/source/c04/p01_manually_consuming_iterator.rst +++ b/source/c04/p01_manually_consuming_iterator.rst @@ -7,8 +7,6 @@ ---------- 你想遍历一个可迭代对象中的所有元素,但是却不想使用for循环。 -| - ---------- 解决方案 ---------- @@ -39,8 +37,6 @@ break print(line, end='') -| - ---------- 讨论 ---------- diff --git a/source/c04/p02_delegating_iteration.rst b/source/c04/p02_delegating_iteration.rst index a8f9e1e..de8581b 100644 --- a/source/c04/p02_delegating_iteration.rst +++ b/source/c04/p02_delegating_iteration.rst @@ -8,8 +8,6 @@ 你构建了一个自定义容器对象,里面包含有列表、元组或其他可迭代对象。 你想直接在你的这个新容器对象上执行迭代操作。 -| - ---------- 解决方案 ---------- @@ -44,8 +42,6 @@ 在上面代码中, ``__iter__()`` 方法只是简单的将迭代请求传递给内部的 ``_children`` 属性。 -| - ---------- 讨论 ---------- diff --git a/source/c04/p03_create_new_iteration_with_generators.rst b/source/c04/p03_create_new_iteration_with_generators.rst index 3562bf3..5b5c120 100644 --- a/source/c04/p03_create_new_iteration_with_generators.rst +++ b/source/c04/p03_create_new_iteration_with_generators.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个自定义迭代模式,跟普通的内置函数比如 ``range()`` , ``reversed()`` 不一样。 -| - ---------- 解决方案 ---------- @@ -43,8 +41,6 @@ [0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875] >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p04_implement_iterator_protocol.rst b/source/c04/p04_implement_iterator_protocol.rst index b1b46bb..5f24baf 100644 --- a/source/c04/p04_implement_iterator_protocol.rst +++ b/source/c04/p04_implement_iterator_protocol.rst @@ -7,8 +7,6 @@ ---------- 你想构建一个能支持迭代操作的自定义对象,并希望找到一个能实现迭代协议的简单方法。 -| - ---------- 解决方案 ---------- @@ -56,8 +54,6 @@ 它首先返回自己本身并迭代每一个子节点并 通过调用子节点的 ``depth_first()`` 方法(使用 ``yield from`` 语句)返回对应元素。 -| - ---------- 讨论 ---------- diff --git a/source/c04/p05_iterating_in_reverse.rst b/source/c04/p05_iterating_in_reverse.rst index 6b50d42..65138eb 100644 --- a/source/c04/p05_iterating_in_reverse.rst +++ b/source/c04/p05_iterating_in_reverse.rst @@ -7,8 +7,6 @@ ---------- 你想反方向迭代一个序列 -| - ---------- 解决方案 ---------- @@ -37,8 +35,6 @@ 要注意的是如果可迭代对象元素很多的话,将其预先转换为一个列表要消耗大量的内存。 -| - ---------- 讨论 ---------- diff --git a/source/c04/p06_define_generator_func_with_extra_state.rst b/source/c04/p06_define_generator_func_with_extra_state.rst index ffd0cdb..d439ae0 100644 --- a/source/c04/p06_define_generator_func_with_extra_state.rst +++ b/source/c04/p06_define_generator_func_with_extra_state.rst @@ -7,8 +7,6 @@ ---------- 你想定义一个生成器函数,但是它会调用某个你想暴露给用户使用的外部状态值。 -| - ---------- 解决方案 ---------- @@ -45,8 +43,6 @@ for lineno, hline in lines.history: print('{}:{}'.format(lineno, hline), end='') -| - ---------- 讨论 ---------- diff --git a/source/c04/p07_taking_slice_of_iterator.rst b/source/c04/p07_taking_slice_of_iterator.rst index 9a31a33..d379fff 100644 --- a/source/c04/p07_taking_slice_of_iterator.rst +++ b/source/c04/p07_taking_slice_of_iterator.rst @@ -7,8 +7,6 @@ ---------- 你想得到一个由迭代器生成的切片对象,但是标准切片操作并不能做到。 -| - ---------- 解决方案 ---------- @@ -44,8 +42,6 @@ 19 >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p08_skip_first_part_of_iterable.rst b/source/c04/p08_skip_first_part_of_iterable.rst index 3f41c67..24641b9 100644 --- a/source/c04/p08_skip_first_part_of_iterable.rst +++ b/source/c04/p08_skip_first_part_of_iterable.rst @@ -7,8 +7,6 @@ ---------- 你想遍历一个可迭代对象,但是它开始的某些元素你并不感兴趣,想跳过它们。 -| - ---------- 解决方案 ---------- @@ -71,8 +69,6 @@ 如果 ``None`` 和3的位置对调,意思就是仅仅获取前三个元素恰恰相反, (这个跟切片的相反操作 ``[3:]`` 和 ``[:3]`` 原理是一样的)。 -| - ---------- 讨论 ---------- diff --git a/source/c04/p09_iterate_over_combination_or_permutation.rst b/source/c04/p09_iterate_over_combination_or_permutation.rst index 635d998..05e01b5 100644 --- a/source/c04/p09_iterate_over_combination_or_permutation.rst +++ b/source/c04/p09_iterate_over_combination_or_permutation.rst @@ -7,8 +7,6 @@ ---------- 你想迭代遍历一个集合中元素的所有可能的排列或组合 -| - ---------- 解决方案 ---------- @@ -95,8 +93,6 @@ itertools模块提供了三个函数来解决这类问题。 ('c', 'c', 'c') >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p10_iterate_over_index_value_pairs_of_sequence.rst b/source/c04/p10_iterate_over_index_value_pairs_of_sequence.rst index a1b782e..efad688 100644 --- a/source/c04/p10_iterate_over_index_value_pairs_of_sequence.rst +++ b/source/c04/p10_iterate_over_index_value_pairs_of_sequence.rst @@ -7,8 +7,6 @@ ---------- 你想在迭代一个序列的同时跟踪正在被处理的元素索引。 -| - ---------- 解决方案 ---------- @@ -71,8 +69,6 @@ 如果某个单词在一行中出现过两次,那么这个行号也会出现两次, 同时也可以作为文本的一个简单统计。 -| - ---------- 讨论 ---------- diff --git a/source/c04/p11_iterate_over_multiple_sequences_simultaneously.rst b/source/c04/p11_iterate_over_multiple_sequences_simultaneously.rst index bf7f3dc..e9e810b 100644 --- a/source/c04/p11_iterate_over_multiple_sequences_simultaneously.rst +++ b/source/c04/p11_iterate_over_multiple_sequences_simultaneously.rst @@ -7,8 +7,6 @@ ---------- 你想同时迭代多个序列,每次分别从一个序列中取一个元素。 -| - ---------- 解决方案 ---------- @@ -67,8 +65,6 @@ (0, 'z') >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p12_iterate_on_items_in_separate_containers.rst b/source/c04/p12_iterate_on_items_in_separate_containers.rst index 53270e2..5e3209d 100644 --- a/source/c04/p12_iterate_on_items_in_separate_containers.rst +++ b/source/c04/p12_iterate_on_items_in_separate_containers.rst @@ -7,8 +7,6 @@ ---------- 你想在多个对象执行相同的操作,但是这些对象在不同的容器中,你希望代码在不失可读性的情况下避免写重复的循环。 -| - ---------- 解决方案 ---------- @@ -57,8 +55,6 @@ # Process item ... -| - ---------- 讨论 ---------- diff --git a/source/c04/p13_create_data_processing_pipelines.rst b/source/c04/p13_create_data_processing_pipelines.rst index 8e857fb..141d023 100644 --- a/source/c04/p13_create_data_processing_pipelines.rst +++ b/source/c04/p13_create_data_processing_pipelines.rst @@ -8,8 +8,6 @@ 你想以数据管道(类似Unix管道)的方式迭代处理数据。 比如,你有个大量的数据需要处理,但是不能将它们一次性放入内存中。 -| - ---------- 解决方案 ---------- @@ -113,8 +111,6 @@ bytes = (int(x) for x in bytecolumn if x != '-') print('Total', sum(bytes)) -| - ---------- 讨论 ---------- diff --git a/source/c04/p14_flattening_nested_sequence.rst b/source/c04/p14_flattening_nested_sequence.rst index 1384a03..7707d10 100644 --- a/source/c04/p14_flattening_nested_sequence.rst +++ b/source/c04/p14_flattening_nested_sequence.rst @@ -7,8 +7,6 @@ ---------- 你想将一个多层嵌套的序列展开成一个单层列表 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ Lewis >>> -| - ---------- 讨论 ---------- diff --git a/source/c04/p15_iterate_in_sorted_order_over_merged_sorted_iterables.rst b/source/c04/p15_iterate_in_sorted_order_over_merged_sorted_iterables.rst index 8a68185..5fda084 100644 --- a/source/c04/p15_iterate_in_sorted_order_over_merged_sorted_iterables.rst +++ b/source/c04/p15_iterate_in_sorted_order_over_merged_sorted_iterables.rst @@ -7,8 +7,6 @@ ---------- 你有一系列排序序列,想将它们合并后得到一个排序序列并在上面迭代遍历。 -| - ---------- 解决方案 ---------- diff --git a/source/c04/p16_replace_infinite_while_loops_with_iterator.rst b/source/c04/p16_replace_infinite_while_loops_with_iterator.rst index 0780fde..1a3222a 100644 --- a/source/c04/p16_replace_infinite_while_loops_with_iterator.rst +++ b/source/c04/p16_replace_infinite_while_loops_with_iterator.rst @@ -8,8 +8,6 @@ 你在代码中使用 ``while`` 循环来迭代处理数据,因为它需要调用某个函数或者和一般迭代模式不同的测试条件。 能不能用迭代器来重写这个循环呢? -| - ---------- 解决方案 ---------- diff --git a/source/c05/p01_read_write_text_data.rst b/source/c05/p01_read_write_text_data.rst index 876adfd..2edba14 100644 --- a/source/c05/p01_read_write_text_data.rst +++ b/source/c05/p01_read_write_text_data.rst @@ -7,8 +7,6 @@ ---------- 你需要读写各种不同编码的文本数据,比如ASCII,UTF-8或UTF-16编码等。 -| - ---------- 解决方案 ---------- @@ -62,8 +60,6 @@ latin-1是字节0-255到U+0000至U+00FF范围内Unicode字符的直接映射。 使用latin-1编码读取一个文件的时候也许不能产生完全正确的文本解码数据, 但是它也能从中提取出足够多的有用数据。同时,如果你之后将数据回写回去,原先的数据还是会保留的。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p02_printing_to_file.rst b/source/c05/p02_printing_to_file.rst index cdb7523..8076ef7 100644 --- a/source/c05/p02_printing_to_file.rst +++ b/source/c05/p02_printing_to_file.rst @@ -7,8 +7,6 @@ ---------- 你想将 ``print()`` 函数的输出重定向到一个文件中去。 -| - ---------- 解决方案 ---------- diff --git a/source/c05/p03_print_with_different_separator_or_line_ending.rst b/source/c05/p03_print_with_different_separator_or_line_ending.rst index 448e72b..e529078 100644 --- a/source/c05/p03_print_with_different_separator_or_line_ending.rst +++ b/source/c05/p03_print_with_different_separator_or_line_ending.rst @@ -7,8 +7,6 @@ ---------- 你想使用 ``print()`` 函数输出数据,但是想改变默认的分隔符或者行尾符。 -| - ---------- 解决方案 ---------- @@ -41,8 +39,6 @@ ... 0 1 2 3 4 >>> -| - ---------- 讨论 ---------- diff --git a/source/c05/p04_read_write_binary_data.rst b/source/c05/p04_read_write_binary_data.rst index 4bcf077..df24d9d 100644 --- a/source/c05/p04_read_write_binary_data.rst +++ b/source/c05/p04_read_write_binary_data.rst @@ -7,8 +7,6 @@ ---------- 你想读写二进制文件,比如图片,声音文件等等。 -| - ---------- 解决方案 ---------- @@ -27,8 +25,6 @@ 在读取二进制数据时,需要指明的是所有返回的数据都是字节字符串格式的,而不是文本字符串。 类似的,在写入的时候,必须保证参数是以字节形式对外暴露数据的对象(比如字节字符串,字节数组对象等)。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p05_write_to_file_not_exist.rst b/source/c05/p05_write_to_file_not_exist.rst index 66e9439..7a2afc2 100644 --- a/source/c05/p05_write_to_file_not_exist.rst +++ b/source/c05/p05_write_to_file_not_exist.rst @@ -8,8 +8,6 @@ 你想像一个文件中写入数据,但是前提必须是这个文件在文件系统上不存在。 也就是不允许覆盖已存在的文件内容。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ 如果文件是二进制的,使用 ``xb`` 来代替 ``xt`` -| - ---------- 讨论 ---------- diff --git a/source/c05/p06_io_operations_on_string.rst b/source/c05/p06_io_operations_on_string.rst index 9227806..3823441 100644 --- a/source/c05/p06_io_operations_on_string.rst +++ b/source/c05/p06_io_operations_on_string.rst @@ -7,8 +7,6 @@ ---------- 你想使用操作类文件对象的程序来操作文本或二进制字符串。 -| - ---------- 解决方案 ---------- @@ -44,8 +42,6 @@ b'binary data' >>> -| - ---------- 讨论 ---------- diff --git a/source/c05/p07_read_write_compressed_datafiles.rst b/source/c05/p07_read_write_compressed_datafiles.rst index 6b1a3a6..86bd9cf 100644 --- a/source/c05/p07_read_write_compressed_datafiles.rst +++ b/source/c05/p07_read_write_compressed_datafiles.rst @@ -7,8 +7,6 @@ ---------- 你想读写一个gzip或bz2格式的压缩文件。 -| - ---------- 解决方案 ---------- @@ -45,8 +43,6 @@ 如上,所有的I/O操作都使用文本模式并执行Unicode的编码/解码。 类似的,如果你想操作二进制数据,使用 ``rb`` 或者 ``wb`` 文件模式即可。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p08_iterate_over_fixed_sized_records.rst b/source/c05/p08_iterate_over_fixed_sized_records.rst index a220aad..439e99a 100644 --- a/source/c05/p08_iterate_over_fixed_sized_records.rst +++ b/source/c05/p08_iterate_over_fixed_sized_records.rst @@ -7,8 +7,6 @@ ---------- 你想在一个固定长度记录或者数据块的集合上迭代,而不是在一个文件中一行一行的迭代。 -| - ---------- 解决方案 ---------- @@ -28,8 +26,6 @@ 这个例子中的 ``records`` 对象是一个可迭代对象,它会不断的产生固定大小的数据块,直到文件末尾。 要注意的是如果总记录大小不是块大小的整数倍的话,最后一个返回元素的字节数会比期望值少。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p09_read_binary_data_into_mutable_buffer.rst b/source/c05/p09_read_binary_data_into_mutable_buffer.rst index 15de5b0..e8c39c9 100644 --- a/source/c05/p09_read_binary_data_into_mutable_buffer.rst +++ b/source/c05/p09_read_binary_data_into_mutable_buffer.rst @@ -8,8 +8,6 @@ 你想直接读取二进制数据到一个可变缓冲区中,而不需要做任何的中间复制操作。 或者你想原地修改数据并将它写回到一个文件中去。 -| - ---------- 解决方案 ---------- @@ -45,8 +43,6 @@ 11 >>> -| - ---------- 讨论 ---------- diff --git a/source/c05/p10_memory_mapping_binary_files.rst b/source/c05/p10_memory_mapping_binary_files.rst index 781137d..01f8ae4 100644 --- a/source/c05/p10_memory_mapping_binary_files.rst +++ b/source/c05/p10_memory_mapping_binary_files.rst @@ -7,8 +7,6 @@ ---------- 你想内存映射一个二进制文件到一个可变字节数组中,目的可能是为了随机访问它的内容或者是原地做些修改。 -| - ---------- 解决方案 ---------- @@ -88,8 +86,6 @@ m = memory_map(filename, mmap.ACCESS_COPY) -| - ---------- 讨论 ---------- diff --git a/source/c05/p11_manipulating_pathnames.rst b/source/c05/p11_manipulating_pathnames.rst index e11a49a..37f316d 100644 --- a/source/c05/p11_manipulating_pathnames.rst +++ b/source/c05/p11_manipulating_pathnames.rst @@ -7,8 +7,6 @@ ---------- 你需要使用路径名来获取文件名,目录名,绝对路径等等。 -| - ---------- 解决方案 ---------- @@ -42,8 +40,6 @@ ('~/Data/data', '.csv') >>> -| - ---------- 讨论 ---------- diff --git a/source/c05/p12_test_for_the_existence_of_file.rst b/source/c05/p12_test_for_the_existence_of_file.rst index f0329bf..9a1565d 100644 --- a/source/c05/p12_test_for_the_existence_of_file.rst +++ b/source/c05/p12_test_for_the_existence_of_file.rst @@ -7,8 +7,6 @@ ---------- 你想测试一个文件或目录是否存在。 -| - ---------- 解决方案 ---------- @@ -58,8 +56,6 @@ 'Wed Apr 28 13:10:34 2010' >>> -| - ---------- 讨论 ---------- diff --git a/source/c05/p13_get_directory_listing.rst b/source/c05/p13_get_directory_listing.rst index d7c1b2f..4c2b5d8 100644 --- a/source/c05/p13_get_directory_listing.rst +++ b/source/c05/p13_get_directory_listing.rst @@ -7,8 +7,6 @@ ---------- 你想获取文件系统中某个目录下的所有文件列表。 -| - ---------- 解决方案 ---------- @@ -52,8 +50,6 @@ pyfiles = [name for name in os.listdir('somedir') if fnmatch(name, '*.py')] -| - ---------- 讨论 ---------- diff --git a/source/c05/p14_bypassing_filename_encoding.rst b/source/c05/p14_bypassing_filename_encoding.rst index 1a5e5ad..c114cb8 100644 --- a/source/c05/p14_bypassing_filename_encoding.rst +++ b/source/c05/p14_bypassing_filename_encoding.rst @@ -7,8 +7,6 @@ ---------- 你想使用原始文件名执行文件的I/O操作,也就是说文件名并没有经过系统默认编码去解码或编码过。 -| - ---------- 解决方案 ---------- @@ -48,8 +46,6 @@ 正如你所见,在最后两个操作中,当你给文件相关函数如 ``open()`` 和 ``os.listdir()`` 传递字节字符串时,文件名的处理方式会稍有不同。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p15_printing_bad_filenames.rst b/source/c05/p15_printing_bad_filenames.rst index 6a86286..1a63602 100644 --- a/source/c05/p15_printing_bad_filenames.rst +++ b/source/c05/p15_printing_bad_filenames.rst @@ -8,8 +8,6 @@ 你的程序获取了一个目录中的文件名列表,但是当它试着去打印文件名的时候程序崩溃, 出现了 ``UnicodeEncodeError`` 异常和一条奇怪的消息—— ``surrogates not allowed`` 。 -| - ---------- 解决方案 ---------- @@ -25,8 +23,6 @@ except UnicodeEncodeError: print(bad_filename(filename)) -| - ---------- 讨论 ---------- diff --git a/source/c05/p16_add_change_encoding_of_already_open_file.rst b/source/c05/p16_add_change_encoding_of_already_open_file.rst index e7a53df..888d73e 100644 --- a/source/c05/p16_add_change_encoding_of_already_open_file.rst +++ b/source/c05/p16_add_change_encoding_of_already_open_file.rst @@ -7,8 +7,6 @@ ---------- 你想在不关闭一个已打开的文件前提下增加或改变它的Unicode编码。 -| - ---------- 解决方案 ---------- @@ -39,8 +37,6 @@ 这样做可能会中断你的终端,这里仅仅是为了演示而已。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p17_write_bytes_to_text_file.rst b/source/c05/p17_write_bytes_to_text_file.rst index 067b422..926eaaf 100644 --- a/source/c05/p17_write_bytes_to_text_file.rst +++ b/source/c05/p17_write_bytes_to_text_file.rst @@ -7,8 +7,6 @@ ---------- 你想在文本模式打开的文件中写入原始的字节数据。 -| - ---------- 解决方案 ---------- diff --git a/source/c05/p18_wrap_existing_file_descriptor_as_file_object.rst b/source/c05/p18_wrap_existing_file_descriptor_as_file_object.rst index 613bfb9..8b2e601 100644 --- a/source/c05/p18_wrap_existing_file_descriptor_as_file_object.rst +++ b/source/c05/p18_wrap_existing_file_descriptor_as_file_object.rst @@ -8,8 +8,6 @@ 你有一个对应于操作系统上一个已打开的I/O通道(比如文件、管道、套接字等)的整型文件描述符, 你想将它包装成一个更高层的Python文件对象。 -| - ---------- 解决方案 ---------- @@ -38,8 +36,6 @@ f = open(fd, 'wt', closefd=False) ... -| - ---------- 讨论 ---------- diff --git a/source/c05/p19_make_temporary_files_and_directories.rst b/source/c05/p19_make_temporary_files_and_directories.rst index 0d08660..94d8fba 100644 --- a/source/c05/p19_make_temporary_files_and_directories.rst +++ b/source/c05/p19_make_temporary_files_and_directories.rst @@ -7,8 +7,6 @@ ---------- 你需要在程序执行时创建一个临时文件或目录,并希望使用完之后可以自动销毁掉。 -| - ---------- 解决方案 ---------- @@ -85,8 +83,6 @@ ... # Directory and all contents destroyed -| - ---------- 讨论 ---------- diff --git a/source/c05/p20_communicating_with_serial_ports.rst b/source/c05/p20_communicating_with_serial_ports.rst index 2f6abdc..3189e44 100644 --- a/source/c05/p20_communicating_with_serial_ports.rst +++ b/source/c05/p20_communicating_with_serial_ports.rst @@ -7,8 +7,6 @@ ---------- 你想通过串行端口读写数据,典型场景就是和一些硬件设备打交道(比如一个机器人或传感器)。 -| - ---------- 解决方案 ---------- @@ -36,8 +34,6 @@ 大多数情况下,简单的串口通信从此变得十分简单。 -| - ---------- 讨论 ---------- diff --git a/source/c05/p21_serializing_python_objects.rst b/source/c05/p21_serializing_python_objects.rst index 188f89c..de603d4 100644 --- a/source/c05/p21_serializing_python_objects.rst +++ b/source/c05/p21_serializing_python_objects.rst @@ -7,8 +7,6 @@ ---------- 你需要将一个Python对象序列化为一个字节流,以便将它保存到一个文件、存储到数据库或者通过网络传输它。 -| - ---------- 解决方案 ---------- @@ -39,8 +37,6 @@ # Restore from a string data = pickle.loads(s) -| - ---------- 讨论 ---------- diff --git a/source/c06/p01_read_write_csv_data.rst b/source/c06/p01_read_write_csv_data.rst index 0b5031e..704630c 100644 --- a/source/c06/p01_read_write_csv_data.rst +++ b/source/c06/p01_read_write_csv_data.rst @@ -7,8 +7,6 @@ ---------- 你想读写一个CSV格式的文件。 -| - ---------- 解决方案 ---------- @@ -103,8 +101,6 @@ f_csv.writeheader() f_csv.writerows(rows) -| - ---------- 讨论 ---------- diff --git a/source/c06/p02_read-write_json_data.rst b/source/c06/p02_read-write_json_data.rst index 39090b3..230f8ba 100644 --- a/source/c06/p02_read-write_json_data.rst +++ b/source/c06/p02_read-write_json_data.rst @@ -7,8 +7,6 @@ ---------- 你想读写JSON(JavaScript Object Notation)编码格式的数据。 -| - ---------- 解决方案 ---------- @@ -47,8 +45,6 @@ with open('data.json', 'r') as f: data = json.load(f) -| - ---------- 讨论 ---------- diff --git a/source/c06/p03_parse_simple_xml_data.rst b/source/c06/p03_parse_simple_xml_data.rst index e2a539b..7bc8139 100644 --- a/source/c06/p03_parse_simple_xml_data.rst +++ b/source/c06/p03_parse_simple_xml_data.rst @@ -7,8 +7,6 @@ ---------- 你想从一个简单的XML文档中提取数据。 -| - ---------- 解决方案 ---------- @@ -61,8 +59,6 @@ 很显然,如果你想做进一步的处理,你需要替换 ``print()`` 语句来完成其他有趣的事。 -| - ---------- 讨论 ---------- diff --git a/source/c06/p04_parse_huge_xml_files_incrementally.rst b/source/c06/p04_parse_huge_xml_files_incrementally.rst index 079f8ac..ae35e74 100644 --- a/source/c06/p04_parse_huge_xml_files_incrementally.rst +++ b/source/c06/p04_parse_huge_xml_files_incrementally.rst @@ -7,8 +7,6 @@ ---------- 你想使用尽可能少的内存从一个超大的XML文档中提取数据。 -| - ---------- 解决方案 ---------- @@ -126,8 +124,6 @@ 结果是:这个版本的代码运行时只需要7MB的内存--大大节约了内存资源。 -| - ---------- 讨论 ---------- diff --git a/source/c06/p05_turning_dictionary_into_xml.rst b/source/c06/p05_turning_dictionary_into_xml.rst index 5ff0337..2dafed7 100644 --- a/source/c06/p05_turning_dictionary_into_xml.rst +++ b/source/c06/p05_turning_dictionary_into_xml.rst @@ -7,8 +7,6 @@ ---------- 你想使用一个Python字典存储数据,并将它转换成XML格式。 -| - ---------- 解决方案 ---------- @@ -62,8 +60,6 @@ 如果你还想保持元素的顺序,可以考虑构造一个 ``OrderedDict`` 来代替一个普通的字典。请参考1.7小节。 -| - ---------- 讨论 ---------- diff --git a/source/c06/p06_parse_modify_rewrite_xml.rst b/source/c06/p06_parse_modify_rewrite_xml.rst index a555803..d5deffa 100644 --- a/source/c06/p06_parse_modify_rewrite_xml.rst +++ b/source/c06/p06_parse_modify_rewrite_xml.rst @@ -7,8 +7,6 @@ ---------- 你想读取一个XML文档,对它最一些修改,然后将结果写回XML文档。 -| - ---------- 解决方案 ---------- @@ -88,8 +86,6 @@ -| - ---------- 讨论 ---------- diff --git a/source/c06/p07_parse_xml_documents_with_namespaces.rst b/source/c06/p07_parse_xml_documents_with_namespaces.rst index a6a6fd5..835bc42 100644 --- a/source/c06/p07_parse_xml_documents_with_namespaces.rst +++ b/source/c06/p07_parse_xml_documents_with_namespaces.rst @@ -7,8 +7,6 @@ ---------- 你想解析某个XML文档,文档中使用了XML命名空间。 -| - ---------- 解决方案 ---------- diff --git a/source/c06/p08_interact_with_relational_database.rst b/source/c06/p08_interact_with_relational_database.rst index cdba3aa..873f9b7 100644 --- a/source/c06/p08_interact_with_relational_database.rst +++ b/source/c06/p08_interact_with_relational_database.rst @@ -7,8 +7,6 @@ ---------- 你想在关系型数据库中查询、增加或删除记录。 -| - ---------- 解决方案 ---------- diff --git a/source/c06/p09_decode_encode_hexadecimal_digits.rst b/source/c06/p09_decode_encode_hexadecimal_digits.rst index 808e865..a34c8b1 100644 --- a/source/c06/p09_decode_encode_hexadecimal_digits.rst +++ b/source/c06/p09_decode_encode_hexadecimal_digits.rst @@ -7,8 +7,6 @@ ---------- 你想将一个十六进制字符串解码成一个字节字符串或者将一个字节字符串编码成一个十六进制字符串。 -| - ---------- 解决方案 ---------- @@ -40,8 +38,6 @@ b'hello' >>> -| - ---------- 讨论 ---------- diff --git a/source/c06/p10_decode_encode_base64.rst b/source/c06/p10_decode_encode_base64.rst index 7fc6b1a..fd493cc 100644 --- a/source/c06/p10_decode_encode_base64.rst +++ b/source/c06/p10_decode_encode_base64.rst @@ -7,8 +7,6 @@ ---------- 你需要使用Base64格式解码或编码二进制数据。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ b'hello' >>> -| - ---------- 讨论 ---------- diff --git a/source/c06/p11_read_write_binary_arrays_of_structures.rst b/source/c06/p11_read_write_binary_arrays_of_structures.rst index 46e849e..8afea93 100644 --- a/source/c06/p11_read_write_binary_arrays_of_structures.rst +++ b/source/c06/p11_read_write_binary_arrays_of_structures.rst @@ -7,8 +7,6 @@ ---------- 你想读写一个二进制数组的结构化数据到Python元组中。 -| - ---------- 解决方案 ---------- @@ -74,8 +72,6 @@ 两种情况下的结果都是一个可返回用来创建该文件的原始元组的可迭代对象。 -| - ---------- 讨论 ---------- diff --git a/source/c06/p12_read_nested_and_variable_sized_binary_structures.rst b/source/c06/p12_read_nested_and_variable_sized_binary_structures.rst index 630718f..e139295 100644 --- a/source/c06/p12_read_nested_and_variable_sized_binary_structures.rst +++ b/source/c06/p12_read_nested_and_variable_sized_binary_structures.rst @@ -7,8 +7,6 @@ ---------- 你需要读取包含嵌套或者可变长记录集合的复杂二进制格式的数据。这些数据可能包含图片、视频、电子地图文件等。 -| - ---------- 解决方案 ---------- @@ -459,8 +457,6 @@ polys.append(poly) return polys -| - ---------- 讨论 ---------- diff --git a/source/c06/p13_summarizing_and_perform_statistics.rst b/source/c06/p13_summarizing_and_perform_statistics.rst index 35180f5..8db0a49 100644 --- a/source/c06/p13_summarizing_and_perform_statistics.rst +++ b/source/c06/p13_summarizing_and_perform_statistics.rst @@ -7,8 +7,6 @@ ---------- 你需要处理一个很大的数据集并需要计算数据总和或其他统计量。 -| - ---------- 解决方案 ---------- @@ -113,8 +111,6 @@ >>> 嗯,看样子2011年10月7日对老鼠们来说是个很忙碌的日子啊!^_^ -| - ---------- 讨论 ---------- diff --git a/source/c07/p01_functions_that_accept_any_number_arguments.rst b/source/c07/p01_functions_that_accept_any_number_arguments.rst index 652979d..5865148 100644 --- a/source/c07/p01_functions_that_accept_any_number_arguments.rst +++ b/source/c07/p01_functions_that_accept_any_number_arguments.rst @@ -7,8 +7,6 @@ ---------- 你想构造一个可接受任意数量参数的函数。 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ 使用这个函数时,所有位置参数会被放到args元组中,所有关键字参数会被放到字典kwargs中。 -| - ---------- 讨论 ---------- diff --git a/source/c07/p02_functions_that_only_accept_keyword_arguments.rst b/source/c07/p02_functions_that_only_accept_keyword_arguments.rst index 8fe8f45..ade8da4 100644 --- a/source/c07/p02_functions_that_only_accept_keyword_arguments.rst +++ b/source/c07/p02_functions_that_only_accept_keyword_arguments.rst @@ -7,8 +7,6 @@ ---------- 你希望函数的某些参数强制使用关键字参数传递 -| - ---------- 解决方案 ---------- @@ -36,8 +34,6 @@ minimum(1, 5, 2, -5, 10) # Returns -5 minimum(1, 5, 2, -5, 10, clip=0) # Returns 0 -| - ---------- 讨论 ---------- diff --git a/source/c07/p03_attach_informatinal_matadata_to_function_arguments.rst b/source/c07/p03_attach_informatinal_matadata_to_function_arguments.rst index 840152a..cad3825 100644 --- a/source/c07/p03_attach_informatinal_matadata_to_function_arguments.rst +++ b/source/c07/p03_attach_informatinal_matadata_to_function_arguments.rst @@ -7,8 +7,6 @@ ---------- 你写好了一个函数,然后想为这个函数的参数增加一些额外的信息,这样的话其他使用者就能清楚的知道这个函数应该怎么使用。 -| - ---------- 解决方案 ---------- @@ -32,8 +30,6 @@ python解释器不会对这些注解添加任何的语义。它们不会被类 尽管你可以使用任意类型的对象给函数添加注解(例如数字,字符串,对象实例等等),不过通常来讲使用类或着字符串会比较好点。 -| - ---------- 讨论 ---------- diff --git a/source/c07/p04_return_multiple_values_from_function.rst b/source/c07/p04_return_multiple_values_from_function.rst index 1cc0fce..03cf162 100644 --- a/source/c07/p04_return_multiple_values_from_function.rst +++ b/source/c07/p04_return_multiple_values_from_function.rst @@ -7,8 +7,6 @@ ---------- 你希望构造一个可以返回多个值的函数 -| - ---------- 解决方案 ---------- @@ -27,8 +25,6 @@ >>> c 3 -| - ---------- 讨论 ---------- diff --git a/source/c07/p05_define_functions_with_default_arguments.rst b/source/c07/p05_define_functions_with_default_arguments.rst index 00154ed..a1b6a6b 100644 --- a/source/c07/p05_define_functions_with_default_arguments.rst +++ b/source/c07/p05_define_functions_with_default_arguments.rst @@ -7,8 +7,6 @@ ---------- 你想定义一个函数或者方法,它的一个或多个参数是可选的并且有一个默认值。 -| - ---------- 解决方案 ---------- @@ -55,8 +53,6 @@ 仔细观察可以发现到传递一个None值和不传值两种情况是有差别的。 -| - ---------- 讨论 ---------- diff --git a/source/c07/p06_define_anonymous_or_inline_functions.rst b/source/c07/p06_define_anonymous_or_inline_functions.rst index 8e90e24..941e2ed 100644 --- a/source/c07/p06_define_anonymous_or_inline_functions.rst +++ b/source/c07/p06_define_anonymous_or_inline_functions.rst @@ -8,8 +8,6 @@ 你想为 ``sort()`` 操作创建一个很短的回调函数,但又不想用 ``def`` 去写一个单行函数, 而是希望通过某个快捷方式以内联方式来创建这个函数。 -| - ---------- 解决方案 ---------- @@ -44,8 +42,6 @@ lambda表达式典型的使用场景是排序或数据reduce等: ['Ned Batchelder', 'David Beazley', 'Raymond Hettinger', 'Brian Jones'] >>> -| - ---------- 讨论 ---------- diff --git a/source/c07/p07_capturing_variables_in_anonymous_functions.rst b/source/c07/p07_capturing_variables_in_anonymous_functions.rst index d0272cb..796a3e3 100644 --- a/source/c07/p07_capturing_variables_in_anonymous_functions.rst +++ b/source/c07/p07_capturing_variables_in_anonymous_functions.rst @@ -7,8 +7,6 @@ ---------- 你用lambda定义了一个匿名函数,并想在定义时捕获到某些变量的值。 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ 30 >>> -| - ---------- 讨论 ---------- diff --git a/source/c07/p08_make_callable_with_fewer_arguments.rst b/source/c07/p08_make_callable_with_fewer_arguments.rst index 9a9ce0b..20ada25 100644 --- a/source/c07/p08_make_callable_with_fewer_arguments.rst +++ b/source/c07/p08_make_callable_with_fewer_arguments.rst @@ -8,8 +8,6 @@ 你有一个被其他python代码使用的callable对象,可能是一个回调函数或者是一个处理器, 但是它的参数太多了,导致调用时出错。 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ 可以看出 ``partial()`` 固定某些参数并返回一个新的callable对象。这个新的callable接受未赋值的参数, 然后跟之前已经赋值过的参数合并起来,最后将所有参数传递给原始函数。 -| - ---------- 讨论 ---------- diff --git a/source/c07/p10_carry_extra_state_with_callback_functions.rst b/source/c07/p10_carry_extra_state_with_callback_functions.rst index 73c5a61..5a7f8f8 100644 --- a/source/c07/p10_carry_extra_state_with_callback_functions.rst +++ b/source/c07/p10_carry_extra_state_with_callback_functions.rst @@ -8,8 +8,6 @@ 你的代码中需要依赖到回调函数的使用(比如事件处理器、等待后台任务完成后的回调等), 并且你还需要让回调函数拥有额外的状态值,以便在它的内部使用到。 -| - ---------- 解决方案 ---------- diff --git a/source/c07/p11_inline_callback_functions.rst b/source/c07/p11_inline_callback_functions.rst index 91385d6..0d6b3ef 100644 --- a/source/c07/p11_inline_callback_functions.rst +++ b/source/c07/p11_inline_callback_functions.rst @@ -8,8 +8,6 @@ 当你编写使用回调函数的代码的时候,担心很多小函数的扩张可能会弄乱程序控制流。 你希望找到某个方法来让代码看上去更像是一个普通的执行序列。 -| - ---------- 解决方案 ---------- @@ -90,8 +88,6 @@ 你会发现,除了那个特别的装饰器和 ``yield`` 语句外,其他地方并没有出现任何的回调函数(其实是在后台定义的)。 -| - ---------- 讨论 ---------- diff --git a/source/c07/p12_access_variables_defined_inside_closure.rst b/source/c07/p12_access_variables_defined_inside_closure.rst index f06675a..9e3252f 100644 --- a/source/c07/p12_access_variables_defined_inside_closure.rst +++ b/source/c07/p12_access_variables_defined_inside_closure.rst @@ -7,8 +7,6 @@ ---------- 你想要扩展函数中的某个闭包,允许它能访问和修改函数的内部变量。 -| - ---------- 解决方案 ---------- @@ -50,8 +48,6 @@ 10 >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p01_change_string_representation_of_instances.rst b/source/c08/p01_change_string_representation_of_instances.rst index a78dfe3..e8d690d 100644 --- a/source/c08/p01_change_string_representation_of_instances.rst +++ b/source/c08/p01_change_string_representation_of_instances.rst @@ -7,8 +7,6 @@ ---------- 你想改变对象实例的打印或显示输出,让它们更具可读性。 -| - ---------- 解决方案 ---------- @@ -53,8 +51,6 @@ p is (3, 4) >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p02_customizing_string_formatting.rst b/source/c08/p02_customizing_string_formatting.rst index 7f875a5..b32f5a2 100644 --- a/source/c08/p02_customizing_string_formatting.rst +++ b/source/c08/p02_customizing_string_formatting.rst @@ -7,8 +7,6 @@ ---------- 你想通过 ``format()`` 函数和字符串方法使得一个对象能支持自定义的格式化。 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ 'The date is 12/21/2012' >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p03_make_objects_support_context_management_protocol.rst b/source/c08/p03_make_objects_support_context_management_protocol.rst index 684cdb5..8123a99 100644 --- a/source/c08/p03_make_objects_support_context_management_protocol.rst +++ b/source/c08/p03_make_objects_support_context_management_protocol.rst @@ -7,8 +7,6 @@ ---------- 你想让你的对象支持上下文管理协议(with语句)。 -| - ---------- 解决方案 ---------- @@ -54,8 +52,6 @@ resp = b''.join(iter(partial(s.recv, 8192), b'')) # conn.__exit__() executes: connection closed -| - ---------- 讨论 ---------- diff --git a/source/c08/p04_save_memory_when_create_large_number_instances.rst b/source/c08/p04_save_memory_when_create_large_number_instances.rst index 03780d1..d659989 100644 --- a/source/c08/p04_save_memory_when_create_large_number_instances.rst +++ b/source/c08/p04_save_memory_when_create_large_number_instances.rst @@ -7,8 +7,6 @@ ---------- 你的程序要创建大量(可能上百万)的对象,导致占用很大的内存。 -| - ---------- 解决方案 ---------- diff --git a/source/c08/p05_encapsulating_names_in_class.rst b/source/c08/p05_encapsulating_names_in_class.rst index f751f24..4f8ef5d 100644 --- a/source/c08/p05_encapsulating_names_in_class.rst +++ b/source/c08/p05_encapsulating_names_in_class.rst @@ -7,8 +7,6 @@ ---------- 你想封装类的实例上面的“私有”数据,但是Python语言并没有访问控制。 -| - ---------- 解决方案 ---------- @@ -69,8 +67,6 @@ Python并不会真的阻止别人访问内部名称。但是如果你这么做 这里,私有名称 ``__private`` 和 ``__private_method`` 被重命名为 ``_C__private`` 和 ``_C__private_method`` ,这个跟父类B中的名称是完全不同的。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p06_create_managed_attributes.rst b/source/c08/p06_create_managed_attributes.rst index 392d9f6..9b81a39 100644 --- a/source/c08/p06_create_managed_attributes.rst +++ b/source/c08/p06_create_managed_attributes.rst @@ -7,8 +7,6 @@ ---------- 你想给某个实例attribute增加除访问与修改之外的其他处理逻辑,比如类型检查或合法性验证。 -| - ---------- 解决方案 ---------- @@ -96,8 +94,6 @@ property的一个关键特征是它看上去跟普通的attribute没什么两样 # Make a property from existing get/set methods name = property(get_first_name, set_first_name, del_first_name) -| - ---------- 讨论 ---------- diff --git a/source/c08/p07_calling_method_on_parent_class.rst b/source/c08/p07_calling_method_on_parent_class.rst index 3d0ab7d..8dcaf06 100644 --- a/source/c08/p07_calling_method_on_parent_class.rst +++ b/source/c08/p07_calling_method_on_parent_class.rst @@ -7,8 +7,6 @@ ---------- 你想在子类中调用父类的某个已经被覆盖的方法。 -| - ---------- 解决方案 ---------- @@ -62,8 +60,6 @@ 否则的话就委派给内部的代理对象 ``self._obj`` 去处理。 这看上去有点意思,因为就算没有显式的指明某个类的父类, ``super()`` 仍然可以有效的工作。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p08_extending_property_in_subclass.rst b/source/c08/p08_extending_property_in_subclass.rst index ab23f0b..3414862 100644 --- a/source/c08/p08_extending_property_in_subclass.rst +++ b/source/c08/p08_extending_property_in_subclass.rst @@ -7,8 +7,6 @@ ---------- 在子类中,你想要扩展定义在父类中的property的功能。 -| - ---------- 解决方案 ---------- @@ -96,8 +94,6 @@ print('Setting name to', value) super(SubPerson, SubPerson).name.__set__(self, value) -| - ---------- 讨论 ---------- diff --git a/source/c08/p09_create_new_kind_of_class_or_instance_attribute.rst b/source/c08/p09_create_new_kind_of_class_or_instance_attribute.rst index 7c0e616..e9965d5 100644 --- a/source/c08/p09_create_new_kind_of_class_or_instance_attribute.rst +++ b/source/c08/p09_create_new_kind_of_class_or_instance_attribute.rst @@ -7,8 +7,6 @@ ---------- 你想创建一个新的拥有一些额外功能的实例属性类型,比如类型检查。 -| - ---------- 解决方案 ---------- @@ -72,8 +70,6 @@ 为了实现请求操作,会相应的操作实例底层的字典(__dict__属性)。 描述器的 ``self.name`` 属性存储了在实例字典中被实际使用到的key。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p10_using_lazily_computed_properties.rst b/source/c08/p10_using_lazily_computed_properties.rst index c1bec0a..1335fe0 100644 --- a/source/c08/p10_using_lazily_computed_properties.rst +++ b/source/c08/p10_using_lazily_computed_properties.rst @@ -8,8 +8,6 @@ 你想将一个只读属性定义成一个property,并且只在访问的时候才会计算结果。 但是一旦被访问后,你希望结果值被缓存起来,不用每次都去计算。 -| - ---------- 解决方案 ---------- @@ -70,8 +68,6 @@ 仔细观察你会发现消息 ``Computing area`` 和 ``Computing perimeter`` 仅仅出现一次。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p12_define_interface_or_abstract_base_class.rst b/source/c08/p12_define_interface_or_abstract_base_class.rst index eab5346..13c3946 100644 --- a/source/c08/p12_define_interface_or_abstract_base_class.rst +++ b/source/c08/p12_define_interface_or_abstract_base_class.rst @@ -7,8 +7,6 @@ ---------- 你想定义一个接口或抽象类,并且通过执行类型检查来确保子类实现了某些特定的方法 -| - ---------- 解决方案 ---------- @@ -94,8 +92,6 @@ def method2(): pass -| - ---------- 讨论 ---------- diff --git a/source/c08/p13_implementing_data_model_or_type_system.rst b/source/c08/p13_implementing_data_model_or_type_system.rst index 634b332..28a8a59 100644 --- a/source/c08/p13_implementing_data_model_or_type_system.rst +++ b/source/c08/p13_implementing_data_model_or_type_system.rst @@ -7,8 +7,6 @@ ---------- 你想定义某些在属性赋值上面有限制的数据结构。 -| - ---------- 解决方案 ---------- diff --git a/source/c08/p14_implementing_custom_containers.rst b/source/c08/p14_implementing_custom_containers.rst index 4c3851f..ac2daf1 100644 --- a/source/c08/p14_implementing_custom_containers.rst +++ b/source/c08/p14_implementing_custom_containers.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个自定义的类来模拟内置的容器类功能,比如列表和字典。但是你不确定到底要实现哪些方法。 -| - ---------- 解决方案 ---------- @@ -75,8 +73,6 @@ 这里面使用到了 ``bisect`` 模块,它是一个在排序列表中插入元素的高效方式。可以保证元素插入后还保持顺序。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p15_delegating_attribute_access.rst b/source/c08/p15_delegating_attribute_access.rst index b88ec33..7a8bc9d 100644 --- a/source/c08/p15_delegating_attribute_access.rst +++ b/source/c08/p15_delegating_attribute_access.rst @@ -7,8 +7,6 @@ ---------- 你想将某个实例的属性访问代理到内部另一个实例中去,目的可能是作为继承的一个替代方法或者实现代理模式。 -| - ---------- 解决方案 ---------- @@ -124,8 +122,6 @@ 通过自定义属性访问方法,你可以用不同方式自定义代理类行为(比如加入日志功能、只读访问等)。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p16_define_more_than_one_constructor_in_class.rst b/source/c08/p16_define_more_than_one_constructor_in_class.rst index 32ee0aa..d28f4e2 100644 --- a/source/c08/p16_define_more_than_one_constructor_in_class.rst +++ b/source/c08/p16_define_more_than_one_constructor_in_class.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个类,除了使用 ``__init__()`` 方法外,还有其他方式可以初始化它。 -| - ---------- 解决方案 ---------- @@ -39,8 +37,6 @@ a = Date(2012, 12, 21) # Primary b = Date.today() # Alternate -| - ---------- 讨论 ---------- diff --git a/source/c08/p17_create_instance_without_invoking_init_method.rst b/source/c08/p17_create_instance_without_invoking_init_method.rst index 2e0f2a1..ee492b6 100644 --- a/source/c08/p17_create_instance_without_invoking_init_method.rst +++ b/source/c08/p17_create_instance_without_invoking_init_method.rst @@ -7,8 +7,6 @@ ---------- 你想创建一个实例,但是希望绕过执行 ``__init__()`` 方法。 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ 8 >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p18_extending_classes_with_mixins.rst b/source/c08/p18_extending_classes_with_mixins.rst index d69376c..8abc87f 100644 --- a/source/c08/p18_extending_classes_with_mixins.rst +++ b/source/c08/p18_extending_classes_with_mixins.rst @@ -8,8 +8,6 @@ 你有很多有用的方法,想使用它们来扩展其他类的功能。但是这些类并没有任何继承的关系。 因此你不能简单的将这些方法放入一个基类,然后被其他类继承。 -| - ---------- 解决方案 ---------- @@ -89,8 +87,6 @@ 这个例子中,可以看到混入类跟其他已存在的类(比如dict、defaultdict和OrderedDict)结合起来使用,一个接一个。 结合后就能发挥正常功效了。 -| - ---------- 讨论 ---------- diff --git a/source/c08/p19_implements_stateful_objects_or_state_machines.rst b/source/c08/p19_implements_stateful_objects_or_state_machines.rst index 2467065..b09c22e 100644 --- a/source/c08/p19_implements_stateful_objects_or_state_machines.rst +++ b/source/c08/p19_implements_stateful_objects_or_state_machines.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个状态机或者是在不同状态下执行操作的对象,但是又不想在代码中出现太多的条件判断语句。 -| - ---------- 解决方案 ---------- @@ -154,8 +152,6 @@ >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p20_call_method_on_object_by_string_name.rst b/source/c08/p20_call_method_on_object_by_string_name.rst index 314a0fd..2e30273 100644 --- a/source/c08/p20_call_method_on_object_by_string_name.rst +++ b/source/c08/p20_call_method_on_object_by_string_name.rst @@ -7,8 +7,6 @@ ---------- 你有一个字符串形式的方法名称,想通过它调用某个对象的对应方法。 -| - ---------- 解决方案 ---------- @@ -56,8 +54,6 @@ # Sort by distance from origin (0, 0) points.sort(key=operator.methodcaller('distance', 0, 0)) -| - ---------- 讨论 ---------- diff --git a/source/c08/p21_implementing_visitor_pattern.rst b/source/c08/p21_implementing_visitor_pattern.rst index 473a0aa..2723ca4 100644 --- a/source/c08/p21_implementing_visitor_pattern.rst +++ b/source/c08/p21_implementing_visitor_pattern.rst @@ -8,8 +8,6 @@ 你要处理由大量不同类型的对象组成的复杂数据结构,每一个对象都需要需要进行不同的处理。 比如,遍历一个树形结构,然后根据每个节点的相应状态执行不同的操作。 -| - ---------- 解决方案 ---------- diff --git a/source/c08/p22_implementing_visitor_pattern_without_recursion.rst b/source/c08/p22_implementing_visitor_pattern_without_recursion.rst index a9c8695..b88bb9c 100644 --- a/source/c08/p22_implementing_visitor_pattern_without_recursion.rst +++ b/source/c08/p22_implementing_visitor_pattern_without_recursion.rst @@ -8,8 +8,6 @@ 你使用访问者模式遍历一个很深的嵌套树形数据结构,并且因为超过嵌套层级限制而失败。 你想消除递归,并同时保持访问者编程模式。 -| - ---------- 解决方案 ---------- diff --git a/source/c08/p23_managing_memory_in_cyclic_data_structures.rst b/source/c08/p23_managing_memory_in_cyclic_data_structures.rst index 1d3e62b..9bdc699 100644 --- a/source/c08/p23_managing_memory_in_cyclic_data_structures.rst +++ b/source/c08/p23_managing_memory_in_cyclic_data_structures.rst @@ -7,8 +7,6 @@ ---------- 你的程序创建了很多循环引用数据结构(比如树、图、观察者模式等),你碰到了内存管理难题。 -| - ---------- 解决方案 ---------- @@ -55,8 +53,6 @@ None >>> -| - ---------- 讨论 ---------- diff --git a/source/c08/p24_making_classes_support_comparison_operations.rst b/source/c08/p24_making_classes_support_comparison_operations.rst index 07ff869..60e23ad 100644 --- a/source/c08/p24_making_classes_support_comparison_operations.rst +++ b/source/c08/p24_making_classes_support_comparison_operations.rst @@ -7,8 +7,6 @@ ---------- 你想让某个类的实例支持标准的比较运算(比如>=,!=,<=,<等),但是又不想去实现那一大丢的特殊方法。 -| - ---------- 解决方案 ---------- @@ -85,8 +83,6 @@ Python类对每个比较操作都需要实现一个特殊方法来支持。 print('Which one is biggest?', max(houses)) # Prints 'h3: 1101-square-foot Split' print('Which is smallest?', min(houses)) # Prints 'h2: 846-square-foot Ranch' -| - ---------- 讨论 ---------- diff --git a/source/c08/p25_creating_cached_instances.rst b/source/c08/p25_creating_cached_instances.rst index 8c635d4..6175c6f 100644 --- a/source/c08/p25_creating_cached_instances.rst +++ b/source/c08/p25_creating_cached_instances.rst @@ -7,8 +7,6 @@ ---------- 在创建一个类的对象时,如果之前使用同样参数创建过这个对象, 你想返回它的缓存引用。 -| - ---------- 解决方案 ---------- @@ -60,8 +58,6 @@ True >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p01_put_wrapper_around_function.rst b/source/c09/p01_put_wrapper_around_function.rst index 4bfa13f..e309563 100644 --- a/source/c09/p01_put_wrapper_around_function.rst +++ b/source/c09/p01_put_wrapper_around_function.rst @@ -7,8 +7,6 @@ ---------- 你想在函数上添加一个包装器,增加额外的操作处理(比如日志、计时等)。 -| - ---------- 解决方案 ---------- @@ -50,8 +48,6 @@ countdown 0.87188299392912 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p02_preserve_function_metadata_when_write_decorators.rst b/source/c09/p02_preserve_function_metadata_when_write_decorators.rst index a97e301..3439c42 100644 --- a/source/c09/p02_preserve_function_metadata_when_write_decorators.rst +++ b/source/c09/p02_preserve_function_metadata_when_write_decorators.rst @@ -7,8 +7,6 @@ ---------- 你写了一个装饰器作用在某个函数上,但是这个函数的重要的元信息比如名字、文档字符串、注解和参数签名都丢失了。 -| - ---------- 解决方案 ---------- @@ -53,8 +51,6 @@ {'n': } >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p03_unwrapping_decorator.rst b/source/c09/p03_unwrapping_decorator.rst index fb2e555..cdb5311 100644 --- a/source/c09/p03_unwrapping_decorator.rst +++ b/source/c09/p03_unwrapping_decorator.rst @@ -7,8 +7,6 @@ ---------- 一个装饰器已经作用在一个函数上,你想撤销它,直接访问原始的未包装的那个函数。 -| - ---------- 解决方案 ---------- @@ -25,8 +23,6 @@ 7 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p04_define_decorator_that_takes_arguments.rst b/source/c09/p04_define_decorator_that_takes_arguments.rst index d654391..765e9e3 100644 --- a/source/c09/p04_define_decorator_that_takes_arguments.rst +++ b/source/c09/p04_define_decorator_that_takes_arguments.rst @@ -7,8 +7,6 @@ ---------- 你想定义一个可以接受参数的装饰器 -| - ---------- 解决方案 ---------- @@ -54,8 +52,6 @@ 内层的函数 ``decorate()`` 接受一个函数作为参数,然后在函数上面放置一个包装器。 这里的关键点是包装器是可以使用传递给 ``logged()`` 的参数的。 -| - ---------- 讨论 ---------- diff --git a/source/c09/p05_define_decorator_with_user_adjustable_attributes.rst b/source/c09/p05_define_decorator_with_user_adjustable_attributes.rst index 3625791..b7e41b0 100644 --- a/source/c09/p05_define_decorator_with_user_adjustable_attributes.rst +++ b/source/c09/p05_define_decorator_with_user_adjustable_attributes.rst @@ -7,8 +7,6 @@ ---------- 你想写一个装饰器来包装一个函数,并且允许用户提供参数在运行时控制装饰器行为。 -| - ---------- 解决方案 ---------- @@ -88,8 +86,6 @@ 5 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p06_define_decorator_that_takes_optional_argument.rst b/source/c09/p06_define_decorator_that_takes_optional_argument.rst index ca902a9..119ca5c 100644 --- a/source/c09/p06_define_decorator_that_takes_optional_argument.rst +++ b/source/c09/p06_define_decorator_that_takes_optional_argument.rst @@ -8,8 +8,6 @@ 你想写一个装饰器,既可以不传参数给它,比如 ``@decorator`` , 也可以传递可选参数给它,比如 ``@decorator(x,y,z)`` 。 -| - ---------- 解决方案 ---------- @@ -46,8 +44,6 @@ 可以看到,``@logged`` 装饰器可以同时不带参数或带参数。 -| - ---------- 讨论 ---------- diff --git a/source/c09/p07_enforcing_type_check_on_function_using_decorator.rst b/source/c09/p07_enforcing_type_check_on_function_using_decorator.rst index 72e86f8..a5011c2 100644 --- a/source/c09/p07_enforcing_type_check_on_function_using_decorator.rst +++ b/source/c09/p07_enforcing_type_check_on_function_using_decorator.rst @@ -7,8 +7,6 @@ ---------- 作为某种编程规约,你想在对函数参数进行强制类型检查。 -| - ---------- 解决方案 ---------- @@ -81,8 +79,6 @@ TypeError: Argument z must be >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p08_define_decorators_as_part_of_class.rst b/source/c09/p08_define_decorators_as_part_of_class.rst index 873cf4d..7b4f72a 100644 --- a/source/c09/p08_define_decorators_as_part_of_class.rst +++ b/source/c09/p08_define_decorators_as_part_of_class.rst @@ -7,8 +7,6 @@ ---------- 你想在类中定义装饰器,并将其作用在其他函数或方法上。 -| - ---------- 解决方案 ---------- @@ -53,8 +51,6 @@ 仔细观察可以发现一个是实例调用,一个是类调用。 -| - ---------- 讨论 ---------- diff --git a/source/c09/p09_define_decorators_as_classes.rst b/source/c09/p09_define_decorators_as_classes.rst index 275c5d0..8050653 100644 --- a/source/c09/p09_define_decorators_as_classes.rst +++ b/source/c09/p09_define_decorators_as_classes.rst @@ -8,8 +8,6 @@ 你想使用一个装饰器去包装函数,但是希望返回一个可调用的实例。 你需要让你的装饰器可以同时工作在类定义的内部和外部。 -| - ---------- 解决方案 ---------- @@ -69,8 +67,6 @@ >>> Spam.bar.ncalls 3 -| - ---------- 讨论 ---------- diff --git a/source/c09/p10_apply_decorators_to_class_and_static_methods.rst b/source/c09/p10_apply_decorators_to_class_and_static_methods.rst index ee7fc6d..47c57ba 100644 --- a/source/c09/p10_apply_decorators_to_class_and_static_methods.rst +++ b/source/c09/p10_apply_decorators_to_class_and_static_methods.rst @@ -7,8 +7,6 @@ ---------- 你想给类或静态方法提供装饰器。 -| - ---------- 解决方案 ---------- @@ -68,8 +66,6 @@ 0.11740279197692871 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p11_write_decorators_that_add_arguments_to_functions.rst b/source/c09/p11_write_decorators_that_add_arguments_to_functions.rst index d7b5d72..96e762a 100644 --- a/source/c09/p11_write_decorators_that_add_arguments_to_functions.rst +++ b/source/c09/p11_write_decorators_that_add_arguments_to_functions.rst @@ -7,8 +7,6 @@ ---------- 你想在装饰器中给被包装函数增加额外的参数,但是不能影响这个函数现有的调用规则。 -| - ---------- 解决方案 ---------- @@ -40,8 +38,6 @@ 1 2 3 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p12_using_decorators_to_patch_class_definitions.rst b/source/c09/p12_using_decorators_to_patch_class_definitions.rst index 5fbd473..dc5d238 100644 --- a/source/c09/p12_using_decorators_to_patch_class_definitions.rst +++ b/source/c09/p12_using_decorators_to_patch_class_definitions.rst @@ -7,8 +7,6 @@ ---------- 你想通过反省或者重写类定义的某部分来修改它的行为,但是你又不希望使用继承或元类的方式。 -| - ---------- 解决方案 ---------- @@ -50,8 +48,6 @@ getting: spam >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p13_using_mataclass_to_control_instance_creation.rst b/source/c09/p13_using_mataclass_to_control_instance_creation.rst index ef13e1e..956bb13 100644 --- a/source/c09/p13_using_mataclass_to_control_instance_creation.rst +++ b/source/c09/p13_using_mataclass_to_control_instance_creation.rst @@ -7,8 +7,6 @@ ---------- 你想通过改变实例创建方式来实现单例、缓存或其他类似的特性。 -| - ---------- 解决方案 ---------- @@ -128,8 +126,6 @@ Python程序员都知道,如果你定义了一个类,就能像函数一样 True >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p14_capture_class_attribute_definition_order.rst b/source/c09/p14_capture_class_attribute_definition_order.rst index b8bc724..5fd085b 100644 --- a/source/c09/p14_capture_class_attribute_definition_order.rst +++ b/source/c09/p14_capture_class_attribute_definition_order.rst @@ -8,8 +8,6 @@ 你想自动记录一个类中属性和方法定义的顺序, 然后可以利用它来做很多操作(比如序列化、映射到数据库等等)。 -| - ---------- 解决方案 ---------- @@ -92,8 +90,6 @@ TypeError: shares expects >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p15_define_metaclass_that_takes_optional_arguments.rst b/source/c09/p15_define_metaclass_that_takes_optional_arguments.rst index 0247ad8..4c5d2f9 100644 --- a/source/c09/p15_define_metaclass_that_takes_optional_arguments.rst +++ b/source/c09/p15_define_metaclass_that_takes_optional_arguments.rst @@ -7,8 +7,6 @@ ---------- 你想定义一个元类,允许类定义时提供可选参数,这样可以控制或配置类型的创建过程。 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ pass super().__init__(name, bases, ns) -| - ---------- 讨论 ---------- diff --git a/source/c09/p16_enforce_argument_signature_on_args_kwargs.rst b/source/c09/p16_enforce_argument_signature_on_args_kwargs.rst index 352aeba..78d272e 100644 --- a/source/c09/p16_enforce_argument_signature_on_args_kwargs.rst +++ b/source/c09/p16_enforce_argument_signature_on_args_kwargs.rst @@ -8,8 +8,6 @@ 你有一个函数或方法,它使用*args和**kwargs作为参数,这样使得它比较通用, 但有时候你想检查传递进来的参数是不是某个你想要的类型。 -| - ---------- 解决方案 ---------- @@ -117,8 +115,6 @@ TypeError: multiple values for argument 'shares' >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p17_enforce_coding_conventions_in_classes.rst b/source/c09/p17_enforce_coding_conventions_in_classes.rst index 6ec4d8d..d437a80 100644 --- a/source/c09/p17_enforce_coding_conventions_in_classes.rst +++ b/source/c09/p17_enforce_coding_conventions_in_classes.rst @@ -7,8 +7,6 @@ ---------- 你的程序包含一个很大的类继承体系,你希望强制执行某些编程规约(或者代码诊断)来帮助程序员保持清醒。 -| - ---------- 解决方案 ---------- @@ -127,8 +125,6 @@ 这种警告信息对于捕获一些微妙的程序bug是很有用的。例如,如果某个代码依赖于传递给方法的关键字参数, 那么当子类改变参数名字的时候就会调用出错。 -| - ---------- 讨论 ---------- diff --git a/source/c09/p18_define_classes_programmatically.rst b/source/c09/p18_define_classes_programmatically.rst index 195b57b..48953b9 100644 --- a/source/c09/p18_define_classes_programmatically.rst +++ b/source/c09/p18_define_classes_programmatically.rst @@ -8,8 +8,6 @@ 你在写一段代码,最终需要创建一个新的类对象。你考虑将类的定义源代码以字符串的形式发布出去。 并且使用函数比如 ``exec()`` 来执行它,但是你想寻找一个更加优雅的解决方案。 -| - ---------- 解决方案 ---------- @@ -90,8 +88,6 @@ 通常这是一个普通的字典,但是它实际上是 ``__prepare__()`` 方法返回的任意对象,这个在9.14小节已经介绍过了。 这个函数需要使用上面演示的 ``update()`` 方法给命名空间增加内容。 -| - ---------- 讨论 ---------- diff --git a/source/c09/p19_initializing_class_members_at_definition_time.rst b/source/c09/p19_initializing_class_members_at_definition_time.rst index 9839c78..78a2da8 100644 --- a/source/c09/p19_initializing_class_members_at_definition_time.rst +++ b/source/c09/p19_initializing_class_members_at_definition_time.rst @@ -62,8 +62,6 @@ AttributeError: can't set attribute >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p20_implement_multiple_dispatch_with_function_annotations.rst b/source/c09/p20_implement_multiple_dispatch_with_function_annotations.rst index 27954a8..ffcecd0 100644 --- a/source/c09/p20_implement_multiple_dispatch_with_function_annotations.rst +++ b/source/c09/p20_implement_multiple_dispatch_with_function_annotations.rst @@ -8,8 +8,6 @@ 你已经学过怎样使用函数参数注解,那么你可能会想利用它来实现基于类型的方法重载。 但是你不确定应该怎样去实现(或者到底行得通不)。 -| - ---------- 解决方案 ---------- @@ -171,8 +169,6 @@ 3 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p21_avoid_repetitive_property_methods.rst b/source/c09/p21_avoid_repetitive_property_methods.rst index 57e9d1f..5482ab8 100644 --- a/source/c09/p21_avoid_repetitive_property_methods.rst +++ b/source/c09/p21_avoid_repetitive_property_methods.rst @@ -69,8 +69,6 @@ self.name = name self.age = age -| - ---------- 讨论 ---------- diff --git a/source/c09/p22_define_context_managers_the_easy_way.rst b/source/c09/p22_define_context_managers_the_easy_way.rst index 27773f2..1d12446 100644 --- a/source/c09/p22_define_context_managers_the_easy_way.rst +++ b/source/c09/p22_define_context_managers_the_easy_way.rst @@ -7,8 +7,6 @@ ---------- 你想自己去实现一个新的上下文管理器,以便使用with语句。 -| - ---------- 解决方案 ---------- @@ -73,8 +71,6 @@ [1, 2, 3, 4, 5] >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p23_executing_code_with_local_side_effects.rst b/source/c09/p23_executing_code_with_local_side_effects.rst index 16ab4b3..dc2d996 100644 --- a/source/c09/p23_executing_code_with_local_side_effects.rst +++ b/source/c09/p23_executing_code_with_local_side_effects.rst @@ -7,8 +7,6 @@ ---------- 你想在使用范围内执行某个代码片段,并且希望在执行后所有的结果都不可见。 -| - ---------- 解决方案 ---------- @@ -57,8 +55,6 @@ 14 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p24_parse_and_analyzing_python_source.rst b/source/c09/p24_parse_and_analyzing_python_source.rst index 684d122..f897ba5 100644 --- a/source/c09/p24_parse_and_analyzing_python_source.rst +++ b/source/c09/p24_parse_and_analyzing_python_source.rst @@ -7,8 +7,6 @@ ---------- 你想写解析并分析Python源代码的程序。 -| - ---------- 解决方案 ---------- @@ -123,8 +121,6 @@ 9 >>> -| - ---------- 讨论 ---------- diff --git a/source/c09/p25_disassembling_python_byte_code.rst b/source/c09/p25_disassembling_python_byte_code.rst index e09b588..3f3762a 100644 --- a/source/c09/p25_disassembling_python_byte_code.rst +++ b/source/c09/p25_disassembling_python_byte_code.rst @@ -7,8 +7,6 @@ ---------- 你想通过将你的代码反编译成低级的字节码来查看它底层的工作机制。 -| - ---------- 解决方案 ---------- @@ -27,8 +25,6 @@ ... >>> -| - ---------- 讨论 ---------- diff --git a/source/c10/p01_make_hierarchical_package_of_modules.rst b/source/c10/p01_make_hierarchical_package_of_modules.rst index 896fe01..fc10d65 100644 --- a/source/c10/p01_make_hierarchical_package_of_modules.rst +++ b/source/c10/p01_make_hierarchical_package_of_modules.rst @@ -7,8 +7,6 @@ ---------- 你想将你的代码组织成由很多分层模块构成的包。 -| - ---------- 解决方案 ---------- @@ -37,8 +35,6 @@ from graphics.primitive import line import graphics.formats.jpg as jpg -| - ---------- 讨论 ---------- diff --git a/source/c10/p02_control_the_import_of_everything.rst b/source/c10/p02_control_the_import_of_everything.rst index a17b60b..003d598 100644 --- a/source/c10/p02_control_the_import_of_everything.rst +++ b/source/c10/p02_control_the_import_of_everything.rst @@ -8,8 +8,6 @@ 当使用'from module import *' 语句时,希望对从模块或包导出的符号进行精确控制。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ # Only export 'spam' and 'grok' __all__ = ['spam', 'grok'] -| - ---------- 讨论 ---------- diff --git a/source/c10/p03_import_submodules_by_relative_names.rst b/source/c10/p03_import_submodules_by_relative_names.rst index 8b25cba..2a2defa 100644 --- a/source/c10/p03_import_submodules_by_relative_names.rst +++ b/source/c10/p03_import_submodules_by_relative_names.rst @@ -46,8 +46,6 @@ 两个import语句都没包含顶层包名,而是使用了spam.py的相对路径。 -| - ---------- 讨论 ---------- diff --git a/source/c10/p04_split_module_into_multiple_files.rst b/source/c10/p04_split_module_into_multiple_files.rst index 9e37eda..fb258ee 100644 --- a/source/c10/p04_split_module_into_multiple_files.rst +++ b/source/c10/p04_split_module_into_multiple_files.rst @@ -8,8 +8,6 @@ 你想将一个模块分割成多个文件。但是你不想将分离的文件统一成一个逻辑模块时使已有的代码遭到破坏。 -| - ---------- 解决方案 ---------- @@ -78,8 +76,6 @@ B.bar >>> -| - ---------- 讨论 ---------- diff --git a/source/c10/p05_separate_directories_import_by_namespace.rst b/source/c10/p05_separate_directories_import_by_namespace.rst index 0fe28c3..2777591 100644 --- a/source/c10/p05_separate_directories_import_by_namespace.rst +++ b/source/c10/p05_separate_directories_import_by_namespace.rst @@ -7,8 +7,6 @@ ---------- 你可能有大量的代码,由不同的人来分散地维护。每个部分被组织为文件目录,如一个包。然而,你希望能用共同的包前缀将所有组件连接起来,不是将每一个部分作为独立的包来安装。 -| - ---------- 解决方案 ---------- @@ -45,8 +43,6 @@ 两个不同的包目录被合并到一起,你可以导入spam.blah和spam.grok,并且它们能够工作。 -| - ---------- 讨论 ---------- diff --git a/source/c10/p06_reloading_modules.rst b/source/c10/p06_reloading_modules.rst index 0b8ef01..c9fdadd 100644 --- a/source/c10/p06_reloading_modules.rst +++ b/source/c10/p06_reloading_modules.rst @@ -7,8 +7,6 @@ ---------- 你想重新加载已经加载的模块,因为你对其源码进行了修改。 -| - ---------- 解决方案 ---------- @@ -22,8 +20,6 @@ >>> -| - ---------- 讨论 ---------- diff --git a/source/c10/p07_make_directory_or_zip_runnable_as_main_script.rst b/source/c10/p07_make_directory_or_zip_runnable_as_main_script.rst index a0d551d..f218a7f 100644 --- a/source/c10/p07_make_directory_or_zip_runnable_as_main_script.rst +++ b/source/c10/p07_make_directory_or_zip_runnable_as_main_script.rst @@ -7,8 +7,6 @@ ---------- 您有已经一个复杂的脚本到涉及多个文件的应用程序。你想有一些简单的方法让用户运行程序。 -| - ---------- 解决方案 ---------- @@ -40,8 +38,6 @@ bash % python3 myapp.zip ... output from __main__.py ... -| - ---------- 讨论 ---------- diff --git a/source/c10/p08_read_datafile_within_package.rst b/source/c10/p08_read_datafile_within_package.rst index a90b7a0..d7b13c8 100644 --- a/source/c10/p08_read_datafile_within_package.rst +++ b/source/c10/p08_read_datafile_within_package.rst @@ -7,8 +7,6 @@ ---------- 你的包中包含代码需要去读取的数据文件。你需要尽可能地用最便捷的方式来做这件事。 -| - ---------- 解决方案 ---------- @@ -31,8 +29,6 @@ 由此产生的变量是包含该文件的原始内容的字节字符串。 -| - ---------- 讨论 ---------- diff --git a/source/c10/p09_add_directories_to_sys_path.rst b/source/c10/p09_add_directories_to_sys_path.rst index 3526b73..de27096 100644 --- a/source/c10/p09_add_directories_to_sys_path.rst +++ b/source/c10/p09_add_directories_to_sys_path.rst @@ -8,8 +8,6 @@ 你无法导入你的Python代码因为它所在的目录不在sys.path里。你想将添加新目录到Python路径,但是不想硬链接到你的代码。 -| - ---------- 解决方案 ---------- @@ -39,8 +37,6 @@ 这个.pth文件需要放在某个Python的site-packages目录,通常位于/usr/local/lib/python3.3/site-packages 或者 ~/.local/lib/python3.3/sitepackages。当解释器启动时,.pth文件里列举出来的存在于文件系统的目录将被添加到sys.path。安装一个.pth文件可能需要管理员权限,如果它被添加到系统级的Python解释器。 -| - ---------- 讨论 ---------- diff --git a/source/c10/p10_import_modules_using_name_given_in_string.rst b/source/c10/p10_import_modules_using_name_given_in_string.rst index cbc0ac3..c4d77f6 100644 --- a/source/c10/p10_import_modules_using_name_given_in_string.rst +++ b/source/c10/p10_import_modules_using_name_given_in_string.rst @@ -7,8 +7,6 @@ ---------- 你想导入一个模块,但是模块的名字在字符串里。你想对字符串调用导入命令。 -| - ---------- 解决方案 ---------- diff --git a/source/c10/p11_load_modules_from_remote_machine_by_hooks.rst b/source/c10/p11_load_modules_from_remote_machine_by_hooks.rst index a4abf13..98c404a 100644 --- a/source/c10/p11_load_modules_from_remote_machine_by_hooks.rst +++ b/source/c10/p11_load_modules_from_remote_machine_by_hooks.rst @@ -8,8 +8,6 @@ You would like to customize Python’s import statement so that it can transparently load modules from a remote machine. -| - ---------- 解决方案 ---------- @@ -474,8 +472,6 @@ module. For instance: return fib(n-1) + fib(n-2) >>> -| - ---------- 讨论 ---------- diff --git a/source/c10/p12_patching_modules_on_import.rst b/source/c10/p12_patching_modules_on_import.rst index d562c28..d4b8898 100644 --- a/source/c10/p12_patching_modules_on_import.rst +++ b/source/c10/p12_patching_modules_on_import.rst @@ -8,8 +8,6 @@ You want to patch or apply decorators to functions in an existing module. However, you only want to do it if the module actually gets imported and used elsewhere. -| - ---------- 解决方案 ---------- @@ -98,8 +96,6 @@ such as shown here: mod.cos = logged(mod.cos) mod.sin = logged(mod.sin) -| - ---------- 讨论 ---------- diff --git a/source/c10/p13_installing_packages_just_for_yourself.rst b/source/c10/p13_installing_packages_just_for_yourself.rst index f1bcc06..ef240ea 100644 --- a/source/c10/p13_installing_packages_just_for_yourself.rst +++ b/source/c10/p13_installing_packages_just_for_yourself.rst @@ -9,8 +9,6 @@ You want to install a third-party package, but you don’t have permission to in into the system Python. Alternatively, perhaps you just want to install a package for your own use, not all users on the system. -| - ---------- 解决方案 ---------- @@ -33,8 +31,6 @@ on sys.path. Thus, packages you install using this technique take priority over the packages already installed on the system (although this is not always the case depending on the behavior of third-party package managers, such as distribute or pip). -| - ---------- 讨论 ---------- diff --git a/source/c10/p14_creating_new_python_environment.rst b/source/c10/p14_creating_new_python_environment.rst index b2b87ff..e053b9f 100644 --- a/source/c10/p14_creating_new_python_environment.rst +++ b/source/c10/p14_creating_new_python_environment.rst @@ -9,8 +9,6 @@ You want to create a new Python environment in which you can install modules and packages. However, you want to do this without installing a new copy of Python or making changes that might affect the system Python installation. -| - ---------- 解决方案 ---------- @@ -56,8 +54,6 @@ A key feature of this interpreter is that its site-packages directory has been s newly created environment. Should you decide to install third-party packages, they will be installed here, not in the normal system site-packages directory. -| - ---------- 讨论 ---------- diff --git a/source/c10/p15_distributing_packages.rst b/source/c10/p15_distributing_packages.rst index 149ad95..6c3d16f 100644 --- a/source/c10/p15_distributing_packages.rst +++ b/source/c10/p15_distributing_packages.rst @@ -7,8 +7,6 @@ ---------- You’ve written a useful library, and you want to be able to give it away to others. -| - ---------- 解决方案 ---------- @@ -72,8 +70,6 @@ This will create a file such as projectname-1.0.zip or projectname-1.0.tar.gz, d on the platform. If it all works, this file is suitable for giving to others or uploading to the `Python Package Index `_. -| - ---------- 讨论 ---------- diff --git a/source/c11/p02_creating_tcp_server.rst b/source/c11/p02_creating_tcp_server.rst index b5b6444..21fbc4d 100644 --- a/source/c11/p02_creating_tcp_server.rst +++ b/source/c11/p02_creating_tcp_server.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个服务器,通过TCP协议和客户端通信。 -| - ---------- 解决方案 ---------- @@ -66,8 +64,6 @@ serv = TCPServer(('', 20000), EchoHandler) serv.serve_forever() -| - ---------- 讨论 ---------- diff --git a/source/c11/p03_creating_udp_server.rst b/source/c11/p03_creating_udp_server.rst index 45ba8f8..4884245 100644 --- a/source/c11/p03_creating_udp_server.rst +++ b/source/c11/p03_creating_udp_server.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个基于UDP协议的服务器来与客户端通信。 -| - ---------- 解决方案 ---------- @@ -47,8 +45,6 @@ (b'Wed Aug 15 20:35:08 2012', ('127.0.0.1', 20000)) >>> -| - ---------- 讨论 ---------- diff --git a/source/c11/p04_generate_range_of_ip_addresses_from_cidr_address.rst b/source/c11/p04_generate_range_of_ip_addresses_from_cidr_address.rst index 04d4452..b55d58c 100644 --- a/source/c11/p04_generate_range_of_ip_addresses_from_cidr_address.rst +++ b/source/c11/p04_generate_range_of_ip_addresses_from_cidr_address.rst @@ -8,8 +8,6 @@ 你有一个CIDR网络地址比如“123.45.67.89/27”,你想将其转换成它所代表的所有IP (比如,“123.45.67.64”, “123.45.67.65”, …, “123.45.67.95”)) -| - ---------- 解决方案 ---------- @@ -89,8 +87,6 @@ IPv4Address('123.45.67.73') >>> -| - ---------- 讨论 ---------- diff --git a/source/c11/p05_creating_simple_rest_based_interface.rst b/source/c11/p05_creating_simple_rest_based_interface.rst index d99b700..48e50e6 100644 --- a/source/c11/p05_creating_simple_rest_based_interface.rst +++ b/source/c11/p05_creating_simple_rest_based_interface.rst @@ -7,8 +7,6 @@ ---------- 你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架。 -| - ---------- 解决方案 ---------- @@ -121,8 +119,6 @@ >>> -| - ---------- 讨论 ---------- diff --git a/source/c11/p06_implement_simple_remote_procedure_call_with_xml_rpc.rst b/source/c11/p06_implement_simple_remote_procedure_call_with_xml_rpc.rst index c2650d2..815775e 100644 --- a/source/c11/p06_implement_simple_remote_procedure_call_with_xml_rpc.rst +++ b/source/c11/p06_implement_simple_remote_procedure_call_with_xml_rpc.rst @@ -7,8 +7,6 @@ ---------- 你想找到一个简单的方式去执行运行在远程机器上面的Python程序中的函数或方法。 -| - ---------- 解决方案 ---------- @@ -68,8 +66,6 @@ False >>> -| - ---------- 讨论 ---------- diff --git a/source/c11/p07_communicate_simply_between_interpreters.rst b/source/c11/p07_communicate_simply_between_interpreters.rst index 6017701..db92beb 100644 --- a/source/c11/p07_communicate_simply_between_interpreters.rst +++ b/source/c11/p07_communicate_simply_between_interpreters.rst @@ -7,8 +7,6 @@ ---------- 你在不同的机器上面运行着多个Python解释器实例,并希望能够在这些解释器之间通过消息来交换数据。 -| - ---------- 解决方案 ---------- @@ -60,8 +58,6 @@ 跟底层socket不同的是,每个消息会完整保存(每一个通过send()发送的对象能通过recv()来完整接受)。 另外,所有对象会通过pickle序列化。因此,任何兼容pickle的对象都能在此连接上面被发送和接受。 -| - ---------- 讨论 ---------- diff --git a/source/c11/p08_implementing_remote_procedure_calls.rst b/source/c11/p08_implementing_remote_procedure_calls.rst index 9814d18..b24390b 100644 --- a/source/c11/p08_implementing_remote_procedure_calls.rst +++ b/source/c11/p08_implementing_remote_procedure_calls.rst @@ -8,8 +8,6 @@ 你想在一个消息传输层如 ``sockets`` 、``multiprocessing connections`` 或 ``ZeroMQ`` 的基础之上实现一个简单的远程过程调用(RPC)。 -| - ---------- 解决方案 ---------- @@ -115,8 +113,6 @@ 要注意的是很多消息层(比如 ``multiprocessing`` )已经使用pickle序列化了数据。 如果是这样的话,对 ``pickle.dumps()`` 和 ``pickle.loads()`` 的调用要去掉。 -| - ---------- 讨论 ---------- diff --git a/source/c11/p09_authenticating_clients_simply.rst b/source/c11/p09_authenticating_clients_simply.rst index 83439e4..7a47a60 100644 --- a/source/c11/p09_authenticating_clients_simply.rst +++ b/source/c11/p09_authenticating_clients_simply.rst @@ -7,8 +7,6 @@ ---------- 你想在分布式系统中实现一个简单的客户端连接认证功能,又不想像SSL那样的复杂。 -| - ---------- 解决方案 ---------- @@ -85,8 +83,6 @@ s.send(b'Hello World') resp = s.recv(1024) -| - ---------- 讨论 ---------- diff --git a/source/c11/p10_add_ssl_to_network_services.rst b/source/c11/p10_add_ssl_to_network_services.rst index 3915362..09df2df 100644 --- a/source/c11/p10_add_ssl_to_network_services.rst +++ b/source/c11/p10_add_ssl_to_network_services.rst @@ -7,8 +7,6 @@ ---------- 你想实现一个基于sockets的网络服务,客户端和服务器通过SSL协议认证并加密传输的数据。 -| - ---------- 解决方案 ---------- @@ -242,8 +240,6 @@ 'client_key.pem'), allow_none=True) -| - ---------- 讨论 ---------- diff --git a/source/c11/p11_pass_socket_file_descriptor_between_processes.rst b/source/c11/p11_pass_socket_file_descriptor_between_processes.rst index 8562c30..0870db6 100644 --- a/source/c11/p11_pass_socket_file_descriptor_between_processes.rst +++ b/source/c11/p11_pass_socket_file_descriptor_between_processes.rst @@ -8,8 +8,6 @@ 你有多个Python解释器进程在同时运行,你想将某个打开的文件描述符从一个解释器传递给另外一个。 比如,假设有个服务器进程相应连接请求,但是实际的相应逻辑是在另一个解释器中执行的。 -| - ---------- 解决方案 ---------- @@ -81,8 +79,6 @@ 此例最重要的部分是服务器接收到的客户端socket实际上被另外一个不同的进程处理。 服务器仅仅只是将其转手并关闭此连接,然后等待下一个连接。 -| - ---------- 讨论 ---------- diff --git a/source/c11/p12_understanding_event_driven_io.rst b/source/c11/p12_understanding_event_driven_io.rst index ac0fb4e..9cb2af0 100644 --- a/source/c11/p12_understanding_event_driven_io.rst +++ b/source/c11/p12_understanding_event_driven_io.rst @@ -8,8 +8,6 @@ 你应该已经听过基于事件驱动或异步I/O的包,但是你还不能完全理解它的底层到底是怎样工作的, 或者是如果使用它的话会对你的程序产生什么影响。 -| - ---------- 解决方案 ---------- @@ -175,8 +173,6 @@ TCP例子的关键点是从处理器中列表增加和删除客户端的操作 对每一个连接,一个新的处理器被创建并加到列表中。当连接被关闭后,每个客户端负责将其从列表中删除。 如果你运行程序并试着用Telnet或类似工具连接,它会将你发送的消息回显给你。并且它能很轻松的处理多客户端连接。 -| - ---------- 讨论 ---------- diff --git a/source/c11/p13_sending_receiving_large_arrays.rst b/source/c11/p13_sending_receiving_large_arrays.rst index 04f5b72..07d0d23 100644 --- a/source/c11/p13_sending_receiving_large_arrays.rst +++ b/source/c11/p13_sending_receiving_large_arrays.rst @@ -7,8 +7,6 @@ ---------- 你要通过网络连接发送和接受连续数据的大型数组,并尽量减少数据的复制操作。 -| - ---------- 解决方案 ---------- @@ -70,8 +68,6 @@ array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) >>> -| - ---------- 讨论 ---------- diff --git a/source/c12/p01_start_stop_thread.rst b/source/c12/p01_start_stop_thread.rst index db79ec8..fd36bdd 100644 --- a/source/c12/p01_start_stop_thread.rst +++ b/source/c12/p01_start_stop_thread.rst @@ -98,8 +98,6 @@ Python解释器在所有线程都终止后才继续执行代码剩余的部分 ... # Terminated return -| - ---------- 讨论 ---------- diff --git a/source/c12/p05_locking_with_deadlock_avoidance.rst b/source/c12/p05_locking_with_deadlock_avoidance.rst index b78177b..10e0e15 100644 --- a/source/c12/p05_locking_with_deadlock_avoidance.rst +++ b/source/c12/p05_locking_with_deadlock_avoidance.rst @@ -7,8 +7,6 @@ ---------- 你正在写一个多线程程序,其中线程需要一次获取多个锁,此时如何避免死锁问题。 -| - ---------- 解决方案 ---------- @@ -126,8 +124,6 @@ 发生崩溃的原因在于,每个线程都记录着自己已经获取到的锁。 ``acquire()`` 函数会检查之前已经获取的锁列表, 由于锁是按照升序排列获取的,所以函数会认为之前已获取的锁的id必定小于新申请到的锁,这时就会触发异常。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p06_storing_thread_specific_state.rst b/source/c12/p06_storing_thread_specific_state.rst index 3d72116..b51ff58 100644 --- a/source/c12/p06_storing_thread_specific_state.rst +++ b/source/c12/p06_storing_thread_specific_state.rst @@ -7,8 +7,6 @@ ---------- 你需要保存正在运行线程的状态,这个状态对于其他的线程是不可见的。 -| - ---------- 解决方案 ---------- @@ -74,8 +72,6 @@ 它之所以行得通的原因是每个线程会创建一个自己专属的套接字连接(存储为self.local.sock)。 因此,当不同的线程执行套接字操作时,由于操作的是不同的套接字,因此它们不会相互影响。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p07_creating_thread_pool.rst b/source/c12/p07_creating_thread_pool.rst index a423280..6420ac8 100644 --- a/source/c12/p07_creating_thread_pool.rst +++ b/source/c12/p07_creating_thread_pool.rst @@ -7,8 +7,6 @@ ---------- 你创建一个工作者线程池,用来相应客户端请求或执行其他的工作。 -| - ---------- 解决方案 ---------- @@ -111,8 +109,6 @@ 例子中返回的handle对象会帮你处理所有的阻塞与协作,然后从工作线程中返回数据给你。 特别的,``a.result()`` 操作会阻塞进程直到对应的函数执行完成并返回一个结果。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p08_perform_simple_parallel_programming.rst b/source/c12/p08_perform_simple_parallel_programming.rst index 642a1a8..856b383 100644 --- a/source/c12/p08_perform_simple_parallel_programming.rst +++ b/source/c12/p08_perform_simple_parallel_programming.rst @@ -7,8 +7,6 @@ ---------- 你有个程序要执行CPU密集型工作,你想让他利用多核CPU的优势来运行的快一点。 -| - ---------- 解决方案 ---------- @@ -124,8 +122,6 @@ 通过这个修改后,运行这个脚本产生同样的结果,但是在四核机器上面比之前快了3.5倍。 实际的性能优化效果根据你的机器CPU数量的不同而不同。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p09_dealing_with_gil_stop_worring_about_it.rst b/source/c12/p09_dealing_with_gil_stop_worring_about_it.rst index 41f1675..3efdad7 100644 --- a/source/c12/p09_dealing_with_gil_stop_worring_about_it.rst +++ b/source/c12/p09_dealing_with_gil_stop_worring_about_it.rst @@ -7,8 +7,6 @@ ---------- 你已经听说过全局解释器锁GIL,担心它会影响到多线程程序的执行性能。 -| - ---------- 解决方案 ---------- @@ -106,8 +104,6 @@ GIL最大的问题就是Python的多线程程序并不能利用多核CPU的优 如果你使用其他工具访问C语言,比如对于Cython的ctypes库,你不需要做任何事。 例如,ctypes在调用C时会自动释放GIL。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p10_defining_an_actor_task.rst b/source/c12/p10_defining_an_actor_task.rst index e505caf..ddcffd6 100644 --- a/source/c12/p10_defining_an_actor_task.rst +++ b/source/c12/p10_defining_an_actor_task.rst @@ -7,8 +7,6 @@ ---------- 你想定义跟actor模式中类似“actors”角色的任务 -| - ---------- 解决方案 ---------- @@ -127,8 +125,6 @@ actor之间的通信是单向和异步的。因此,消息发送者不知道消 p.send('World') p.close() -| - ---------- 讨论 ---------- diff --git a/source/c12/p11_implement publish_subscribe_messaging.rst b/source/c12/p11_implement publish_subscribe_messaging.rst index 436fa47..4196872 100644 --- a/source/c12/p11_implement publish_subscribe_messaging.rst +++ b/source/c12/p11_implement publish_subscribe_messaging.rst @@ -7,8 +7,6 @@ ---------- 你有一个基于线程通信的程序,想让它们实现发布/订阅模式的消息通信。 -| - ---------- 解决方案 ---------- @@ -77,8 +75,6 @@ 尽管对于这个问题有很多的变种,不过万变不离其宗。 消息会被发送给一个交换机,然后交换机会将它们发送给被绑定的订阅者。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p12_using_generators_as_alternative_to_threads.rst b/source/c12/p12_using_generators_as_alternative_to_threads.rst index 0c7df7b..86b6330 100644 --- a/source/c12/p12_using_generators_as_alternative_to_threads.rst +++ b/source/c12/p12_using_generators_as_alternative_to_threads.rst @@ -7,8 +7,6 @@ ---------- 你想使用生成器(协程)替代系统线程来实现并发。这个有时又被称为用户级线程或绿色线程。 -| - ---------- 解决方案 ---------- @@ -337,8 +335,6 @@ 有一个就绪的任务队列,并且还有因I/O休眠的任务等待区域。 还有很多调度器负责在就绪队列和I/O等待区域之间移动任务。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p13_polling_multiple_thread_queues.rst b/source/c12/p13_polling_multiple_thread_queues.rst index 0a0475a..a1c3711 100644 --- a/source/c12/p13_polling_multiple_thread_queues.rst +++ b/source/c12/p13_polling_multiple_thread_queues.rst @@ -8,8 +8,6 @@ 你有一个线程队列集合,想为到来的元素轮询它们, 就跟你为一个客户端请求去轮询一个网络连接集合的方式一样。 -| - ---------- 解决方案 ---------- @@ -94,8 +92,6 @@ 如果你试着运行它,你会发现这个消费者会接受到所有的被放入的元素,不管元素被放进了哪个队列中。 -| - ---------- 讨论 ---------- diff --git a/source/c12/p14_launching_daemon_process_on_unix.rst b/source/c12/p14_launching_daemon_process_on_unix.rst index a17aa64..3529750 100644 --- a/source/c12/p14_launching_daemon_process_on_unix.rst +++ b/source/c12/p14_launching_daemon_process_on_unix.rst @@ -7,8 +7,6 @@ ---------- 你想编写一个作为一个在Unix或类Unix系统上面运行的守护进程运行的程序。 -| - ---------- 解决方案 ---------- @@ -133,8 +131,6 @@ bash % daemon.py stop bash % -| - ---------- 讨论 ---------- diff --git a/source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst b/source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst index 831b5bf..827097b 100644 --- a/source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst +++ b/source/c13/p01_accept_input_via_redirect_pips_or_input_files.rst @@ -8,8 +8,6 @@ 你希望你的脚本接受任何用户认为最简单的输入方式。包括将命令行的输出通过管道传递给该脚本、 重定向文件到该脚本,或在命令行中传递一个文件名或文件名列表给该脚本。 -| - ---------- 解决方案 ---------- @@ -33,8 +31,6 @@ Python内置的 ``fileinput`` 模块让这个变得简单。如果你有一个 $ ./filein.py /etc/passwd # Reads /etc/passwd to stdout. $ ./filein.py < /etc/passwd # Reads /etc/passwd to stdout. -| - ---------- 讨论 ---------- diff --git a/source/c13/p02_terminate_program_with_an_error_message.rst b/source/c13/p02_terminate_program_with_an_error_message.rst index 6240984..f962d5c 100644 --- a/source/c13/p02_terminate_program_with_an_error_message.rst +++ b/source/c13/p02_terminate_program_with_an_error_message.rst @@ -7,8 +7,6 @@ ---------- 你想向标准错误打印一条消息并返回某个非零状态码来终止程序运行 -| - ---------- 解决方案 ---------- @@ -20,8 +18,6 @@ 它会将消息在 ``sys.stderr`` 中打印,然后程序以状态码1退出。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p03_parsing_command_line_options.rst b/source/c13/p03_parsing_command_line_options.rst index 770aba6..5a65b07 100644 --- a/source/c13/p03_parsing_command_line_options.rst +++ b/source/c13/p03_parsing_command_line_options.rst @@ -7,8 +7,6 @@ ---------- 你的程序如何能够解析命令行选项(位于sys.argv中) -| - ---------- 解决方案 ---------- @@ -103,8 +101,6 @@ 对于选项值的进一步处理由程序来决定,用你自己的逻辑来替代 ``print()`` 函数。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p04_prompt_for_password_at_runtime.rst b/source/c13/p04_prompt_for_password_at_runtime.rst index bfea877..940d02e 100644 --- a/source/c13/p04_prompt_for_password_at_runtime.rst +++ b/source/c13/p04_prompt_for_password_at_runtime.rst @@ -8,8 +8,6 @@ 你写了个脚本,运行时需要一个密码。此脚本是交互式的,因此不能将密码在脚本中硬编码, 而是需要弹出一个密码输入提示,让用户自己输入。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ 在此代码中,``svc_login()`` 是你要实现的处理密码的函数,具体的处理过程你自己决定。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p05_getting_terminal_size.rst b/source/c13/p05_getting_terminal_size.rst index 3404a28..d46edc6 100644 --- a/source/c13/p05_getting_terminal_size.rst +++ b/source/c13/p05_getting_terminal_size.rst @@ -7,8 +7,6 @@ ---------- 你需要知道当前终端的大小以便正确的格式化输出。 -| - ---------- 解决方案 ---------- @@ -28,8 +26,6 @@ 24 >>> -| - ---------- 讨论 ---------- diff --git a/source/c13/p06_executing_external_command_and_get_its_output.rst b/source/c13/p06_executing_external_command_and_get_its_output.rst index 583c4c9..2c6f4fd 100644 --- a/source/c13/p06_executing_external_command_and_get_its_output.rst +++ b/source/c13/p06_executing_external_command_and_get_its_output.rst @@ -7,8 +7,6 @@ ---------- 你想执行一个外部命令并以Python字符串的形式获取执行结果。 -| - ---------- 解决方案 ---------- @@ -66,8 +64,6 @@ 需要注意的是在shell中执行命令会存在一定的安全风险,特别是当参数来自于用户输入时。 这时候可以使用 ``shlex.quote()`` 函数来讲参数正确的用双引用引起来。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p07_copy_move_files_and_directories.rst b/source/c13/p07_copy_move_files_and_directories.rst index 9e1c0b9..bdb1a3e 100644 --- a/source/c13/p07_copy_move_files_and_directories.rst +++ b/source/c13/p07_copy_move_files_and_directories.rst @@ -7,8 +7,6 @@ ---------- 你想哟啊复制或移动文件和目录,但是又不想调用shell命令。 -| - ---------- 解决方案 ---------- @@ -61,8 +59,6 @@ already been provided to do it. For example: shutil.copytree(src, dst, ignore=shutil.ignore_patterns('*~','*.pyc')) -| - ---------- 讨论 ---------- diff --git a/source/c13/p08_creating_and_unpacking_archives.rst b/source/c13/p08_creating_and_unpacking_archives.rst index 124b581..13c87a6 100644 --- a/source/c13/p08_creating_and_unpacking_archives.rst +++ b/source/c13/p08_creating_and_unpacking_archives.rst @@ -7,8 +7,6 @@ ---------- 你需要创建或解压常见格式的归档文件(比如.tar, .tgz或.zip) -| - ---------- 解决方案 ---------- @@ -34,8 +32,6 @@ ('tar', 'uncompressed tar file'), ('zip', 'ZIP file')] >>> -| - ---------- 讨论 ---------- diff --git a/source/c13/p09_find_files_by_name.rst b/source/c13/p09_find_files_by_name.rst index 418453b..aecc4cb 100644 --- a/source/c13/p09_find_files_by_name.rst +++ b/source/c13/p09_find_files_by_name.rst @@ -8,8 +8,6 @@ 你需要写一个涉及到文件查找操作的脚本,比如对日志归档文件的重命名工具, 你不想在Python脚本中调用shell,或者你要实现一些shell不能做的功能。 -| - ---------- 解决方案 ---------- @@ -36,8 +34,6 @@ .. code-block:: python bash % ./findfile.py . myfile.txt -| - ---------- 讨论 ---------- diff --git a/source/c13/p10_read_configuration_files.rst b/source/c13/p10_read_configuration_files.rst index fee416f..d8a394b 100644 --- a/source/c13/p10_read_configuration_files.rst +++ b/source/c13/p10_read_configuration_files.rst @@ -7,8 +7,6 @@ ---------- 怎样读取普通.ini格式的配置文件? -| - ---------- 解决方案 ---------- @@ -98,8 +96,6 @@ ================================= >>> -| - ---------- 讨论 ---------- diff --git a/source/c13/p11_add_logging_to_simple_scripts.rst b/source/c13/p11_add_logging_to_simple_scripts.rst index 67b18e2..f6817c5 100644 --- a/source/c13/p11_add_logging_to_simple_scripts.rst +++ b/source/c13/p11_add_logging_to_simple_scripts.rst @@ -7,8 +7,6 @@ ---------- 你希望在脚本和程序中将诊断信息写入日志文件。 -| - ---------- 解决方案 ---------- @@ -116,8 +114,6 @@ the basicConfig() call. For example: 如果你想修改配置,可以直接编辑文件logconfig.ini即可。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p12_add_logging_to_libraries.rst b/source/c13/p12_add_logging_to_libraries.rst index 498e5f4..3ae0a88 100644 --- a/source/c13/p12_add_logging_to_libraries.rst +++ b/source/c13/p12_add_logging_to_libraries.rst @@ -7,8 +7,6 @@ ---------- 你想给某个函数库增加日志功能,但是又不能影响到那些不使用日志功能的程序。 -| - ---------- 解决方案 ---------- @@ -45,8 +43,6 @@ CRITICAL:somelib:A Critical Error! >>> -| - ---------- 讨论 ---------- diff --git a/source/c13/p13_making_stopwatch_timer.rst b/source/c13/p13_making_stopwatch_timer.rst index fb09ee6..19fc606 100644 --- a/source/c13/p13_making_stopwatch_timer.rst +++ b/source/c13/p13_making_stopwatch_timer.rst @@ -7,8 +7,6 @@ ---------- 你想记录程序执行多个任务所花费的时间 -| - ---------- 解决方案 ---------- @@ -78,8 +76,6 @@ countdown(1000000) print(t2.elapsed) -| - ---------- 讨论 ---------- diff --git a/source/c13/p14_putting_limits_on_memory_and_cpu_usage.rst b/source/c13/p14_putting_limits_on_memory_and_cpu_usage.rst index 655a6ef..d2090e5 100644 --- a/source/c13/p14_putting_limits_on_memory_and_cpu_usage.rst +++ b/source/c13/p14_putting_limits_on_memory_and_cpu_usage.rst @@ -7,8 +7,6 @@ ---------- 你想对在Unix系统上面运行的程序设置内存或CPU的使用限制。 -| - ---------- 解决方案 ---------- @@ -49,8 +47,6 @@ 像这样设置了内存限制后,程序运行到没有多余内存时会抛出 ``MemoryError`` 异常。 -| - ---------- 讨论 ---------- diff --git a/source/c13/p15_luanch_a_web_browser.rst b/source/c13/p15_luanch_a_web_browser.rst index 43463be..2bc2c59 100644 --- a/source/c13/p15_luanch_a_web_browser.rst +++ b/source/c13/p15_luanch_a_web_browser.rst @@ -7,8 +7,6 @@ ---------- 你想通过脚本启动浏览器并打开指定的URL网页 -| - ---------- 解决方案 ---------- @@ -50,8 +48,6 @@ 对于支持的浏览器名称列表可查阅`Python文档 `_ -| - ---------- 讨论 ---------- diff --git a/source/c14/p01_testing_output_sent_to_stdout.rst b/source/c14/p01_testing_output_sent_to_stdout.rst index f0a7534..a4f9582 100644 --- a/source/c14/p01_testing_output_sent_to_stdout.rst +++ b/source/c14/p01_testing_output_sent_to_stdout.rst @@ -8,8 +8,6 @@ 你的程序中有个方法会输出到标准输出中(sys.stdout)。也就是说它会将文本打印到屏幕上面。 你想写个测试来证明它,给定一个输入,相应的输出能正常显示出来。 -| - ---------- 解决方案 ---------- @@ -50,8 +48,6 @@ mymodule.urlprint(protocol, host, domain) self.assertEqual(fake_out.getvalue(), expected_url) -| - ---------- 讨论 ---------- diff --git a/source/c14/p02_patching_objects_in_unit_tests.rst b/source/c14/p02_patching_objects_in_unit_tests.rst index 3b72335..1f7bfcf 100644 --- a/source/c14/p02_patching_objects_in_unit_tests.rst +++ b/source/c14/p02_patching_objects_in_unit_tests.rst @@ -8,8 +8,6 @@ 你写的单元测试中需要给指定的对象打补丁, 用来断言它们在测试中的期望行为(比如,断言被调用时的参数个数,访问指定的属性等)。 -| - ---------- 解决方案 ---------- @@ -61,8 +59,6 @@ patch('example.patch3') as mock3: ... -| - ---------- 讨论 ---------- diff --git a/source/c14/p03_testing_for_exceptional_conditions_in_unit_tests.rst b/source/c14/p03_testing_for_exceptional_conditions_in_unit_tests.rst index b2f65f8..764b99c 100644 --- a/source/c14/p03_testing_for_exceptional_conditions_in_unit_tests.rst +++ b/source/c14/p03_testing_for_exceptional_conditions_in_unit_tests.rst @@ -7,8 +7,6 @@ ---------- 你想写个测试用例来准确的判断某个异常是否被抛出。 -| - ---------- 解决方案 ---------- @@ -43,8 +41,6 @@ else: self.fail('IOError not raised') -| - ---------- 讨论 ---------- diff --git a/source/c14/p04_logging_test_output_to_file.rst b/source/c14/p04_logging_test_output_to_file.rst index 662176e..09b62f3 100644 --- a/source/c14/p04_logging_test_output_to_file.rst +++ b/source/c14/p04_logging_test_output_to_file.rst @@ -7,8 +7,6 @@ ---------- 你希望将单元测试的输出写到到某个文件中去,而不是打印到标准输出。 -| - ---------- 解决方案 ---------- @@ -40,8 +38,6 @@ with open('testing.out', 'w') as f: main(f) -| - ---------- 讨论 ---------- diff --git a/source/c14/p05_skip_or_anticipate_test_failures.rst b/source/c14/p05_skip_or_anticipate_test_failures.rst index d92e640..b0ec3ff 100644 --- a/source/c14/p05_skip_or_anticipate_test_failures.rst +++ b/source/c14/p05_skip_or_anticipate_test_failures.rst @@ -7,8 +7,6 @@ ---------- 你想在单元测试中忽略或标记某些测试会按照预期运行失败。 -| - ---------- 解决方案 ---------- @@ -59,8 +57,6 @@ OK (skipped=2, expected failures=1) -| - ---------- 讨论 ---------- diff --git a/source/c14/p06_handle_multiple_exceptions.rst b/source/c14/p06_handle_multiple_exceptions.rst index 10b2195..05abc12 100644 --- a/source/c14/p06_handle_multiple_exceptions.rst +++ b/source/c14/p06_handle_multiple_exceptions.rst @@ -7,8 +7,6 @@ ---------- 你有一个代码片段可能会抛出多个不同的异常,怎样才能不创建大量重复代码就能处理所有的可能异常呢? -| - ---------- 解决方案 ---------- @@ -53,8 +51,6 @@ ``OSError`` 是 ``FileNotFoundError`` 和 ``PermissionError`` 异常的基类。 -| - ---------- 讨论 ---------- diff --git a/source/c14/p07_catching_all_exceptions.rst b/source/c14/p07_catching_all_exceptions.rst index bf49eb9..86763c6 100644 --- a/source/c14/p07_catching_all_exceptions.rst +++ b/source/c14/p07_catching_all_exceptions.rst @@ -7,8 +7,6 @@ ---------- 怎样捕获代码中的所有异常? -| - ---------- 解决方案 ---------- @@ -25,8 +23,6 @@ 这个将会捕获除了 ``SystemExit`` 、 ``KeyboardInterrupt`` 和 ``GeneratorExit`` 之外的所有异常。 如果你还想捕获这三个异常,将 ``Exception`` 改成 ``BaseException`` 即可。 -| - ---------- 讨论 ---------- diff --git a/source/c14/p08_creating_custom_exceptions.rst b/source/c14/p08_creating_custom_exceptions.rst index dfa3378..1fd585f 100644 --- a/source/c14/p08_creating_custom_exceptions.rst +++ b/source/c14/p08_creating_custom_exceptions.rst @@ -7,8 +7,6 @@ ---------- 在你构建的应用程序中,你想将底层异常包装成自定义的异常。 -| - ---------- 解决方案 ---------- @@ -40,8 +38,6 @@ except ProtocolError as e: ... -| - ---------- 讨论 ---------- diff --git a/source/c14/p09_raise_exception_in_response_to_another_exception.rst b/source/c14/p09_raise_exception_in_response_to_another_exception.rst index c688720..8042c7e 100644 --- a/source/c14/p09_raise_exception_in_response_to_another_exception.rst +++ b/source/c14/p09_raise_exception_in_response_to_another_exception.rst @@ -7,8 +7,6 @@ ---------- 你想捕获一个异常后抛出另外一个不同的异常,同时还得在异常回溯中保留两个异常的信息。 -| - ---------- 解决方案 ---------- @@ -98,8 +96,6 @@ RuntimeError: A parsing error occurred >>> -| - ---------- 讨论 ---------- diff --git a/source/c14/p10_reraising_the_last_exception.rst b/source/c14/p10_reraising_the_last_exception.rst index 6e359fe..3bb2557 100644 --- a/source/c14/p10_reraising_the_last_exception.rst +++ b/source/c14/p10_reraising_the_last_exception.rst @@ -7,8 +7,6 @@ ---------- 你在一个 ``except`` 块中捕获了一个异常,现在想重新抛出它。 -| - ---------- 解决方案 ---------- @@ -32,8 +30,6 @@ ValueError: invalid literal for int() with base 10: 'N/A' >>> -| - ---------- 讨论 ---------- diff --git a/source/c14/p11_issuing_warning_messages.rst b/source/c14/p11_issuing_warning_messages.rst index 7d821b1..59cf848 100644 --- a/source/c14/p11_issuing_warning_messages.rst +++ b/source/c14/p11_issuing_warning_messages.rst @@ -7,8 +7,6 @@ ---------- 你希望自己的程序能生成警告信息(比如废弃特性或使用问题)。 -| - ---------- 解决方案 ---------- @@ -48,8 +46,6 @@ SyntaxWarning, RuntimeWarning, ResourceWarning, 或 FutureWarning. DeprecationWarning: logfile argument is deprecated bash % -| - ---------- 讨论 ---------- diff --git a/source/c14/p12_debugging_basic_program_crashes.rst b/source/c14/p12_debugging_basic_program_crashes.rst index 8aa9c2e..bcd6a1c 100644 --- a/source/c14/p12_debugging_basic_program_crashes.rst +++ b/source/c14/p12_debugging_basic_program_crashes.rst @@ -7,8 +7,6 @@ ---------- 你的程序奔溃后该怎样去调试它? -| - ---------- 解决方案 ---------- @@ -109,8 +107,6 @@ 当程序比较大二你想调试控制流程以及函数参数的时候这个就比较有用了。 例如,一旦调试器开始运行,你就能够使用 ``print`` 来观测变量值或敲击某个命令比如 ``w`` 来获取追踪信息。 -| - ---------- 讨论 ---------- diff --git a/source/c14/p13_profiling_and_timing_your_program.rst b/source/c14/p13_profiling_and_timing_your_program.rst index 13e6b61..54106ce 100644 --- a/source/c14/p13_profiling_and_timing_your_program.rst +++ b/source/c14/p13_profiling_and_timing_your_program.rst @@ -8,8 +8,6 @@ 你想测试你的程序运行所花费的时间并做性能测试。 -| - ---------- 解决方案 ---------- @@ -130,8 +128,6 @@ 1.0270336690009572 >>> -| - ---------- 讨论 ---------- diff --git a/source/c14/p14_make_your_program_run_faster.rst b/source/c14/p14_make_your_program_run_faster.rst index fa5a7d5..1f6afc6 100644 --- a/source/c14/p14_make_your_program_run_faster.rst +++ b/source/c14/p14_make_your_program_run_faster.rst @@ -7,8 +7,6 @@ ---------- 你的程序运行太慢,你想在不使用复杂技术比如C扩展或JIT编译器的情况下加快程序运行速度。 -| - ---------- 解决方案 ---------- @@ -206,8 +204,6 @@ 有些人并没有很好的理解或信任Python的内存模型,滥用 ``copy.deepcopy()`` 之类的函数。 通常在这些代码中是可以去掉复制操作的。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p01_access_ccode_using_ctypes.rst b/source/c15/p01_access_ccode_using_ctypes.rst index 4df392d..6b42597 100644 --- a/source/c15/p01_access_ccode_using_ctypes.rst +++ b/source/c15/p01_access_ccode_using_ctypes.rst @@ -8,8 +8,6 @@ 你有一些C函数已经被编译到共享库或DLL中。你希望可以使用纯Python代码调用这些函数, 而不用编写额外的C代码或使用第三方扩展工具。 -| - ---------- 解决方案 ---------- @@ -122,8 +120,6 @@ 4.242640687119285 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p02_write_simple_c_extension_module.rst b/source/c15/p02_write_simple_c_extension_module.rst index 64dac69..642231d 100644 --- a/source/c15/p02_write_simple_c_extension_module.rst +++ b/source/c15/p02_write_simple_c_extension_module.rst @@ -7,8 +7,6 @@ ---------- 你想不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块。 -| - ---------- 解决方案 ---------- @@ -154,8 +152,6 @@ Python的二进制分发通常使用了Microsoft Visual Studio来构建。 为了让这些扩展能正常工作,你需要使用同样或兼容的工具来编译它。 参考相应的 `Python文档 `_ -| - ---------- 讨论 ---------- diff --git a/source/c15/p03_write_extension_function_operate_on_arrays.rst b/source/c15/p03_write_extension_function_operate_on_arrays.rst index efa3d95..9a2cec1 100644 --- a/source/c15/p03_write_extension_function_operate_on_arrays.rst +++ b/source/c15/p03_write_extension_function_operate_on_arrays.rst @@ -8,8 +8,6 @@ 你想编写一个C扩展函数来操作数组,可能是被array模块或类似Numpy库所创建。 不过,你想让你的函数更加通用,而不是针对某个特定的库所生成的数组。 -| - ---------- 解决方案 ---------- @@ -89,8 +87,6 @@ 2.0 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p04_manage_opaque_pointers_in_c_extension_modules.rst b/source/c15/p04_manage_opaque_pointers_in_c_extension_modules.rst index ed4127f..cfcfa06 100644 --- a/source/c15/p04_manage_opaque_pointers_in_c_extension_modules.rst +++ b/source/c15/p04_manage_opaque_pointers_in_c_extension_modules.rst @@ -8,8 +8,6 @@ 你有一个扩展模块需要处理C结构体中的指针, 但是你又不想暴露结构体中任何内部细节给Python。 -| - ---------- 解决方案 ---------- @@ -89,8 +87,6 @@ 2.8284271247461903 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p05_define_and_export_c_api_from_extension_modules.rst b/source/c15/p05_define_and_export_c_api_from_extension_modules.rst index 26180e2..96e2d6e 100644 --- a/source/c15/p05_define_and_export_c_api_from_extension_modules.rst +++ b/source/c15/p05_define_and_export_c_api_from_extension_modules.rst @@ -9,8 +9,6 @@ 你想在其他扩展模块中使用这些函数,但是不知道怎样将它们链接起来, 并且通过C编译器/链接器来做看上去特别复杂(或者不可能做到)。 -| - ---------- 解决方案 ---------- @@ -211,8 +209,6 @@ 2.000000 3.000000 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p06_calling_python_from_c.rst b/source/c15/p06_calling_python_from_c.rst index 05d881c..aac8b08 100644 --- a/source/c15/p06_calling_python_from_c.rst +++ b/source/c15/p06_calling_python_from_c.rst @@ -8,8 +8,6 @@ 你想在C中安全的执行某个Python调用并返回结果给C。 例如,你想在C语言中使用某个Python函数作为一个回调。 -| - ---------- 解决方案 ---------- @@ -163,8 +161,6 @@ 7.0 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p07_release_the_gil_in_c_extensions.rst b/source/c15/p07_release_the_gil_in_c_extensions.rst index e3319c0..32c8bc5 100644 --- a/source/c15/p07_release_the_gil_in_c_extensions.rst +++ b/source/c15/p07_release_the_gil_in_c_extensions.rst @@ -8,8 +8,6 @@ 你想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL)。 -| - ---------- 解决方案 ---------- @@ -30,8 +28,6 @@ return result; } -| - ---------- 讨论 ---------- diff --git a/source/c15/p08_mix_threads_from_c_and_python.rst b/source/c15/p08_mix_threads_from_c_and_python.rst index 53ebb1e..b735bd7 100644 --- a/source/c15/p08_mix_threads_from_c_and_python.rst +++ b/source/c15/p08_mix_threads_from_c_and_python.rst @@ -9,8 +9,6 @@ 有些线程是在C中创建的,超出了Python解释器的控制范围。 并且一些线程还使用了Python C API中的函数。 -| - ---------- 解决方案 ---------- @@ -43,8 +41,6 @@ 每次调用 ``PyGILState_Ensure()`` 都要相应的调用 ``PyGILState_Release()`` . -| - ---------- 讨论 ---------- diff --git a/source/c15/p09_wrap_c_code_with_swig.rst b/source/c15/p09_wrap_c_code_with_swig.rst index 7fc7a80..76dd1b5 100644 --- a/source/c15/p09_wrap_c_code_with_swig.rst +++ b/source/c15/p09_wrap_c_code_with_swig.rst @@ -7,8 +7,6 @@ ---------- 你想让你写的C代码作为一个C扩展模块来访问,想通过使用 `Swig包装生成器 `_ 来完成。 -| - ---------- 解决方案 ---------- @@ -162,8 +160,6 @@ swig的输出就是两个文件,sample_wrap.c和sample.py。 2.0 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p10_wrap_existing_c_code_with_cython.rst b/source/c15/p10_wrap_existing_c_code_with_cython.rst index 307e77f..68738b1 100644 --- a/source/c15/p10_wrap_existing_c_code_with_cython.rst +++ b/source/c15/p10_wrap_existing_c_code_with_cython.rst @@ -7,8 +7,6 @@ ---------- 你想使用Cython来创建一个Python扩展模块,用来包装某个已存在的C函数库。 -| - ---------- 解决方案 ---------- @@ -160,8 +158,6 @@ 2.8284271247461903 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p11_use_cython_to_write_high_performance_array_operation.rst b/source/c15/p11_use_cython_to_write_high_performance_array_operation.rst index 8948060..dac8375 100644 --- a/source/c15/p11_use_cython_to_write_high_performance_array_operation.rst +++ b/source/c15/p11_use_cython_to_write_high_performance_array_operation.rst @@ -8,8 +8,6 @@ 你要写高性能的操作来自NumPy之类的数组计算函数。 你已经知道了Cython这样的工具会让它变得简单,但是并不确定该怎样去做。 -| - ---------- 解决方案 ---------- @@ -107,8 +105,6 @@ 正如你看到的,它要快很多——这是一个很有趣的结果,因为NumPy版本的核心代码还是用C语言写的。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p12_turning_function_pointer_into_callable.rst b/source/c15/p12_turning_function_pointer_into_callable.rst index 8f7f0fa..005d062 100644 --- a/source/c15/p12_turning_function_pointer_into_callable.rst +++ b/source/c15/p12_turning_function_pointer_into_callable.rst @@ -8,8 +8,6 @@ 你已经获得了一个被编译函数的内存地址,想将它转换成一个Python可调用对象, 这样的话你就可以将它作为一个扩展函数使用了。 -| - ---------- 解决方案 ---------- @@ -38,8 +36,6 @@ 0.0 >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p13_pass_null_terminated_string_to_c_libraries.rst b/source/c15/p13_pass_null_terminated_string_to_c_libraries.rst index 5df4d53..0a827a3 100644 --- a/source/c15/p13_pass_null_terminated_string_to_c_libraries.rst +++ b/source/c15/p13_pass_null_terminated_string_to_c_libraries.rst @@ -8,8 +8,6 @@ 你要写一个扩展模块,需要传递一个NULL结尾的字符串给C函数库。 不过,你不是很确定怎样使用Python的Unicode字符串去实现它。 -| - ---------- 解决方案 ---------- @@ -132,8 +130,6 @@ 但是它们并不检查字符串中间是否嵌入了NULL字节。 因此,如果这个很重要的话,那你需要自己去做检查了。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p14_pass_unicode_strings_to_c_libraries.rst b/source/c15/p14_pass_unicode_strings_to_c_libraries.rst index 833f7a8..a6a7075 100644 --- a/source/c15/p14_pass_unicode_strings_to_c_libraries.rst +++ b/source/c15/p14_pass_unicode_strings_to_c_libraries.rst @@ -7,8 +7,6 @@ ---------- 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode。 -| - ---------- 解决方案 ---------- @@ -85,8 +83,6 @@ 仔细观察这个面向字节的函数 ``print_chars()`` 是怎样接受UTF-8编码数据的, 以及 ``print_wchars()`` 是怎样接受Unicode编码值的 -| - ---------- 讨论 ---------- diff --git a/source/c15/p15_converting_c_string_to_python.rst b/source/c15/p15_converting_c_string_to_python.rst index 9160280..b776ffc 100644 --- a/source/c15/p15_converting_c_string_to_python.rst +++ b/source/c15/p15_converting_c_string_to_python.rst @@ -7,8 +7,6 @@ ---------- 怎样将C中的字符串转换为Python字节或一个字符串对象? -| - ---------- 解决方案 ---------- @@ -58,8 +56,6 @@ C字符串使用一对 ``char *`` 和 ``int`` 来表示, 对于宽字符串,并没有对字符数据进行解析——它被假定是原始Unicode编码指针,可以被直接转换成Python。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p16_work_with_c_strings_of_dubious_encoding.rst b/source/c15/p16_work_with_c_strings_of_dubious_encoding.rst index fc5b63f..1a7d591 100644 --- a/source/c15/p16_work_with_c_strings_of_dubious_encoding.rst +++ b/source/c15/p16_work_with_c_strings_of_dubious_encoding.rst @@ -9,8 +9,6 @@ 例如,可能C中的数据期望是UTF-8,但是并没有强制它必须是。 你想编写代码来以一种优雅的方式处理这些不合格数据,这样就不会让Python奔溃或者破坏进程中的字符串数据。 -| - ---------- 解决方案 ---------- @@ -82,8 +80,6 @@ 仔细观察结果你会发现,不合格字符串被编码到一个Python字符串中,并且并没有产生错误, 并且当它被回传给C的时候,被转换为和之前原始C字符串一样的字节。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p17_pass_filenames_to_c_extensions.rst b/source/c15/p17_pass_filenames_to_c_extensions.rst index 1ebab66..0a098eb 100644 --- a/source/c15/p17_pass_filenames_to_c_extensions.rst +++ b/source/c15/p17_pass_filenames_to_c_extensions.rst @@ -7,8 +7,6 @@ ---------- 你需要向C库函数传递文件名,但是需要确保文件名根据系统期望的文件名编码方式编码过。 -| - ---------- 解决方案 ---------- @@ -58,8 +56,6 @@ PyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len); -| - ---------- 讨论 ---------- diff --git a/source/c15/p18_pass_open_files_to_c_extensions.rst b/source/c15/p18_pass_open_files_to_c_extensions.rst index b72abfc..1fcff6e 100644 --- a/source/c15/p18_pass_open_files_to_c_extensions.rst +++ b/source/c15/p18_pass_open_files_to_c_extensions.rst @@ -7,8 +7,6 @@ ---------- 你在Python中有一个打开的文件对象,但是需要将它传给要使用这个文件的C扩展。 -| - ---------- 解决方案 ---------- @@ -36,8 +34,6 @@ ``PyFile_FromFd()`` 的参数对应内置的 ``open()`` 函数。 NULL表示编码、错误和换行参数使用默认值。 -| - ---------- 讨论 ---------- diff --git a/source/c15/p19_read_file_like_objects_from_c.rst b/source/c15/p19_read_file_like_objects_from_c.rst index c3f9fa5..d99e737 100644 --- a/source/c15/p19_read_file_like_objects_from_c.rst +++ b/source/c15/p19_read_file_like_objects_from_c.rst @@ -7,8 +7,6 @@ ---------- 你要写C扩展来读取来自任何Python类文件对象中的数据(比如普通文件、StringIO对象等)。 -| - ---------- 解决方案 ---------- @@ -92,8 +90,6 @@ World >>> -| - ---------- 讨论 ---------- diff --git a/source/c15/p20_consuming_an_iterable_from_c.rst b/source/c15/p20_consuming_an_iterable_from_c.rst index 12fcde1..628bb25 100644 --- a/source/c15/p20_consuming_an_iterable_from_c.rst +++ b/source/c15/p20_consuming_an_iterable_from_c.rst @@ -7,8 +7,6 @@ ---------- 你想写C扩展代码处理来自任何可迭代对象如列表、元组、文件或生成器中的元素。 -| - ---------- 解决方案 ---------- @@ -37,8 +35,6 @@ return Py_BuildValue(""); } -| - ---------- 讨论 ---------- diff --git a/source/c15/p21_diagnosing_segmentation_faults.rst b/source/c15/p21_diagnosing_segmentation_faults.rst index 6f80c3f..9b2697e 100644 --- a/source/c15/p21_diagnosing_segmentation_faults.rst +++ b/source/c15/p21_diagnosing_segmentation_faults.rst @@ -8,8 +8,6 @@ 解释器因为某个分段错误、总线错误、访问越界或其他致命错误而突然间奔溃。 你想获得Python堆栈信息,从而找出在发生错误的时候你的程序运行点。 -| - ---------- 解决方案 ---------- @@ -43,8 +41,6 @@ 尽管这个并不能告诉你C代码中哪里出错了,但是至少能告诉你Python里面哪里有错。 -| - ---------- 讨论 ----------