-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1_return.py
84 lines (64 loc) · 2.37 KB
/
1_return.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""An example of calculating non-sufficient fund (NSF) amount from
a user's transactions
"""
from chalk import online
from chalk.features import features, DataFrame, FeatureTime
from datetime import datetime
import pytz
@features
class Transaction:
id: str
amount: float
memo: str
on: FeatureTime
user_id: "User.id"
user: "User"
# Computed properties
clean_memo: str
is_nsf: bool
@features
class User:
id: int
transactions: DataFrame[Transaction]
# Computed properties
nsf_amount: float
@online
def get_clean_memo(memo: Transaction.memo) -> Transaction.clean_memo:
computed = memo.lower()
for prefix in ("sale", "pos", "tst", "sq"):
computed = computed.removeprefix(prefix).strip()
return computed
@online
def get_transaction_is_nsf(
memo_clean: Transaction.clean_memo,
) -> Transaction.is_nsf:
return "nsf" in memo_clean.lower()
@online
def get_nsf_amount(amounts: User.transactions[Transaction.is_nsf is True, Transaction.amount]) -> User.nsf_amount:
"""
In this resolver, we calculate the total NSF ammount for our users.
"""
return amounts.sum()
# Below we generate a couple dummy resolvers to make the example fully runnable without connected
# datasources.
@online
def get_test_users() -> DataFrame[User.id]:
return DataFrame([
User(id=1),
User(id=2),
])
@online
def get_test_transactions() -> (
DataFrame[Transaction.id, Transaction.user_id, Transaction.amount, Transaction.memo, Transaction.on]
):
return DataFrame([
Transaction(id=1, user_id=1, amount=-277.0, memo="directdep", on=datetime(2014, 8, 12)),
Transaction(id=2, user_id=1, amount=-10_001.0, memo="other", on=datetime(2014, 8, 12)),
Transaction(id=3, user_id=1, amount=42.1, memo="tetst nsf", on=datetime(2014,8, 12)),
Transaction(id=4, user_id=1, amount=-1303.0, memo="paycheck", on=datetime(2014,8, 12)),
Transaction(id=5, user_id=1, amount=124.0, memo="test", on=datetime(2014,8, 12)),
Transaction(id=7, user_id=2, amount=2132.04, memo="undefined", on=datetime(2014,8, 12)),
Transaction(id=6, user_id=2, amount=-1.0, memo="sale nsf", on=datetime(2014,8, 12)),
Transaction(id=8, user_id=2, amount=-30.0, memo="tst nsf", on=datetime(2014,8, 12)),
Transaction(id=9, user_id=2, amount=-999.99, memo="payroll", on=datetime(2014,8, 12)),
])