Effective python (V): Built-in module

1, and consider contextlib with statement rewrite reusable try / finally Code

  1. with lock:print('lock is held')Equivalent try:print('lock is held'), finally:lock.releaseusing the with statement to avoid tedious
  1. Developers can use the built-in contextmanager decorator contextlib module to handle write your own objects and functions to support with the statement, doing so is more convenient than the standard wording, if you use the standard way to write, you need to define a new class and provided __enter__ and _ _exit__ method
    from contextlib import contextmanager
    
    #上下文管理器
    @contextmanager
    def test():
      print("初始化")
      #可以在这里开启资源,如log等级上升
      try:
        print("with开始")
        #如果有错误会通过yield弹出
        yield
      finally:
        print("离开时释放资源")
    
    with test():
      print("go")
    
  1. An object can pop in yield at specified as a local variable by as, it can interact with in

2, using pickle reliable operation copyreg

  1. Built-in module can pickle python python object into a byte stream, can also deserialize python byte object, but the data is actually a pickle processing program, may be mixed malicious information, the program causing damage, json data generated by a secure information, how to describe the objects that make up only and will not cause additional risk, so the byte stream pickle processing should not be spread from an untrusted program
  1. For example, define a GameStateclass, instantiate an object state=GameState()that contains the player's current state of the game of life, gold and so on, players out of the game when the state written directly to a file, read the game loads
    with open(path,'wb') as f:
      pickle.dump(state,f)
    
    with open(path,'rb') as f:
      state_after=pickle.load(f)
    print(state_after.__dict__)
    
  1. If the class adds some new properties, but saved the object is still old, it needs to use copyreg, the first to add a class __init__constructor, by way of parameter passing initialization properties, and then use the following code registration function, sequence and deserialization can still use according to the original way
    def unpickle_game_state(kwargs):
      return GameState(**kwargs)#返回State实例化
    
    def pickle_game_state(game_state):#参数为State对象
      kwargs=game_state.__dict__#获取其属性
    
      #序列化封装需要返回反序列化的函数和参数
      return unpickle_game_state,(kwargs,) 
    
    copyreg.pickle(GameState,pickle_game_state)#注册pickle函数
    
  1. Management version number, modification of the pickle copyreg registration function, a version number adding parameters inside kwargs['version]=2, and then manipulate the deserialize function according to the version number, the version can be compatible
  1. The introduction of a fixed path, if reconstruction, delete or modify the class name, then deserialized when an error occurs, register to use copyreg.pickle, will automatically point unpickle function, so do not worry about the question of amending the class name, but if not use copyreg registered, then modify the class name after deserialization will error

4, the processing module uses the datetime local time instead of time module

  1. time module, built-in time module has a function called localtime, you can put UNIX timestamp (timestamp that is, the time from the UTC origin UNIX timing the number of seconds) converted to your local time zone of the host computer, this module is not stable enough, only host time zone conversion time, other areas to be wrong, you should try to do, but the use of datetime module
    from time import localtime,strftime,strptime,mktime
    time_format='%Y-%m-%d %H:%M:%S'
    time_str=strftime(time_format,localtime(1407694710))
    #将时间戳转换为当地时间
    print(time_str)
    
    #将本地时间转化为UTC时间
    #strptime解析时间字符串,mktime将本地时间转换为UNIX时间戳
    print(mktime(strptime(strptime(time_str,time_format))))
    
  1. datetime module
    from datetime import datetime,timezone
    from time import mktime
    
    #UTC时间转本地时间
    now=datetime(2014,8,10,18,18,30)
    now_utc=now.replace(tzinfo=timezone.utc)
    #注意此处若想可靠的转换时区,还需要搭配pytz模块
    now_local=now_utc.astimezone()#此处只包含UTC时区
    print(now_local)
    
    #本地时间转UTC格式时间戳
    time_str='2014-08-10 11:18:30'
    time_format='%Y-%m-%d %H:%M:%S'
    now=datetime.strptime(time_str,time_format)
    time_tuple=now.timetuple()
    utc_now=mktime(time_tuple)
    print(time_tuple)
    
  1. To not simultaneously perform a reliable switching operation between the regions, but also with the module pytz

5, built-in data structures and algorithms (the developer should not be re-implement themselves, because they are difficult to write)

  1. Deque, deque Collections class module, inserted from the queue head and tail of a removable element or only the complexity of O (1) time
    d=deque()
    d.append(1)
    x=d.popleft()
    
  1. Ordered dictionary, OrderedDict Collections class module, it is possible to bond the insertion order, in order to retain the key-value dictionary. Standard dictionaries are unordered, that is to say, may be different iterations of the iterative sequence on the same key for the two dictionaries
    a=OrderedDict()
    a['x']=1
    
  1. Default dictionary defaultdict Collections class module, with the present embodiment int create a dictionary function, the default value is 0
    stats=defaultdict()
    stats['my_counter']+=1
    
  1. Stack queue (Priority Queue), heapq module heappush, heappop nsmallest and other functions, the stack structure can be created in a standard list, the time complexity of O (logn), a list of common O (n)
    a=[]
    heappush(a,5)
    heappush(a,3)
    heappush(a,7)
    heappush(a,4)
    #总是能弹出优先级较高的元素,
    #默认是越小元素优先级越高
    print(heappop(a)) 
    #即使调用sort后依然能保持堆结构
    a.sort()
    
  1. Binary search, bisect module bisect_left function, use the Index to find complexity is O (n), dichotomized O (logn), Note: Before using the list to be sorted, a listing bisect search one million elements, with index Search list contains 14 elements, the time spent almost
    i_index = bisect_left(alist,number)
    
  1. Iterator-related tools, itertools module can be divided into three categories

♦ be able to connect the iterator function:

  • chain: a plurality of sequentially connected to iterators an iterator
  • cycle: the various elements of an infinite repetition iterator
  • tee: to split into a plurality of parallel iterator iterator
  • zip_longest: the built-in functions similar to a zip, can cope with different lengths iterator

♦ function to filter element from the iterator:

  • islice: without copy, cut according to the index value acquired iterator iterator part
  • takewhile: determining when the function is True, the process returns by one element from the iterator
  • dropwhile: first place to begin is False from decision function, element by element, returned iterator
  • filterfalse: make the determination by one return False function of all the elements from the iterator, with the opposite effect filter function

♦ iterator able to combine the elements as a function:

  • product: The calculation of the Cartesian product of the elements in the iterator (is x * y), and returns. Product can be used to derive a list of rewrite operations deeply nested
  • a permutations: in the iterator constructed using elements of length N ordered, for example:permutations('ABCD',2) # AB AC AD BA BC BD CA CB CD DA DB DC
  • Combination: Construction of random combinations of length N, with the embodiment iterator elements:combinations('ABCD', 2) # AB AC AD BC BD CD

NOTE: If you find yourself needing to write a very troublesome iterative procedure, should take the time to look at the documents to see if there itertools existing tools can be used

6, in the case of emphasis on accuracy, using decimal module Decimal class, which provides 28 decimal places, fixed-point mathematical operations to default, there is a need to provide higher accuracy

  1. dec=Decimal('1.45')Decimal calculations made between accurate results, the type of calculations is still Decimal type,print(dec)
  1. Quantize provide built-in function, it can accurately adjust the value according to the type Decimal precision and rounding mode result=dec.quantize(Decimal('0.01'),rounding=ROUND_UP),print(result)
  1. To use the precision is not limited express rational way, you can consider using Fraction class that contains a built-in module in the fractions

7, learn to use pypi, Python central warehouse: https://pypi.python.org

  1. If you encounter an unfamiliar programming problem, you should go inside and see other people's code PyPI
  1. Python3 installation package pip3 version of the package, pip install version python2

Guess you like

Origin www.cnblogs.com/shitianfang/p/12589230.html