Skip to content

Commit 59e8134

Browse files
committed
e1007_manipulation.py
1 parent 2daa02e commit 59e8134

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/data-scratch-library/dsl/c10_working_with_data/e1007_manipulation.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import defaultdict
22
from dataclasses import dataclass
33
from datetime import date
4-
from typing import List
4+
from typing import Dict
55

66
from dsl.c10_working_with_data.e1004_named_tuples import StockPrice
77

@@ -72,10 +72,11 @@ def find_largest_and_smallest_changes(data):
7272

7373
def average_daily_change_by_month(all_changes):
7474
"""Return the average daily percentage change by month."""
75-
changes_by_month: List[DailyChange] = {month: [] for month in range(1, 13)}
75+
changes_by_month: Dict[int, DailyChange] = {month: [] for month in range(1, 13)}
7676
for change in all_changes:
7777
changes_by_month[change.date.month].append(change)
7878
avg_daily_change = {
79-
month: sum(change.pct_change for change in changes) / len(changes)
79+
month: sum(change.pct_change for change in changes) / len(all_changes)
8080
for month, changes in changes_by_month.items()
8181
}
82+
return avg_daily_change

src/data-scratch-library/tests/test_c10_working_with_data/test_e1007_manipulation.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1+
from datetime import date
2+
13
from dsl.c10_working_with_data.e1007_manipulation import average_daily_change_by_month, DailyChange, \
24
find_largest_and_smallest_changes, StockPrice, day_over_day_changes, max_prices_by_symbol, max_stock_price
35

46

57
def test_max_stock_price():
6-
data = [StockPrice("AAPL", "2023-10-01", 150.0),
7-
StockPrice("AAPL", "2023-10-02", 155.0)]
8+
data = [StockPrice("AAPL", date(2023, 10, 1), 150.0),
9+
StockPrice("AAPL", date(2023, 10, 2), 155.0)]
810
assert max_stock_price(data, 'AAPL') == 155.0
911

12+
1013
def test_max_prices_by_symbol():
11-
data = [StockPrice("AAPL", "2023-10-01", 150.0),
12-
StockPrice("AAPL", "2023-10-02", 155.0),
13-
StockPrice("MSFT", "2023-10-01", 300.0),
14-
StockPrice("MSFT", "2023-10-02", 305.0)]
14+
data = [StockPrice("AAPL", date(2023, 10, 1), 150.0),
15+
StockPrice("AAPL", date(2023, 10, 2), 155.0),
16+
StockPrice("MSFT", date(2023, 10, 1), 300.0),
17+
StockPrice("MSFT", date(2023, 10, 2), 305.0)]
1518
max_prices = max_prices_by_symbol(data)
1619
assert max_prices['AAPL'] == 155.0
1720
assert max_prices['MSFT'] == 305.0
1821

22+
1923
def test_day_over_day_changes():
20-
prices = [StockPrice("AAPL", "2023-10-01", 150.0),
21-
StockPrice("AAPL", "2023-10-02", 155.0)]
24+
prices = [StockPrice("AAPL", date(2023, 10, 1), 150.0),
25+
StockPrice("AAPL", date(2023, 10, 2), 155.0)]
2226
changes = day_over_day_changes(prices)
2327
assert len(changes) == 1
2428
assert changes[0].pct_change == (155.0 / 150.0 - 1)
2529

30+
2631
def test_find_largest_and_smallest_changes():
27-
data = [StockPrice("AAPL", "2023-10-01", 150.0),
28-
StockPrice("AAPL", "2023-10-02", 155.0),
29-
StockPrice("AAPL", "2023-10-03", 140.0)]
32+
data = [StockPrice("AAPL", date(2023, 10, 1), 150.0),
33+
StockPrice("AAPL", date(2023, 10, 2), 155.0),
34+
StockPrice("AAPL", date(2023, 10, 3), 140.0)]
3035
max_change, min_change = find_largest_and_smallest_changes(data)
3136
assert max_change.pct_change == (155.0 / 150.0 - 1)
3237
assert min_change.pct_change == (140.0 / 155.0 - 1)
3338

39+
3440
def test_average_daily_change_by_month():
3541
changes = [
36-
DailyChange("AAPL", "2023-10-01", 0.05),
37-
DailyChange("AAPL", "2023-10-02", -0.03)
42+
DailyChange("AAPL", date(2023, 10, 1), 0.05),
43+
DailyChange("AAPL", date(2023, 10, 2), -0.03)
3844
]
3945
averages = average_daily_change_by_month(changes)
4046
assert averages[10] == (0.05 - 0.03) / 2
41-

0 commit comments

Comments
 (0)