Skip to content

Commit 11b6203

Browse files
authored
Merge pull request #8 from rfdearborn/rfdearborn/some-cleanup
get_path_or_empty cleanup
2 parents dbf2708 + 097e5aa commit 11b6203

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

dbt_docs_to_notion.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,31 @@ def make_request(endpoint, querystring='', method='GET', **request_kwargs):
3131
return resp.json()
3232

3333

34-
def get_path_or_empty(parent_object, path_array, zero_value=''):
34+
def get_paths_or_empty(parent_object, paths_array, zero_value=''):
35+
"""Used for catalog_nodes accesses, since structure is variable"""
36+
for path in paths_array:
3537
obj = parent_object
36-
for el in path_array:
37-
if el not in obj:
38-
return zero_value
39-
obj = obj[el]
38+
for el in path:
39+
if el not in obj:
40+
obj = zero_value
41+
break
42+
obj = obj[el]
43+
if obj != zero_value:
44+
return obj
4045

41-
return obj
42-
43-
44-
def get_num_rows(catalog_nodes, model_name):
45-
zero_value = NUMERIC_ZERO_VALUE
46-
keys = ['num_rows', 'row_count']
47-
for key in keys:
48-
num_rows = get_path_or_empty(catalog_nodes, [model_name, 'stats', key, 'value'], NUMERIC_ZERO_VALUE)
49-
if num_rows != NUMERIC_ZERO_VALUE:
50-
return num_rows
51-
52-
return NUMERIC_ZERO_VALUE
53-
54-
55-
def get_bytes(catalog_nodes, model_name):
56-
zero_value = NUMERIC_ZERO_VALUE
57-
keys = ['num_bytes', 'bytes']
58-
for key in keys:
59-
num_rows = get_path_or_empty(catalog_nodes, [model_name, 'stats', key, 'value'], NUMERIC_ZERO_VALUE)
60-
if num_rows != NUMERIC_ZERO_VALUE:
61-
return num_rows
62-
63-
return NUMERIC_ZERO_VALUE
46+
return zero_value
6447

6548

6649
def get_owner(data, catalog_nodes, model_name):
67-
# Check for an owner field explicitly named in the DBT Config
68-
# If none present, fall back to database table owner
69-
owner = get_path_or_empty(data, ['config', 'meta', 'owner'], None)
70-
if owner != None:
71-
return owner
50+
"""
51+
Check for an owner field explicitly named in the DBT Config
52+
If none present, fall back to database table owner
53+
"""
54+
owner = get_paths_or_empty(data, [['config', 'meta', 'owner']], None)
55+
if owner is not None:
56+
return owner
7257

73-
return get_path_or_empty(catalog_nodes, [model_name, 'metadata', 'owner'], '')
58+
return get_paths_or_empty(catalog_nodes, [[model_name, 'metadata', 'owner']], '')
7459

7560

7661
def main():
@@ -153,7 +138,7 @@ def main():
153138
}
154139
}
155140

156-
print('created creating database')
141+
print('creating database')
157142
database_creation_resp = make_request(
158143
endpoint='databases/',
159144
querystring='',
@@ -207,7 +192,11 @@ def main():
207192
}
208193
}
209194
]
210-
col_names_and_data = list(get_path_or_empty(catalog_nodes, [model_name, 'columns'], {}).items())
195+
col_names_and_data = list(get_paths_or_empty(
196+
catalog_nodes,
197+
[[model_name, 'columns']],
198+
{}
199+
).items())
211200
for (col_name, col_data) in col_names_and_data[:98]: # notion api limit is 100 table rows
212201
columns_table_children_obj.append(
213202
{
@@ -427,10 +416,20 @@ def main():
427416
]
428417
},
429418
"Approx Rows": {
430-
"number": get_num_rows(catalog_nodes, model_name)
419+
"number": get_paths_or_empty(
420+
catalog_nodes,
421+
[[model_name, 'stats', 'num_rows', 'value'],
422+
[model_name, 'stats', 'row_count', 'value']],
423+
NUMERIC_ZERO_VALUE
424+
)
431425
},
432426
"Approx GB": {
433-
"number":get_bytes(catalog_nodes, model_name) /1e9
427+
"number": get_paths_or_empty(
428+
catalog_nodes,
429+
[[model_name, 'stats', 'bytes', 'value'],
430+
[model_name, 'stats', 'num_bytes', 'value']],
431+
NUMERIC_ZERO_VALUE
432+
) / 1e9
434433
},
435434
"Depends On": {
436435
"rich_text": [

0 commit comments

Comments
 (0)