user2242044
user2242044

Reputation: 9213

Adding/Referencing Dictionary Values through Python Loop

I am using a python library called Simple_Salesforce to query an online CRM database. I am storing the query string and the results of the query in a dictionary. Query_String and Results are unique to each table. I am running in some issues with the loop because the first key in each table in unique. How do I incorporate this into a loop and then how do I print out the data associated with just a key? It's almost like I need to make Cases and Opportunity keys for the table itself, but not sure if this is possible.

from simple_salesforce import Salesforce

#Define all data
Query_Dict = [
    {'Cases' : [], 'Results' : [], "Query_String" : "Select Id, Opportunity From Case where Opportunity <> null"},
    {'Opportunity' : [], 'Results' : [], "Query_String" : "Select Id, Opportunity_Name From Opportunity where Status = 'New'"},
]

#open Salesforce Connection
sf = Salesforce(username='myusername', password='mypassword', security_token='mytoken')

#Dump the query results into a results key for each row then convert to proper format
for tbl in Query_Dict:
       tbl['Results'] = sf.query_all(tbl['Query_String']) 
       tbl['Cases'] = records tbl['Cases']['records']
       tbl['Opportunity'] = records tbl['Opportunity']['records']

#print results associated with Cases key
print Query_dict['Cases']

Upvotes: 0

Views: 73

Answers (1)

abarnert
abarnert

Reputation: 365677

It's almost like I need to make Cases and Opportunity keys for the table itself

You're on the right track.

One way to do exactly what you suggested is to have a key named, say, Table_Name, e.g., Table_Name: "Cases", so you can do this:

tbl[tbl['Table_Name']] = …

(Since the right half of your assignment statement isn't valid Python code and I'm not sure what it's supposed to mean, I'm not sure how exactly to fix it, but presumably it just means replacing "Cases" with tbl['Table_Name'] again.)


But I think a better solution is to keep a dictionary of tables, instead of a list, and key that off the table names:

Query_Dict = {
    'Cases': {'Cases' : [], 'Results' : [], "Query_String" : "Select Id, Opportunity From Case where Opportunity <> null"},
    'Opportunity': {'Opportunity' : [], 'Results' : [], "Query_String" : "Select Id, Opportunity_Name From Opportunity where Status = 'New'"},
}

And now, iterate the key-value pairs with viewitems*:

for name, tbl in Query_Dict.viewitems():
    tbl[name] = …

* Note that viewitems is 2.7-specific. If you want to be compatible with earlier 2.x versions, you can use iteritems; if you want to be forward-compatible with 3.x, you can just use items (as long as Query_Dict is small enough that making an unnecessary temporary list is harmless).

And this is exactly what's needed to make your last line of code work as written (except for the capitalization typo):

print Query_Dict['Cases']

Because Query_Dict is now actually a dictionary, with 'Cases' as a key, instead of a list, this does exactly what you want.

Upvotes: 2

Related Questions