Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
system-design-primer
Commits
7fb0ca88
Commit
7fb0ca88
authored
Mar 04, 2017
by
Donne Martin
Browse files
Add Mint solution
parent
831906f9
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
solutions/system_design/mint/README.md
0 → 100644
View file @
7fb0ca88
This diff is collapsed.
Click to expand it.
solutions/system_design/mint/__init__.py
0 → 100644
View file @
7fb0ca88
solutions/system_design/mint/mint.png
0 → 100644
View file @
7fb0ca88
290 KB
solutions/system_design/mint/mint_basic.png
0 → 100644
View file @
7fb0ca88
119 KB
solutions/system_design/mint/mint_mapreduce.py
0 → 100644
View file @
7fb0ca88
# -*- coding: utf-8 -*-
from
mrjob.job
import
MRJob
class
SpendingByCategory
(
MRJob
):
def
__init__
(
self
,
categorizer
):
self
.
categorizer
=
categorizer
...
def
current_year_month
(
self
):
"""Return the current year and month."""
...
def
extract_year_month
(
self
,
timestamp
):
"""Return the year and month portions of the timestamp."""
...
def
handle_budget_notifications
(
self
,
key
,
total
):
"""Call notification API if nearing or exceeded budget."""
...
def
mapper
(
self
,
_
,
line
):
"""Parse each log line, extract and transform relevant lines.
Emit key value pairs of the form:
(2016-01, shopping), 25
(2016-01, shopping), 100
(2016-01, gas), 50
"""
timestamp
,
seller
,
amount
=
line
.
split
(
'
\t
'
)
period
=
self
.
extract_year_month
(
timestamp
)
if
period
==
self
.
current_year_month
():
yield
(
period
,
category
),
amount
def
reducer
(
self
,
key
,
value
):
"""Sum values for each key.
(2016-01, shopping), 125
(2016-01, gas), 50
"""
total
=
sum
(
values
)
self
.
handle_budget_notifications
(
key
,
total
)
yield
key
,
sum
(
values
)
def
steps
(
self
):
"""Run the map and reduce steps."""
return
[
self
.
mr
(
mapper
=
self
.
mapper
,
reducer
=
self
.
reducer
)
]
if
__name__
==
'__main__'
:
SpendingByCategory
.
run
()
solutions/system_design/mint/mint_snippets.py
0 → 100644
View file @
7fb0ca88
# -*- coding: utf-8 -*-
class
DefaultCategories
(
Enum
):
HOUSING
=
0
FOOD
=
1
GAS
=
2
SHOPPING
=
3
...
seller_category_map
=
{}
seller_category_map
[
'Exxon'
]
=
DefaultCategories
.
GAS
seller_category_map
[
'Target'
]
=
DefaultCategories
.
SHOPPING
class
Categorizer
(
object
):
def
__init__
(
self
,
seller_category_map
,
seller_category_overrides_map
):
self
.
seller_category_map
=
seller_category_map
self
.
seller_category_overrides_map
=
seller_category_overrides_map
def
categorize
(
self
,
transaction
):
if
transaction
.
seller
in
self
.
seller_category_map
:
return
self
.
seller_category_map
[
transaction
.
seller
]
if
transaction
.
seller
in
self
.
seller_category_overrides_map
:
seller_category_map
[
transaction
.
seller
]
=
\
self
.
manual_overrides
[
transaction
.
seller
].
peek_min
()
return
self
.
seller_category_map
[
transaction
.
seller
]
return
None
class
Transaction
(
object
):
def
__init__
(
self
,
timestamp
,
seller
,
amount
):
self
.
timestamp
=
timestamp
self
.
seller
=
seller
self
.
amount
=
amount
class
Budget
(
object
):
def
__init__
(
self
,
template_categories_to_budget_map
):
self
.
categories_to_budget_map
=
template_categories_to_budget_map
def
override_category_budget
(
self
,
category
,
amount
):
self
.
categories_to_budget_map
[
category
]
=
amount
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment