in notebooks/notebook_template_review.py [0:0]
def validate(self, notebook: Notebook) -> bool:
"""
Parse the objective cell.
Find the description, uses and steps.
"""
self.desc = ''
self.uses = ''
self.steps = ''
self.costs = []
ret = True
cell = notebook.get()
if not cell['source'][0].startswith("### Objective"):
ret = notebook.report_error(ErrorCode.ERROR_OBJECTIVE_NOTFOUND, "Objective section not found")
notebook.costs = []
return ret
in_desc = True
in_uses = False
in_steps = False
for line in cell['source'][1:]:
# TOC anchor
if line.startswith('<a name='):
continue
if line.startswith('This tutorial uses'):
in_desc = False
in_steps = False
in_uses = True
self.uses += line
continue
elif line.startswith('The steps performed'):
in_desc = False
in_uses = False
in_steps = True
self.steps += line
continue
if in_desc:
if len(self.desc) > 0 and line.strip() == '':
in_desc = False
continue
self.desc += line
elif in_uses:
sline = line.strip()
if len(sline) == 0:
self.uses += '\n'
else:
ch = sline[0]
if ch in ['-', '*', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
self.uses += line
elif in_steps:
sline = line.strip()
if len(sline) == 0:
self.steps += '\n'
else:
ch = sline[0]
if ch in ['-', '*', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
# check for italic font setting
if ch == '*' and sline[1] != ' ':
in_steps = False
# special case
elif sline.startswith('* Prediction Service'):
in_steps = False
else:
self.steps += line
elif ch == '#':
in_steps = False
if self.desc == '':
ret = notebook.report_error(ErrorCode.ERROR_OBJECTIVE_MISSING_DESC, "Objective section missing desc")
else:
self.desc = self.desc.lstrip()
bracket = False
paren = False
sentences = ""
for _ in range(len(self.desc)):
if self.desc[_] == '[':
bracket = True
continue
elif self.desc[_] == ']':
bracket = False
continue
elif self.desc[_] == '(':
paren = True
elif self.desc[_] == ')':
paren = False
continue
if not paren:
sentences += self.desc[_]
sentences = sentences.split('.')
if len(sentences) > 1:
self.desc = sentences[0] + '.\n'
if self.desc.startswith('In this tutorial, you learn') or self.desc.startswith('In this notebook, you learn'):
self.desc = self.desc[22].upper() + self.desc[23:]
if self.uses == '':
ret = notebook.report_error(ErrorCode.ERROR_OBJECTIVE_MISSING_USES, "Objective section missing uses services list")
else:
if 'BigQuery' in self.uses:
self.costs.append('BQ')
if 'Vertex' in self.uses:
self.costs.append('Vertex')
if 'Dataflow' in self.uses:
self.costs.append('Dataflow')
if self.steps == '':
ret = notebook.report_error(ErrorCode.ERROR_OBJECTIVE_MISSING_STEPS, "Objective section missing steps list")
notebook.costs = self.costs
return ret