In [1]:
from IPython import nbformat
In [2]:
test = nbformat.read("/home/carl/Documents/Code/Projects/PyscesToolbox/documentation/notebooks/SymCA.ipynb",nbformat.NO_CONVERT)
In [6]:
type(test.cells[0])
Out[6]:
In [7]:
new_base = {k:v for k,v in test.iteritems() if k != 'cells'}
In [8]:
new_base['cells'] = []
In [9]:
for cell in test.cells:
if remove_cell_with(cell['source'],'# To avoid duplication'):
new_lines = []
for line in iterlines(cell['source']):
new_line = remove_ex_comment(line)
new_line = remove_ex(new_line)
new_line = remove_line_with(new_line, '#remove_next')
new_lines.append(new_line)
new_source = combine_lines(new_lines)
new_cell = {k:v for k,v in cell.iteritems() if k != u'source'}
new_cell[u'source'] = new_source
new_cell = nbformat.NotebookNode(new_cell)
new_base['cells'].append(new_cell)
In [10]:
type(new_base['cells'][0])
Out[10]:
In [11]:
new = nbformat.NotebookNode(new_base)
In [12]:
nbformat.write(new,'/home/carl/Documents/Code/Projects/PyscesToolbox/documentation/notebooks/SymCA_test.ipynb')
In [3]:
def remove_ex_comment(line):
if line.startswith('#') and '#ex' in line:
return ''
else:
return line
def remove_line_with(line, pattern):
if pattern in line:
return ''
else:
return line
def remove_ex(line):
return line.replace('#ex','')
def remove_cell_with(cell, pattern):
if pattern in cell:
return None
else:
return cell
In [4]:
# new_lines = []
# for line in iterlines(test.cells[0]['source']):
# new_line = remove_ex_comment(line)
# new_line = remove_ex(new_line)
# new_lines.append(new_line)
In [5]:
def iterlines(text):
lines = []
current_line = ''
for char in text:
current_line = current_line + char
if char == '\n':
lines.append(current_line)
current_line = ''
lines.append(current_line)
return lines
def combine_lines(lines):
new = ''
for each in lines:
new = new + each
return new
In [17]:
x = 'asd\nasdwqeqwe\nasdwewrwqr\nwiioasdoisad'
In [4]:
print x.split('\n')
In [8]:
x[-1]
Out[8]:
In [10]:
x
Out[10]:
In [12]:
[line + '\n' for line in x.split('\n')]
Out[12]:
In [19]:
def iterlines(text):
"""
"""
lines = text.split('\n')
if text[-1] == '\n':
lines = [line + '\n' for line in lines[:-1]]
return lines
else:
lines = [line + '\n' for line in lines[:-1]] + [lines[-1]]
return lines
In [20]:
iterlines(x)
Out[20]:
In [21]:
from sys import path
In [23]:
from os import path
In [30]:
path.splitext('/home/carl/Documents/Code/Projects/PyscesToolbox/documentation/notebooks/SymCA_test.ipynb')
Out[30]:
In [32]:
In [ ]: