Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_single_chars(text="""'This is a single line comment'''
'and another one'
# and comments
"and line breaks"
"and it ends here'''"
1 + 1
"""):
sp = StringParser('python')
for line in text.splitlines():
assert not sp.is_quoted()
sp.read_line(line)
language = _SCRIPT_EXTENSIONS[ext]['language']
twenty_hash_re = re.compile(r'^#( |)#{19,}\s*$')
double_percent_re = re.compile(r'^{}( %%|%%)$'.format(comment))
double_percent_and_space_re = re.compile(r'^{}( %%|%%)\s'.format(comment))
nbconvert_script_re = re.compile(r'^{}( | In\[[0-9 ]*\]:?)'.format(comment))
vim_folding_markers_re = re.compile(r'^{}\s*'.format(comment) + '{{{')
vscode_folding_markers_re = re.compile(r'^{}\s*region'.format(comment))
twenty_hash_count = 0
double_percent_count = 0
magic_command_count = 0
rspin_comment_count = 0
vim_folding_markers_count = 0
vscode_folding_markers_count = 0
parser = StringParser(language='R' if ext in ['.r', '.R'] else 'python')
for line in lines:
parser.read_line(line)
if parser.is_quoted():
continue
# Don't count escaped Jupyter magics (no space between %% and command) as cells
if double_percent_re.match(line) or double_percent_and_space_re.match(line) or \
nbconvert_script_re.match(line):
double_percent_count += 1
if not line.startswith(comment) and is_magic(line, language):
magic_command_count += 1
if twenty_hash_re.match(line) and ext == '.py':
twenty_hash_count += 1
def uncomment_magic(source, language='python', global_escape_flag=True, explicitly_code=True):
"""Unescape Jupyter magics"""
parser = StringParser(language)
next_is_magic = False
for pos, line in enumerate(source):
if not parser.is_quoted() and (next_is_magic or is_magic(line, language, global_escape_flag, explicitly_code)):
source[pos] = unesc(line, language)
next_is_magic = language == 'python' and _LINE_CONTINUATION_RE.match(line)
parser.read_line(line)
return source
else:
end_of_cell = i + 1
if len(lines) <= i + 1 or _BLANK_LINE.match(
lines[i + 1]):
return end_of_cell, i + 2, explicit_end_of_cell_marker
return end_of_cell, i + 1, explicit_end_of_cell_marker
else:
# 20 # or more
for i, line in enumerate(lines[1:], 1):
if not line.startswith(self.comment):
if _BLANK_LINE.match(line):
return i, i + 1, False
return i, i, False
elif self.cell_type == 'code':
parser = StringParser('python')
for i, line in enumerate(lines):
if parser.is_quoted():
parser.read_line(line)
continue
if self.start_of_new_markdown_cell(line):
if i > 0 and _BLANK_LINE.match(lines[i - 1]):
return i - 1, i, False
return i, i, False
parser.read_line(line)
return len(lines), len(lines), False
def find_region_end(self, lines):
"""Find the end of the region started with start and end markers"""
if self.metadata and 'cell_type' in self.metadata:
self.cell_type = self.metadata.pop('cell_type')
else:
self.cell_type = 'code'
parser = StringParser(self.language or self.default_language)
for i, line in enumerate(lines):
# skip cell header
if self.metadata is not None and i == 0:
continue
if parser.is_quoted():
parser.read_line(line)
continue
parser.read_line(line)
# New code region
# Simple code pattern in LightScripts must be preceded with a blank line
if self.start_code_re.match(line) or (
self.simple_start_code_re and self.simple_start_code_re.match(line) and
(self.cell_marker_start or i == 0 or _BLANK_LINE.match(lines[i - 1]))):
def need_explicit_marker(source, language='python', global_escape_flag=True, explicitly_code=True):
"""Does this code needs an explicit cell marker?"""
if language != 'python' or not global_escape_flag or not explicitly_code:
return False
parser = StringParser(language)
for line in source:
if not parser.is_quoted() and is_magic(line, language, global_escape_flag, explicitly_code):
if not is_magic(line, language, global_escape_flag, False):
return True
parser.read_line(line)
return False
def escape_code_start(source, ext, language='python'):
"""Escape code start with '# '"""
parser = StringParser(language)
for pos, line in enumerate(source):
if not parser.is_quoted() and is_escaped_code_start(line, ext):
source[pos] = _SCRIPT_EXTENSIONS.get(ext, {}).get('comment', '#') + ' ' + line
parser.read_line(line)
return source
def comment_magic(source, language='python', global_escape_flag=True, explicitly_code=True):
"""Escape Jupyter magics with '# '"""
parser = StringParser(language)
next_is_magic = False
for pos, line in enumerate(source):
if not parser.is_quoted() and (next_is_magic or is_magic(line, language, global_escape_flag, explicitly_code)):
source[pos] = _COMMENT[language] + ' ' + line
next_is_magic = language == 'python' and _LINE_CONTINUATION_RE.match(line)
parser.read_line(line)
return source