每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,歡迎關注開源日報。交流QQ群:202790710;電報群 https://t.me/OpeningSourceOrg


今日推薦開源項目:《Wtfpython——更有趣的python教程

推薦理由:Python 教程千千萬萬,試試這個更有趣的?

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:

website_url = form_website_url(website, username)
# Eg: form_website_url('github', 'manu-chroma') returns 'github.com/manu-chroma'

if website_url_response.http_code == 404:
  username available
else:
  username taken

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