Handling a dictionary

I’m using pyairtable to pull data from Airtable, which is working.

I would like to append a tableDAT with the some of the incoming data.

Last time I did this, the following snippet (from pdubost’s tutorial) worked:

for record in table.all():
	locTable.appendRow(
		[record ["fields"] ["photos"] [0] ["url"],
		])

Which, allow me to learn out loud here, looks like a for loop that appends the defined tableDAT with the relevant data for each incoming record.

I keep getting errors. I was up to my elbows in Python resources til 2:00am last night and still couldn’t crack it.

I suspect the way the dictionary is handled has changed and I’m formatting incorrectly?..

Any help appreciated.

I’m happy to help with this but without your data, I’d just be guessing. Can you post an example .toe or send to ivan@derivative.ca. I definitely need to know exactly what the incoming data looks like and it would help to see a mock-up of how you’d like the final table to look.

Also, it would help a lot to know what the errors are you are getting.
Also doing a printout of your record to know how it looks like will help alot.
I’m simply suspecting that photos is an empty array, so you are getting an error because index is out of bounds. But could be anything, really.

Thanks for the help @Ivan and @alphamoonbase.

When I run this:

[shoot, pulled down the screen cap so I can redact it first]

(I can send you the Airtable API for that specific base, which formats the same outgoing data more nicely, but I hesitate to send that over the open inter webs.)

The Airtable base is going to be updating live, as folks upload photos through JotForm (which then sends it to Airtable)

All I need is the “url” data inside “photos” which is inside “fields” parsed into a tableDAT, where I can then plug it into a moviefileInTOP. I will then use a ReplicatorCOMP, so as new records come into the airTable-Base → tdTable, it all updates live.

As soon as folks send a photo it shows up on screen.

I got it all working last year. However, I’m now on a MBP M1, the latest release of TD. Also, what was air table-python-wrapper, is now pyairtable. I know just enough to get myself in trouble. :cold_sweat:

Here what the data looks like in TD (with the email addresses redacted!)

When I run this:

I get his error:

So print(table.all()) works

And my attempt to populate a tableDAT breaks it:
for records in table.all(): locTable.appendRow( [records["fields"]["photos"][0]["url"], ])

In your first record, “fields” is empty. So you either need to do a careful check, like:

for records in table.all():
	if records['fields'] and records['fields']['photos'] and records['fields']['photos'][0] and records['fields']['photos'][0]['url']:
		locTable.appendRow([records['fields']['photos'][0]['url'],])

or a brute force check like:

for records in table.all():
	try:
		locTable.appendRow([records['fields']['photos'][0]['url'],])
	except:
		pass
1 Like

Got it, thank you. That really helps.

I’m out and about so can’t test TD right now. I’ve deleted the empty records and will try the options you’ve offered.

Yep, that did it, thank you @Ivan. Life saver.

I owe you a beer if you’re ever on the west coast. (of Canada)

Glad to help! I may take you up on that some day. I live in Oregon and have been scheming a trip to Vancouver :slight_smile:

Just a little note on the try/except block:
It is bad practice to implement a catch-all except as this would obfuscate other errors as well.
Instead, except specific events like KeyError or IndexError.

array = []
dictionary = {}

try:
	array[0] #throws IndexError
	dicrionary["foo"] #throws KeyError
	op("does_not_exist").appendRow( "something" ) #throws AttributeError
except (KeyError, IndexError):
	pass

If you just do except, the appendRow would also throw an error, but you would not know because the except hides the error message from you.

2 Likes

@alphamoonbase is definitely right about that.

In general, you should always use careful error checking unless you really don’t care what the error is. Also, you should keep anything inside the “try” clause as simple as possible because, as pointed out, you won’t see any effects of other errors you may have missed.

Yes, do come to Vancouver. And I will buy you a beer.

Loved my trip along the Oregon Coast a few years back. Public! No people!

I think I understand this, but barely.

I don’t think my process is serving me well:

  1. watching tutorials to find bits and pieces I understand
  2. duct taping everything together
  3. discover something doesn’t work
  4. be baffled as to why because I don’t have Python fundamentals in place
  5. frantically look for more tutorials hoping I can find the answer
  6. be in constant terror as our performance deadline approaches
  7. finally get everything working (usually with the generous help of the community)
  8. forget how terrifying the approaching deadline terror is
  9. repeat 6 months later

So, I’m starting a freeCodeCamp.org Python course on YouTube.
Thanks again for the help.

1 Like

Building strong Python foundations will help with TouchDesigner development A LOT.