mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-08-14 09:25:48 -04:00
Add some private projects
This commit is contained in:
parent
c8cd5cdbc4
commit
07aa882d51
80 changed files with 5216 additions and 41 deletions
3
soundcloud/solution/.gitignore
vendored
Normal file
3
soundcloud/solution/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.python-version
|
||||
__pycache__
|
||||
*.pyc
|
50
soundcloud/solution/README.md
Normal file
50
soundcloud/solution/README.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
## SoundCloud backend engineering code review interview exercise
|
||||
|
||||
### Introduction
|
||||
|
||||
Welcome to the SoundCloud backend Pull Request review challenge!
|
||||
|
||||
This repository contains a simulation of a microservice. The service is
|
||||
intentionally simplistic. The code for the service is python, but only using
|
||||
basic aspects of the language.
|
||||
We intend this to read like pseudocode - and in particular
|
||||
_we do not judge your submission based on your knowledge of Python_. Exposure to
|
||||
any popular programming language(s) should be enough to effectively work this exercise.
|
||||
|
||||
You will be reviewing Pull Request #1 (present under the "Pull Requests" tab).
|
||||
It's meant to be a simplified simulation of the sort of code review you might
|
||||
encounter in your everyday work.
|
||||
|
||||
> Also see: [a quick introduction to GitHub pull requests](https://help.github.com/en/articles/about-pull-request-reviews#about-pull-request-reviews).
|
||||
|
||||
## Your challenge
|
||||
|
||||
The code should be robust and reasonably feature-complete.
|
||||
We only want to merge this Pull Request once you and your (imaginary) colleague are confident that:
|
||||
|
||||
- The implementation actually supports the requested feature.
|
||||
- The implementation is likely to work, including proper handling of
|
||||
easily-foreseeable corner cases.
|
||||
- The implementation is not easy for a mailcious user to attack.
|
||||
- Other engineers can productively and confidently change this code, meaning:
|
||||
- The code is clear and straightforward to reason through.
|
||||
- It has high-quality unit tests and good test coverage.
|
||||
- It's well-organized and code elements have intention-revealing names.
|
||||
- There are comments, where those are useful.
|
||||
|
||||
In short, we are interested in gaining some insight into what is important to
|
||||
you when you review code. We'll be concentrating especially on what you feel
|
||||
is important to change, and the way you communicate that. Please provide change
|
||||
requests, ask questions, and otherwise make comments as you normally would when
|
||||
reviewing code.
|
||||
|
||||
The only GitHub review features that matter in this exercise are
|
||||
commenting-related: commenting on the whole Pull Request, and comments targeted at specific
|
||||
areas of the code. If you prefer to make use of Github's markdown features
|
||||
in your comments, please feel free to do so - but if you haven't used these features
|
||||
before, please do not use them.
|
||||
|
||||
As a general guideline, this exercise should take about 30-60 minutes. You are free
|
||||
to take more (or less) time than that, however.
|
||||
|
||||
When you're done, please notify your SoundCloud recruiter contact via email.
|
49
soundcloud/solution/src/clients/product_detail_client.py
Normal file
49
soundcloud/solution/src/clients/product_detail_client.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
"""
|
||||
Interview note:
|
||||
This is a client "stub", here for demonstration and documentation
|
||||
purposes only.
|
||||
|
||||
In a real production environment, this client would send requests over
|
||||
the network to a product detail backend service.
|
||||
|
||||
The product detail backend service is responsible for retrieving product
|
||||
detail records from its datastore (e.g., a mysql database), and
|
||||
returning them to callers.
|
||||
"""
|
||||
class ProductDetailClient:
|
||||
|
||||
def lookup(product_token):
|
||||
"""
|
||||
Given a product token, makes a blocking call to the lookup endpoint of
|
||||
the product detail backend service,
|
||||
and returns a corresponding product detail record, if found.
|
||||
If no record is found, returns None.
|
||||
|
||||
Example result:
|
||||
|
||||
{"token": "AAA",
|
||||
"description": "Red Bike",
|
||||
"price_cents": 45000}
|
||||
"""
|
||||
pass
|
||||
|
||||
def batch_lookup(product_tokens):
|
||||
"""
|
||||
Given a list of product tokens, makes a blocking call to the
|
||||
batch lookup endpoint of the product detail backend service,
|
||||
and returns a map of product token to product detail record, for all
|
||||
that are found.
|
||||
Any records that cannot be found are ommitted from the result.
|
||||
|
||||
Example result:
|
||||
|
||||
{"AAA": {
|
||||
"token": "AAA",
|
||||
"description": "Red Bike",
|
||||
"price_cents": 45000},
|
||||
"BBB": {
|
||||
"token": "BBB",
|
||||
"description": "Blue Bike",
|
||||
"price_cents": 37500}}
|
||||
"""
|
||||
pass
|
44
soundcloud/solution/src/clients/product_search_client.py
Normal file
44
soundcloud/solution/src/clients/product_search_client.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
"""
|
||||
Interview note:
|
||||
This is a client "stub", here for demonstration and documentation
|
||||
purposes only.
|
||||
|
||||
In a real production environment, this client would send requests over
|
||||
the network to a product search backend service.
|
||||
|
||||
The product search backend service is responsible for performing lookups
|
||||
against a product search index (e.g., elasticsearch) and returning product
|
||||
tokens that are search result "hits".
|
||||
"""
|
||||
class ProductSearchClient:
|
||||
|
||||
def search_for_all_in_price_range(lower_cents, upper_cents, start_record, num_records):
|
||||
"""
|
||||
Given a price range plus pagination information,
|
||||
returns a list of product tokens of products
|
||||
whose price falls in the range.
|
||||
|
||||
The price range is inclusive - that is,
|
||||
a given price "p" matches if:
|
||||
|
||||
upper_cents >= p >= lower_cents
|
||||
|
||||
If no products have prices in the price range,
|
||||
returns an empty list.
|
||||
|
||||
Product tokens are ordered by price, from lowest to highest.
|
||||
|
||||
Out of the total list of possible product tokens,
|
||||
only the "page" of product tokens starting at the list position
|
||||
start_record, and ending at start_record+num_records, are returned.
|
||||
|
||||
If start_record+num_records is greater than the number of actual
|
||||
result records, the resulting list size will be (accordingly)
|
||||
smaller than num_records.
|
||||
|
||||
Example result:
|
||||
|
||||
["AAA",
|
||||
"BBB"]
|
||||
"""
|
||||
pass
|
43
soundcloud/solution/src/service.py
Normal file
43
soundcloud/solution/src/service.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import re
|
||||
|
||||
TOKEN_REGEX = re.compile("^[0-9A-F-]+$")
|
||||
|
||||
class Service:
|
||||
def __init__(self, product_detail_client, product_search_client):
|
||||
self.product_detail_client = product_detail_client
|
||||
self.product_search_client = product_search_client
|
||||
|
||||
def lookup(self, product_token):
|
||||
"""
|
||||
Given a valid product token,
|
||||
lookup product detail information from the product detail backend service.
|
||||
|
||||
- Results in Bad Request if the token is invalid
|
||||
- Results in Not Found if the product is unknown to the backend service
|
||||
"""
|
||||
if not TOKEN_REGEX.match(product_token):
|
||||
return {"status": 400}
|
||||
|
||||
lookup_result = self.product_detail_client.lookup(product_token)
|
||||
if lookup_result:
|
||||
return {"status": 200, "product": lookup_result}
|
||||
else:
|
||||
return {"status": 404}
|
||||
|
||||
def search_by_price_range(self, lower_cents, upper_cents, start_record, num_records):
|
||||
page_of_product_tokens = \
|
||||
self.product_search_client.search_for_all_in_price_range(
|
||||
lower_cents,
|
||||
upper_cents,
|
||||
start_record,
|
||||
num_records)
|
||||
|
||||
product_detail_results = []
|
||||
for product_token in page_of_product_tokens:
|
||||
product_detail_results.append(self.lookup(product_token)["product"])
|
||||
|
||||
return {"status": 200, "products": product_detail_results}
|
||||
|
||||
if __name__ == "__main__":
|
||||
a = Service()
|
||||
print(a.lookup('aaa'))
|
95
soundcloud/solution/test/service_test.py
Normal file
95
soundcloud/solution/test/service_test.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../src"))
|
||||
|
||||
from service import Service
|
||||
|
||||
|
||||
class ProductDetailClientTestDouble:
|
||||
def __init__(self):
|
||||
self.contents = {}
|
||||
|
||||
def lookup(self, product_token):
|
||||
if product_token in self.contents:
|
||||
return self.contents[product_token]
|
||||
else:
|
||||
return None
|
||||
|
||||
class ProductSearchClientTestDouble:
|
||||
def __init__(self):
|
||||
self.search_to_results = {}
|
||||
|
||||
def search_for_all_in_price_range(self, lower_cents, upper_cents, start_record, num_records):
|
||||
return self.search_to_results[(lower_cents, upper_cents, start_record, num_records)]
|
||||
|
||||
class ServiceTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.detail_client = ProductDetailClientTestDouble()
|
||||
self.search_client = ProductSearchClientTestDouble()
|
||||
self.service = Service(self.detail_client, self.search_client)
|
||||
|
||||
def test_lookup_success(self):
|
||||
expected_product = {
|
||||
"token": "AAA",
|
||||
"description": "Red Bike",
|
||||
"price_cents": 45000
|
||||
}
|
||||
|
||||
self.detail_client.contents["AAA"] = expected_product
|
||||
|
||||
self.assertEqual(
|
||||
self.service.lookup("AAA"),
|
||||
{"status": 200,
|
||||
"product": expected_product})
|
||||
|
||||
def test_lookup_not_found(self):
|
||||
self.assertEqual(
|
||||
self.service.lookup("AAA"),
|
||||
{"status": 404})
|
||||
|
||||
def test_lookup_bad_input(self):
|
||||
self.assertEqual(
|
||||
self.service.lookup("this-is-not-a-uuid"),
|
||||
{"status": 400})
|
||||
|
||||
def test_search_price_range(self):
|
||||
brown_bike = {
|
||||
"token": "AAA",
|
||||
"description": "Brown Bike",
|
||||
"price_cents": 10000
|
||||
}
|
||||
|
||||
blue_bike = {
|
||||
"token": "BBB",
|
||||
"description": "Blue Bike",
|
||||
"price_cents": 37500
|
||||
}
|
||||
|
||||
red_bike = {
|
||||
"token": "CCC",
|
||||
"description": "Red Bike",
|
||||
"price_cents": 45000
|
||||
}
|
||||
|
||||
gray_bike = {
|
||||
"token": "DDD",
|
||||
"description": "Gray Bike",
|
||||
"price_cents": 99900
|
||||
}
|
||||
|
||||
self.detail_client.contents["AAA"] = brown_bike
|
||||
self.detail_client.contents["BBB"] = blue_bike
|
||||
self.detail_client.contents["CCC"] = red_bike
|
||||
self.detail_client.contents["DDD"] = gray_bike
|
||||
|
||||
self.search_client.search_to_results[(30000, 50000, 0, 10)] = ["BBB", "CCC"]
|
||||
|
||||
self.assertEqual(
|
||||
self.service.search_by_price_range(30000, 50000, 0, 10),
|
||||
{"status": 200,
|
||||
"products": [blue_bike, red_bike]})
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Add table
Add a link
Reference in a new issue