Skip to content

Commit c23c08d

Browse files
authored
Merge pull request #56 from cal-itp/params-magic
probably very hacky way to add papermill parameters at runtime
2 parents 2edce07 + 65b13a5 commit c23c08d

8 files changed

+23
-26
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
2-
max-line-length=88
2+
max-line-length=120
33
ignore=E203, W503

calitp/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# flake8: noqa
22

3-
__version__ = "0.0.15"
3+
__version__ = "0.0.16"
44

55
from .sql import get_table, write_table, query_sql, to_snakecase, get_engine
66
from .storage import save_to_gcfs, read_gcfs

calitp/config.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def is_development():
2020
# if a person can write data, then they need to set AIRFLOW_ENV
2121
if is_pipeline():
2222
if "AIRFLOW_ENV" not in os.environ:
23-
raise KeyError(
24-
"Pipeline admin must set AIRFLOW_ENV env variable explicitly"
25-
)
23+
raise KeyError("Pipeline admin must set AIRFLOW_ENV env variable explicitly")
2624

2725
env = os.environ["AIRFLOW_ENV"]
2826

calitp/magics.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from IPython.core.magic import register_cell_magic
23
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
34
from IPython.display import Markdown, display
@@ -13,9 +14,7 @@
1314
help="Print the code to markdown, in addition to running",
1415
)
1516
@argument("-o", "--output", type=str, help="A variable name to save the result as")
16-
@argument(
17-
"-q", "--quiet", action="store_true", help="Whether to hide the result printout"
18-
)
17+
@argument("-q", "--quiet", action="store_true", help="Whether to hide the result printout")
1918
@register_cell_magic
2019
def sql(line, cell):
2120
# %%sql -m
@@ -35,3 +34,13 @@ def sql(line, cell):
3534

3635
if not args.quiet:
3736
return res
37+
38+
39+
@register_cell_magic
40+
def capture_parameters(line, cell):
41+
shell = get_ipython()
42+
shell.run_cell(cell, silent=True)
43+
# We assume the last line is a tuple
44+
tup = [s.strip() for s in cell.strip().split("\n")[-1].split(",")]
45+
46+
print(json.dumps({identifier: shell.user_ns[identifier] for identifier in tup if identifier}))

calitp/sql.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717

1818
class CreateTableAs(Executable, ClauseElement):
19-
def __init__(
20-
self, name, select, replace=False, if_not_exists=False, partition_by=None
21-
):
19+
def __init__(self, name, select, replace=False, if_not_exists=False, partition_by=None):
2220
self.name = name
2321
self.select = select
2422
self.replace = replace
@@ -34,9 +32,7 @@ def visit_insert_from_select(element, compiler, **kw):
3432
if_not_exists = " IF NOT EXISTS" if element.if_not_exists else ""
3533

3634
# TODO: visit partition by clause
37-
partition_by = (
38-
f" PARTITION BY {element.partition_by}" if element.partition_by else ""
39-
)
35+
partition_by = f" PARTITION BY {element.partition_by}" if element.partition_by else ""
4036

4137
return f"""
4238
CREATE{or_replace} TABLE{if_not_exists} {name}
@@ -113,9 +109,7 @@ def write_table(
113109
@require_pipeline("write_table")
114110
def _write_table_df(sql_stmt, table_name, engine=None, replace=True):
115111
if_exists = "replace" if replace else "fail"
116-
return sql_stmt.to_gbq(
117-
format_table_name(table_name), project_id=get_project_id(), if_exists=if_exists
118-
)
112+
return sql_stmt.to_gbq(format_table_name(table_name), project_id=get_project_id(), if_exists=if_exists)
119113

120114

121115
def query_sql(fname, write_as=None, replace=False, dry_run=False, as_df=True):
@@ -176,9 +170,7 @@ def sql_patch_comments(table_name, field_comments, table_comments=None, bq_clien
176170
if bq_client is None:
177171
from google.cloud import bigquery
178172

179-
bq_client = bigquery.Client(
180-
project=get_project_id(), location=CALITP_BQ_LOCATION
181-
)
173+
bq_client = bigquery.Client(project=get_project_id(), location=CALITP_BQ_LOCATION)
182174

183175
tbl = bq_client.get_table(table_name)
184176
old_schema = tbl.schema

calitp/storage.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ def get_fs(gcs_project="", **kwargs):
77
if is_cloud():
88
return gcsfs.GCSFileSystem(project=gcs_project, token="cloud", **kwargs)
99
else:
10-
return gcsfs.GCSFileSystem(
11-
project=gcs_project, token="google_default", **kwargs
12-
)
10+
return gcsfs.GCSFileSystem(project=gcs_project, token="google_default", **kwargs)
1311

1412

1513
@require_pipeline("save_to_gcfs")

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 120

setup.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
_version_re = re.compile(r"__version__\s+=\s+(.*)")
88

99
with open("calitp/__init__.py", "rb") as f:
10-
version = str(
11-
ast.literal_eval(_version_re.search(f.read().decode("utf-8")).group(1))
12-
)
10+
version = str(ast.literal_eval(_version_re.search(f.read().decode("utf-8")).group(1)))
1311

1412
setup(
1513
name="calitp",

0 commit comments

Comments
 (0)