Python Magic Function: Comparison

發表留言

Multiprocessing: Pickle Issue

multiprocessing use pickle module to serialize things among the process, but doesn’t support functions with closures, lambdas, or functions in __main__

Here is the example I try:

solution: dill

dill and multiprocessing: pathos

dill: a utility to serialize all of python
– pox: utilities for filesystem exploration and automated builds
– klepto: persistent caching to memory, disk, or database
– multiprocess: better multiprocessing and multithreading in python
– ppft: distributed and parallel python
– pyina: MPI parallel map and cluster scheduling
– pathos: graph management and execution in heterogenous computing

 

1 則迴響

Python Composition

Basics

  • Inheritance could be brittle (a change may require changes elsewhere)
  • Decoupled code could work independently or interactively
  • classes interactions will work with an interface
  • Not checking types: polymorphic and Pythonic

Here is an example for inheritance and composition working together:

 

發表留言

Python Abstract Class

basics

  • An abstract class is not designed to and can’t not construct instances, but subclassed by regular classes
  • An interface / methods that defined in abstract class must be implement by its subclass
  • The python abc module enables the creation of abstract class

發表留言

Python Depth-First Order

Multiple inheritance

After Python 2.3, add a role for diamond shape ambiguous inheritance

(Make C inherit A in this example)

發表留言

Python Inheriting the Constructor

Basics

  • __init__ can be inherited
  • if a class doesn’t have an __init__ constructor, Python will check its parent class, as soon as it finds one, Python calls it and stops looking
  • we can use the super() function to call method in the parents class, or we may want to initialize in the parent class

發表留言

Python Polymorphism

Basics

  • Different classes has same interface (i.e., method name), we can say that this group of related classes implement the same action
  • Being able to work with different types to call methods without checking it’s possible and handle errors using exception handling is called DOCK TYPING
  • Checking the type ahead of time is generally considered un-pythonic

An example from stackoverflow:

or an built in len method as an interface implemented by list and set object:

 

發表留言

Serialize and Deserialze Sqlalchemy Nested Objects with JSON

At first we build a relationship :

and we want to dump a county instance to json like this:

extending JSONEncoder:

if you want to decode your json file, simply supply a function for object_hood attribute:

object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).

when a json file loads, we can print the parameter “dict” which pass in to object_hood function, the whole process will loads from the most inner object (a dict) to the outer objects look like this:

發表留言