Skip to content

Commit

Permalink
Change table format (#13)
Browse files Browse the repository at this point in the history
* Use a cleaner table format for output

The old table format used for the command outputs was clean but it was
hard to distinguish between the table and information outside the table
(like total capital gains). I changed the table format to use `psql`
format which adds borders around the table headers.

Also included a minor change where I changed the poetry package name to
cad-capgains. This is the name that people will use when downloading the
package off PyPi. The CLI command still remains as 'capgains', as does
the python module.

* Fix total ACB not subtracting superficial loss

We had a bug where we weren't subtracting the superficial loss from the
running ACB. Fixed this bug and fixed the test that should've uncovered
this.
  • Loading branch information
EmilMaric authored Sep 3, 2020
1 parent c197807 commit 633b7c2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 63 deletions.
4 changes: 2 additions & 2 deletions capgains/commands/capgains_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def capgains_calc(transactions, year, tickers=None):
click.echo("No capital gains\n")
continue
total_gains = _get_total_gains(calculated_dicts)
click.echo("[Total Gains = {0:,.2f}]\n".format(total_gains))
click.echo("[Total Gains = {0:,.2f}]".format(total_gains))
headers = calculated_dicts[0].keys()
rows = [t.values() for t in calculated_dicts]
output = tabulate.tabulate(rows, headers=headers,
output = tabulate.tabulate(rows, headers=headers, tablefmt="psql",
colalign=colalign, floatfmt=floatfmt)
click.echo("{}\n".format(output))
2 changes: 1 addition & 1 deletion capgains/commands/capgains_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ def capgains_show(transactions, tickers=None):
headers = transaction_dicts[0].keys()
rows = [t.values() for t in transaction_dicts]
output = tabulate.tabulate(rows, headers=headers, colalign=colalign,
floatfmt=floatfmt)
tablefmt="psql", floatfmt=floatfmt)
click.echo(output)
6 changes: 4 additions & 2 deletions capgains/ticker_gains.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def add_transactions(self, transactions, exchange_rates):
transaction.superficial_loss = \
self._is_superficial_loss(transaction, transactions)
if transaction.superficial_loss:
transaction.acb -= transaction.capital_gain
transaction.acb_delta -= transaction.capital_gain
superficial_loss = transaction.capital_gain
transaction.acb -= superficial_loss
transaction.acb_delta -= superficial_loss
transaction.capital_gain = 0
self._total_acb -= superficial_loss

def _superficial_window_filter(self, transaction, min_date, max_date):
"""Filter out BUY transactions that fall within
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
[tool.poetry]
name = "capgains"
# Store the file as 'cad-capgains' on PyPi, but have the CLI command be named
# 'capgains'
name = "cad-capgains"
version = "0.1.0"
description = "A CLI tool to calculate your capital gains"
license = "MIT"
readme = "README.md"
authors = ["Emil Maric"]
packages = [
{ include = "capgains" },
]

[tool.poetry.dependencies]
python = "^3.5"
Expand Down
44 changes: 25 additions & 19 deletions tests/test_capgains.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ def test_show_no_ticker_arg(testfiles_dir, transactions):

assert result.exit_code == 0
assert result.output == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2017-02-15 ESPP PURCHASE ANET BUY 100 50.00 10.00 USD
2018-02-20 RSU VEST GOOGL BUY 30 20.00 10.00 USD
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD
2019-02-15 ESPP PURCHASE ANET BUY 50 130.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2017-02-15 | ESPP PURCHASE | ANET | BUY | 100 | 50.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | GOOGL | BUY | 30 | 20.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD |
| 2019-02-15 | ESPP PURCHASE | ANET | BUY | 50 | 130.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501


Expand All @@ -56,11 +58,13 @@ def test_show_ticker_arg(testfiles_dir, transactions):

assert result.exit_code == 0
assert result.output == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2017-02-15 ESPP PURCHASE ANET BUY 100 50.00 10.00 USD
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD
2019-02-15 ESPP PURCHASE ANET BUY 50 130.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2017-02-15 | ESPP PURCHASE | ANET | BUY | 100 | 50.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD |
| 2019-02-15 | ESPP PURCHASE | ANET | BUY | 50 | 130.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501


Expand All @@ -78,10 +82,11 @@ def test_calc_no_ticker_arg(testfiles_dir, transactions, exchange_rates_mock):
assert result.output == """\
ANET-2018
[Total Gains = 6,970.00]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- ------- ------------ ---------- --------------- ---------- -------------- ----------- --------
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD 50 11,980.00 6,970.00 -5,010.00 5,010.00
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------|
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD | 50 | 11,980.00 | 6,970.00 | -5,010.00 | 5,010.00 |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
GOOGL-2018
No capital gains
Expand All @@ -103,10 +108,11 @@ def test_calc_ticker_arg(testfiles_dir, transactions, exchange_rates_mock):
assert result.output == """\
ANET-2018
[Total Gains = 6,970.00]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- ------- ------------ ---------- --------------- ---------- -------------- ----------- --------
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD 50 11,980.00 6,970.00 -5,010.00 5,010.00
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------|
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD | 50 | 11,980.00 | 6,970.00 | -5,010.00 | 5,010.00 |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
""" # noqa: E501

Expand Down
38 changes: 21 additions & 17 deletions tests/test_capgains_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def test_no_ticker(transactions, capfd, exchange_rates_mock):
assert out == """\
ANET-2018
[Total Gains = 6,970.00]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- ------- ------------ ---------- --------------- ---------- -------------- ----------- --------
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD 50 11,980.00 6,970.00 -5,010.00 5,010.00
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------|
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD | 50 | 11,980.00 | 6,970.00 | -5,010.00 | 5,010.00 |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
GOOGL-2018
No capital gains
Expand All @@ -30,10 +31,11 @@ def test_tickers(transactions, capfd, exchange_rates_mock):
assert out == """\
ANET-2018
[Total Gains = 6,970.00]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- ------- ------------ ---------- --------------- ---------- -------------- ----------- --------
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD 50 11,980.00 6,970.00 -5,010.00 5,010.00
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------|
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD | 50 | 11,980.00 | 6,970.00 | -5,010.00 | 5,010.00 |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+----------+
""" # noqa: E501

Expand Down Expand Up @@ -99,11 +101,12 @@ def test_superficial_loss_not_displayed(capfd, exchange_rates_mock):
out, _ = capfd.readouterr()
assert out == """\
ANET-2018
[Total Gains = 1,779.80]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- -------- ------------ ---------- --------------- ---------- -------------- ----------- -----
2018-12-01 RSU VEST ANET SELL 1 1,000.00 10.00 USD 0 1,980.00 1,779.80 -200.20 0.00
[Total Gains = -8,160.00]
+------------+--------------------+----------+----------+-------+----------+--------------+------------+-----------------+------------+----------------+-------------+-------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+----------+--------------+------------+-----------------+------------+----------------+-------------+-------|
| 2018-12-01 | RSU VEST | ANET | SELL | 1 | 1,000.00 | 10.00 | USD | 0 | 1,980.00 | -8,160.00 | -10,140.00 | 0.00 |
+------------+--------------------+----------+----------+-------+----------+--------------+------------+-----------------+------------+----------------+-------------+-------+
""" # noqa: E501

Expand Down Expand Up @@ -145,9 +148,10 @@ def test_calc_mixed_currencies(capfd, requests_mock):
assert out == """\
ANET-2018
[Total Gains = -5,000.00]
date transaction_type ticker action qty price commission currency share_balance proceeds capital_gain acb_delta acb
---------- ------------------ -------- -------- ----- ------- ------------ ---------- --------------- ---------- -------------- ----------- -----
2018-02-20 RSU VEST ANET SELL 100 50.00 0.00 CAD 0 5,000.00 -5,000.00 -10,000.00 0.00
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+-------+
| date | transaction_type | ticker | action | qty | price | commission | currency | share_balance | proceeds | capital_gain | acb_delta | acb |
|------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+-------|
| 2018-02-20 | RSU VEST | ANET | SELL | 100 | 50.00 | 0.00 | CAD | 0 | 5,000.00 | -5,000.00 | -10,000.00 | 0.00 |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+-----------------+------------+----------------+-------------+-------+
""" # noqa: E501
48 changes: 28 additions & 20 deletions tests/test_capgains_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ def test_no_filter(transactions, capfd):
CapGainsShow.capgains_show(transactions)
out, _ = capfd.readouterr()
assert out == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2017-02-15 ESPP PURCHASE ANET BUY 100 50.00 10.00 USD
2018-02-20 RSU VEST GOOGL BUY 30 20.00 10.00 USD
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD
2019-02-15 ESPP PURCHASE ANET BUY 50 130.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2017-02-15 | ESPP PURCHASE | ANET | BUY | 100 | 50.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | GOOGL | BUY | 30 | 20.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD |
| 2019-02-15 | ESPP PURCHASE | ANET | BUY | 50 | 130.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501


Expand All @@ -29,11 +31,13 @@ def test_ticker(transactions, capfd):
CapGainsShow.capgains_show(transactions, tickers=['ANET'])
out, _ = capfd.readouterr()
assert out == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2017-02-15 ESPP PURCHASE ANET BUY 100 50.00 10.00 USD
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD
2019-02-15 ESPP PURCHASE ANET BUY 50 130.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2017-02-15 | ESPP PURCHASE | ANET | BUY | 100 | 50.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD |
| 2019-02-15 | ESPP PURCHASE | ANET | BUY | 50 | 130.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501


Expand All @@ -55,9 +59,11 @@ def test_known_ticker_and_unknown_ticker(transactions, capfd):
CapGainsShow.capgains_show(transactions, ['GOOGL', 'FB'])
out, _ = capfd.readouterr()
assert out == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2018-02-20 RSU VEST GOOGL BUY 30 20.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2018-02-20 | RSU VEST | GOOGL | BUY | 30 | 20.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501


Expand All @@ -68,10 +74,12 @@ def test_multiple_tickers(transactions, capfd):
CapGainsShow.capgains_show(transactions, ['ANET', 'GOOGL'])
out, _ = capfd.readouterr()
assert out == """\
date transaction_type ticker action qty price commission currency
---------- ------------------ -------- -------- ----- ------- ------------ ----------
2017-02-15 ESPP PURCHASE ANET BUY 100 50.00 10.00 USD
2018-02-20 RSU VEST GOOGL BUY 30 20.00 10.00 USD
2018-02-20 RSU VEST ANET SELL 50 120.00 10.00 USD
2019-02-15 ESPP PURCHASE ANET BUY 50 130.00 10.00 USD
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
| date | transaction_type | ticker | action | qty | price | commission | currency |
|------------+--------------------+----------+----------+-------+---------+--------------+------------|
| 2017-02-15 | ESPP PURCHASE | ANET | BUY | 100 | 50.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | GOOGL | BUY | 30 | 20.00 | 10.00 | USD |
| 2018-02-20 | RSU VEST | ANET | SELL | 50 | 120.00 | 10.00 | USD |
| 2019-02-15 | ESPP PURCHASE | ANET | BUY | 50 | 130.00 | 10.00 | USD |
+------------+--------------------+----------+----------+-------+---------+--------------+------------+
""" # noqa: E501
Loading

0 comments on commit 633b7c2

Please sign in to comment.