Skip to content

Instantly share code, notes, and snippets.

@kirankotari
Created July 3, 2021 09:39
Show Gist options
  • Save kirankotari/3d51c4956e33b5f811fea7f708fae634 to your computer and use it in GitHub Desktop.
Save kirankotari/3d51c4956e33b5f811fea7f708fae634 to your computer and use it in GitHub Desktop.
Class Decorators
class Decorator:
profile = None
workspace_id = None
def __init__(self, profile="default"):
self.profile = profile
def make_pretty(func):
def inner(self):
print("I got decorated")
func(self, self.profile)
return inner
def my_decorator(var=None, create=True, delete=True):
def wrap(func):
def wrapped_f(self, *args, **kwargs):
if getattr(self, var) is None:
self.workspace_id = self.get_id()
if self.profile is None or kwargs.get("set_profile", False):
kwargs['set_profile'] = True
kwargs['profile'] = kwargs.get('profile', 'default')
func(self, *args, **kwargs)
kwargs['set_profile'] = False
#profile = getattr(self, profile)
profile = self.profile
if create:
print(f"profile: {profile} created")
print(f"workspace id: {self.workspace_id}")
func(self, *args, **kwargs)
if delete:
print(f"profile {profile} deleted")
print(f"workspace id: {self.workspace_id}")
return wrapped_f
return wrap
def get_id(self):
return "A1B2C3"
@make_pretty
def ordinary(self, profile):
print("I am ordinary")
print(f"profile={profile}")
@my_decorator("workspace_id")
def workspace(self, profile="default", set_profile=False):
if set_profile:
print("I am setting profile")
self.profile = profile
return
print("I am in workspace")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment