Scripting with CoCo
1. Scripting with CoCo
Welcome back to the course, where so far you've worked with CoCo inside a session, one question at a time.2. Beyond the session
In this video, you'll turn that kind of work into scripts, so it runs again without anyone typing it.3. Beyond the session
Along the way, CoCo itself runs inside one.4. What a script is
A script is a file of steps you can run again and again.5. What a script is
It gives the same result every time, so you can share it or rerun it next month.6. What a script is
You can also put it on a schedule, so it runs on its own.7. SQL or Python
Scripts can be SQL, and that's enough when the data sits in Snowflake and the question fits a query, like totals, rankings, or joins.8. SQL or Python
Python helps when the work goes beyond a query. It can read files outside your warehouse, clean up messy values, and save the result as a table. You don't write it yourself: CoCo writes it, and you review it.9. Why the CLI
CoCo can also run inside a script, and only the CLI allows that, because the other surfaces need someone typing. That's why many developers use the CLI for scheduled jobs, like a weekly report.10. A file from marketing
Let's write a first script. The marketing team at our electronics store sends its promotion costs as a CSV file, and we want to know whether each promo code paid off.11. Ask for a script
We ask CoCo for three things: compare the file with our sales, save the result as a table, and keep the steps as a script.12. CoCo reads the file
CoCo starts by looking at the file.13. CoCo reads the file
Next, it works out what needs cleaning.14. CoCo reads the file
Only then does it write any code.15. Approve, then run
It asks before saving the script.16. Approve, then run
Then it asks again before running it. That run approves everything inside the script, so read the script first.17. The result
The table is saved, and only the education discount paid for itself. The other two promotions cost more than they brought in.18. Next quarter
Here's where a script pays off. Next quarter, marketing sends a new file with updated costs. You run the same script with python3,19. Next quarter
and the table updates in seconds, without asking CoCo again or spending credits. This time, all three promotions paid off.20. Run CoCo with cortex exec
Now let's run CoCo itself from a script. cortex exec takes the prompt you pass it and runs that one task from the shell.21. Run CoCo with cortex exec
CoCo works through the task and prints its answer.22. Run CoCo with cortex exec
Then it exits, with no session left open. Because there's no session to keep open, a script or a schedule can run it.23. Set the limits first
Since nobody is there to click approve, you decide up front what CoCo may use. Blocking write, edit and bash makes the job read-only, and SQL that changes data is refused.24. Set the limits first
Max turns limits the steps, here to five.25. Context in the prompt
With no one to answer a follow-up, the prompt has to carry the context. Name the tables, as you did when exploring the data, so CoCo answers within its limit.26. The full command
Here's the full command, with its key flags highlighted. CoCo replies in one sentence: WELCOME10 brought in the most revenue.27. A weekly report
This is where the two ideas meet. The product team wants a promo report every Monday.28. A weekly report
One script computes the numbers in Python, so they're exact.29. A weekly report
Then it calls cortex exec once to write a short summary, and saves both to a report file.30. The report
We ask CoCo to write that script, then run it with python3. When it finishes, cat report dot md prints the report: the table of numbers, followed by CoCo's two sentences. Put the script on a schedule, and the team gets this report every Monday without anyone running it.31. Commands from this video
To recap, CoCo writes scripts you can run again with python3.32. Commands from this video
cortex exec runs CoCo inside a script.33. Commands from this video
Its flags set the limits on what CoCo can do.34. Let's practice!
Now it's your turn to build and run scripts with CoCo.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.