Fluent Python (note)

Source: Internet
Author: User

There are many artifice in fluent python, and the whole book emphasizes how to maximize the use of the Python standard library. This article introduces many of the infrequently used data types, operations, libraries, etc. of Python, and it should be helpful to get started with Python and to improve your understanding of Python. Now read through a few sympathetic actions:

Main classifications for Python built-in sequence types:

Divided by the types of elements that can be stored: container sequence and flat sequence

    1. The container sequence, which is what can be placed as an element, including another sequence. It is important to note that if an element is a sequence type, it is often a reference that needs to be carefully stored.
      Common container sequences include: List,tuple,array.array,collections.deque and so on.
    2. A flat sequence that holds atomic-level elements, where values are stored instead of references.
      Common flat sequences include: Str,bytes,bytearray, Memoryview, Array.array, etc.

Can be modified by sequence: variable sequence and immutable sequence

    1. Variable sequence: Can be added, deleted, modified sequence of operations, including list, ByteArray, Array.array, Collections.deque, Memoryview and so on.
    2. Immutable sequence: The sequence of these operations cannot be performed, including tuple, str, bytes, etc.
A variant of the dictionary

Many variants with similar dictionary types are available in the Collections module in the standard library.

Orderdict: This type saves the order when the key is added, so the key iteration order is always the same

Chainmap: The type can hold several different mapping pairs, and when the key is searched, the objects are looked up and down as a whole until the key is found

Counter: This mapping type will give the key an integer technology, each time a key is added to the counter, so this type can be used to count the hash object, or as a multi-set to use.

UserDict: This class actually writes the standard dict with Python again. Typically used instead of dict when programmers want to create their own dict by inheriting Dict. The main reason is that the direct inheritance of native dict is a bug.

Defaultdict: A choice to handle keys that cannot be found
When a key is not in the map, we also want to get a default value. This is defaultdict, which is the subclass of Dict, and implements the missing method.

The implementation of dict and the resulting results
键必须是可散列的:一个可散列的对象必须满足以下要求。    (1) 支持 hash() 函数,并且通过 __hash__() 方法所得到的散列值是不变的。    (2) 支持通过 __eq__() 方法来检测相等性。    (3) 若 a == b 为真,则 hash(a) == hash(b) 也为真。    所有由用户自定义的对象默认都是可散列的,因为它们的散列值由 id() 来获取,而    且它们都是不相等的。字典在内存上开销很大(用内存换效率)。    元组取代字典就能节省空间的原因有两个:    (1) 避免了散列表所耗费的空间,    (2) 无需把记录中字段的名字在每个元素里都存一遍。键的查询很快键的次序取决于添加顺序往字典里添加新键可能会改变已有键的顺序
The implementation of the set and the resulting result
Collections.namedtuple can be used to build a tuple with a field name and a class with a name

Creating a named tuple requires two parameters, one is the class name, and the other is the name of each field of the class. Latter
Can be either an iterative object consisting of several strings, or a string of field names separated by spaces.

>>> from collections import namedtuple>>> City = namedtuple('City', 'name country population coordinates')>>> tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667)) >>> tokyoCity(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722,139.691667))>>> tokyo.population 36.933>>> tokyo.coordinates(35.689722, 139.691667)>>> tokyo[1]'JP'>>> City = namedtuple('City_Name', 'name country population coordinates')>>> tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))>>> tokyoCity_Name(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722, 139.691667))
When the list is not preferred
    1. If we need a list that contains only numbers, then Array.array is more efficient than list. Array supports the
      There are operations related to variable sequences, including. Pop,. Insert, and. Extend. In addition, the array is provided from the file
      Faster methods of reading and depositing files, such as. frombytes and. ToFile.
    2. Set is optimized for checking whether an element exists
    3. Memoryview is a built-in class that allows the user to manipulate the same array without copying the contents of different tangent
    4. High-order array and matrix operations with NumPy and scipy
    5. Use two-way queues and other forms of queuing (Collections.deque bidirectional queue classes, queue in the queue class, Lifoqueue and Priorityqueue, multiprocessing. Queue, HEAPQ can use variable sequences as heap queues or priority queues)
Python formatted output

When you format the output, the difference between%r and%s is like the difference between the repr () function processing object and the STR () function.

    • %s, str (), more intelligent;
    • %r---repr (), processing is simple and straightforward; when dealing with simple objects, there is little difference.

This article highlights some of the different uses of the two:

    1. When working with strings
>> s = 'world'>> print('hello %s'%s)hello world>> print('hello %r'%s)hello 'world'>> str(s)'world'>> repr(s)"'world'"2. datetime 库中的 datetime 对象>> from datetime import datetime >> timeinfo = datetime.today()>> timeinfodatetime.datetime(2016, 6, 7, 21, 17, 34, 925488)>> type(timeinfo)datetime.datetime>> repr(timeinfo)'datetime.datetime(2016, 6, 7, 21, 17, 34, 925488)'>> str(timeinfo)'2016-06-07 21:17:34.925488'
disassembly function Python opcode

The Python dis module supports disassembly of Python code to generate bytecode instructions.

  in[1]: def test (): ... x = 1 ... If x < 3: ... return "yes" ... else: ... "return" no "in[2]: Dis.dis (test) 2 0 LO              Ad_const 1 (1) 3 Store_fast 0 (x) 3 6 Load_fast 0 (x) 9 Load_const 2 (3) Compare_op 0 (<) pop_jump_if_ FALSE 4 load_const 3 (' yes ') Return_value 6 >> 2             2 Load_const 4 (' no ') return_value Load_const 0 (None) Return_value >>> def add (A, b = 0): ... RETURN a + b ... >>> >>> Dis.dis (add) 2 0 load_fast 0 (a) 2 Load_fast 1 (b) 4 Binary_ad D 6 return_value>>>  

Class Memoryview (obj) is a python built-in class, if you want to use Memoryview to reference an object, then this object must support buffer protocol, Python3 Zhongyuan (built-in) The bytes and bytearray,memoryview that support the buffer protocol can read and manipulate the same piece of memory in different ways, and the original memory bytes are not moved at random. Similar to the strong turn in C, the advantage is that there is no memory copy.

For example, use Memoryview to modify the data for a short integer signed integer array.

from array import arrayfrom random import randomnumbers = array('h', [-2, -1, 0, 1, 2]) #signed shortmemv = memoryview(numbers)      #5个短整型有符号整数的数组创建一个memoryviewprint (len(memv))               #打印长度print (memv.tolist())           #转换成列表形式memv_oct = memv.cast('B')       #内存共享 转换成无符号字符类型print (memv_oct.tolist())memv_oct[5] = 4                 #把位置5的字节赋值成4print (numbers)                 #因为我们把占 2 个字节的整数的高位字节改成了 4,所以这个有符号整数的值就变成了 1024输出如下:5                       #数组长度[-2, -1, 0, 1, 2]       #列表形式显示[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]#长度扩大一倍 转换为无符号字符类型array('h', [-2, -1, 1024, 1, 2])   #原来的数组被修改

ByteArray is a variable (mutable) byte sequence, relative to Str in Python2, but Str is immutable (immutable).
In Python3, because STR is UNICODE-encoded by default, only ByteArray can be accessed by byte.
Comparison of the following two behaviors:
The simple point is that the slicing operation of STR and ByteArray produces new slices of STR and bytearry and copies the data, not after using Memoryview.

Examples in the Python2

Do not use Memoryview

A = ' aaaaaa '
b = A[:2] # will produce a new string

A = ByteArray (' aaaaaa ')
b = A[:2] # will produce a new ByteArray
B[:2] = ' BB ' # changes to B do not affect a
ByteArray (b ' aaaaaa ')
ByteArray (b ' BB ')

Using Memoryview

A = ' aaaaaa '
Ma = Memoryview (a)
Ma.readonly # read-only Memoryview
MB = Ma[:2] # does not produce a new string

A = ByteArray (' aaaaaa ')
Ma = Memoryview (a)
Ma.readonly # writable Memoryview
MB = Ma[:2] # will not produce a new ByteArray
Mb[:2] = ' BB ' # change to MB is a change to Ma
Mb.tobytes ()
' BB '
Ma.tobytes ()
' Bbaaaa '

There are a variety of callable types in Python, so the callable () function is judged:

>>> abs, str, 13(<built-in function abs>, <class 'str'>, 13)>>> [callable(obj) for obj in (abs, str, 13)][True, True, False]
Random.shuffle scrambled Sequence
>>> import random>>> a=range(10)>>> random.shuffle(a)>>> a[1, 0, 8, 5, 6, 7, 9, 3, 2, 4]>>> random.shuffle(a)>>> a[7, 5, 6, 2, 1, 8, 9, 0, 3, 4]
Vim commonly used fast
    • 0→ Digit Zero, to the wardrobe
    • $→ to the end of our line
    • a→ inserted after the cursor
    • o→ inserts a new row after the current row
    • o→ inserting a new row before the current line
    • Cw→ replaces characters from the position of the cursor to the end of a word
    • . → (decimal point) You can repeat the last command
    • ng→ to Nth Line (note that the G in the command is uppercase, and I generally use: N to nth rows, such as: 137 To line 137th)
    • gg→ to the first line. (equivalent to 1G, or: 1)
    • g→ to the last line.
    • In Insert mode, you can enter the beginning of a word, and then press or the auto-fill function will appear ...
Built-in functions
Mathfunction descriptionabs () Returns absolute value of a numberdivmod () Returns quotient and remainder of integers Divisionmax () Returns the largest of the given arguments or items in an iterablemin () Returns the smallest of the giv En arguments or items in an ITERABLEPOW () raises a number to a powerround () Rounds a floating-point valuesum () Sums th  E items of an iterabletype conversionfunction descriptionascii () Returns A string containing a printable representation of an objectbin () converts an integer to a binary stringbool () converts an argument to a Boolean valuechr () Returns String representation of character given by integer Argumentcomplex () Returns a complex number constructed from argument Sfloat () Returns a floating-point object constructed from a number or stringhex () converts an integer to a hexadecimal s Tringint () Returns An integer object constructed from a number or stringoct () converts an integer to an octal Stringor D () Returns integer REPresentation of a CHARACTERREPR () Returns A string containing a printable representation of an OBJECTSTR () Returns a St     Ring version of an objecttype () Returns the type of an object or creates a new type Objectiterables and iteratorsfunction Descriptionall () Returns True if all elements of an iterable is Trueany () Returns True if any elements of a ITER Able is trueenumerate () Returns a list of tuples containing indices and values from an iterablefilter () Filters elemen TS from an iterableiter () Returns an iterator Objectlen () Returns the length of an Objectmap () applies a function to Every item of an ITERABLENEXT () retrieves the next item from an Iteratorrange () generates a range of integer valuesrevers Ed () Returns a reverse iteratorslice () Returns a slice objectsorted () Returns a sorted list from an iterablezip () Cr  Eates an iterator, aggregates elements from Iterablescomposite Data typefunction Descriptionbytearray () creates and Returns an object of thE ByteArray classbytes () creates and returns a bytes object (similar to ByteArray, but immutable) dict () creates a dict ob Jectfrozenset () Creates a frozenset ObjectList () constructs a list objectobject () Returns a new featureless objectset ( ) creates a set Objecttuple () creates a tuple objectclasses, Attributes, and Inheritancefunction Descriptionclassmeth OD () Returns A class method for a functiondelattr () deletes an attribute from an objectgetattr () Returns the value O f A named attribute of a objecthasattr () Returns True If an object has a given attributeisinstance () determines whet She an object was an instance of a given classissubclass () determines whether a class is a subclass of a given Classprop Erty () Returns a property value of a classsetattr () sets the value of a named attribute of an objectsuper () Returns a P Roxy object, delegates method calls to a parent or sibling classinput/outputfunction Descriptionformat () Convert s a value to a formatted Representationinput () Reads input from the Consoleopen () Opens a file and returns a file Objectprint () Prints to a text Stream or the Consolevariables, References, and Scopefunction Descriptiondir () Returns a list of names in current Loc    Al Scope or a list of object Attributesglobals () Returns A dictionary representing the current global symbol TableID () Returns the identity of an objectlocals () Updates and Returns A dictionary representing current local symbol TableVar  S () Returns __dict__ attribute for a module, class, or Objectmiscellaneousfunction descriptioncallable () Returns True If object appears Callablecompile () compiles source into a code or AST Objecteval () evaluates a Python expressionexec (  ) Implements dynamic execution of Python Codehash () Returns The hash value of a objecthelp () invokes the built-in Help Systemmemoryview () Returns A Memory View Objectstaticmethod () Returns a static method for a function__import__ () I Nvoked by the Import STATement 
Special methods unrelated to operators
类别 方法名字符串 / 字节序列表示形式 __repr__、__str__、__format__、__bytes__数值转换 __abs__、__bool__、__complex__、__int__、__float__、__hash__、__index__集合模拟 __len__、__getitem__、__setitem__、__delitem__、__contains__迭代枚举 __iter__、__reversed__、__next__可调用模拟 __call__上下文管理 __enter__、__exit__实例创建和销毁 __new__、__init__、__del__属性管理 __getattr__、__getattribute__、__setattr__、__delattr__、__dir__属性描述符 __get__、__set__、__delete__跟类相关的服务 __prepare__、__instancecheck__、__subclasscheck__
Bisect module manages ordered sequences
bisect.bisect_left(a,x, lo=0, hi=len(a)) :查找在有序列表 a 中插入 x 的index。lo 和 hi 用于指定列表的区间,默认是使用整个列表。如果 x 已经存在,在其左边插入。返回值为 index。bisect.bisect_right(a,x, lo=0, hi=len(a))bisect.bisect(a, x,lo=0, hi=len(a)) :这2个函数和 bisect_left 类似,但如果 x 已经存在,在其右边插入。bisect.insort_left(a,x, lo=0, hi=len(a)) :在有序列表 a 中插入 x。和 a.insert(bisect.bisect_left(a,x, lo, hi), x) 的效果相同。bisect.insort_right(a,x, lo=0, hi=len(a))bisect.insort(a, x,lo=0, hi=len(a)) :和 insort_left 类似,但如果 x 已经存在,在其右边插入。Bisect 模块提供的函数可以分两类: bisect* 只用于查找 index, 不进行实际的插入;而 insort* 则用于实际插入。
When list is not the optimal choice, Dict is the core type of Python, but it is the result of space-time, compared to the memory, tuple is a good alternative to the DICT structure, set to do the inclusion and the weight is appropriate.
from array import array  from random import randomfloats = array('d', (random() for i in range(10**7)))  fp = open('floats.bin', 'wb')floats.tofile(fp)  fp.close()floats2 = array('d')  fp = open('floats.bin', 'rb')floats2.fromfile(fp, 10**7)  fp.close()floats2 == floats
Python_ built-in four types of queues
From queue import Queue #LILO队列q = Queue () #创建队列对象q. Put (0) #在队列尾部插入元素q. Put (1) q.put (2) print (' Lilo queue ', Q.queue) #查看队列中的所有 Element print (Q.get ()) #返回并删除队列头部元素print (q.queue) from queue import Lifoqueue #LIFO队列lifoQueue = Lifoqueue () lifoqueue.put (1) Lifoqueue.put (2) lifoqueue.put (3) print (' LIFO queue ', Lifoqueue.queue) lifoqueue.get () #返回并删除队列尾部元素lifoQueue. Get () print (lifoqueue.queue) from queue import Priorityqueue #优先队列priorityQueue = Priorityqueue () #创建优先队列对象priorityQueue. Put (3) #插 into element Priorityqueue.put (#插入元素priorityQueue) #插入元素print (priorityqueue.queue) # View all elements in the priority queue Priorityqueue.put (1) #插入元素priorityQueue. Put (2) #插入元素print (' Priority queue: ', Priorityqueue.queue) # View all elements in the priority queue Priorityqueue.get () #返回并删除优先级最低的元素print (' remaining elements after deletion ', Priorityqueue.queue) Priorityqueue.get () # Returns and removes the element with the lowest priority print (' leftover elements after deletion ', priorityqueue.queue) #删除后剩余元素priorityQueue. Get () #返回并删除优先级最低的元素print (' leftover elements after deletion ', Priorityqueue.queue) #删除后剩余元素priorityQueue. Get () #返回并删除优先级最低的元素print (' remaining elements after deletion ', Priorityqueue.queue) # Remaining elements after deletion prioriTyqueue.get () #返回并删除优先级最低的元素print (' All deleted: ', priorityqueue.queue) #查看优先级队列中的所有元素from collections Import Deque # Double-ended Queue dequequeue = deque ([' Eric ', ' John ', ' Smith ']) print (dequequeue) dequequeue.append (' Tom ') # Insert the new element on the right dequequeue.appendleft (' Terry ') #在左侧插入新元素print (Dequequeue) dequequeue.rotate (2) #循环右移2次print (' queue ' after 2 cycles right), Dequequeue) Dequequeue.popleft () #返回并删除队列最左端元素print (' queue after deletion of the leftmost element: ', Dequequeue) Dequequeue.pop () #返回并删除队列最右端元素print (' Delete the queue after the right-most element: ', Dequequeue ') the above queue can be used in multiple threads and is thread-safe, but cannot be used for communication in multiple processes. In a multi-process, this is required: From multiprocessing import process, Queuemyqueue = Queue (100) # # Reference 80392493 www.cnblogs.com/cmnz/p/ 6936181.html
Key words
from keyword import kwlistprint(kwlist)
Builtins Module
import builtinsdir(builtins)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: