Skip to content

Commit c5d926c

Browse files
committed
feat: added cards dummy implementation
1 parent bd52caa commit c5d926c

File tree

11 files changed

+780
-196
lines changed

11 files changed

+780
-196
lines changed

README.md

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,38 @@
1-
# python_nbdev_starter
1+
# nbdev_cards
22

33

44
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
55

6-
This file will become your README and also the index of your
7-
documentation.
6+
A deck of cards demo of [nbdev](https://nbdev.fast.ai) based on ideas
7+
from [Think Python 2nd
8+
Edition](https://greenteapress.com/wp/think-python-2e/) by Allen B.
9+
Downey.
810

9-
## Developer Guide
11+
## Install
1012

11-
If you are new to using `nbdev` here are some useful pointers to get you
12-
started.
13+
Install using:
1314

14-
### Install python_nbdev_starter in Development mode
15+
pip install nbdev-cards
1516

16-
``` sh
17-
# make sure python_nbdev_starter package is installed in development mode
18-
$ pip install -e .
17+
or:
1918

20-
# make changes under nbs/ directory
21-
# ...
19+
conda install -c fastai nbdev-cards
2220

23-
# compile to have changes apply to python_nbdev_starter
24-
$ nbdev_prepare
25-
```
26-
27-
## Usage
28-
29-
### Installation
30-
31-
Install latest from the GitHub
32-
[repository](https://github.com/moneebullah25/python_nbdev_starter):
33-
34-
``` sh
35-
$ pip install git+https://github.com/moneebullah25/python_nbdev_starter.git
36-
```
37-
38-
or from [conda](https://anaconda.org/moneebullah25/python_nbdev_starter)
39-
40-
``` sh
41-
$ conda install -c moneebullah25 python_nbdev_starter
42-
```
21+
## How to use
4322

44-
or from [pypi](https://pypi.org/project/python_nbdev_starter/)
23+
This lib provides a `Card` class you can use to create, display, and
24+
compare playing cards:
4525

46-
``` sh
47-
$ pip install python_nbdev_starter
26+
``` python
27+
Card(1,3)
4828
```
4929

50-
### Documentation
51-
52-
Documentation can be found hosted on this GitHub
53-
[repository](https://github.com/moneebullah25/python_nbdev_starter)’s
54-
[pages](https://moneebullah25.github.io/python_nbdev_starter/).
55-
Additionally you can find package manager specific guidelines on
56-
[conda](https://anaconda.org/moneebullah25/python_nbdev_starter) and
57-
[pypi](https://pypi.org/project/python_nbdev_starter/) respectively.
58-
59-
## How to use
30+
3♦️
6031

61-
Fill me in please! Don’t forget code examples:
32+
Suits are numbered according to this list:
6233

6334
``` python
64-
1+1
35+
suits
6536
```
6637

67-
2
38+
['♣️', '♦️', '❤️', '♠️']

nbs/00_card.ipynb

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#| default_exp card"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"# card--A basic playing card\n",
17+
"\n",
18+
"> A simple API for creating and using playing cards"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"#| export\n",
28+
"from fastcore.utils import *"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"#| hide\n",
38+
"from nbdev.showdoc import *\n",
39+
"from fastcore.test import *"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"#| export\n",
49+
"suits = [\"♣️\",\"♦️\",\"❤️\",\"♠️\"]\n",
50+
"ranks = [None, \"A\"] + [str(x) for x in range(2,11)] + [\"J\", \"Q\", \"K\"]"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"metadata": {},
56+
"source": [
57+
"We will be using numbers to represent playing card clubs and ranks. These are the suits:"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"data": {
67+
"text/plain": [
68+
"['♣️', '♦️', '❤️', '♠️']"
69+
]
70+
},
71+
"execution_count": null,
72+
"metadata": {},
73+
"output_type": "execute_result"
74+
}
75+
],
76+
"source": [
77+
"suits"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"For instance the suit at index `0`:"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"'♣️'"
96+
]
97+
},
98+
"execution_count": null,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"suits[0]"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"These are the ranks:"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": null,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"[None, 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']"
123+
]
124+
},
125+
"execution_count": null,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"ranks"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"For instance the rank at index `1` (note that there isn't a playing card at position `0`, since we want the ranks to match the indicies where possible):"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [
146+
{
147+
"data": {
148+
"text/plain": [
149+
"'A'"
150+
]
151+
},
152+
"execution_count": null,
153+
"metadata": {},
154+
"output_type": "execute_result"
155+
}
156+
],
157+
"source": [
158+
"ranks[1]"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": [
167+
"#| export\n",
168+
"class Card:\n",
169+
" \"A playing card\"\n",
170+
" def __init__(self,\n",
171+
" suit:int, # An index into `suits`\n",
172+
" rank:int): # An index into `ranks`\n",
173+
" self.suit,self.rank = suit,rank\n",
174+
" def __str__(self): return f\"{ranks[self.rank]}{suits[self.suit]}\"\n",
175+
" __repr__ = __str__"
176+
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"metadata": {},
181+
"source": [
182+
"Here's an example of creating and displaying a card:"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": null,
188+
"metadata": {},
189+
"outputs": [
190+
{
191+
"data": {
192+
"text/plain": [
193+
"3♦️"
194+
]
195+
},
196+
"execution_count": null,
197+
"metadata": {},
198+
"output_type": "execute_result"
199+
}
200+
],
201+
"source": [
202+
"c = Card(suit=1, rank=3)\n",
203+
"c"
204+
]
205+
},
206+
{
207+
"cell_type": "markdown",
208+
"metadata": {},
209+
"source": [
210+
"## Comparison operators"
211+
]
212+
},
213+
{
214+
"cell_type": "markdown",
215+
"metadata": {},
216+
"source": [
217+
"Equality, less than, and greater than work on the rank and suit indices."
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": null,
223+
"metadata": {},
224+
"outputs": [],
225+
"source": [
226+
"#| export\n",
227+
"@patch\n",
228+
"def __eq__(self:Card, a:Card): return (self.suit,self.rank)==(a.suit,a.rank)\n",
229+
"@patch\n",
230+
"def __lt__(self:Card, a:Card): return (self.suit,self.rank)<(a.suit,a.rank)\n",
231+
"@patch\n",
232+
"def __gt__(self:Card, a:Card): return (self.suit,self.rank)>(a.suit,a.rank)"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"For instance, here's a test of equality..."
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"metadata": {},
246+
"outputs": [],
247+
"source": [
248+
"test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3))"
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"metadata": {},
255+
"outputs": [],
256+
"source": [
257+
"#| hide\n",
258+
"test_ne(Card(suit=2, rank=3), Card(suit=1, rank=3))\n",
259+
"test_ne(Card(suit=1, rank=2), Card(suit=1, rank=3))"
260+
]
261+
},
262+
{
263+
"cell_type": "markdown",
264+
"metadata": {},
265+
"source": [
266+
"...and a test of `<`..."
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"metadata": {},
273+
"outputs": [],
274+
"source": [
275+
"assert Card(suit=1, rank=3)<Card(suit=2, rank=3)"
276+
]
277+
},
278+
{
279+
"cell_type": "markdown",
280+
"metadata": {},
281+
"source": [
282+
"...and finally of `>`:"
283+
]
284+
},
285+
{
286+
"cell_type": "code",
287+
"execution_count": null,
288+
"metadata": {},
289+
"outputs": [],
290+
"source": [
291+
"assert Card(suit=3, rank=3)>Card(suit=2, rank=3)\n",
292+
"assert not Card(suit=1, rank=3)>Card(suit=2, rank=3)"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": []
301+
}
302+
],
303+
"metadata": {
304+
"kernelspec": {
305+
"display_name": "python3",
306+
"language": "python",
307+
"name": "python3"
308+
}
309+
},
310+
"nbformat": 4,
311+
"nbformat_minor": 4
312+
}

0 commit comments

Comments
 (0)