comparison configmix/config.py @ 491:de776953337b

Remove repeated type(obj) calls
author Franz Glasner <f.glasner@feldmann-mg.com>
date Fri, 17 Dec 2021 16:56:31 +0100
parents ea4b7fac02d6
children 6a0f761ff35b
comparison
equal deleted inserted replaced
490:ea4b7fac02d6 491:de776953337b
628 raise ValueError("relative refs not supported") 628 raise ValueError("relative refs not supported")
629 return self.getvar(pu.fragment, default=default) 629 return self.getvar(pu.fragment, default=default)
630 630
631 def substitute_variables_in_obj(self, obj): 631 def substitute_variables_in_obj(self, obj):
632 """Recursively expand variables in the object tree `obj`.""" 632 """Recursively expand variables in the object tree `obj`."""
633 if isinstance(obj, self._TEXTTYPE): 633 ty = type(obj)
634 if issubclass(ty, self._TEXTTYPE):
634 # a string - really replace the value 635 # a string - really replace the value
635 return self.expand_variable(obj) 636 return self.expand_variable(obj)
636 elif isinstance(obj, dict): 637 elif issubclass(ty, dict):
637 newdict = type(obj)() 638 newdict = ty()
638 for k in obj: 639 for k in obj:
639 newdict[k] = self.substitute_variables_in_obj(obj[k]) 640 newdict[k] = self.substitute_variables_in_obj(obj[k])
640 return newdict 641 return newdict
641 elif isinstance(obj, list): 642 elif issubclass(ty, list):
642 return [self.substitute_variables_in_obj(i) for i in obj] 643 return [self.substitute_variables_in_obj(i) for i in obj]
643 elif isinstance(obj, tuple): 644 elif issubclass(ty, tuple):
644 tmp = [self.substitute_variables_in_obj(i) for i in obj] 645 tmp = [self.substitute_variables_in_obj(i) for i in obj]
645 return type(obj)(tmp) 646 return ty(tmp)
646 elif isinstance(obj, set): 647 elif issubclass(ty, set):
647 newset = type(obj)() 648 newset = ty()
648 for i in obj: 649 for i in obj:
649 newset.add(self.substitute_variables_in_obj(i)) 650 newset.add(self.substitute_variables_in_obj(i))
650 else: 651 else:
651 return obj 652 return obj
652 653