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/03/08 17:40] – Added a string section jypeter | other:python:misc_by_jyp [2023/03/28 16:00] – Added a matplotlib section 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 39: | Line 40: | ||
True</ | True</ | ||
- | ==== Playing with strings ==== | ||
- | === Filenames, etc === | + | ===== Playing with strings ===== |
- | Check [[other: | ||
- | === Splitting strings === | + | ==== Splitting |
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 | 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 | ||
Line 64: | Line 63: | ||
>>> | >>> | ||
[' | [' | ||
+ | |||
+ | |||
==== Working with paths and filenames ==== | ==== Working with paths and filenames ==== | ||
Line 149: | Line 150: | ||
>>> | >>> | ||
>>> | >>> | ||
- | ==== 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 173: | Line 176: | ||
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 191: | Line 198: | ||
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 229: | Line 238: | ||
</ | </ | ||
- | ==== 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 ! | ||
+ | |||
+ | |||
+ | ===== Working with global variables ===== | ||
+ | |||
+ | There is a good chance you don't actually want/need a //global// variable. Be sure to use the '' | ||
+ | |||
+ | * [[https:// | ||
+ | * Simple module example\\ < | ||
+ | |||
+ | def set_myvar(new_val): | ||
+ | # Note: need to explicitly define a global variable (of a module) | ||
+ | # as ' | ||
+ | # Otherwise, the value will not be REdefined outside the function | ||
+ | global _myvar | ||
+ | _myvar = new_val | ||
+ | |||
+ | def get_myvar(): | ||
+ | return _myvar | ||
+ | |||
+ | def myfunc(nb_repeat = 10): | ||
+ | print(nb_repeat * _myvar)</ | ||
+ | * [[https:// | ||
+ | ===== Sorting | ||
* When dealing with **numerical values**, you should use the [[https:// | * When dealing with **numerical values**, you should use the [[https:// | ||
Line 246: | Line 292: | ||
[' | [' | ||
- | ==== 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 268: | Line 400: | ||
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 === | + | |
+ | ==== Applying a ufunc over all the elements of an array ==== | ||
There are all sorts of //ufuncs// (Universal Functions), and we will just use below '' | There are all sorts of //ufuncs// (Universal Functions), and we will just use below '' | ||
Line 301: | Line 434: | ||
(3.0, 4.5, 8.0)</ | (3.0, 4.5, 8.0)</ | ||
- | === Applying a ufunc over specified sections of an array === | + | |
+ | ==== Applying a ufunc over specified sections of an array ==== | ||
The [[https:// | The [[https:// | ||
Line 319: | Line 453: | ||
>>> | >>> | ||
array([3. , 4.5, 8. ])</ | array([3. , 4.5, 8. ])</ | ||
+ | |||
+ | |||
+ | ===== matplotlib related stuff ===== | ||
+ | |||
+ | |||
/* | /* | ||
- | ==== Tip template ==== | + | ===== Tip template |
< | < |
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter