Python programming: Creating high-quality programs using design patterns, concurrency, and libraries read notes

Source: Internet
Author: User
Tags object serialization

python programming: Creating high-quality programs using design patterns, concurrency, and librariesDirectory
    • 1 Create design mode
    • 2 structural design mode
    • 3 behavioral design mode
    • 4 advanced concurrency
    • 5 expanded python
    • 6 Advanced network programming
    • 7 tkinter
    • 8 opengl
Create Design Patterns
    1. Abstract Factory
      1. @classmethod def make_xxx (Class, ...)
    2. Builder
      1. With Open (FileName, "w", encoding= ' Utf-8 ') as F:f.write (x)
      2. One more layer of mapping package okay?
      3. Sequence with map unpacking:function (*args, **kwargs)
      4. super (). Add_title (t) #=> baseclass.add_title (self, t)?
    3. Factory method
    4. Prototype
      1. For code in Itertools.chain ((0X26C0,0X26C2), Range (0x2654,0x2660)): ...
      2. SetAttr (sys.module[__name__], name, Class) #=> globals ()[Name]=class
    5. Single case
      1. Key = "{} ({})". Format (name, currency)
Structural Design Patterns
  1. Adapter (Python mock object?) ): Reconcile the caller with the callee, making the interface consistent
    1. Class Renderer (METACLASS=ABC. Abcmeta):
      @classmethod
      def __subclasshook__ (Class, subclass): # Built-in method Isinstanc E () This method determines whether Arg1 is a subclass of Arg2 3.3+
      If class is Renderer:
      attributes = Collec tions. Chainmap (* (superclass.__dict__ for superclass in subclass.__mro__))
      methods = ("header", "Para Gaph "," footer ")
      If all (method in attributes for method in methods): Return True (Special case: and call Able (method))
      return notimplemented
    2. or
      @Qtrac. Has_methods ("header", "Paragaph", "footer")
      class Renderer (METACLASS=ABC. Abcmeta): Pass
  2. Bridging: Bridge interface and implementation (not directly inherited)
  3. Combination
    1. Composite is the element of the container type
  4. Decorator
    1. After implementing __eq__ and __lt__, you can go through @functools.total_ordering support < <= = = = >= >
    2. When implementing decorator, the wrapper plus decoration @functools.wraps (func) can make the __name__ __doc__ of the wrapper function the same as the original Func
    3. def wrapper (*args, **kwargs): ... return func (*args, **kwargs)
    4. Class Decorators: Manipulating Class objects with getattr/setattr/property and so on
  5. Appearance: Abstract set of identical interfaces for different implementations
    1. ? Binding method: Bound=self.f vs. unbound=class.f
  6. Enjoy RMB (Flyweight)
    1. Java:string#intern?
    2. P53 Python can execute arbitrary code during serialization, unsafe (how?). )
  7. Proxy: Represents another object with one object
    1. ' Remote agent '
    2. RPYC Library
Behavioral Design Patterns
  1. Responsibility chain
    1. P61 any function with a yield statement can be used as a generator, using @coroutine and infinite loops to turn it into a co-process (feeling there is a bit associated with scheme's CPS)
    @coroutine
    def Handler (successor):
    While True:
    event = (yield)
    Successor.send (Event)
  2. Command
    1. Undo-redo
  3. Interpreter
    1. Dsl:ply pyparsing
    2. Eval ()
    3. Ast.literal_eval ()
    4. EXEC ()
    5. Execute code with child process: with subprocess. Popen ([Sys.executable, "-"], ...) as process: ...
    6. stderr = Re.sub (", Line (\d+)", Lambda m:str (int (m.group (1))-offset), stderr)
    7. PyPy "Sandbox"
  4. Iterators
    1. Sequence protocol: Implementing __GETITEM__
    2. Two-parameter ITER (callable, Sentinel): __call__ required
    3. Iterator protocol: __iter__ () and __next__ ()
  5. Mediation: Implements docking of a set of interfaces with multiple objects (m:n connection? )
    1. Equivalent to various eventlistener in GUI programming?
    2. using a co-process to implement intermediaries ?
  6. Memo
    1. Pickle module? (That is, object serialization/deserialization)
  7. Observers
    1. In fact, it is the callback that the interested person registers separately, when the object state changes, all observers will be notified
    2. MVC: View is the observer of the model?
    3. Other: Database trigger, Django signaling system, QT Signal-slot
  8. State
  9. Strategy
    1. Algorithm (tactical?) ) switchable
  10. Template method
  11. Visitor (often used in the compiler's AST Traversal code generation)
    1. P114 Pypng uses RGBA format, while image is ARGB
Advanced Concurrency
    1. Compute intensive: Avoid Gil
      1. Multiprocessing. Joinablequeue.join/task_done ()
      2. Multiprocessing. Queue.get/get_nowait ()
      3. 3.2 Concurrent.futures Module
        1. Concurrent.futures.ProcessPoolExecutor
    2. I/O intensive: The main factor is network latency, which does not have much to do with threads or processes
    3. Case study: Concurrent GUI applications
      1. Multiprocessing. Value
Expand Python
    1. PyPy JIT
    2. cTYPES: Slightly
    3. Cython
      1. . pxd file:
        cdef extern from "Hyphen.h":
        ctypedef struct Hyphendict:pass
        hyphendict* hnj_hyphen_load (char* filename)
      2. . pyx
    4. Interacting with C + +: SWIG, SIP, boost::p Ython, Cffi
Advanced Network Programming
    1. XML-RPC: Slightly
    2. RPyC: Slightly
Tkinter
    1. Pygtk/pygobject
    2. Pyqt4/pyside
    3. Tkinter
      1. Modal: Global/Application-level/window-level/None
    4. WxPython
OpenGL

Python programming: Creating high-quality programs using design patterns, concurrency, and libraries read notes

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.