JabberJabber
JabberJabber

Reputation: 383

Dynamically Modify Python Method and Arguments

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:

  1. Create a function called: CreateDriveTimePolygons
  2. Add the input parameters
  3. reference the function within the class that can handle these inputs.
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:

  1. How can I change the signature name of the method?
  2. How can I modify the input parameters of the method?

Thank you

The goal is to not use kwargs or args

Upvotes: 6

Views: 2407

Answers (4)

Mykola Semenov
Mykola Semenov

Reputation: 802

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

Upvotes: 2

Bhushan
Bhushan

Reputation: 2364

I think you might want to try Metaclass in Python.

https://realpython.com/python-metaclasses/#defining-a-class-dynamically

Upvotes: 0

Premkumar chalmeti
Premkumar chalmeti

Reputation: 1018

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?

Upvotes: 1

Aryan
Aryan

Reputation: 29

For point 2,If by modify you mean passing a variable number of arguements/parameters , you can refer this link :

https://www.geeksforgeeks.org/args-kwargs-python/

If by modify you mean change the values passed in the function, then you could just store the values in variables and pass those variables as arguements/parameters in the method.

Upvotes: 0

Related Questions