Advertisement
cirossmonteiro

anchor_class

Jun 19th, 2022
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import re
  2.  
  3. class Anchor:
  4.     def __is_private_method(self, name):
  5.         patt = r'_([A-Z]{1}\w+)__(\w+)'
  6.         match = re.search(patt, name)
  7.         if match is not None:
  8.             class_name, method_name = match.group(1), match.group(2)
  9.             if class_name != 'Anchor':
  10.                 return class_name, method_name
  11.         return None, None
  12.  
  13.     def transformPrivateInPublic(self):
  14.         for attribute in dir(self):
  15.             class_name, method_name = self.__is_private_method(attribute)
  16.             if class_name is not None:
  17.                 private_function = getattr(self, f'_{class_name}__{method_name}')
  18.                 setattr(self, method_name, private_function)
  19.  
  20. def leak_private_methods(Parent):
  21.     class Child(Parent):
  22.         def __init__(self, *args, **kwargs):
  23.             super().__init__(*args, **kwargs)
  24.             self.transformPrivateInPublic()
  25.     return Child
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement