每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,欢迎关注开源日报。交流QQ群:202790710;电报群 https://t.me/OpeningSourceOrg


今日推荐开源项目:《Wtfpython——更有趣的python教程

推荐理由:Python 教程千千万万,试试这个更有趣的?

2018年3月29日:开源日报第21期

Wtfpython讲解了大量的Python编译器的内容。其实它们不一定很实用,只要程序员按照规范的语法输入基本上是不会碰到其中的“BUG“的,不过其中有一部分内容是真的很有趣或者可能有用,这里将其中几个较为有用或有趣的摘录下来,看看能否激起大家前去一阅的兴趣。

Tips:

1.python中对“-”号的定义:Pyhon的自由度真的很高啊,不过这个自由度是建立在定义严格的基础上的。-”号是“+”号的反向,具体一点,就是这样:

开源项目精选:Wtfpython——更有趣的python教程

 

2.多线程处理:多个Python线程不会同时运行你的Python代码。多线程看起来很直观,可以派生出几个线程,让他们同时执行你的Python代码,但是由于Python存在全局解释器锁(Global Interpreter Lock),你所做的只是让你的线程在相同的内核上轮流执行。Python线程适用于IO绑定任务,但为了实现CPU绑定任务的Python实际并行化,您可能需要使用Python 多处理模块(multiprocessing module)。

有关全局解释器锁的内容可以查看官方文档:

https://wiki.python.org/moin/GlobalInterpreterLock

 

3.关于count函数:

‘abc’.count(”) == 4.

Count函数用于统计字符串中某个子串的出现次数,以下代码模拟了该函数的执行过程,或许可以解释这个问题:

def count(s, sub):

result = 0

for i in range(len(s) + 1 – len(sub)):

result += (s[i:i + len(sub)] == sub)

return result

 

4.两个特殊字符串:

nan与inf意义分别是“不存在”与“无穷大”,这两个概念在被强制转换为float类型后生效。即:

a=float(‘inf’) 正无穷大

b=float(‘-inf’)负无穷大

c=float(‘nan’)不存在的数

这样的话会有:

>>>a+3

inf

>>>b+3

-inf

>>>c+3

nan

Ps:无限的哈希值是10 5 xπ

 

5.+=的优先级要比+高,除非你把+号连接的内容打上括号。

 

6.以Python为舟,看看Python的哲学:

>>>import this

>>> love = this

>>> this is love

True

>>> love is True

False

>>> love is False

False

>>> love is not True or False

True

>>> love is not True or False; love is love  # Love is complicated(复杂的)

True

算一个梗吧,哈哈。

这是this模块的内容https://hg.python.org/cpython/file/c3896275c0f6/Lib/this.py

 

7.global的使用:

在函数外定义的变量,可以被函数引用,但是如果函数内有同名变量的赋值(不论位置),引用都会触发错误。

例:

a = 1

def some_func():

return a

 

def another_func():

a += 1

return a

运行:

>>> some_func()

1

>>> another_func()

UnboundLocalError: local variable ‘a’ referenced before assignment

除非改成这样:

def another_func()

global a

a += 1

return a

 

8、关于哈希:

some_dict = {}

some_dict[5.5] = “Ruby”

some_dict[5.0] = “JavaScript”

some_dict[5] = “Python”

运行:

>>> some_dict[5.5]

“Ruby”

>>> some_dict[5.0]

“Python”

>>> some_dict[5]

“Python”

 

由于5.0的哈希值与5相同,因此JavaScript被Python覆盖了(emmmm,满满的恶意)

测试两键哈希是否相同用hash(a)==hash(b)即可。

由于存取只以哈希值为标准,因此字典也被称作哈希表。

 

9.当并列的语句都含有return时,以最后一句的内容为准。

def some_func():

try:

return ‘from_try’

finally:

return ‘from_finally’

运行:

>>> some_func()

‘from_finally’

 

更多内容:

https://github.com/satwikkansal/wtfpython


今日推荐英文原文:《My first open source project and Google Code-in》原文作者:Manvendra Singh

原文链接:https://opensource.googleblog.com/2018/03/my-first-open-source-project-and-google.html

 

My first open source project and Google Code-in

About two years ago, my friend Gyan and I built a small web app which checked whether or not a given username was available on a few popular social media websites. The idea was simple: judge availability of the username on the basis of an HTTP response. Here’s a pseudo-code example:

2018年3月29日:开源日报第21期

Much to our delight, it worked! Well, almost. It had a lot of bugs but we didn’t care much at the time. It was my first Python project and the first time I open sourced my work. I always look back on it as a cool idea, proud that I made it and learned a lot in the process.

But the project had been abandoned until John from coala approached me. John suggested we use it for Google Code-in because one of coala’s tasks for the students was to create accounts on a few common coding related websites. Students could use the username availability tool to find a good single username–people like their usernames to be consistent across websites–and coala could use it to verify that the accounts were created.

I had submitted a few patches to coala in the past, so this sounded good to me! The competition clashed with my vacation plans, but I wanted to get involved, so I took the opportunity to become a mentor.

Over the course of the program, students not only used the username availability tool but they also began making major improvements. We took the cue and began adding tasks specifically about the tool. Here are just a few of the things students added:

  • Regex to determine whether a given username was valid for any given website
  • More websites, bringing it to a total of 13
  • Tests (!)

The web app is online so you can check username availability too!

I had such a fun time working with students in Google Code-in, their enthusiasm and energy was amazing. Special thanks to students Andrew, Nalin, Joshua, and biscuitsnake for all the time and effort you put into the project. You did really useful work and I hope you learned from the experience!

I want to thank John for approaching me in the first place and suggesting we use and improve the project. He was an unstoppable force throughout the competition, helping both students and fellow mentors. John even helped me with code reviews to really refine the work students submitted, and help them improve based on the feedback.

Kudos to the Google Open Source team for organizing it so well and lowering the barriers of entry to open source for high school students around the world.


每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,欢迎关注开源日报。交流QQ群:202790710;电报群 https://t.me/OpeningSourceOrg