Skip to content

Instantly share code, notes, and snippets.

@PaddyAlton
Last active September 16, 2022 14:37
Show Gist options
  • Save PaddyAlton/889f2b5e1153f501db4be37cb5fdd2d1 to your computer and use it in GitHub Desktop.
Save PaddyAlton/889f2b5e1153f501db4be37cb5fdd2d1 to your computer and use it in GitHub Desktop.
Two functions for dumbly copying BigQuery results from the UI and printing them in a format fit for Slack
def ctrlc_bigquery():
"""
ctrlc_bigquery
Dumb function to make it super easy to copy/paste BigQuery results from the UI
and print them in a nice format (e.g. dump into Slack in a code block)
"""
from pandas.io.clipboard import clipboard_get
text = clipboard_get()
L = [e.strip() for e in text.splitlines()]
if not L[0].startswith("Row"):
raise ValueError("Please copy the header row as well")
headers = L[0].split("\t")
L = headers + L[1:]
n_cols = len(headers)
cols = [L[i::n_cols] for i in range(n_cols)]
z = zip(*cols)
widths = [max([len(s) for s in col]) for col in cols]
for *t, in z:
record = "".join(["{:"+str(w)+"} " for w in widths]).format(*t)
print(record)
def ctrlc_bigquery_json():
"""
ctrlc_bigquery_json
This function takes the JSON output of BigQuery
(which you can copy with a button), and similarly
prints it in a nice format that you can paste into
a Slack codeblock
(to some extent, it can handle nested results)
"""
from pandas.io.clipboard import clipboard_get
from json import loads
text = clipboard_get()
records = loads(text)
headers = list(records[0].keys())
cols = [[key] + [j[key] for j in records] for key in headers]
z = zip(*cols)
widths = [max([len(str(s)) for s in col]) for col in cols]
for *t, in z:
record = "".join(["{:"+str(w)+"} " for w in widths]).format(*[str(v) for v in t])
print(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment