0

This is probably has a very solution but I am struggling with this a bit. I have 3 almost similar functions I'd like to refactor into one.

Here's the function:

def delete_labels_replication_controller(provider, obj_type):
    obj_label_list = []
    obj_type_list = provider.mgmt.list_replication_controller()

def delete_labels_node(provider, obj_type):
    obj_label_list = []
    obj_type_list = provider.mgmt.list_node()

def delete_labels_image(provider, obj_type):
    obj_label_list = []
    obj_type_list = provider.mgmt.list_image()

Now, as you see, the only thing that changes is the provider.mgmt.xxx class and it changes according to the obj_type I want to pass into the function. Question is how to use only 1 function for all of them and replace only the .xxx part of the object? Thanks!

2
  • Not stated in the question, but am I right to assume that there's coming more after those two lines? Because otherwise, none of those three functions is doing anything at all. Commented Jul 3, 2017 at 14:32
  • Yes, there's more. I just leave what's relevant to my question Commented Jul 3, 2017 at 14:32

3 Answers 3

1

I am sure there is a better solution but maybe using getattr like this?

def delete_labels_by_type(obj_type):
    obj_type_list = getattr(provider.mgmt, obj_type)()     
    return obj_type_list
Sign up to request clarification or add additional context in comments.

Comments

0

Do like this

def myFunction(provider, obj_type, flag_type)
    obj_label_list = []
    if flag_type == 1:
        obj_type_list = provider.mgmt.list_replication_controller()
    elif flag_type == 2:
        obj_type_list = provider.mgmt.list_node()
    elif flag_type == 3:
        obj_type_list = provider.mgmt.list_image()
    

1 Comment

DONT "do like this", there is a way better solution.
0

You could just pass the obj_type_list as another parameter to the function:

def delete_labels(provider, obj_type, obj_type_list):
    obj_label_list = []
    ...

and then call like this delete_labels(provider, obj_type, provider.mgmt.list_replication_controller()) (and if that's the only time you need the provider parameter, you can remove it entirely)

Or pass a reference to the getter method:

def delete_labels(provider, obj_type, getter):
    obj_label_list = []
    obj_type_list = getter(provider.mgmt)
    ...

and then call like this: delete_labels(provider, obj_type, MgmtClass.list_replication_controller)

In both cases, you can move the bulk of the three functions (the common part not shown in the question) into that new function and keep the original functions for easier usage:

def delete_labels_replication_controller(provider, obj_type):
    return delete_labels(provider, obj_type, provider.mgmt.list_replication_controller())

1 Comment

Personally, I would use the first variant to reduce code duplication and combine it with the third for easier usage in the remaining code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.