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