Alike

“Alike” by Daniel Martínez Lara & Rafa Cano Méndez

Python sort by multiple keys

Say we have

L = ["a", "b", "zz", "zzz", "aa"]

We want to sort by length first, then by alphabetical order. i.e. we want to obtain [‘a’, ‘b’, ‘aa’, ‘zz’, ‘zzz’] in the end. How to do that?

The usual sorted() will give us this.

>>> sorted(L)
['a', 'aa', 'b', 'zz', 'zzz']

Very much alphabetical, not what we want.

Try sorted() with key=len will give us this.

>>> sorted(L, key=len)
['a', 'b', 'zz', 'aa', 'zzz']

The string length is taken care of, but that’s just half of it.

Notice zz appears before aa, that’s because zz is before aa in the original input list. The compare function len thinks they are equal.

Turns out we can expand the compare function a little to use a lambda function, so that we can pack in a little more custom logic into the comparison operation.

>>> sorted(L, key=lambda x: len(x))
['a', 'b', 'zz', 'aa', 'zzz']

This gives us the same result. But with a lambda function, we can do more than just giving a built-in or predefined function.

Since we want to sort by length first, then by alphabetical order, how should we look at a string? What is “aa” compared to “zzz”? We can look at “aa” as (2, “aa”), which is a tuple with int 2 and the string “aa”. For “zzz” that’s (3, “zzz”).

So now, we can do our comparison using tuples.

>>> sorted(L, key=lambda x: (len(x), x))
['a', 'b', 'aa', 'zz', 'zzz']

Now the resulting order seems right. To sort in descending order for a particular field, just put a negative sign there to reverse the order.

The first 20 hours — how to learn anything | Josh Kaufman | TEDxCSU

Just put in 20 hours, no need to set aside 10,000 hours.

Python global interpreter lock

This article explains the python GIL pretty well.

https://www.obytes.com/blog/2018/explaining-python-interpreter-lock/

In Python 2, a thread can at most perform 100 instructions/ticks before releasing the GIL. This setting can be changed with sys.setcheckinterval(). Then the OS will decide which thread will pick up the GIL next, possibly the same thread again based on the criteria the OS uses. The problem here is that the low-priority threads may keep waiting forever.

In Python 3, a thread can at most run for 5ms before releasing the GIL. Then with some system-level signals the same thread won’t pick up the GIL again immediately after. Sounds more fair. However since an I/O call will lead to voluntarily releasing the GIL, a thread with many I/O calls will keep losing the GIL to other CPU bound threads each time by 5ms (at most, say there is a long-running thread). So worst case scenario a thread with 1000 I/O calls will have an overhead of 1000 * 5ms = 5 sec. That’s quite something since you can save that 5 sec by running a non-threaded version. Ironic it is.

The root of the problem here is the GIL, or the fact that CPython is single-threaded for its memory management. Jython, IronPython, etc took away the GIL using transactional memory, but they don’t support C extensions.

Hopefully someday we will have CPython without the GIL.

Airflow Web Authentication

My airflow version is v.1.10.1 and I am using python 3.5.2

I tried user._set_password but after looking at the users db table the password field is null…

After looking at the source, the following works for me.

from flask_bcrypt import generate_password_hash
user._password = generate_password_hash('your_password', 12)

Be sure to use SSL.

Ref:
https://airflow.apache.org/security.html
https://stackoverflow.com/questions/48075826/airflow-authentication-setups-fails-with-attributeerror-cant-set-attribute/48946256

你只是公司的草,卻是家庭的天

你只是公司的草,卻是家庭的天,在公司,你真的不重要!

不管你在什麼職位,你只是單位的草!

你離開了公司,太陽照樣升起,你並沒有想像中的那麼重要,即使你是幹部或主管,莫說有幾個人對你是出於真心,一旦退休凋零,又有幾人會把你記牢?人走茶涼是公司的常態,別以為自己是公司不可缺少的棟樑柱!

在家裡,你真的很重要!不管你在哪裡,離開了你,家就不能稱其為家,孩子就沒有了爸媽。

你千萬不要以為為了養家就可以離家,家之所以為家,那是抱團取暖的地方,那是陽光普照的小世界,那是孩子溫暖舒適的小港灣。

人生短暫,如果想為你的愛人留下美好的回憶,讓長大的孩子留下美麗的念想,你真的很重要。

有時候,生命可以很堅強,也很多時候,生命卻真的很脆弱。因為,人生無常!請珍惜與家人在一起的今天,今天之所以是今天,那是因為是今天。

透支身體換得所謂的成功和名利,會瞬間一掃而光。最終還要再用錢財去修復我們的身體,給家庭增添了負擔,最終帶給親人感傷!

倘若你因任何原因離世,那麼,對於你的家和家人,天就真的塌下來了!那是不可彌補的痛,那是撕心裂肺的傷。

過熱地在意功成名就,卻冷淡了家庭的溫暖,忽視了對孩子的教育,輕視了對老人的盡孝,這不是你所愛的人真正願望,孩子的不成器才是你一輩子重大的創傷!

在公司裡,今天再大的事,明天就是小事;今年再大的事,明年就是故事;在任再大的事,離任只是傳說!

當你退休之後,你才知道什麼才是真正的根!對於我們每一個人,適度工作,保重身體,多陪家人,教育孩子才是最重​​要的!

少在外面應酬,多對老人照顧;少些酒肉朋友,多與家人交流;少些花花腸子,多點相夫(婦)教子,一個人只有盡到本分才有福分。

所以,請記住:你,只有在家裡是無人可以取代的!對於你的家人,你才是最重要的!家人苛求的並不是榮華富貴,而是需要有你的關心和陪伴。

Rolling Knee Pads

Such a good idea.