Skip to content

Commit c477955

Browse files
committed
add supermarket katas
1 parent 233d0d0 commit c477955

22 files changed

Lines changed: 730 additions & 0 deletions

src/supermarket_receipt/.gitignore

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
.pytest_cache
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask instance folder
58+
instance/
59+
60+
# Sphinx documentation
61+
docs/_build/
62+
63+
# MkDocs documentation
64+
/site/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
venv
75+
.venv
76+
77+
.idea
78+
.vscode

src/supermarket_receipt/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Supermarket Receipt in [Python](https://www.python.org/)
2+
3+
## Setup
4+
5+
* Have Python installed
6+
* Clone the repository
7+
* On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory
8+
* On the command line, install requirements, e.g. on the`python -m pip install -r requirements.txt`
9+
10+
## Running Tests
11+
12+
On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory and run
13+
14+
```
15+
pytest
16+
```
17+
18+
## Optional: Running [TextTest](https://www.texttest.org/) Tests
19+
20+
Install TextTest according to the [instructions](https://www.texttest.org/index.html#getting-started-with-texttest) (platform specific).
21+
22+
On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory and run
23+
24+
```
25+
texttest -a sr -d .
26+
```

src/supermarket_receipt/catalog.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
class SupermarketCatalog:
3+
4+
def add_product(self, product, price):
5+
raise Exception("cannot be called from a unit test - it accesses the database")
6+
7+
def unit_price(self, product):
8+
raise Exception("cannot be called from a unit test - it accesses the database")
9+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from enum import Enum
2+
3+
4+
class Product:
5+
def __init__(self, name, unit):
6+
self.name = name
7+
self.unit = unit
8+
9+
10+
class ProductQuantity:
11+
def __init__(self, product, quantity):
12+
self.product = product
13+
self.quantity = quantity
14+
15+
16+
class ProductUnit(Enum):
17+
EACH = 1
18+
KILO = 2
19+
20+
21+
class SpecialOfferType(Enum):
22+
THREE_FOR_TWO = 1
23+
TEN_PERCENT_DISCOUNT = 2
24+
TWO_FOR_AMOUNT = 3
25+
FIVE_FOR_AMOUNT = 4
26+
27+
class Offer:
28+
def __init__(self, offer_type, product, argument):
29+
self.offer_type = offer_type
30+
self.product = product
31+
self.argument = argument
32+
33+
34+
class Discount:
35+
def __init__(self, product, description, discount_amount):
36+
self.product = product
37+
self.description = description
38+
self.discount_amount = discount_amount
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
.pytest_cache
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask instance folder
58+
instance/
59+
60+
# Sphinx documentation
61+
docs/_build/
62+
63+
# MkDocs documentation
64+
/site/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
venv
75+
.venv
76+
77+
.idea
78+
.vscode
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Supermarket Receipt in [Python](https://www.python.org/)
2+
3+
## Setup
4+
5+
* Have Python installed
6+
* Clone the repository
7+
* On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory
8+
* On the command line, install requirements, e.g. on the`python -m pip install -r requirements.txt`
9+
10+
## Running Tests
11+
12+
On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory and run
13+
14+
```
15+
pytest
16+
```
17+
18+
## Optional: Running [TextTest](https://www.texttest.org/) Tests
19+
20+
Install TextTest according to the [instructions](https://www.texttest.org/index.html#getting-started-with-texttest) (platform specific).
21+
22+
On the command line, enter the `SupermarketReceipt-Refactoring-Kata/python` directory and run
23+
24+
```
25+
texttest -a sr -d .
26+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
class SupermarketCatalog:
3+
4+
def add_product(self, product, price):
5+
raise Exception("cannot be called from a unit test - it accesses the database")
6+
7+
def unit_price(self, product):
8+
raise Exception("cannot be called from a unit test - it accesses the database")
9+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from enum import Enum
2+
3+
4+
class Product:
5+
def __init__(self, name, unit):
6+
self.name = name
7+
self.unit = unit
8+
9+
10+
class ProductQuantity:
11+
def __init__(self, product, quantity):
12+
self.product = product
13+
self.quantity = quantity
14+
15+
16+
class ProductUnit(Enum):
17+
EACH = 1
18+
KILO = 2
19+
20+
21+
class SpecialOfferType(Enum):
22+
THREE_FOR_TWO = 1
23+
TEN_PERCENT_DISCOUNT = 2
24+
TWO_FOR_AMOUNT = 3
25+
FIVE_FOR_AMOUNT = 4
26+
27+
class Offer:
28+
def __init__(self, offer_type, product, argument):
29+
self.offer_type = offer_type
30+
self.product = product
31+
self.argument = argument
32+
33+
34+
class Discount:
35+
def __init__(self, product, description, discount_amount):
36+
self.product = product
37+
self.description = description
38+
self.discount_amount = discount_amount
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
class ReceiptItem:
3+
def __init__(self, product, quantity, price, total_price):
4+
self.product = product
5+
self.quantity = quantity
6+
self.price = price
7+
self.total_price = total_price
8+
9+
10+
class Receipt:
11+
def __init__(self):
12+
self._items = []
13+
self._discounts = []
14+
15+
def total_price(self):
16+
total = 0
17+
for item in self.items:
18+
total += item.total_price
19+
for discount in self.discounts:
20+
total += discount.discount_amount
21+
return total
22+
23+
def add_product(self, product, quantity, price, total_price):
24+
self._items.append(ReceiptItem(product, quantity, price, total_price))
25+
26+
def add_discount(self, discount):
27+
self._discounts.append(discount)
28+
29+
@property
30+
def items(self):
31+
return self._items[:]
32+
33+
@property
34+
def discounts(self):
35+
return self._discounts[:]

0 commit comments

Comments
 (0)