# External API functions that we can't modify def some_api_func_1(arg_val_1): print('======================') print(arg_val_1) def some_api_func_2(arg_val_3, arg_val_6): print('======================') print(arg_val_3) print(arg_val_6) # Wrapper Implementation def get_arg_map(): """ Maybe a helper function with params to help you build your external API arg map """ return { 'arg_val_1': 1, 'arg_val_2': 2, 'arg_val_3': 3, 'arg_val_4': 4, 'arg_val_5': 5, 'arg_val_6': 6, 'arg_val_7': 7, } def api_wrapper(func_type): """ Wrapper function that organizes the external API functions and arguments. Benefits of this implementation are: - You can generate your args in a separate function - You have a declarative view of the external API (declare function name and args explicitly) - You can add new external API definitions easily to api_map below @param string func_type: key values for api_map below @param dict arg_map: stores key-values of all external API arg values. """ arg_map = get_arg_map() api_map = { 'api_1': { 'function': some_api_func_1, 'args': [ arg_map['arg_val_1'], ], }, 'api_2': { 'function': some_api_func_2, 'args': [ arg_map['arg_val_3'], arg_map['arg_val_6'], ], }, } api_func = api_map[func_type] # You can also set the above api_map['args'] as a dict and unpack below using # **api_func['args'] to ignore position entirely. return api_func['function'](*api_func['args']) # Test api_wrapper('api_1') # 1 api_wrapper('api_2') # 3, 6 # Notes # This isn't the best way to do it. In general, writing wrappers to abstract things too much isn't that good either # since there is an overhead in understanding the wrapper. It maybe worthwhile to just write this api_wrapper as a # APIWrapper class where you can organize things better. # In bigger projects, we actually prefer to be more explicit and call individual functions. Accessing functions through # many layers makes debugging hard, and it's easier to catch errors on individual functions that throw them. # If you are trying to organize external libraries methods, I recommend making a class and do something like this below # import the external API methods e.g. some_api_func_1, some_api_func_2 class APIWrapper(): def __init__(self): pass def api_func_1(self, arg1): # you can add more functionality here but I'm just returning the exact behavior using the original function return some_api_func_1(arg1) def api_func_2(self, arg3, arg6): # you can add more functionality here but I'm just returning the exact behavior using the original function return some_api_func_1(arg3, arg6) def other_helper_utility_methods_for_class(self): pass # I prefer this approach since it's well-documented, and it's just importing external methods almost like importing a library, and you # organize your work in this layer. It would also allow people to call individual functions than going through the wrapper layer. # TLDR wrappers are nice but don't overuse them. I think in your case you maybe needing it for something small so go with it, but it will # end up being more work and overhead to understand them instead of just calling something atomically e.g. APIWrapper.api_func_2(arg3, arg6)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question