我在过去的10个月里一直在学习python。或者我正在运行更新Salesforce中对象的程序。它的销售队伍确实无所谓。
重要的是,在相同的程序中有很多小的变化,我最终基本上写了相同的函数,但略有修改。
我想尽可能地巩固,但我不确定如何。 我已经将下面的代码添加了一些描述
我的问题围绕如何使这样的功能可扩展。我不得不经常重复自己,但我不确定如何使这项工作。 谢谢!
def getSfdcAccounts(login,pw):
'''
This function queries Salesforce and pulls down a
list of accounts that meet the criteria of xyz, simple
'''
svc = beatbox.Client()
svc.login(login, pw)
qr = svc.query("select Id, Name, Website, type from Account where xyz")
for rec in qr[sf.records:]:
do something
def getSfdcAccounts2(login,pw):
'''
This function queries Salesforce and pulls down a
list of accounts that meet the criteria of xyz, zyx and abc, simple
However, This is almost identical to the original function but has
significant variations. Should I be combining these?
'''
svc = beatbox.Client()
svc.login(login, pw)
qr = svc.query("select Id, BillingAddress, Name, Website, type from Account where xyz, zyx, abc")
for rec in qr[sf.records:]:
do something
def updateSfdcOjbect(lowin,pw,object,info):
'''
This function pushes a dictionary into Salesforce
and updates the object based on the keys of the dict, simple
'''
svc = beatbox.Client()
svc.login(login, pw)
for i in info:
update_dict = {
'type': str(object),
'Id': info[0],
'Website': info[1]
'BillingAddress': info[2]
'NumberOfEmployees':info[3]
'Industry':info[4]
}
results2 = svc.update(update_dict)
def updateSfdcOjbect(lowin,pw,object,info):
'''
This function pushes a dictionary into Salesforce
and updates the object based on the keys of the dict, simple
However, the keys and values are slightly different. I want
to update the Shipping Address, not Billing Address
As well as the number of products rather than employees
'''
svc = beatbox.Client()
svc.login(login, pw)
for i in info:
update_dict = {
'type': str(object),
'Id': info[0],
'Website': info[1]
'ShippingAddress': info[2]
'Nummber_of_Products__c':info[3]
'Sic_Code__c':info[4]
}
results2 = svc.update(update_dict)
答案 0 :(得分:1)
基本原则是采用功能之间相同的部分 并将它们放入一个共同的功能,然后传入它们的部分 不同。所以,以你的两个函数为例:
def getSfdcAccounts(login,pw):
'''
This function queries Salesforce and pulls down a
list of accounts that meet the criteria of xyz, simple
'''
svc = beatbox.Client()
svc.login(login, pw)
qr = svc.query("select Id, Name, Website, type from Account where xyz")
for rec in qr[sf.records:]:
do something
def getSfdcAccounts2(login,pw):
'''
This function queries Salesforce and pulls down a
list of accounts that meet the criteria of xyz, zyx and abc, simple
However, This is almost identical to the original function but has
significant variations. Should I be combining these?
'''
svc = beatbox.Client()
svc.login(login, pw)
qr = svc.query("select Id, BillingAddress, Name, Website, type from Account where xyz, zyx, abc")
for rec in qr[sf.records:]:
do something
你可以创建一个新功能:
def getSfdcAccounts(login, pw, query):
svc = beatbox.Client()
svc.login(login, pw)
qr = svc.query(query)
for rec in qr[sf.records:]:
do something
然后用不同的查询调用它:
getSfdcAccounts(login, pw, "select Id, Name, Website, type from Account where xyz"):
getSfdcAccounts(login, pw, "select Id, BillingAddress, Name, Website, type from Account where xyz, zyx, abc"):
除此之外,我同意其他人所说的关于将它放入一个类并处理那里的连接设置的内容。
答案 1 :(得分:0)
这些函数都应该在同一个类中声明为方法。在类的构造函数中,您可以共享beatbox设置:
self.svc = beatbox.Client()
self.svc.login(login, pw)
答案 2 :(得分:0)
class BeatboxClient(object):
def __init__(self, login, pw):
self.svc = beatbox.Client()
self.svc.login(login, pw)
def processAccounts(self, query):
qr = self.svc.query(query)
for rec in qr[sf.records:]:
do something
def updateSfdcOjbect(object, info):
for i in info:
update_dict = {
'type': str(object),
'Id': info[0],
'Website': info[1]
'BillingAddress': info[2]
'NumberOfEmployees':info[3]
'Industry':info[4]
}
results2 = self.svc.update(update_dict)
def updateSfdcOjbect(object, info):
for i in info:
update_dict = {
'type': str(object),
'Id': info[0],
'Website': info[1]
'ShippingAddress': info[2]
'Nummber_of_Products__c':info[3]
'Sic_Code__c':info[4]
}
results2 = self.svc.update(update_dict)