Dictionaries

List of available functions

Examples

A few examples are given below

import wilfried.utilities.dictionaries as dic

data    = {'a':1, 'b':2, 'c':3}
keys    = ['a', 'b', 'c', 'e']
default = [3, 2, 1, 5]

# Generate new dictionary with default values if they do not exist yet
new_data = dic.setDict(data, keys=keys, default=default)
print(new_data)

# Make sure that all the above keys exist in the dictionary
dic.checkInDict(new_data, keys=['a', 'b', 'c', 'e'], dictName='new_data')

# Remove keys 'a' and 'c'
small_data = dic.removeKeys(new_data, keys=['a', 'c'])
print(small_data)

Documentation

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Functions acting on dictionaries.

utilities.dictionaries.checkDictKeys(dictionary, keys=[], dictName='NOT PROVIDED')[source]

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Check that all the dictionary keys are among the list keys.

Parameters
  • dictionary (dict) – dictionary from which we want to test if given keys are in keys list

  • dictName (str) – (Optional) dictionary variable name printed in the error message

  • keys (list[str]) – (Optional) list of authorised key names

Returns

None if dictionary keys are all in keys list

Raises

KeyError – if one of the keys is not in keys list

utilities.dictionaries.checkInDict(dictionary, keys=[], dictName='NOT PROVIDED')[source]

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Check that the given keys exist in the dictionary.

Parameters
  • dictionary (dict) – dictionary from which we want to test if given keys already exist

  • dictName (str) – (Optional) dictionary variable name printed in the error message

  • keys (list[str]) – (Optional) key names we want to test if they exist in dictionary

Returns

None if keys exist

Raises

KeyError – if one of the keys is missing in the dictionary

utilities.dictionaries.concatenateDictValues(myDicts, astropyTable=True)[source]

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Take a dictionary containing either numpy arrays or numpy structured arrays as values and concatenate them.

Warning

NOT WORKING PROPERLY !

Note

  • If working with structured arrays/astropy tables, be careful to have the same column names/fields, otherwise the concatenation shall still produce a result which may be different from what would be expected.

  • When combining a standard array with a masked one, a new mask (with False values everywhere) will be concatenated to the previous one so that the masks will be preserved.

Parameters
  • myDicts (dict or list of dicts) – either a single dictionary containing numpy arrays or a list of dictionnaries (numpy arrays as values as well)

  • astropyTable (bool) – (Optional) whether to return an astropy table or not. If False, it will return a numpy masked array.

Returns

table where each numpy array in the dictionary has been concatenated to the others, or a list of tables

Return type

masked ndarray or astropy Table of list[masked ndarray] or list[astropy Table]

Raises
  • ValueError – if myDicts is an empty list

  • TypeError

    • if at least one element in myDicts is not a dict

    • if myDicts is not a list

utilities.dictionaries.removeKeys(dictionary, keys=[])[source]

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Creates a new dictionary with given keys removed.

Parameters
  • dictionary (dict) – dictionary from which the keys are removed

  • keys (list[str]) – (Optional) keys to remove from the dictionary

Returns

new dictionary with given keys removed

Return type

dict

utilities.dictionaries.setDict(dictionary, keys=[], default=[])[source]

Code author: Wilfried Mercier - IRAP <wilfried.mercier@irap.omp.eu>

Create a new dictionary whose keys are either retrieved from a dictionary, or set to default values if not present in the dictionary.

Parameters
  • dictionary (dict) – dictionary from which the values in keys list are retrieved

  • default (list) – (Optional) list of default values for keys listed in keys if no value is found in dictionary

  • keys (list[str]) – (Optional) list of key names which should be retrieved either from dictionary or from default

Returns

new dictionary with keys listed in keys having values either from dictionary or from default

Return type

dict