Testing `%nbdev_show_doc` and showing how it works.

Showing multiple elements with a single %nbdev_show_doc

https://github.com/fastai/fastai2/blob/master/nbs/00_torch_core.ipynb makes a few show_doc calls which could be written with one %nbdev_show_doc

class TitledInt[source]

TitledInt() :: Int

An int with show

class TitledStr[source]

TitledStr() :: Str

An str with show

class TitledFloat[source]

TitledFloat(x=0) :: Float

A float with show

In the example above ↑

  • title_level= can appear anywhere in the list of names
  • list of things are classes but could be functions etc
  • could also be written as a space separated list
    • %nbdev_show_doc TitledInt TitledStr TitledFloat title_level=3

Not having to pass name= might be nice

https://github.com/fastai/fastai2/blob/master/nbs/03_data.core.ipynb makes a few show_doc calls which could be written with one %nbdev_show_doc without having to pass name=

class DataLoaders[source]

DataLoaders(*loaders, path='.', device=None) :: GetAttr

Basic wrapper around several DataLoaders.

DataLoaders.__getitem__[source]

DataLoaders.__getitem__(i)

Retrieve DataLoader at i (0 is training, 1 is validation)

DataLoaders.train[source]

Training DataLoader

DataLoaders.valid[source]

Validation DataLoader

DataLoaders.train_ds[source]

Training Dataset

DataLoaders.valid_ds[source]

Validation Dataset

In the example above ↑

  • The . tells nbdev_show_doc to show class members
  • could also be written as a comma separated list
    • %nbdev_show_doc DataLoaders ., __getitem__, train, valid, train_ds, valid_ds
  • or use a mixture of separators
    • %nbdev_show_doc DataLoaders . __getitem__, train,valid, train_ds valid_ds

DataLoaders.[source]

Training DataLoader

DataLoaders.train[source]

Training DataLoader

You also get name for free when using the * wildcard

class DataLoaders[source]

DataLoaders(*loaders, path='.', device=None) :: GetAttr

Basic wrapper around several DataLoaders.

DataLoaders.__getitem__[source]

DataLoaders.__getitem__(i)

Retrieve DataLoader at i (0 is training, 1 is validation)

DataLoaders.train[source]

Training DataLoader

DataLoaders.valid[source]

Validation DataLoader

DataLoaders.train_ds[source]

Training Dataset

DataLoaders.valid_ds[source]

Validation Dataset

DataLoaders.to[source]

DataLoaders.to(device)

Use device

DataLoaders.cuda[source]

DataLoaders.cuda()

Use the gpu if available

DataLoaders.cpu[source]

DataLoaders.cpu()

Use the cpu

DataLoaders.new_empty[source]

DataLoaders.new_empty()

Create a new empty version of self with the same transforms

DataLoaders.from_dblock[source]

DataLoaders.from_dblock(dblock, source, path='.', bs=64, val_bs=None, shuffle_train=True, device=None, **kwargs)

Create a dataloaders from a given dblock

In the example above ↑

  • The * tells nbdev_show_doc to show all public members
  • in this case, nbdev_show_doc sees everything in _docs as a public member
    • hopefully this is a good shortcut if you use fastcore @docs

show doc with nested classes, enums etc

from enum import Enum

class Thing():
    "A thing"
    def __init__(self):
        "initialize a thing"
    def do_something(self):
        "make the thing do something"
    def a(self):
        "a does something ..."
    @property
    def c_thing(self):
        "c is a property of Thing"
        return 5
    _docs=dict(int='This element is not part of Thing',)
        
class Color(Thing):
    "A few colors"
    RED=1
    GREEN=2
    def __init__(self):
        "initialize a color"
        self.x='should make no difference'
    def a(self):
        "a does nothing"
    def b(self, a):
        "b does nothing with `a`"
    @property
    def c(self):
        "c is a property"
        return 5
    class Hue():
        "The hue of a color"
        def apply(self):
            "Apply this hue"
    class Shade(Enum):
        "The shade"
        DARK=0
        LIGHT=1

Color.Hue.apply[source]

Color.Hue.apply()

Apply this hue

Color.Shade.LIGHT[source]

The shade

'just_because_you_can...'[source]

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

class Color[source]

Color() :: Thing

A few colors

class Color.Hue[source]

Color.Hue()

The hue of a color

Color.Shade[source]

Enum = [DARK, LIGHT]

The shade

Color.__init__[source]

Color.__init__()

initialize a color

Color.a[source]

Color.a()

a does nothing

Color.b[source]

Color.b(a)

b does nothing with a

Color.c[source]

c is a property

Color.RED[source]

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.int(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

int('0b100', base=0) 4

In the example above ↑

  • The * tells nbdev_show_doc to show all public members
  • in this case, Color doesn't have _docs so we use "inspect" logic and list members that:
    • don't start with single _
    • are defined by the element being inspected
    • are either executable or are properties

In the example below ↓

  • Thing does have _docs but it's not valid
  • so we fall back to the "inspect" logic

class Thing[source]

Thing()

A thing

Thing.__init__[source]

Thing.__init__()

initialize a thing

Thing.a[source]

Thing.a()

a does something ...

Thing.c_thing[source]

c is a property of Thing

Thing.do_something[source]

Thing.do_something()

make the thing do something

nested functions etc

func[source]

func()

Main function

func.tagged_on_thing[source]

func.tagged_on_thing()

function 2

checking auto show doc

Unless we show doc for these elements in the notebook, add_show_docs will auto show:

from fastai2.imports import *
class A:
    "a class"
    class NestedA:
        "a nested class"
class B():
    "b class"
def c():
    "c func"
d="d is an object def"
@patch
def e(x:A.NestedA):
    "patch e onto A.NestedA"

c[source]

c()

c func

class A[source]

A()

a class

class B[source]

B()

b class

class A.NestedA[source]

A.NestedA()

a nested class

A.NestedA.e[source]

A.NestedA.e(x:NestedA)

patch e onto A.NestedA

is it a problem that

  • %nbdev_show_doc -> A.NestedA.e (this one gets doc linked)
  • show_doc-> NestedA.e

NestedA.e[source]

NestedA.e(x:NestedA)

patch e onto A.NestedA