Add dir with some small projects

This commit is contained in:
bt3gl 2022-03-23 18:41:46 +04:00
parent 0da3cbd74a
commit 0bdc950e7f
55 changed files with 12 additions and 1212 deletions

View 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

View 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

View 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'))