Fluent Python (note)

Source: Internet
Author: User
Tags abs array length extend hashable integer division iterable shuffle


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. The key must be hashable:
A hashable object must meet the following requirements.
     (1) The hash() function is supported, and the hash value obtained by the __hash__() method is constant.
     (2) Support for equality by the __eq__() method.
     (3) If a == b is true, then hash(a) == hash(b) is also true.
     All user-defined objects are hashable by default because their hash values are obtained by id() , and
     And they are all unequal.
2. The dictionary is very expensive in memory (using memory for efficiency).
     There are two reasons why a tuple can replace a dictionary to save space:
     (1) Avoiding the space spent on hash tables,
     (2) There is no need to save the name of the field in the record in each element.
3. The key query is very fast
4. The order of the keys depends on the order of addition
5. Adding a new key to the dictionary may change the order of the existing keys.
The implementation of the set and the resulting result
1. The combined elements must be hashable
2. Collection and consumption of memory
3. It can be very efficient to determine whether an element exists in a collection
4. The order of the elements depends on the order in which they are added to the collection
5. Adding elements to the collection may change the order of the elements already in the collection. 
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)) 
>>> tokyo
City(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))
>>> tokyo
City_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
      Chip
    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 object in the datetime library
>> from datetime import datetime
>> timeinfo = datetime.today()

>> timeinfo
Datetime.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 LOAD_CONST               1 (1)
              3 STORE_FAST               0 (x)
 
  3           6 LOAD_FAST                0 (x)
              9 LOAD_CONST               2 (3)
             12 COMPARE_OP               0 (<)
             15 POP_JUMP_IF_FALSE       22
 
  4          18 LOAD_CONST               3 ('yes')
             21 RETURN_VALUE        
 
  6     >>   22 LOAD_CONST               4 ('no')
             25 RETURN_VALUE        
             26 LOAD_CONST               0 (None)
             29 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_ADD
              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 array
From random import random

Numbers = array('h', [-2, -1, 0, 1, 2]) #signed short
Memv = memoryview(numbers) #5 short integer signed arrays to create a memoryview
Print (len(memv)) #print length
Print (memv.tolist()) #convert to list form

Memv_oct = memv.cast('B') #Memory Sharing Convert to unsigned character type
Print (memv_oct.tolist())

Memv_oct[5] = 4 # Assign the position 5 byte to 4
Print (numbers) #because we changed the high byte of the 2-byte integer to 4, the value of this signed integer becomes 1024

The output is as follows:

5 #array length
[-2, -1, 0, 1, 2] #list form display
[254, 255, 255, 255, 0, 0, 1, 0, 2, 0]# double the length to convert to unsigned character type
Array('h', [-2, -1, 1024, 1, 2]) #The original array was modified 


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
A
ByteArray (b ' aaaaaa ')
B
ByteArray (b ' BB ')

Using Memoryview

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

A = ByteArray (' aaaaaa ')
Ma = Memoryview (a)
Ma.readonly # writable Memoryview
False
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 pressorthe auto-fill function will appear...
Built-in functions
Math
Function    Description
abs()   Returns absolute value of a number
divmod()    Returns quotient and remainder of integer division
max()   Returns the largest of the given arguments or items in an iterable
min()   Returns the smallest of the given arguments or items in an iterable
pow()   Raises a number to a power
round() Rounds a floating-point value
sum()   Sums the items of an iterable

Type Conversion
Function    Description
ascii() Returns a string containing a printable representation of an object
bin()   Converts an integer to a binary string
bool()  Converts an argument to a Boolean value
chr()   Returns string representation of character given by integer argument
complex()   Returns a complex number constructed from arguments
float() Returns a floating-point object constructed from a number or string
hex()   Converts an integer to a hexadecimal string
int()   Returns an integer object constructed from a number or string
oct()   Converts an integer to an octal string
ord()   Returns integer representation of a character
repr()  Returns a string containing a printable representation of an object
str()   Returns a string version of an object
type()  Returns the type of an object or creates a new type object

Iterables and Iterators
Function    Description
all()   Returns True if all elements of an iterable are true
any()   Returns True if any elements of an iterable are true
enumerate() Returns a list of tuples containing indices and values from an iterable
filter()    Filters elements from an iterable
iter()  Returns an iterator object
len()   Returns the length of an object
map()   Applies a function to every item of an iterable
next()  Retrieves the next item from an iterator
range() Generates a range of integer values
reversed()  Returns a reverse iterator
slice() Returns a slice object
sorted()    Returns a sorted list from an iterable
zip()   Creates an iterator that aggregates elements from iterables

Composite Data Type
Function    Description
bytearray() Creates and returns an object of the bytearray class
bytes() Creates and returns a bytes object (similar to bytearray, but immutable)
dict()  Creates a dict object
frozenset() Creates a frozenset object
list()  Constructs a list object
object()    Returns a new featureless object
set()   Creates a set object
tuple() Creates a tuple object

Classes, Attributes, and Inheritance
Function    Description
classmethod()   Returns a class method for a function
delattr()   Deletes an attribute from an object
getattr()   Returns the value of a named attribute of an object
hasattr()   Returns True if an object has a given attribute
isinstance()    Determines whether an object is an instance of a given class
issubclass()    Determines whether a class is a subclass of a given class
property()  Returns a property value of a class
setattr()   Sets the value of a named attribute of an object
super() Returns a proxy object that delegates method calls to a parent or sibling class

Input/Output
Function    Description
format()    Converts a value to a formatted representation
input() Reads input from the console
open()  Opens a file and returns a file object
print() Prints to a text stream or the console

Variables, References, and Scope
Function    Description
dir()   Returns a list of names in current local scope or a list of object attributes
globals()   Returns a dictionary representing the current global symbol table
id()    Returns the identity of an object
locals()    Updates and returns a dictionary representing current local symbol table
vars()  Returns __dict__ attribute for a module, class, or object

Miscellaneous
Function    Description
callable()  Returns True if object appears callable
compile()   Compiles source into a code or AST object
eval()  Evaluates a Python expression
exec()  Implements dynamic execution of Python code
hash()  Returns the hash value of an object
help()  Invokes the built-in help system
memoryview()    Returns a memory view object
staticmethod()  Returns a static method for a function
__import__()    Invoked by the import statement
Special methods unrelated to operators
Category method name
String/byte sequence representation __repr__, __str__, __format__, __bytes__
Numeric conversion __abs__, __bool__, __complex__, __int__, __float__, __hash__, __index__
Collection simulation __len__, __getitem__, __setitem__, __delitem__, __contains__
Iterative enumeration __iter__, __reversed__, __next__
Callable simulation __call__
Context management __enter__, __exit__
Instance creation and destruction __new__, __init__, __del__
Attribute Management __getattr__, __getattribute__, __setattr__, __delattr__, __dir__
Attribute descriptor __get__, __set__, __delete__
Class-related services __prepare__, __instancecheck__, __subclasscheck__
Bisect module manages ordered sequences
Bisect.bisect_left(a,x, lo=0, hi=len(a)) :
Find the index in which x is inserted in the ordered list a. Lo and hi are used to specify the range of the list. The default is to use the entire list. If x already exists, insert it on the left side. The return value is index.
Bisect.bisect_right(a,x, lo=0, hi=len(a))
Bisect.bisect(a, x,lo=0, hi=len(a)) :
These two functions are similar to bisect_left, but if x already exists, it is inserted on the right side.
Bisect.insort_left(a,x, lo=0, hi=len(a)) :
Insert x in the ordered list a. Same effect as 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)) :
Similar to insort_left, but if x already exists, insert it to the right.
The functions provided by the Bisect module can be divided into two categories: bisect* is only used to find index, no actual insertion is made; insort* is used for actual insertion.
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 random
Floats = 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

# Python2.7 - Python3.7 has this problem
``
>>> a=(1,2,[1,2,3,4,5])
>>> a[2]+=[1,1,1]
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     a[2]+=[1,1,1]
TypeError: 'tuple' object does not support item assignment
>>> a
(1, 2, [1, 2, 3, 4, 5, 1, 1, 1])
>>> a[2].extend([2,2,2])
>>> a
(1, 2, [1, 2, 3, 4, 5, 1, 1, 1, 2, 2, 2])
>>>
```
Python_ built-in four types of queues
From queue import Queue #LILO queue
q = Queue() #Create a queue object
Q.put(0) #Insert elements at the end of the queue
Q.put(1)
Q.put(2)
Print('LILOQueue', q.queue) #View all the elements in the queue
Print(q.get()) #return and delete the queue header element
Print(q.queue)

From queue import LifoQueue #LIFO queue
lifoQueue = LifoQueue()
lifoQueue.put(1)
lifoQueue.put(2)
lifoQueue.put(3)
Print('LIFOQueue', lifoQueue.queue)
lifoQueue.get() #return and delete the tail element of the queue
lifoQueue.get()
Print(lifoQueue.queue)

From queue import PriorityQueue # priority queue
priorityQueue = PriorityQueue() #Create priority queue object
priorityQueue.put(3) #insert element
priorityQueue.put(78) #insert element
priorityQueue.put(100) #insert element
Print(priorityQueue.queue) #View all the elements in the priority queue
priorityQueue.put(1) #insert element
priorityQueue.put(2) #insert element
Print('Priority Queue:', priorityQueue.queue) #View all elements in the priority queue
priorityQueue.get() #return and delete the lowest priority element
Print('remaining elements after deletion', priorityQueue.queue)
priorityQueue.get() #return and delete the lowest priority element
Print('remaining elements after deletion', priorityQueue.queue) #remaining elements after deletion
priorityQueue.get() #return and delete the lowest priority element
Print('remaining elements after deletion', priorityQueue.queue) #remaining elements after deletion
priorityQueue.get() #return and delete the lowest priority element
Print('remaining elements after deletion', priorityQueue.queue) #remaining elements after deletion
priorityQueue.get() #return and delete the lowest priority element
Print('All deleted: ', priorityQueue.queue) #View all elements in the priority queue

From collections import deque
dequeQueue = deque(['Eric','John','Smith'])
Print(dequeQueue)
dequeQueue.append('Tom') #Insert new element on the right side
dequeQueue.appendleft('Terry') #Insert new elements on the left side
Print(dequeQueue)
dequeQueue.rotate(2) loops right 2 times
Print('Queue after shifting right 2 times', dequeQueue)
dequeQueue.popleft() #return and delete the leftmost element of the queue
Print('Delete the leftmost element: ', dequeQueue)
dequeQueue.pop() #return and delete the rightmost element of the queue
Print('Delete the rightmost element: ', dequeQueue)


The above queues are available in multithreading and are thread safe, but cannot be used for communication in multiple processes. In a multi-process, you need to use this:
From multiprocessing import Process, Queue
Myqueue = Queue(100)


## Reference

Https://blog.csdn.net/sinat_38682860/article/details/80392493
Https://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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.