geodesymiami / rsmas_insar

RSMAS InSAR code
https://rsmas-insar.readthedocs.io/
GNU General Public License v3.0
58 stars 22 forks source link

Python coding suggestions #545

Open falkamelung opened 1 month ago

falkamelung commented 1 month ago

Falk: Useful code snippets from Kawan's run_plot_precipitation_all.py :

https://github.com/geodesymiami/Precip_cron/blob/9ca7d19f937e7dce45b548a8cd61ebe832690fe3/run_plot_precipitation_all.py#L13-L16

https://github.com/geodesymiami/Precip_cron/blob/9ca7d19f937e7dce45b548a8cd61ebe832690fe3/run_plot_precipitation_all.py#L44-L47

https://github.com/geodesymiami/Precip_cron/blob/9ca7d19f937e7dce45b548a8cd61ebe832690fe3/run_plot_precipitation_all.py#L88

Kawan: Regarding the last point, there are two ways to initialize an argparse.Namespace.

The way I did it in run_plot_precip… is:

inps = argparse.Namespace(style=style, name=[volcano], no_show=True)

import argparse kwargs = {'key':'value', 'hello':'world', 'im_not': 'paid_enough’}

option 1

argparse.Namespace(** kwargs) Namespace(key='value', hello='world', im_not='paid_enough’)

option 2

argparse.Namespace(key='value', hello='world', im_not='paid_enough') Namespace(key='value', hello='world', im_not='paid_enough’)

But result is the same object. But passing **kwargs is great when you have a lot of key word arguments or want to pass things dynamically.

This is also the case for (or any other object with a similar init method).

https://github.com/python/cpython/blob/e8c91d90ba8fab410a27fad4f709cc73f6ffcbf4/Lib/argparse.py#L1295-L1297