Python advanced programming and asynchronous IO concurrent programming (5) custom sequence class

Python advanced programming and asynchronous IO concurrent programming (5) custom sequence class

5-1 Sequence classification in python

Classification of sequence types: 1. Container sequence: list, tuple, deque (the container can hold any type of data)

my_list=[]
my_list.append(1)
my_list.append("a")

 

2. Flat sequence: str, bytes, bytearray, arry.arry 3. Variable sequence: list, deque, bytearry, array 4. Immutable: str, tuple, bytes

5-2 The abc inheritance relationship of sequence types in python

from collections import abc
Sequence MutableSequence
 

5-3 The difference between sequence +, += and extend method

a =[1,2]
c=a+[3,4]
print(c)

# 
a+=[3,4]
a+=(3,4)

a.extend(range(3))
a.append([1,2])
print(a)
 

5-4 Implement sliceable objects


 
import numbers
class Group:
    def __init__(self,group_name,company_name,staffs)
        self.group_name=name
        self.company_name=company_name
        self.staffs=staffs
        
    def __reversed__(self):
        pass
    def __getitem__(self,item):
        cls=type(self)
        if isinstance(item,slice):
            return cls(group_name=self.group_name,company_name=self.company_name,staffs=self.staffs[item])
        elif isinstance(item,numbers.Integral):
            return  cls(group_name=self.group_name,company_name=self.company_name,staffs=[self.staffs[item]])
    def __len__(self):
        pass
    def __iter__(self):
        pass
    def __contains__(self,item):
        pass
staffs=["bobbay1","imooc","bobby2","bobby3"]
group=Group(company_name="imooc",group_name="user",staffs=[""])
sub_group=group[:2]
sub_group=group[0]
queryset

 

5-5 bisect maintains sorted sequence

import bisect
# 
# 
inter_list=[]
bisect.insort(inter_list,3)
bisect.insort(inter_list,2)
bisect.insort(inter_list,5)
bisect.insort(inter_list,1)
bisect.insort(inter_list,6)
print(inter_list)
print(bisect.bisect_left(inter_list,3))

# 
90-100 A
80-90 B
["ABCDE"]
print(inter_list)

 

5-6 When should we not use lists

#array,deque
# 
import array
 #array list array 
 my_array=array.array("i")
 my_array.append(1)
 my_array.append("abc")
 
 

5-7 List comprehensions, generator expressions, dictionary comprehensions

# 
# 1 20 
odd_list=[]
for i in range(21):
    if i %2=1:
        odd_list.append(i)

odd_list=[i for i in range(21) if i%2 == 1]
print(type(odd_list))

# 

def hadle_item(item):
    return item*item
odd_list=[hadle_item(i) for i in range(21) if i%2 == 1]
# 
print(type(odd_list) 

# 
odd_gen= i for i in range(21) if i%2 == 1)  
print(type(odd_list))
odd_list=list(odd_gen)

print(type(odd_list))
print(odd_list)

# 
my_dict = {"bobby1":22,"bobby2":23,"imooc":5}
reversed_dict={value:key for key,value in my_dict.items()}
pritn(reversed_dict)

# 
# my_set = set(my_dict.keys())
my_set={key for key,value in my_dict.items()}
print(type(my_set))
print(my_set)

 

5-8 Chapter Summary

Summary: custom sequence type, python protocol, sequence abc relationship, it can be sliced when the sequence is satisfied, sequence + and += and extended relationship bisect manages sortable sequence, list comprehension, generator expression, dictionary deduction formula.