other:python:misc_by_jyp
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
other:python:misc_by_jyp [2022/02/21 15:47] – [numpy related stuff] Added np.unique example jypeter | other:python:misc_by_jyp [2022/12/12 14:50] – Improved by changing the sections' levels jypeter | ||
---|---|---|---|
Line 5: | Line 5: | ||
</ | </ | ||
- | ==== Reading/ | ||
+ | ===== Reading/ | ||
< | < | ||
Line 17: | Line 17: | ||
</ | </ | ||
- | ==== Generating (aka raising) an error ==== | + | |
+ | ===== Generating (aka raising) an error ===== | ||
This will stop the script, unless it is called in a function, and the code calling the function explicitely catches and deals with errors | This will stop the script, unless it is called in a function, and the code calling the function explicitely catches and deals with errors | ||
Line 25: | Line 26: | ||
- | ==== Stopping a script ==== | + | ===== Stopping a script |
A user can use '' | A user can use '' | ||
Line 32: | Line 33: | ||
- | ==== Checking if a file/ | + | ===== Checking if a file/ |
< | < | ||
Line 38: | Line 39: | ||
>>> | >>> | ||
True</ | True</ | ||
+ | |||
+ | |||
+ | ===== Playing with strings ===== | ||
+ | |||
+ | ==== Filenames, etc... ==== | ||
+ | |||
+ | Check [[other: | ||
+ | |||
+ | ==== Splitting strings ==== | ||
+ | |||
+ | It's easy to split a string with multiple blank delimiters, or a specific delimiter, but it can be harder to deal with sub-strings | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | [' | ||
+ | |||
==== Working with paths and filenames ==== | ==== Working with paths and filenames ==== | ||
Line 124: | Line 153: | ||
>>> | >>> | ||
>>> | >>> | ||
- | ==== Using command-line arguments ==== | ||
- | === The extremely easy but non-flexible way: sys.argv === | + | |
+ | ===== Using command-line arguments ===== | ||
+ | |||
+ | ==== The extremely easy but non-flexible way: sys.argv | ||
The name of a script, the number of arguments (including the name of the script), and the arguments (as strings) can be accessed through the '' | The name of a script, the number of arguments (including the name of the script), and the arguments (as strings) can be accessed through the '' | ||
Line 148: | Line 179: | ||
2 tas_tes.nc</ | 2 tas_tes.nc</ | ||
- | === The C-style way: getopt === | + | |
+ | ==== The C-style way: getopt | ||
Use [[https:// | Use [[https:// | ||
- | === The deprecated Python way: optparse === | + | |
+ | ==== The deprecated Python way: optparse | ||
[[https:// | [[https:// | ||
- | === The current Python way: argparse === | + | |
+ | ==== The current Python way: argparse | ||
[[https:// | [[https:// | ||
- | ==== Using ordered dictionaries ==== | + | |
+ | ===== Using ordered dictionaries | ||
**Dictionary order is guaranteed to be insertion order**! Note that the [[https:// | **Dictionary order is guaranteed to be insertion order**! Note that the [[https:// | ||
Line 166: | Line 201: | ||
Check the [[https:// | Check the [[https:// | ||
- | ==== Using sets ==== | + | |
+ | ===== Using sets ===== | ||
[[https:// | [[https:// | ||
- | ==== Printing a readable version of long lists or dictionaries ==== | + | |
+ | ===== Printing a readable version of long lists or dictionaries | ||
The [[https:// | The [[https:// | ||
Line 204: | Line 241: | ||
</ | </ | ||
- | ==== Sorting ==== | + | |
+ | ===== Storing objects and data in a file (shelve and friends) ===== | ||
+ | |||
+ | The built-in [[other: | ||
+ | |||
+ | More options: | ||
+ | * Some [[other: | ||
+ | * Working with [[other: | ||
+ | |||
+ | |||
+ | ===== Using a configuration file ===== | ||
+ | |||
+ | The built-in [[https:// | ||
+ | |||
+ | Note: a configuration file is also a way to easily store and exchange text data ! | ||
+ | |||
+ | ===== Sorting | ||
* When dealing with **numerical values**, you should use the [[https:// | * When dealing with **numerical values**, you should use the [[https:// | ||
Line 221: | Line 274: | ||
[' | [' | ||
- | ==== numpy related stuff ==== | + | ===== numpy related stuff ===== |
- | === Finding and counting unique values === | + | ==== Using a numpy array to store arbitrary objects ==== |
+ | |||
+ | The numpy arrays are usually used to store [[https:// | ||
+ | |||
+ | It is also possible to store **arbitrary** Python objects in an array, rather than using nested lists or dictionaries! | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | array([[None, | ||
+ | | ||
+ | >>> | ||
+ | (2, 3) | ||
+ | >>> | ||
+ | None | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[None, | ||
+ | | ||
+ | None, None]], dtype=object)</ | ||
+ | |||
+ | |||
+ | ==== Dealing with a variable number of indices ==== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | ... | ||
+ | [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]) | ||
+ | >>> | ||
+ | (10, 10) | ||
+ | |||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0.], | ||
+ | [1., 0.], | ||
+ | [0., 1.], | ||
+ | [0., 0.]]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], | ||
+ | [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], | ||
+ | [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], | ||
+ | [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]]) | ||
+ | >>> | ||
+ | (4, 10) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | array([[-1., | ||
+ | [-1., -1.], | ||
+ | [-1., -1.], | ||
+ | [-1., -1.]]) | ||
+ | >>> | ||
+ | array([[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | [ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 1., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., -1., -1., 0., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., -1., -1., 1., 0., 0., 0.], | ||
+ | [ 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], | ||
+ | [ 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], | ||
+ | [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])</ | ||
+ | |||
+ | |||
+ | ==== Finding and counting unique values | ||
Use '' | Use '' | ||
Line 230: | Line 369: | ||
>>> | >>> | ||
array([1. , 2. , 1. , 2. , 2. , 1.5, 1. , 1.5, 2. , 1.5]) | array([1. , 2. , 1. , 2. , 2. , 1.5, 1. , 1.5, 2. , 1.5]) | ||
+ | |||
>>> | >>> | ||
array([1. , 1.5, 2. ]) | array([1. , 1.5, 2. ]) | ||
- | >>> | + | >>> |
- | (array([1. , 1.5, 2. ]), array([3, 3, 4])) | + | >>> |
- | >>> | + | array([1. , 1.5, 2. ]) |
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ])</ | array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ])</ | ||
+ | |||
+ | |||
+ | ==== Applying a ufunc over all the elements of an array ==== | ||
+ | |||
+ | There are all sorts of //ufuncs// (Universal Functions), and we will just use below '' | ||
+ | |||
+ | < | ||
+ | >>> | ||
+ | 15.5 | ||
+ | >>> | ||
+ | 15.5 | ||
+ | >>> | ||
+ | 15.5 | ||
+ | |||
+ | # Compute the sum of the elements of ' | ||
+ | # AND keep (accumulate) the intermediate results | ||
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | >>> | ||
+ | array([ 3, 6, 10]) | ||
+ | |||
+ | # The accumulated values can be used as indices to separate the different groups of sorted values! | ||
+ | >>> | ||
+ | array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ]) | ||
+ | >>> | ||
+ | array([1., 1., 1.]) | ||
+ | >>> | ||
+ | array([1.5, 1.5, 1.5]) | ||
+ | >>> | ||
+ | array([2., 2., 2., 2.]) | ||
+ | |||
+ | # Compute the sum of each equal-value group | ||
+ | >>> | ||
+ | (3.0, 4.5, 8.0)</ | ||
+ | |||
+ | |||
+ | ==== Applying a ufunc over specified sections of an array ==== | ||
+ | |||
+ | The [[https:// | ||
+ | |||
+ | < | ||
+ | # We need to add the beginning index (0), AND remove the last index | ||
+ | # (reduceat will automatically go to the end of the input array | ||
+ | >>> | ||
+ | array([3, 3, 4]) | ||
+ | >>> | ||
+ | >>> | ||
+ | 10 | ||
+ | >>> | ||
+ | [0, 3, 6] | ||
+ | |||
+ | # Compute the sums over the selected intervals with just one call | ||
+ | >>> | ||
+ | array([3. , 4.5, 8. ])</ | ||
/* | /* | ||
- | ==== Tip template ==== | + | ===== Tip template |
< | < |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter