Skip to content

Commit fc999ee

Browse files
committed
update
2 parents 34f34e6 + dbd2cd1 commit fc999ee

4 files changed

Lines changed: 256 additions & 1 deletion
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
原文:[Python Weekly Issue 254](http://us2.campaign-archive2.com/?u=e2e180baf855ac797ef407fc7&id=289a703264&e=148158c7b4)
2+
3+
---
4+
5+
欢迎来到Python Weekly第254期。如果你正在找工作,那么一定要看一看我们的赞助商,Hired,提供的一个超棒的平台。
6+
7+
8+
# 来自赞助商
9+
10+
[![](https://gallery.mailchimp.com/e2e180baf855ac797ef407fc7/images/7394541b-6b55-4fde-8756-6b7547029f1b.png)](https://hired.com/?utm_source=newsletters&utm_medium=pythonweekly&utm_campaign=q3-16)
11+
12+
你处在技术最前沿,但是找工作怎么还像90年代那样呢?[今天,就试试Hired吧](https://hired.com/?utm_source=newsletters&utm_medium=pythonweekly&utm_campaign=q3-16),让4,000+家顶级公司争夺面试你的机会。和录用一起的,有预付薪资和股票,并且我们的职业教练会一直等着你回来(这里没有太过头的招聘人员!)
13+
14+
15+
# 文章,教程和讲座
16+
17+
[Episode #69:写一手优秀的编程博客](https://talkpython.fm/episodes/show/69/write-an-excellent-programming-blog)
18+
19+
你有博客吗?在上面,你写了多少篇文章呢?你有没有发现一直写作,或者开始进行一些技术写作很难呢?本周,我们或许可以帮助你解决这些问题。
20+
21+
[如何扩展Django User Model](http://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html) | [中文版](../Django/如何扩展Django User模型.md)
22+
23+
本教程介绍了一些策略,你可以用它们来简单的扩展默认的Django User Model,这样,你就无需一切从头开始。
24+
25+
[我是如何构建一个Slack机器人来帮助我在San Francisco找房子的](https://www.dataquest.io/blog/apartment-finding-slackbot/)
26+
27+
[机器学习很好玩!第四部分:使用深度学习进行面部识别](https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78)
28+
29+
[在你的笔记本电脑上分析和可视化大数据交互:Datashading 2010年美国人口普查](https://www.continuum.io/blog/developer-blog/analyzing-visualizing-big-data-interactively-laptop-datashading)
30+
31+
[愚蠢的Python技巧:滥用显式self](https://medium.com/@hwayne/stupid-python-tricks-abusing-explicit-self-53d46b72e9e0)
32+
33+
[Django Channels和Celery示例](http://vincenttide.com/blog/1/django-channels-and-celery-example/) [中文版](../Django/Django Channels和Celery示例.md)
34+
35+
[为Numpy编写类型桩](http://www.machinalis.com/blog/writing-type-stubs-for-numpy/)
36+
37+
[使用Ptrace提取Python堆栈](https://eklitzke.org/extracting-the-python-stack-using-ptrace)
38+
39+
40+
# 本周Python工作
41+
42+
[Phrasee招聘棒棒哒的Python开发者](http://jobs.pythonweekly.com/jobs/awesome-python-developer/)
43+
44+
你是一个棒棒哒的开发者,能让事情完成,并且干得漂亮。你将负责所有的事情,从具体说明到编码(我们将提供红牛,如果它有用的话),以及测试和部署。当然,还有错误修复(总会有些bug的,是吧?!?)。
45+
46+
[Kwalee招聘游戏后端开发者](http://jobs.pythonweekly.com/jobs/game-backend-developer/)
47+
48+
作为我们的资深游戏服务器开发者,我们需要你提供高质量、新颖且非常有趣的手机游戏。你将帮助领导我们的服务器的设计和架构,实现服务器特性以达到健壮的支持数百万的手机游戏玩家。你创造的系统要出色且可靠,同时又是安全可扩展的。你的代码需要具备最高质量。
49+
50+
[The Texas Tribune招聘软件工程师](http://jobs.pythonweekly.com/jobs/software-engineer-15/)
51+
52+
你将成为不断提高我们新闻编辑室系统(包括一个Django CMS及其与外部API的交互)和原型新工具的团队的一员,我们使用现代的工作流(包括Docker, HTTPS, 和Grunt),而你将总是要接受新的挑战。你典型的一天的工作可能包括改造一个Django app,对一个MailChimp API导入进行故障排除,帮助记者处理CMS问题,或者为网站计划一个新特性。
53+
54+
[Canopy Innovations招聘全栈开发者](http://jobs.pythonweekly.com/jobs/full-stack-developer-4/)
55+
56+
我们正在寻找具有可靠Python及/或者Rails经验的全栈开发者,加入我们小而关系密切的团队,帮助我们构建有助于医疗保健机构跨越语言障碍沟通,提升非英语系病人的照顾质量的下一代工具。
57+
58+
59+
# 好玩的项目,工具和库
60+
61+
[Kickstarter推出Python for Entrepreneurs](https://www.kickstarter.com/projects/mikeckennedy/python-for-entrepreneurs-video-course)
62+
63+
许多教程会教你参与构建web应用的技术。很少有课程会教你实际上怎么将该产品上线,使之实际可用。这里,Python for Entrepreneurs会教你如何构建自己的网站,以及如何将其上线。
64+
65+
[DeepHeart](https://github.com/jisaacso/DeepHeart)
66+
67+
DeepHeart是一个为2016年Physionet挑战赛(根据心电图(PCG)数据预测心脏异常)设计的神经网络。该挑战提供来自一些病人的心脏记录,这些记录标记了正常和异常。
68+
69+
[choronzon](https://github.com/CENSUS/choronzon)
70+
71+
一个发展中的基于知识的模糊器。
72+
73+
[Pymunk](http://www.pymunk.org/en/latest/)
74+
75+
Pymunk是一个易于使用的pythonic的2D物理库,无论何时你需要用python处理2D刚体物理,都可以使用它。它可以完美的让你在你的游戏、演示或者其他应用中使用2D物理!它建立在非常强大的2D物理库Chipmunk之上。
76+
77+
[Coffer](https://github.com/Max00355/Coffer)
78+
79+
用于创建隔离及便携的开发环境的一个轻量平台。
80+
81+
[tomviz](https://github.com/OpenChemistry/tomviz)
82+
83+
用于3D断层扫描数据进行处理、可视化和分析的跨平台的开源应用。
84+
85+
[django-herald](https://github.com/worthwhile/django-herald)
86+
87+
Django消息库,用于从传输方法中分离内容。
88+
89+
[APKiD](https://github.com/rednaga/APKiD)
90+
91+
APKiD为你提供APK如何制作的信息。它可以识别许多编译器,包装器,混淆器和其他奇奇怪怪的东西。它是Android系的PEiD。
92+
93+
[words2map](https://github.com/overlap-ai/words2map)
94+
95+
使用字向量的在线自然语言处理
96+
97+
[Snapper](https://github.com/dxa4481/Snapper)
98+
99+
一个安全工具,用于抓取许多网络主机的截图。这个工具在DNS枚举或通过nmap / nessus枚举网络主机后可以发挥作用。
100+
101+
102+
# 最新发布
103+
104+
[IronPython 2.7.6 RC1](https://github.com/IronLanguages/main/releases/tag/ipy-2.7.6-rc1)
105+
106+
该版本的大部分工作是修复了一些小的长期存在的问题,新增一个基于C#的名为ipyc的IronPython“编译器”(它在pyc.py之上添加了一些新特性),来自@jdhardy的一个主要的内存泄漏修复(该修复应该会解决过去发现的一些内存泄漏问题),以及将Silverlight从包中去除。[IronPython 2.7.6 RC2](https://github.com/IronLanguages/main/releases/tag/ipy-2.7.6-rc2)是RC1的一个小更新,它正确添加了IronPython“编译器”到zip和msi。
107+
108+
109+
# 近期活动和网络研讨会
110+
111+
[DC的2016年8月Python聚会 - Washington, DC](http://www.meetup.com/DCPython/events/231925808/)
112+
113+
在这次谈话中,我们将快速浏览一下Blaze库,看看它能做什么不能做什么,以及它如何让你的生活更轻松一点。

Python Weekly/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
- [Issue 250](./Python Weekly Issue 250.md)
1313
- [Issue 251](./Python Weekly Issue 251.md)
1414
- [Issue 252](./Python Weekly Issue 252.md)
15-
- [Issue 253](./Python Weekly Issue 253.md)
15+
- [Issue 253](./Python Weekly Issue 253.md)
16+
- [Issue 254](./Python Weekly Issue 254.md)

raw/How I built a Slack bot to help me find an apartment in San Francisco.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
原文:[How I built a Slack bot to help me find an apartment in San Francisco](https://www.dataquest.io/blog/apartment-finding-slackbot/)
23

34
---
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
原文:[Stupid Python Tricks: Abusing Explicit Self](https://medium.com/@hwayne/stupid-python-tricks-abusing-explicit-self-53d46b72e9e0)
2+
3+
---
4+
5+
In Ruby, you define a method like this:
6+
7+
```ruby
8+
class Foo
9+
def bar(baz)
10+
baz
11+
end
12+
end
13+
```
14+
15+
In Python, you define it like this:
16+
17+
```python
18+
class Foo:
19+
def bar(self, baz): # What's self doing there?
20+
return baz # Not even using it
21+
```
22+
That “explicit self” is the subject of many, many, many discussions, and tons of people find it really confusing. I want to provide a brief explanation, as well as find some ways to abuse that explicit self that you should never, ever do. Ready? Let’s go.
23+
24+
# Instance Methods Are Weird
25+
26+
Let’s define the following class:
27+
28+
```python
29+
class Number:
30+
def __init__(self, x: int) -> None:
31+
self.x = x
32+
self.minus = lambda y: self.x - y # we'll come back to this
33+
def plus(self, y: int) -> int:
34+
return self.x + y
35+
one = Number(1)
36+
```
37+
38+
What’s one.plus(2)? That’s pretty easy:
39+
40+
```python
41+
>>> print(one.plus(2))
42+
3
43+
```
44+
45+
But what if we do Number.plus(one, 2)?
46+
47+
```python
48+
>>> print(Number.plus(one, 2)) # ???
49+
3 # !!!
50+
```
51+
52+
Here, plus is a “bound method”. The object doesn’t *furious air quotes* really have a method called ‘plus’. Instead, “one.plus(N)” is a shorthand for “Number.plus(one, N)”, the “unbound method”. With that in mind, it’s pretty obvious why self is needed: plus is a class method that takes two parameters: the object we’re using, and the number we’re adding.
53+
54+
It’s a little more complicated than that (Python 3 simplifies things a bit), but that’s a pretty useful lie that makes it a lot easier to work out the logic here. Let’s provide a slightly more complex example:
55+
56+
```python
57+
>>> Number.times = lambda self, y: self.x * y
58+
>>> print(one.times(2)) # automatically valid
59+
2
60+
```
61+
62+
When we call “one.times(2)”, it automatically translates that to “Number.times(one, 2)”, which we just defined. This becomes a lot easier to reason about with the explicit self.
63+
64+
Now let’s double back to that “self.minus” in the initializer. There, we’re not defining the method “properly”. It’s not part of the class definition. We’re just assigning a property to the object which also happens to be callable. Since this isn’t a proper method, it shouldn’t be translated to a bound method in the actual object, so we can drop the self. Similarly, there shouldn’t be an unbound version on the class, so trying to use that will be an error. Let’s give that a test:
65+
66+
```python
67+
>>> print(one.minus(2))
68+
-1
69+
>>> print(Number.minus(one, 2))
70+
Traceback (most recent call last):
71+
File "<stdin>", line 1, in <module>
72+
AttributeError: type object 'Number' has no attribute 'minus'
73+
```
74+
75+
Boom.
76+
77+
# Callback Hell
78+
79+
Quick aside before the really stupid stuff. Recall that in Python, functions are first class objects. That means that you can pass functions to other functions like any other kind of object. This means you can do the following:
80+
81+
```python
82+
>>> def subtract_two(minus):
83+
... return(minus(2))
84+
...
85+
>>> print(subtract_two(one.minus))
86+
-1
87+
```
88+
89+
The function we pass in is the bound method of our object. This means that when we call it, it has access to the original object. We can use this for callbacks. Instead of giving a function complete access to our object, we give it only minimal access: just one method. Sometimes this is a good idea. Other times, it means you have to rethink your architecture. If you don’t see why, spend a few weeks writing Javascript front-ends and get back to me.
90+
91+
(Aside on the aside: you should probably check out Javascript anyway, because object prototyping is really close to what we’re doing with unbound methods and JS uses prototyping much more openly and explicitly.)
92+
93+
# The Really Stupid Stuff
94+
95+
Okay, back on track. We’ve seen that if you create a new unbound method, it adds a bound method to all existing objects. It also works with modification: change the unbound method and you change all the bound methods. This means you can dynamically adjust a class based on the runtime. For example, if you’re getting a ton of calls to a particular method, you can start caching it.
96+
97+
Here’s a toy example. We want to add a division method to Number. We want to start logging all divisions if and only if at some point we try to divide by zero. We can do it like this:
98+
99+
```python
100+
def _divide(self, y):
101+
try:
102+
return self.x / y
103+
except ZeroDivisionError:
104+
print("Tried to divide {} by 0".format(self.x))
105+
def __newdivide(self, y):
106+
print("{} / {}".format(self.x, y))
107+
return _divide(self, y)
108+
self.__class__.divide = __newdivide
109+
Number.divide = _divide
110+
>>> six = Number(6)
111+
>>> eight = Number(8)
112+
>>>
113+
>>> print(six.divide(3))
114+
2.0
115+
>>> print(eight.divide(0))
116+
Tried to divide 8 by 0
117+
None
118+
>>> print(six.divide(3))
119+
6 / 3
120+
2.0
121+
```
122+
123+
One object throwing an error triggered improved logging on a completely different one. There’s a few extremely specific cases where manipulating unbound methods might be a good idea. Practically, though, don’t do this in production code. Programming is hard enough as it is. You don’t need your method definitions changing underneath you.
124+
125+
# Summary
126+
127+
When you define an instance method on a class, it creates a corresponding “bound” method on the object. This aliases to the unbound method on the class.
128+
If you define an instance method directly on the object, there’s no corresponding unbound method on the class.
129+
Modifying the unbound method will also change all existing bound methods.
130+
You can use this to dynamically modify classes.
131+
Don’t.
132+
No really, don’t.
133+
134+
# Further Reading
135+
136+
* [Python method objects](https://docs.python.org/3/tutorial/classes.html#method-objects)
137+
* [Prototyping in Javascript](http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/)
138+
* [Static, class, and abstract methods](https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods) (also has an important note on how Py2 is a little more complicated)
139+
* [Callback hell](http://callbackhell.com/)
140+
* [A rebuttal to callback hell](http://thecodebarbarian.com/2015/03/20/callback-hell-is-a-myth)

0 commit comments

Comments
 (0)