User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2022/05/23 16:01]
jypeter [numpy related stuff] Changed the VIEW update warnng example
other:python:misc_by_jyp [2022/12/12 12:11]
jypeter Added the "Storing objects and data in a file" section
Line 228: Line 228:
  
 </​code>​ </​code>​
 +
 +==== Storing objects and data in a file (shelve and friends) ====
 +
 +The built-in [[other:​python:​jyp_steps?​s[]=shelve#​the_shelve_package|shelve]] module can be **easily** used for storing temporary/​intermediate data
 +
 +More options:
 +  * Some [[other:​python:​jyp_steps#​data_file_formats|non-NetCDF]] file formats
 +  * Working with [[other:​python:​jyp_steps#​netcdf_filesusing_cdms2_xarray_and_netcdf4|NetCDF]] files
  
 ==== Sorting ==== ==== Sorting ====
Line 248: Line 256:
 ==== numpy related stuff ==== ==== numpy related stuff ====
  
 +=== Using a numpy array to store arbitrary objects ===
 +
 +The numpy arrays are usually used to store [[https://​numpy.org/​doc/​stable/​reference/​arrays.scalars.html|scalars]] of the same type (see also the [[https://​numpy.org/​doc/​stable/​reference/​arrays.dtypes.html|Data type objects (dtype)]]), very often numerical values.
 +
 +It is also possible to store **arbitrary** Python objects in an array, rather than using nested lists or dictionaries!
 +
 +<​code>>>>​ some_array = np.empty((2,​ 3), dtype=object)
 +>>>​ some_array
 +array([[None,​ None, None],
 +       ​[None,​ None, None]], dtype=object)
 +>>>​ some_array.shape
 +(2, 3)
 +>>>​ print(some_array[-1,​ -1])
 +None
 +>>>​ some_array[-1,​ 0] = filled_contour # e.g. save an existing cartopy filled contour object
 +>>>​ some_array
 +array([[None,​ None, None],
 +       ​[<​cartopy.mpl.contour.GeoContourSet object at 0x2ab679e8bf10>,​
 +        None, None]], dtype=object)</​code>​
 +        ​
 === Dealing with a variable number of indices === === Dealing with a variable number of indices ===
  
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter