Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 687
JabberJabber Avatar asked Apr 17 '20 17:04

JabberJabber


People also ask

How do you pass a dynamic argument in Python?

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.

How to dynamically create function in Python?

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.

What are keyword arguments in Python?

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.

Do Python functions need a return?

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.


Video Answer


2 Answers

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
like image 194
nik7 Avatar answered Oct 17 '22 18:10

nik7


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?

like image 28
Premkumar chalmeti Avatar answered Oct 17 '22 17:10

Premkumar chalmeti