# 0220 about byte string # ---------------------------------------------------- def test_0220(): # byte string은 대부분의 str 함수가 적용된다 # - 대신 주어진 타입은 byte string 여야 함 : regex도 마찬가지 b = b'Hello World' print(b[0:5]) print(b.startswith(b'Hello')) # print(b.startswith('Hello')) # type error! print(b.split()) print(b.replace(b'Hello', b'Yello')) print(re.split(b'[:,]', b'FOO:BAR,SPAM')) # - bytearray로 만들어도 동일하게 적용됨 ba = bytearray(b) print(ba[0:5]) # 나머지 함수도 결과 같음 # byte string 만의 특징들 # - 단위요소는 숫자로 나옴옴 # - format 함수 없음 def test_022001(): # byte string to string b = b'Hello World' s = str(b, 'utf-8') print(b, s) if __name__ == '__main__': call_funcs(vars(), lambda s: s.startswith('test_'))
# mapping names to sequence elements # - using namedtuple def test_0118(): from collections import namedtuple Subscriber = namedtuple('Subscriber', ['addr', 'joined']) sub = Subscriber('*****@*****.**', '2012-10-19') print(sub.addr, sub.joined) # 원본 데이터를 변환하면서 reduce 함수 적용하기 def test_0119(): # use generator exp nums = [1, 2, 3, 4, 5] s = sum(x * x for x in nums) print('sum:', s) if __name__ == '__main__': import doctest doctest.testmod(verbose=True) common_util.call_funcs(vars(), lambda s: s.startswith('test_')) ''' test_funcs = [v for k, v in vars().items() if k.startswith('test_')] for func in test_funcs: print('call {} ================='.format(func)) func() '''