You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Solution without using CommandManager, maybe better and more maintanable when do_* commands are registered in the ActionManager instead of the respective classes. greet i.e. could still call another class
defprecmd(self, line):
# Add some methods based on conditionifsome_condition:
setattr(self, 'do_bye', self.bye)
setattr(self, 'do_greet', self.greet)
returnlinedefpostcmd(self, stop, line):
# Remove some methods based on conditionifnotsome_condition:
delattr(self, 'do_bye')
delattr(self, 'do_greet')
returnstopdefbye(self, arg):
# Do something herepassdefgreet(self, arg):
# Do something herepass
Another Idea:
importtypesimportcmdclassMyCmd(cmd.Cmd):
passdefadd_do_methods(obj):
ifsome_condition:
defdo_bye(self, arg):
# Do something herepassdefdo_greet(self, arg):
# Do something herepassdo_methods= {'do_bye': do_bye, 'do_greet': do_greet}
new_class_dict=dict(obj.__class__.__dict__)
new_class_dict.update(do_methods)
new_class=types.new_class(obj.__class__.__name__, (obj.__class__,), {}, lambdans: ns.update(new_class_dict))
obj.__class__=new_classdefremove_do_methods(obj):
ifnotsome_condition:
new_class=types.new_class(obj.__class__.__name__, (obj.__class__,), {})
new_class_dict=dict(new_class.__dict__)
formethod_namein ['do_bye', 'do_greet']:
ifmethod_nameinnew_class_dict:
delnew_class_dict[method_name]
new_class=types.new_class(obj.__class__.__name__, (obj.__class__,), {}, lambdans: ns.update(new_class_dict))
obj.__class__=new_class# Create an instance of MyCmdmy_cmd=MyCmd()
# Add the do_* methodsadd_do_methods(my_cmd)
# Call the methodsmy_cmd.do_bye('arg')
my_cmd.do_greet('arg')
# Remove the do_* methodsremove_do_methods(my_cmd)
In this example, the add_do_methods() function adds the do_bye() and do_greet() methods to the class of the specified object if the some_condition is true. It does this by creating a new dictionary that includes the existing class dictionary as well as the new methods, and then creating a new class that derives from the existing class with the updated dictionary. It then sets the class of the specified object to the new class.
The remove_do_methods() function does the opposite, removing the do_bye() and do_greet() methods from the class of the specified object if the some_condition is false. It does this by creating a new dictionary that includes the existing class dictionary with the methods removed, and then creating a new class that derives from the existing class with the updated dictionary. It then sets the class of the specified object to the new class.
By using dynamic class creation and setting the class of the object to the new class, you can add and remove methods from an existing class at runtime.
The text was updated successfully, but these errors were encountered:
miron
changed the title
Solution without using CommandManager, maybe better and more maintanable when do_* commands are registered in the ActionManager instead of the respective classes. greet i.e. could still call another class
without CommandManager
Mar 14, 2023
Solution without using CommandManager, maybe better and more maintanable when do_* commands are registered in the ActionManager instead of the respective classes. greet i.e. could still call another class
Another Idea:
In this example, the add_do_methods() function adds the do_bye() and do_greet() methods to the class of the specified object if the some_condition is true. It does this by creating a new dictionary that includes the existing class dictionary as well as the new methods, and then creating a new class that derives from the existing class with the updated dictionary. It then sets the class of the specified object to the new class.
The remove_do_methods() function does the opposite, removing the do_bye() and do_greet() methods from the class of the specified object if the some_condition is false. It does this by creating a new dictionary that includes the existing class dictionary with the methods removed, and then creating a new class that derives from the existing class with the updated dictionary. It then sets the class of the specified object to the new class.
By using dynamic class creation and setting the class of the object to the new class, you can add and remove methods from an existing class at runtime.
Originally posted by @miron in #10 (comment)
The text was updated successfully, but these errors were encountered: