I will keep this short. I was reviewing code for Hex Filter (To filter out non-character ASCII values)
code:
HEX_FILTER = ''.join([(len(repr(chr(i))) == 3) and chr(i) or '.' for i in range(256)])
Output:
................................ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[.]^_`abcdefghijklmnopqrstuvwxyz{|}~..................................¡¢£¤¥¦§¨©ª«¬.®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
I was confused on what's going on, so I wrote my approach to get above output. My code is as follows:
HEX_FILTER = ''.join(['.' if (len(repr(chr(i))) != 3) else chr(i) for i in range(256)])
The idea is repr of non-alphabet will be more than 3. If it's not 3 use '.' else use the character.
I cannot wrap my mind around on as to, How len(repr(chr(i))) == 3) and chr(i) or '.' equates to '.' if (len(repr(chr(i))) != 3) else chr(i)
andandoris used instead ofifandelse. That got me wondering, which one of them is efficient in terms of time complexity. And is there a way to useandandorin complex if-else branching, Even more on what's happening behind the sceneandandoroperators work for any object that supports being evaluated in a boolean context, so there are many more possible combinations to test.