In [1]:
from IPython import nbformat

In [2]:
test = nbformat.read("/home/carl/Documents/Code/Projects/PyscesToolbox/documentation/notebooks/SymCA.ipynb",nbformat.NO_CONVERT)

Needed functionality

Removing #ex from end of coding line

Removing #ex comment

Remove comment #remove_next

Remove cells with:

"# To avoid duplication - do not run #ex"

and

"display(Image(path.join(notebook_dir,'images','sc_model_graph_1.png'))) #ex"


In [6]:
type(test.cells[0])


Out[6]:
IPython.nbformat.notebooknode.NotebookNode

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]:
IPython.nbformat.notebooknode.NotebookNode

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')


['asd', 'asdwqeqwe', 'asdwewrwqr', 'wiioasdoisad']

In [8]:
x[-1]


Out[8]:
'\n'

In [10]:
x


Out[10]:
'asd\nasdwqeqwe\nasdwewrwqr\nwiioasdoisad\n'

In [12]:
[line + '\n' for line in x.split('\n')]


Out[12]:
['asd\n', 'asdwqeqwe\n', 'asdwewrwqr\n', 'wiioasdoisad\n', '\n']

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]:
['asd\n', 'asdwqeqwe\n', 'asdwewrwqr\n', 'wiioasdoisad']

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]:
('/home/carl/Documents/Code/Projects/PyscesToolbox/documentation/notebooks/SymCA_test',
 '.ipynb')

In [32]:



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-8d1a89f30f93> in <module>()
----> 1 str.join(['1','2','3'])

TypeError: descriptor 'join' requires a 'str' object but received a 'list'

In [ ]: