Get argument from different method - python

I am passing object as follows
test = TestClass(parameter1=''first_name', parameter2='last_name')
And in another method I am accessing parameter1 as
print test.parameter1
I get output as first_name
But if I have variable which contains value assigned to parameter1. Can I use variable to get value?
var = 'parameter1'
What's the right way to perform like test.var? For this I get attribution error.
What's the programming word for 'parameter1', is it options, argument options?

Guess you need getattr function:
getattr(test, var)

Related

Retrieve the name of an instance of DataFrame, passed as an argument to the function

I am looking to retrieve the name of an instance of DataFrame, that I pass as an argument to my function, to be able to use this name in the execution of the function.
Example in a script:
display(df_on_step_42)
I would like to retrieve the string "df_on_step_42" to use in the execution of the display function (that display the content of the DataFrame).
As a last resort, I can pass as argument of DataFrame and its name:
display(df_on_step_42, "df_on_step_42")
But I would prefer to do without this second argument.
PySpark DataFrames are non-transformable, so in our data pipeline, we cannot systematically put a name attribute to all the new DataFrames that come from other DataFrames.
You can use the globals() dictionary to search for your variable by matching it using eval.
As #juanpa.arrivillaga mentions, this is fundamentally bad design, but if you need to, here is one way to do this inspired by this old SO answer for python2 -
import pandas as pd
df_on_step_42 = pd.DataFrame()
def get_var_name(var):
for k in globals().keys():
try:
if eval(k) is var:
return k
except:
pass
get_var_name(df_on_step_42)
'df_on_step_42'
Your display would then look like -
display(df_on_step_42, get_var_name(df_on_step_42))
Caution
This will fail for views of variables since they are just pointing to the memory of the original variable. This means that the original variable occurs first in the global dictionary during an iteration of the keys, it will return the name of the original variable.
a = 123
b = a
get_var_name(b)
'a'
I finally found a solution to my problem using the inspect and re libraries.
I use the following lines which correspond to the use of the display() function
import inspect
import again
def display(df):
frame = inspect.getouterframes(inspect.currentframe())[1]
name = re.match("\s*(\S*).display", frame.code_context[0])[1]
print(name)
display(df_on_step_42)
The inspect library allows me to get the call context of the function, in this context, the code_context attribute gives me the text of the line where the function is called, and finally the regex library allows me to isolate the name of the dataframe given as parameter.
It’s not optimal but it works.

Can I pass a undefined variable parameter in python?

I wish to change a string like function.
let my function string is like 'x**2+2*x+1'.but I want to change x by r.
And I don't want to use x as a str during this replacement.
To do this I make a method whose parameter are formula and vav i.e., variable
So, I tried This:
def ModifyFormula(formula,vav):
vav=str(vav)
san_vav='r'
formula=formula.replace(vav,san_vav)
return formula
ModifyFormula('x**2+2*x+1',x)
But I got NameError: name 'x' is not defined, It is pretty obvious that is a error due to a undefined in last line of the code .
How I can approach the goal to 'r**2+2*r+1' without using x as str ?
It's not possible.
The last line will always be decoded.

How can I call a method from a name string in Python?

I am using an API to call specific information from a website. I need to be able to parse through the list to utilize the functions. Example:
list = ['doThis','doThat']
for item in list:
sampleobject.item
The issue is when I use this, I get an error saying "sampleobject has no attribute 'item'".
Is there a way that I can pull the quote out of the string to do this?
Try:
methods = ['doThis','doThat']
for method_name in methods:
method = getattr(sampleobject, method_name)
method()
Though it would be easier to do:
sampleobject.doThis()
sampleobject.doThat()
You can call getattr(sampleobject, item) to get the content of a property with the name equal to what is stored in item, which is an element from your list.
I think the problem is not about quotes at all. The problem is that syntax object.member means: evaluate a property named member that is stored in a variable named object. And you expect it to mean: evaluated a property with the name stored in member.

How to acces to a class variable with a string?

I have the following code:
import Parameters
def ReadOptionFromParametersFile(self,variable_name):
if (hasattr(Parameters, str(variable_name))):
return Parameters.variable_name
else:
return 0
I want to call this function for different variables. In case the variable is defined on that Parameter file i want to read the value doing Parameters.variable_name. The problem is obviously that the file has no variable called variable_name.
I also tried:
Parameters.str(variable_name)
or
Parameters.eval(variable_name)
You can use getattr to access an attribute by its string name:
return getattr(Parameters, variable_name)
In fact, your entire method can be refactored to just:
def ReadOptionFromParametersFile(self, variable_name):
return getattr(Parameters, variable_name, 0)
The third argument to getattr is an optional default value to return if the attribute is not found.

How to pass extra arguments to callback register functions with twisted python api?

I have the following python code using the twisted API.
def function(self,filename):
def results(result):
//do something
for i in range(int(numbers)) :
name = something that has to do with the value of i
df = function_which_returns_a defer(name)
df.addCallback(results)
It uses the Twisted API. What i want to achieve is to pass to the callbacked function (results) the value of the name which is constructed in every iteration without changing the content of the functions_which_returns_a defer() function along with the deferred object of course. In every result of the functions_which_returns_a deffer the value of the name should be passed to results() to do something with this. I.e: at the first iteration when the execution reach the results function i need the function hold the result of the deffered object along with the value of name when i=0,then when i=1 the defered object will be passed with the value of name, and so on.So i need every time the result of the defer object when called with the name variable alond with the name variable. When i try to directly use the value of nameinside results() it holds always the value of the last iteration which is rationale, since function_which_returns_a defer(name) has not returned.
You can pass extra arguments to a Deferred callback at the Deferred.addCallback call site by simply passing those arguments to Deferred.addCallback:
def function(self,filename):
def results(result, name):
# do something
for i in range(int(numbers)) :
name = something that has to do with the value of i
df = function_which_returns_a defer(name)
df.addCallback(results, name)
You can also pass arguments by keyword:
df.addCallback(results, name=name)
All arguments passed like this to addCallback (or addErrback) are passed on to the callback function.

Categories

Resources