如何编写for循环以动态调用函数中的值?
How to write a for loop to dynamically call values in a function?
我有几个 table 想要 运行 我的 python 功能;但是我很难写我的脚本来做到这一点。
这是我的 table 变量:
object_to_export = ['Opportunity','Asset']
这是我的代码:
object_to_export = ['Opportunity','Asset']
def sf_login():
"""
Purpose: This function is designed to validate your Salesforce credentials and create/return an instance or session.
Note: If we have already signed in, this will just return the original object.
Note: The credentials are being held inside of the config.py file.
"""
session_id, instance = SalesforceLogin(username=config.username,
password=config.password,
security_token=config.security_token)
sf = Salesforce(instance=instance,
session_id=session_id)
return sf
def sf_object_extract():
"""
Purpose: This function is designed to extract data from Salesforce objects and return the results.
Note: For sample sake, I have set the limit to 1000 records per object.
"""
dd = sf_login()
set_limit = 1000
for sf_obj in object_to_export:
desc = dd.sf_obj.describe()
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM {} limit {}".format(','.join(field_names), sf_obj, set_limit)
results = dd.query_all(soql)
return results
sf_object_extract()
当我 运行 我的代码收到此错误时:
Traceback (most recent call last):
File "/Users/m/PycharmProjects/test/sf_etl_hw_aaa_v1.py", line 43, in <module>
sf_object_extract()
File "/Users/m/PycharmProjects/test/sf_etl_hw_aaa_v1.py", line 37, in sf_object_extract
desc = dd.sf_obj.describe()
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/api.py", line 565, in describe
headers=headers
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/api.py", line 771, in _call_salesforce
exception_handler(result, self.name)
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/util.py", line 68, in exception_handler
raise exc_cls(result.url, result.status_code, name, response_content)
simple_salesforce.exceptions.SalesforceResourceNotFound: Resource sf_obj Not Found. Response content: [{'errorCode': 'NOT_FOUND', 'message': 'The requested resource does not exist'}]
知道为什么我会收到此错误并修复我的脚本以将 table 参数传递给我的 desc
和 soql
变量吗?
补充:我还尝试将 desc 变量设置为
desc = int("dd.{}.describe()".format(sf_obj))
但是它给我这个错误:
desc = int("dd.{}.describe()".format(sf_obj))
ValueError: invalid literal for int() with base 10: 'dd.Opportunity.describe()'
object_to_export = ['Opportunity','Asset']
def sf_login():
"""
Purpose: This function is designed to validate your Salesforce credentials and
create/return an instance or session.
Note: If we have already signed in, this will just return the original object.
Note: The credentials are being held inside of the config.py file.
"""
session_id, instance = SalesforceLogin(username=config.username,
password=config.password,
security_token=config.security_token)
sf = Salesforce(instance=instance,
session_id=session_id)
return sf
def sf_object_extract():
"""
Purpose: This function is designed to extract data from Salesforce objects and
return the results.
Note: For sample sake, I have set the limit to 1000 records per object.
"""
dd = sf_login()
set_limit = 1000
for sf_obj in object_to_export:
desc = getattr(dd, sf_obj).describe() #dd.sf_obj.describe()
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM {} limit {}".format(','.join(field_names), sf_obj,
set_limit)
results = dd.query_all(soql)
return results
sf_object_extract()
你需要使用 getattr(obj, name_of_member) 查看注释行
和示例:
class d():
def __init__(self):
self.x = 0
q = d()
#to get the value of 'x' member of q you can do 2 things
x_value = getattr(q, 'x')
x_value = q.x
我有几个 table 想要 运行 我的 python 功能;但是我很难写我的脚本来做到这一点。
这是我的 table 变量:
object_to_export = ['Opportunity','Asset']
这是我的代码:
object_to_export = ['Opportunity','Asset']
def sf_login():
"""
Purpose: This function is designed to validate your Salesforce credentials and create/return an instance or session.
Note: If we have already signed in, this will just return the original object.
Note: The credentials are being held inside of the config.py file.
"""
session_id, instance = SalesforceLogin(username=config.username,
password=config.password,
security_token=config.security_token)
sf = Salesforce(instance=instance,
session_id=session_id)
return sf
def sf_object_extract():
"""
Purpose: This function is designed to extract data from Salesforce objects and return the results.
Note: For sample sake, I have set the limit to 1000 records per object.
"""
dd = sf_login()
set_limit = 1000
for sf_obj in object_to_export:
desc = dd.sf_obj.describe()
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM {} limit {}".format(','.join(field_names), sf_obj, set_limit)
results = dd.query_all(soql)
return results
sf_object_extract()
当我 运行 我的代码收到此错误时:
Traceback (most recent call last):
File "/Users/m/PycharmProjects/test/sf_etl_hw_aaa_v1.py", line 43, in <module>
sf_object_extract()
File "/Users/m/PycharmProjects/test/sf_etl_hw_aaa_v1.py", line 37, in sf_object_extract
desc = dd.sf_obj.describe()
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/api.py", line 565, in describe
headers=headers
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/api.py", line 771, in _call_salesforce
exception_handler(result, self.name)
File "/Users/m/venv/lib/python3.7/site-packages/simple_salesforce/util.py", line 68, in exception_handler
raise exc_cls(result.url, result.status_code, name, response_content)
simple_salesforce.exceptions.SalesforceResourceNotFound: Resource sf_obj Not Found. Response content: [{'errorCode': 'NOT_FOUND', 'message': 'The requested resource does not exist'}]
知道为什么我会收到此错误并修复我的脚本以将 table 参数传递给我的 desc
和 soql
变量吗?
补充:我还尝试将 desc 变量设置为
desc = int("dd.{}.describe()".format(sf_obj))
但是它给我这个错误:
desc = int("dd.{}.describe()".format(sf_obj))
ValueError: invalid literal for int() with base 10: 'dd.Opportunity.describe()'
object_to_export = ['Opportunity','Asset']
def sf_login():
"""
Purpose: This function is designed to validate your Salesforce credentials and
create/return an instance or session.
Note: If we have already signed in, this will just return the original object.
Note: The credentials are being held inside of the config.py file.
"""
session_id, instance = SalesforceLogin(username=config.username,
password=config.password,
security_token=config.security_token)
sf = Salesforce(instance=instance,
session_id=session_id)
return sf
def sf_object_extract():
"""
Purpose: This function is designed to extract data from Salesforce objects and
return the results.
Note: For sample sake, I have set the limit to 1000 records per object.
"""
dd = sf_login()
set_limit = 1000
for sf_obj in object_to_export:
desc = getattr(dd, sf_obj).describe() #dd.sf_obj.describe()
field_names = [field['name'] for field in desc['fields']]
soql = "SELECT {} FROM {} limit {}".format(','.join(field_names), sf_obj,
set_limit)
results = dd.query_all(soql)
return results
sf_object_extract()
你需要使用 getattr(obj, name_of_member) 查看注释行
和示例:
class d():
def __init__(self):
self.x = 0
q = d()
#to get the value of 'x' member of q you can do 2 things
x_value = getattr(q, 'x')
x_value = q.x