How to dynamically call a method in python? - variables

I would like to call an object method dynamically.
The variable "MethodWanted" contains the method I want to execute, the variable "ObjectToApply" contains the object.
My code so far is:
MethodWanted=".children()"
print eval(str(ObjectToApply)+MethodWanted)
But I get the following error:
exception executing script
File "<string>", line 1
<pos 164243664 childIndex: 6 lvl: 5>.children()
^
SyntaxError: invalid syntax
I also tried without str() wrapping the object, but then I get a "cant use + with str and object types" error.
When not dynamically, I can just execute this code to get the desired result:
ObjectToApply.children()
How to do that dynamically?

Methods are just attributes, so use getattr() to retrieve one dynamically:
MethodWanted = 'children'
getattr(ObjectToApply, MethodWanted)()
Note that the method name is children, not .children(). Don't confuse syntax with the name here. getattr() returns just the method object, you still need to call it (jusing ()).

Related

SQL ''Field" = {variable}' Select by attribute arcpy

I am trying to run a select by attribute where I select all points where "Id" field matches the numeric variable point_id. point_id = 375.
I've tried a few quotation styles and using curly brackets to call my variable. I'm not the most familiar with SQL queries and get an error saying the positional argument follows the keyword string. I have also tried storing my SQL as a variable on it's own called a whereClause and get the same error.
First attempt code
arcpy.management.SelectLayerByAttribute(in_layer_or_view = deer,
selection_type = "NEW_SELECTION",
f'"Id"={point_id}')
Second attempt code
The is a Python issue, not related to ArcGIS or SQL.
You are trying to pass three arguments. For the first two arguments you're using keyword argument (explicitly specifying the argument name: in_layer_or_view = deer), but for the third one you're using positional argument (letting python assign the value to the appropriate argument based on the order of the arguments).
The execption you're getting is telling you that you can't mix the two types this way. Once you started using keyword arguments in the function call, all of the next argument must be passed with their explicit name too.
To fix this, you can use positional argument for all of the arguments (i.e. not specifing argument names at all), or alternatively keep specifing the names for all of the rest of the arguments.
In your case, this should work:
arcpy.management.SelectLayerByAttribute(in_layer_or_view=deer,
selection_type="NEW_SELECTION",
where_clause=f'"Id"={point_id}')
or alternatively:
arcpy.management.SelectLayerByAttribute(deer,
"NEW_SELECTION",
f'"Id"={point_id}')

var error = new Error(message); Why am I getting this message in the terminal?

Everything works effectively until I add the const amountOut to the file.
Code with Error & location of function that breaks code
You are passing 2 parameters into a function that is expecting 3. The second parameter is one value because you have it as an array in brackets - [AddressFrom, AddressTo].
You will need to either modify how you are passing in the parameters to the getAmountsOut function or modify the getAmountsOut function to accept 2 parameters, with one being an array.

Declaring dynamic call of BAdi?

I want to define an object for BAdI implementation that will not initialize the BAdI by its name in declaration.
Example of what I don't want:
DATA l_split_badi TYPE REF TO fieb_get_bank_stmts_x.
Question 1 : I want something like:
DATA l_split_badi TYPE REF TO object.
lv_class_name = 'fieb_get_bank_stmts_x'.
create object l_split_badi type (lv_class_name).
If I declare like above, I get the following syntax error:
"L_SPLIT_BADI" is not a valid BAdI handle here.
The reason why I need to perform such implementation is, while importing the Change request to a system which has an older SAP version, the import fails because of BAdI declaration with TYPE REF TO (because that BAdI doesn't exist in the system).
My idea is with dynamic declaration to avoid the pre-check on importing the Change request.
Any idea is welcome !
Thanks to all !
EDIT Question 2 : after solution proposed by Sandra Rossi to use DATA l_split_badi TYPE REF TO cl_badi_base and GET BADI l_split_badi TYPE ('FIEB_GET_BANK_STMTS_X'), I get the same syntax error at line CALL BADI l_split_badi->split below:
CALL BADI l_split_badi->split
EXPORTING
i_string = lv_cont
IMPORTING
et_string = lt_xml_string
EXCEPTIONS
split_not_possible = 1
wrong_format = 2.
Question 1
For BAdIs which are part of an Enhancement Spot (display the BAdI via the transaction code SE18 and you will know), you must not use CREATE OBJECT, but instead GET BADI which has a dynamic variant:
DATA badi TYPE REF TO cl_badi_base.
TRY.
GET BADI badi TYPE ('FIEB_GET_BANK_STMTS_X')...
CATCH cx_badi_unknown_error INTO DATA(lx).
" The BAdI doesn't exist, handle the case...
ENDTRY.
EDIT: notice that the instance declaration is referencing CL_BADI_BASE, the super class of all BAdI definitions.
Question 2
Calling the method SPLIT statically is invalid because SPLIT doesn't exist in CL_BADI_BASE. You must use the dynamic variant of CALL BADI:
CALL BADI l_split_badi->('SPLIT')
EXPORTING
i_string = lv_cont
IMPORTING
et_string = lt_xml_string
EXCEPTIONS
split_not_possible = 1
wrong_format = 2.

TypeError: Cannot read property 'splice' of undefined- Angular 5

I am trying to splice object from an array of list of object, passing an object index as the first argument splice(index, 1), but it results in the following error:
Cannot read property 'splice' of undefined.
this.MypostList is somehow evaluated empty in your code. So splice could not be executed on the empty object. Please put the code where you tried to evaluate this.MypostList

How to find an element with its value property (which is created durin runtime)

different ways I used to click on that element are,
driver.findElement(By.xpath(".//*[#value=.,"+NewStateName+"]")).click();
driver.findElement(By.cssSelector(".//*[#value=.,'"+NewStateName+"']")).click();
driver.findElement(By.value(NewStateName));
driver.findElement(By.linkText(NewStateName));
driver.findElement(By.xpath("//span[contains(.,"+NewStateName+")]"));
In above code 'NewStstaeName is a string variable for which the value is dynamically generated during run time.
Error returned:
org.openqa.selenium.InvalidSelectorException: The given selector .//*[#value=.,NEWSTATEABC21M] is either invalid or does not result in a WebElement. The following error occurred:
I think you'll need quotes around the value of NewStateName, try these:
driver.findElement(By.xpath(".//*[#value=.,'"+NewStateName+"']")).click();
driver.findElement(By.xpath("//span[contains(.,'"+NewStateName+"')]"));
The resulting selector will then look like:
.//*[#value=.,'NEWSTATEABC21M']