User Tools

Site Tools


techs:programming:learn-python

Learn Python

1%   Scipy Lecture Notes, One document to learn numerics, science, and data with Python.

1%   Python - 100天从新手到大师

10 Essential Operations for Spatial Data in Python
有关于KD-Tree的介绍。


Copy or deepcopy

Just remember, assignment never copies. See here.

Save and Load

Package Save Load
numpy np.save(), np.savez() np.load()
np.savetxt() np.loadtxt()

Errors and Warnings

raise Exception('# lablablab...')  # like matlab error('...')
 
from warnings import warn
warn('# blablabla...')  # like marlab warn('...)

Indexing

Function description
numpy.ma Masked arrays are arrays that may have missing or invalid entries. A masked array is the combination of a standard numpy.ndarray and a mask.

`threading` and `multiprocessing`

https://www.liaoxuefeng.com/wiki/897692888725344/923056295693632

在windows下和在linux下的表现和性能不一样。

process 比 thread 要贵;thread 无法突破多核的限制,process 可以充分利用多核多CPU;

https://blog.floydhub.com/multiprocessing-vs-threading-in-python-what-every-data-scientist-needs-to-know/

  • Threading should be used for programs involving IO or user interaction.
  • Multiprocessing should be used for CPU bound, computation-intensive programs.

Others

Iterate Combination Indexes

Iterate combinations of different lists while still get the combination indexes: (from here)

def enumerated_product(*args):
    yield from zip(itertools.product(*(range(len(x)) for x in args)), itertools.product(*args))
 
time0 = time.time()
for idx, pair in enumerated_product(grid_dt, grid_lons, grid_lats):

Tight layout keeps shrink in iterative plotting:

fig.canvas.draw()
plt.tight_layout()

Use __name__ == "__main__"

Note that it's a best practice to use the if __name__ == "__main__" guard for scripts anyway, so that code isn't unexpectedly executed if you find you need to import your script into another script at some point in the future. (see https://stackoverflow.com/a/24374798/11172061)

techs/programming/learn-python.txt · Last modified: 2021/10/07 15:22 by foreverph