Skip to content

Commit

Permalink
Utility functions for pruning and merging
Browse files Browse the repository at this point in the history
Co-authored-by: Esther Le Rouzic <esther.lerouzic@orange.com>
  • Loading branch information
jktjkt and EstherLerouzic committed May 31, 2019
1 parent 47a41e7 commit 166d8a3
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion gnpy/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import json

import numpy as np
from csv import writer
import numpy as np
from numpy import pi, cos, sqrt, log10
from scipy import constants

Expand Down Expand Up @@ -199,3 +199,42 @@ def rrc(ffs, baud_rate, alpha):
p_inds = np.where(np.logical_and(np.abs(ffs) > 0, np.abs(ffs) < l_lim))
hf[p_inds] = 1
return sqrt(hf)

def update_dicts_recursively(dict1, dict2):
"""Updates contents of dicts recursively
>>> d1 = {'params': {'restrictions': {'preamp_variety_list': [], 'booster_variety_list': []}}}
>>> d2 = {'params': {'target_pch_out_db': -20}}
>>> update_dicts_recursively(d1, d2)
{'params': {'restrictions': {'preamp_variety_list': [], 'booster_variety_list': []}, 'target_pch_out_db': -20}}
>>> d3 = {'params': {'restrictions': {'preamp_variety_list': ['foo'], 'booster_variety_list': ['bar']}}}
>>> update_dicts_recursively(d1, d3)
{'params': {'restrictions': {'preamp_variety_list': [], 'booster_variety_list': []}}}
"""

copy_dict1 = dict1.copy()
for key in dict2:
if key in dict1:
if isinstance(dict1[key], dict):
copy_dict1[key] = update_dicts_recursively(copy_dict1[key], dict2[key])
else:
copy_dict1[key] = dict2[key]
return copy_dict1

def silent_remove(this_list, elem):
"""Remove matching elements from a list without raising ValueError
>>> li = [0, 1]
>>> silent_remove(li, 1)
>>> li
[0]
>>> silent_remove(li, 1)
>>> li
[0]
"""

try:
this_list.remove(elem)
except ValueError:
pass

0 comments on commit 166d8a3

Please sign in to comment.