IPython Documentation

Table Of Contents

Previous topic

nbformat.v2.convert

Next topic

nbformat.v2.nbjson

This Page

nbformat.v2.nbbase

Module: nbformat.v2.nbbase

Inheritance diagram for IPython.nbformat.v2.nbbase:

The basic dict based notebook format.

The Python representation of a notebook is a nested structure of dictionary subclasses that support attribute access (IPython.utils.ipstruct.Struct). The functions in this module are merely helpers to build the structs in the right form.

Authors:

  • Brian Granger

Class

NotebookNode

class IPython.nbformat.v2.nbbase.NotebookNode(*args, **kw)

Bases: IPython.utils.ipstruct.Struct

__init__(*args, **kw)

Initialize with a dictionary, another Struct, or data.

Parameters :

args : dict, Struct

Initialize with one dict or Struct

kw : dict

Initialize with key, value pairs.

Examples

>>> s = Struct(a=10,b=30)
>>> s.a
10
>>> s.b
30
>>> s2 = Struct(s,c=30)
>>> sorted(s2.keys())
['a', 'b', 'c']
allow_new_attr(allow=True)

Set whether new attributes can be created in this Struct.

This can be used to catch typos by verifying that the attribute user tries to change already exists in this Struct.

clear() → None. Remove all items from D.
copy()

Return a copy as a Struct.

Examples

>>> s = Struct(a=10,b=30)
>>> s2 = s.copy()
>>> type(s2) is Struct
True
dict()
static fromkeys(S[, v]) → New dict with keys from S and values equal to v.

v defaults to None.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
has_key(k) → True if D has a key k, else False
hasattr(key)

hasattr function available as a method.

Implemented like has_key.

Examples

>>> s = Struct(a=10)
>>> s.hasattr('a')
True
>>> s.hasattr('b')
False
>>> s.hasattr('get')
False
items() → list of D's (key, value) pairs, as 2-tuples
iteritems() → an iterator over the (key, value) items of D
iterkeys() → an iterator over the keys of D
itervalues() → an iterator over the values of D
keys() → list of D's keys
merge(__loc_data__=None, _Struct__conflict_solve=None, **kw)

Merge two Structs with customizable conflict resolution.

This is similar to update(), but much more flexible. First, a dict is made from data+key=value pairs. When merging this dict with the Struct S, the optional dictionary ‘conflict’ is used to decide what to do.

If conflict is not given, the default behavior is to preserve any keys with their current value (the opposite of the update() method’s behavior).

Parameters :

__loc_data : dict, Struct

The data to merge into self

__conflict_solve : dict

The conflict policy dict. The keys are binary functions used to resolve the conflict and the values are lists of strings naming the keys the conflict resolution function applies to. Instead of a list of strings a space separated string can be used, like ‘a b c’.

kw : dict

Additional key, value pairs to merge in

Notes

The __conflict_solve dict is a dictionary of binary functions which will be used to solve key conflicts. Here is an example:

__conflict_solve = dict(
    func1=['a','b','c'],
    func2=['d','e']
)

In this case, the function func1() will be used to resolve keys ‘a’, ‘b’ and ‘c’ and the function func2() will be used for keys ‘d’ and ‘e’. This could also be written as:

__conflict_solve = dict(func1='a b c',func2='d e')

These functions will be called for each key they apply to with the form:

func1(self['a'], other['a'])

The return value is used as the final merged value.

As a convenience, merge() provides five (the most commonly needed) pre-defined policies: preserve, update, add, add_flip and add_s. The easiest explanation is their implementation:

preserve = lambda old,new: old
update   = lambda old,new: new
add      = lambda old,new: old + new
add_flip = lambda old,new: new + old  # note change of order!
add_s    = lambda old,new: old + ' ' + new  # only for str!

You can use those four words (as strings) as keys instead of defining them as functions, and the merge method will substitute the appropriate functions for you.

For more complicated conflict resolution policies, you still need to construct your own functions.

Examples

This show the default policy:

>>> s = Struct(a=10,b=30)
>>> s2 = Struct(a=20,c=40)
>>> s.merge(s2)
>>> sorted(s.items())
[('a', 10), ('b', 30), ('c', 40)]

Now, show how to specify a conflict dict:

>>> s = Struct(a=10,b=30)
>>> s2 = Struct(a=20,b=40)
>>> conflict = {'update':'a','add':'b'}
>>> s.merge(s2,conflict)
>>> sorted(s.items())
[('a', 20), ('b', 70)]
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E], **F) → None. Update D from dict/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → list of D's values
viewitems() → a set-like object providing a view on D's items
viewkeys() → a set-like object providing a view on D's keys
viewvalues() → an object providing a view on D's values

Functions

IPython.nbformat.v2.nbbase.from_dict(d)
IPython.nbformat.v2.nbbase.new_author(name=None, email=None, affiliation=None, url=None)

Create a new author.

IPython.nbformat.v2.nbbase.new_code_cell(input=None, prompt_number=None, outputs=None, language=u'python', collapsed=False)

Create a new code cell with input and output

IPython.nbformat.v2.nbbase.new_metadata(name=None, authors=None, license=None, created=None, modified=None, gistid=None)

Create a new metadata node.

IPython.nbformat.v2.nbbase.new_notebook(metadata=None, worksheets=None)

Create a notebook by name, id and a list of worksheets.

IPython.nbformat.v2.nbbase.new_output(output_type=None, output_text=None, output_png=None, output_html=None, output_svg=None, output_latex=None, output_json=None, output_javascript=None, output_jpeg=None, prompt_number=None, etype=None, evalue=None, traceback=None)

Create a new code cell with input and output

IPython.nbformat.v2.nbbase.new_text_cell(cell_type, source=None, rendered=None)

Create a new text cell.

IPython.nbformat.v2.nbbase.new_worksheet(name=None, cells=None)

Create a worksheet by name with with a list of cells.