Raphael +
  • Stay hungry, Stay foolish.
  • albus.zly@gmail.com
    albus12138

    Fluent Python笔记

    Part 1: 序言

    Chapter 1 | 数据模型

    Part 2: 数据结构

    Chapter 2 | 序列

    	from collections import namedtuple
    	City = namedtuple('City', 'name country population coordinates')
    	tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
    	# >>> tokyo
    	# City(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722, 139.691667))
    	# >>> tokyo.population
    	# 36.933
    	# >>> tokyo[1]
    	# 'JP'
    	
    	l = list(range(10))
    	# l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    	l[2:5] = [20, 30]
    	# l = [0, 1, 20, 30, 5, 6, 7, 8, 9] 不足的元素会被删除
    	del l[5:7]
    	# l = [0, 1, 20 ,30, 5, 8, 9]
    	# l[2:5] = 100 错误!
    	l[2:5] = [100]
    	# l = [0, 1, 100, 8, 9]
    	

    Chapter 3 | 字典和集合

    Chapter 4 | 文本和字节

    	from unicodedata import normalize
    
    	def nfc_equal(str1, str2):
    		return normalize('NFC', str1) == normalize('NFC', str2)
    
    	def fold_equal(str1, str2):
    		return (normalize('NFC', str1).casefold() == normalize('NFC', str2).casefold())
    	
    - 极限标准化
    	- 去除区分标志 (声调, 变音符号等等), 可以避免用户错误使用区分符号, 但也会大大降低搜索精度
    
    	import unicodedata
    	import string
    
    	def shave_marks(txt):
    		norm_txt = unicodedata.normalize('NFD', txt)
    		shaved = ''.join(c for c in norm_txt if not unicodedata.combining(c))
    		return unicodedata.normalize('NFC', shaved)
    
    	def shave_marks_latin(txt):
    		norm_txt = unicodedata.normalize('NFD', txt)
    		latin_base = False
    		keepers = []
    		for c in norm_txt:
    			if unicodedata.combining(c) and latin_base:
    				continue  # ignore diacritic on Latin base char
    			keepers.append(c)
    			# if it isn't combining char, it's a new base char
    			if not unicodedata.combining(c):
    				latin_base = c in string.ascii_letters
    		shaved = ''.join(keepers)
    		return unicodedata.normalize('NFC', shaved)
    
    	single_map = str.maketrans("""‚ƒ„†ˆ‹‘’“”•–—˜›""",
    							   """'f"*^<''""---~>""")
    
    	multi_map = str.maketrans({
    		'€': '<euro>',
    		'…': '...',
    		'Œ': 'OE',
    		'™': '(TM)',
    		'œ': 'oe',
    		'‰': '<per mille>',
    		'‡': '**',
    	})
    
    	multi_map.update(single_map)
    
    
    	def dewinize(txt):
    		return txt.translate(multi_map)
    
    
    	def asciize(txt):
    		no_marks = shave_marks_latin(dewinize(txt))
    		no_marks = no_marks.replace('ß', 'ss')
    		return unicodedata.normalize('NFKC', no_marks)
    	

    Part 3: 函数

    Chapter 5 | 头等函数

    Chapter 6 | 设计模式

    Chapter 7 | 装饰器和闭包

    @clock
    def factorial(n):
        return 1 if n < 2 else n*factorial(n-1)
    
    # 等价于直接作为参数传入
    clock(factorial(n))
    
    @d1
    @d2
    def func():
        pass
    
    d1(d2(func()))
    
    def register(active=True):
        def decorator(func):
            if active:
    		    do_something()
            else:
                do_something_else()
            return func
        return decorator
    
    @register(active=True)
    def func():
        pass
    

    Part 4: 面向对象

    Chapter 8 | 对象的引用,可变性和回收

    class Demo:
        def __init__(self, data=None):
            if data is None:
                self.data = []
            else:
                self.data = list(data)
    
    import weakref
    a = {0, 1}
    wref = weakref.ref(a)
    print(wref())
    del a
    print(wref())
    print(wref() is None)
    print(wref() is None)
    

    Chapter 9 | Pythonic 对象

    @classmethod
    def frombytes(cls, octets):
        typecode = chr(octets[0])
    	memv = memoryview(octets[1:]).cast(typecode)
    	return cls(*memv)
    

    To be Continued… Last updated: Feb 2, 2019

    Blog

    Coding

    Bookmarks