A Mimimal example of using nbdev
I am following nbdev.fast.ai's minimal example that uses code from Think Python by Allen Downey.
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import random
class Card:
"""Represents a standard playing card.
Attributes:
suit: integer 0-3
rank: integer 1-13
"""
suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]
rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"]
def __init__(self, suit=0, rank=2):
self.suit = suit
self.rank = rank
def __str__(self):
"""Returns a human-readable string representation."""
return '%s of %s' % (Card.rank_names[self.rank],
Card.suit_names[self.suit])
def __eq__(self, other):
"""Checks whether self and other have the same rank and suit.
returns: boolean
"""
return self.suit == other.suit and self.rank == other.rank
def __lt__(self, other):
"""Compares this card to other, first by suit, then rank.
returns: boolean
"""
t1 = self.suit, self.rank
t2 = other.suit, other.rank
return t1 < t2
def __repr__(self): return self.__str__()
Docs, tests and source code can be combined into the same context.
Comments and tests are rendered by the documentation system.
The assert
statements will automatically become tests that are run by the continuous integration system that is set up by default in nbdev
in your GitHub repository.
Card
is a class that represents a single card in a deck of cards. For example:
Card(suit=2, rank=11)
c = Card(suit=1,rank =3)
assert str(c) == '3 of Diamonds'
c2 = Card(suit=2, rank =11)
assert str(c2)=='Jack of Hearts'
You can do comparisons of cards too
assert c2 > c