I am using Python, and I get a definition from a REST endpoint of the parameters here:
https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons?f=json
What I want to do is the following:
OnlineMethod(above_url).CreateDriveTimePolygons(Input_Location=(25,-34), Drive_Times="5,12,31")
I can use setattr
on the obj to define a pre-made function, but my question is the following:
Thank you
The goal is to not use kwargs or args
Two ways: Either use a variable number of arguments using * on the parameter or treat the parameter as an iterable. Your first function, using a named argument followed by a variable number of positional arguments, is VERY confusing.
Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.
A keyword argument is where you provide a name to the variable as you pass it into the function. One can think of the kwargs as being a dictionary that maps each keyword to the value that we pass alongside it. That is why when we iterate over the kwargs there doesn't seem to be any order in which they were printed out.
A Python function will always have a return value. There is no notion of procedure or routine in Python. So, if you don't explicitly use a return value in a return statement, or if you totally omit the return statement, then Python will implicitly return a default value for you.
You can always use a hammer to fix a microscope eval
or exec
to dynamically write Python code. But in this case you'll have a lot more responsibility to prevent bugs.
class A:
def __init__(self):
pass
A.some_method = lambda self, x: print(x ** 2)
a = A()
a.some_method(20) # 400
args = ['x', 'y', 'z']
A.new_method = eval(f'lambda self, {", ".join(args)}: print(sum([{", ".join(args)}]))')
a.new_method(1, 2, 3) # 6
I think I've solved a similar kind of problem. When I was working on API versioning
like if user agent request contains v1 in URL params then initialize the v1 APIs/ manager classes or v2 and so on.
For this, we can use partial implementation on the adapter pattern by creating a map for a set of classes/methods in your case.
class OnlineMethod(object):
...
def __init__(self, url):
pass
def create_drive_time_polygons(self, *args, **kwargs):
pass
...
action_method_map = {
'CreateDriveTimePolygons': OnlineMethod().create_drive_time_polygons,
}
action_method_map['CreateDriveTimePolygons'](Input_Location=(25,-34), Drive_Times="5,12,31", Output_Drive_Time_Polygons=[1,2,3])
action_method_map[action_from_url](Input_Location, Drive_Times, Output_Drive_Time_Polygons)
Does that make sense?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With