【问题标题】:How to write a for loop to dynamically call values in a function?如何编写一个for循环来动态调用函数中的值?
【发布时间】:2020-04-16 23:47:56
【问题描述】:

我有几个表,我想通过它们运行我的 python 函数;但是我很难编写脚本来做到这一点。

这是我的表格变量:

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'}]

知道为什么我会收到此错误并修复我的脚本以将表参数传递给我的descsoql 变量吗?

补充:我也尝试将 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()'

【问题讨论】:

    标签: python python-3.x function simple-salesforce


    【解决方案1】:
    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多