Skip to content

Commit 3aeb9b1

Browse files
author
yidao620c
committed
10.11小节待续...
1 parent cf980d4 commit 3aeb9b1

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

source/c10/p11_load_modules_from_remote_machine_by_hooks.rst

+14-19
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@
55
----------
66
问题
77
----------
8-
You would like to customize Python’s import statement so that it can transparently load
9-
modules from a remote machine.
8+
你想自定义Python的import语句,使得它能从远程机器上面透明的加载模块。
109

1110
----------
1211
解决方案
1312
----------
14-
First, a serious disclaimer about security. The idea discussed in this recipe would be
15-
wholly bad without some kind of extra security and authentication layer. That said, the
16-
main goal is actually to take a deep dive into the inner workings of Python’s import
17-
statement. If you get this recipe to work and understand the inner workings, you’ll have
18-
a solid foundation of customizing import for almost any other purpose. With that out
19-
of the way, let’s carry on.
13+
首先要提出来的是安全问题。本届讨论的思想如果没有一些额外的安全和认知机制的话会很糟糕。
14+
也就是说,我们的主要目的是深入分析Python的import语句机制。
15+
如果你理解了本节内部原理,你就能够为其他任何目的而自定义import。
16+
有了这些,让我们继续向前走。
2017

18+
本节核心是设计导入语句的扩展功能。有很多种方法可以做这个,
19+
不过为了演示的方便,我们开始先构造下面这个Python代码结构:
2120

22-
At the core of this recipe is a desire to extend the functionality of the import statement.
23-
There are several approaches for doing this, but for the purposes of illustration, start by
24-
making the following directory of Python code:
25-
26-
.. code-block:: python
21+
::
2722

2823
testcode/
2924
spam.py
@@ -32,8 +27,8 @@ making the following directory of Python code:
3227
__init__.py
3328
blah.py
3429

35-
The content of these files doesn’t matter, but put a few simple statements and functions
36-
in each file so you can test them and see output when they’re imported. For example:
30+
这些文件的内容并不重要,不过我们在每个文件中放入了少量的简单语句和函数,
31+
这样你可以测试它们并查看当它们被导入时的输出。例如:
3732

3833
.. code-block:: python
3934
@@ -58,11 +53,10 @@ in each file so you can test them and see output when they’re imported. For ex
5853
# grok/blah.py
5954
print("I'm grok.blah")
6055
61-
The goal here is to allow remote access to these files as modules. Perhaps the easiest way
62-
to do this is to publish them on a web server. Simply go to the testcode directory and
63-
run Python like this:
56+
这里的目的是允许这些文件作为模块被远程访问。
57+
也许最简单的方式就是将它们发布到一个web服务器上面。在testcode目录中像下面这样运行Python:
6458

65-
.. code-block:: python
59+
::
6660

6761
bash % cd testcode
6862
bash % python3 -m http.server 15000
@@ -71,6 +65,7 @@ run Python like this:
7165
Leave that server running and start up a separate Python interpreter. Make sure you can
7266
access the remote files using urllib. For example:
7367

68+
7469
.. code-block:: python
7570
7671
>>> from urllib.request import urlopen

source/c11/p13_sending_receiving_large_arrays.rst

-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@
7979
不过,这样最终会创建数据的一个复制。
8080
就算你只是零碎的做这些,你的代码最终还是会有大量的小型复制操作。
8181

82-
This recipe gets around this by playing a sneaky trick with memoryviews. Essentially, a
83-
memoryview is an overlay of an existing array. Not only that, memoryviews can be cast
84-
to different types to allow interpretation of the data in a different manner. This is the
85-
purpose of the following statement:
8682
本节通过使用内存视图展示了一些魔法操作。
8783
本质上,一个内存视图就是一个已存在数组的覆盖层。不仅仅是那样,
8884
内存视图还能以不同的方式转换成不同类型来表现数据。

0 commit comments

Comments
 (0)