Coding Exercises · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: Exercise 1: encode stocks and trades ✓ Passed
Complete CustomEncoder, a json.JSONEncoder subclass, so that json.dumps(activity, cls=CustomEncoder) works for any structure containing Stock and Trade objects.
- A Stock or Trade becomes a JSON object with all its fields (as_dict()) plus a key "object" whose value is the class name ("Stock" or "Trade"). - A datetime becomes its ISO string, e.g. "2018-11-22T10:05:12". - A date becomes its ISO string, e.g. "2018-11-22". - A Decimal becomes a string with the exact digits, e.g. "337.60". - Anything else unsupported must still raise TypeError.
The classes are given; don't change them.
import json
from datetime import date, datetime
from decimal import Decimal
class Stock:
def __init__(self, symbol, date, open_, high, low, close, volume):
self.symbol = symbol
self.date = date
self.open = open_
self.high = high
self.low = low
self.close = close
self.volume = volume
def as_dict(self):
return dict(symbol=self.symbol, date=self.date, open=self.open, high=self.high,
low=self.low, close=self.close, volume=self.volume)
class Trade:
def __init__(self, symbol, timestamp, order, price, volume, commission):
self.symbol = symbol
self.timestamp = timestamp
self.order = order
self.price = price
self.commission = commission
self.volume = volume
def as_dict(self):
return dict(symbol=self.symbol, timestamp=self.timestamp, order=self.order,
price=self.price, volume=self.volume, commission=self.commission)
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (Stock, Trade)):
return obj.as_dict()
return super().default(obj)
def make_stock():
return Stock('TSLA', date(2018, 11, 22), Decimal('338.19'), Decimal('338.64'),
Decimal('337.60'), Decimal('338.19'), 365_607)
def make_trade():
return Trade('TSLA', datetime(2018, 11, 22, 10, 5, 12), 'buy', Decimal('338.25'),
100, Decimal('9.99'))
def encode(obj):
return json.loads(json.dumps(obj, cls=CustomEncoder))
def test_stock():
"""a Stock becomes a dict with a type marker"""
got = encode(make_stock())
expected = {'symbol': 'TSLA', 'date': '2018-11-22', 'open': '338.19', 'high': '338.64',
'low': '337.60', 'close': '338.19', 'volume': 365607, 'object': 'Stock'}
assert got == expected, got
def test_trade():
"""a Trade keeps its full timestamp"""
got = encode(make_trade())
expected = {'symbol': 'TSLA', 'timestamp': '2018-11-22T10:05:12', 'order': 'buy',
'price': '338.25', 'volume': 100, 'commission': '9.99', 'object': 'Trade'}
assert got == expected, got
def test_activity_structure():
"""works inside nested lists and dicts"""
got = encode({'quotes': [make_stock()], 'trades': [make_trade(), make_trade()]})
assert [q['object'] for q in got['quotes']] == ['Stock'], got
assert [t['object'] for t in got['trades']] == ['Trade', 'Trade'], got
def test_plain_values():
"""dates, datetimes and Decimals work on their own too"""
got = encode([date(2020, 1, 2), datetime(2020, 1, 2, 3, 4, 5), Decimal('0.10')])
assert got == ['2020-01-02', '2020-01-02T03:04:05', '0.10'], got
def test_unknown_type():
"""other unsupported types still raise TypeError"""
try:
json.dumps({'z': 1 + 2j}, cls=CustomEncoder)
except TypeError:
pass
else:
raise AssertionError('complex numbers should raise TypeError')
Hints (3)
- Whatever
defaultreturns is encoded recursively, so the dict fromas_dict()may still contain dates and Decimals: add a branch for each value type. type(obj).__name__gives'Stock'or'Trade'.str(Decimal('337.60'))is'337.60'; going throughfloatwould lose the trailing zero.
Show a solution
import json
from datetime import date, datetime
from decimal import Decimal
class Stock:
def __init__(self, symbol, date, open_, high, low, close, volume):
self.symbol = symbol
self.date = date
self.open = open_
self.high = high
self.low = low
self.close = close
self.volume = volume
def as_dict(self):
return dict(symbol=self.symbol, date=self.date, open=self.open, high=self.high,
low=self.low, close=self.close, volume=self.volume)
class Trade:
def __init__(self, symbol, timestamp, order, price, volume, commission):
self.symbol = symbol
self.timestamp = timestamp
self.order = order
self.price = price
self.commission = commission
self.volume = volume
def as_dict(self):
return dict(symbol=self.symbol, timestamp=self.timestamp, order=self.order,
price=self.price, volume=self.volume, commission=self.commission)
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (Stock, Trade)):
result = obj.as_dict()
result['object'] = type(obj).__name__
return result
if isinstance(obj, datetime): # check before date: datetime is a subclass
return obj.isoformat()
if isinstance(obj, date):
return obj.isoformat()
if isinstance(obj, Decimal):
return str(obj)
return super().default(obj)Exercise 2: Exercise 2: decode them again ✓ Passed
JSON produced by the Exercise 1 encoder marks stocks and trades with "object": "Stock" / "object": "Trade". Dates, timestamps and prices are ISO strings and decimal strings.
Write:
- decode_financials(d): given a dict, return a Stock or Trade built from it (with real date, datetime, Decimal and int values) if it carries one of those markers, otherwise return d unchanged; - load_activity(text): decode a JSON string, converting marked objects anywhere in the document.
The classes (with __eq__) are given.
import json
from datetime import date, datetime
from decimal import Decimal
class Stock:
def __init__(self, symbol, date, open_, high, low, close, volume):
self.symbol = symbol
self.date = date
self.open = open_
self.high = high
self.low = low
self.close = close
self.volume = volume
def __eq__(self, other):
return type(other) is Stock and vars(self) == vars(other)
class Trade:
def __init__(self, symbol, timestamp, order, price, volume, commission):
self.symbol = symbol
self.timestamp = timestamp
self.order = order
self.price = price
self.commission = commission
self.volume = volume
def __eq__(self, other):
return type(other) is Trade and vars(self) == vars(other)
def decode_financials(d):
# your code here
return d
def load_activity(text):
return json.loads(text)
STOCK = ('{"symbol": "TSLA", "date": "2018-11-22", "open": "338.19", "high": "338.64", '
'"low": "337.60", "close": "338.19", "volume": 365607, "object": "Stock"}')
TRADE = ('{"symbol": "AAPL", "timestamp": "2018-11-22T10:30:05", "order": "sell", '
'"price": "177.01", "volume": 20, "commission": "9.99", "object": "Trade"}')
def test_stock():
"""a marked dict becomes an equal Stock"""
got = load_activity(STOCK)
expected = Stock('TSLA', date(2018, 11, 22), Decimal('338.19'), Decimal('338.64'),
Decimal('337.60'), Decimal('338.19'), 365607)
assert got == expected, vars(got) if hasattr(got, '__dict__') else got
def test_trade():
"""a marked dict becomes an equal Trade"""
got = load_activity(TRADE)
expected = Trade('AAPL', datetime(2018, 11, 22, 10, 30, 5), 'sell', Decimal('177.01'),
20, Decimal('9.99'))
assert got == expected, vars(got) if hasattr(got, '__dict__') else got
def test_exact_types():
"""values have their real types"""
s = load_activity(STOCK)
assert type(s.date) is date, f'date is {type(s.date).__name__}'
assert isinstance(s.low, Decimal) and str(s.low) == '337.60', repr(s.low)
t = load_activity(TRADE)
assert type(t.timestamp) is datetime, f'timestamp is {type(t.timestamp).__name__}'
def test_anywhere():
"""marked objects are converted at any depth"""
got = load_activity('{"quotes": [' + STOCK + '], "deep": {"x": [[' + TRADE + ']]}}')
assert isinstance(got['quotes'][0], Stock), got
assert isinstance(got['deep']['x'][0][0], Trade), got
def test_other_dicts_unchanged():
"""unmarked dicts stay dicts"""
d = {'a': 1, 'object': 'Bond'}
assert decode_financials(d) == d
assert load_activity('{"meta": {"source": "feed"}}') == {'meta': {'source': 'feed'}}
Hints (3)
date.fromisoformat,datetime.fromisoformatandDecimal(text)reverse the encoder's conversions exactly.- Pass
object_hook=decode_financialstojson.loads: it's called for every JSON object at every depth. - Use
d.get('object')so dicts without a marker don't raiseKeyError.
Show a solution
import json
from datetime import date, datetime
from decimal import Decimal
class Stock:
def __init__(self, symbol, date, open_, high, low, close, volume):
self.symbol = symbol
self.date = date
self.open = open_
self.high = high
self.low = low
self.close = close
self.volume = volume
def __eq__(self, other):
return type(other) is Stock and vars(self) == vars(other)
class Trade:
def __init__(self, symbol, timestamp, order, price, volume, commission):
self.symbol = symbol
self.timestamp = timestamp
self.order = order
self.price = price
self.commission = commission
self.volume = volume
def __eq__(self, other):
return type(other) is Trade and vars(self) == vars(other)
def decode_stock(d):
return Stock(d['symbol'], date.fromisoformat(d['date']),
Decimal(d['open']), Decimal(d['high']), Decimal(d['low']),
Decimal(d['close']), int(d['volume']))
def decode_trade(d):
return Trade(d['symbol'], datetime.fromisoformat(d['timestamp']), d['order'],
Decimal(d['price']), int(d['volume']), Decimal(d['commission']))
DECODERS = {'Stock': decode_stock, 'Trade': decode_trade}
def decode_financials(d):
decoder = DECODERS.get(d.get('object'))
return decoder(d) if decoder else d
def load_activity(text):
return json.loads(text, object_hook=decode_financials)Exercise 3: Exercise 3: let the declarations be the schema ✓ Passed
Schema libraries derive serialization from declared field types. Dataclasses declare their fields and types too, so we can do the same with the standard library.
Write two generic functions that work for any dataclass whose fields are annotated with str, int, date, datetime or Decimal:
- to_dict(obj): return a dict of JSON-native values: date/datetime as ISO strings, Decimal as a string, str and int unchanged. - from_dict(cls, data): build an instance of the dataclass cls, converting each value back according to the field's declared type. If a field is missing or data has keys that aren't fields, raise ValueError.
Use dataclasses.fields(cls); each field has .name and .type (the annotated class).
import json
from dataclasses import dataclass, fields
from datetime import date, datetime
from decimal import Decimal
@dataclass
class Stock:
symbol: str
date: date
open: Decimal
high: Decimal
low: Decimal
close: Decimal
volume: int
@dataclass
class Trade:
symbol: str
timestamp: datetime
order: str
price: Decimal
volume: int
commission: Decimal
def to_dict(obj):
# your code here
pass
def from_dict(cls, data):
# your code here
pass
STOCK = Stock('TSLA', date(2018, 11, 22), Decimal('338.19'), Decimal('338.64'),
Decimal('337.60'), Decimal('338.19'), 365607)
TRADE = Trade('TSLA', datetime(2018, 11, 22, 10, 5, 12), 'buy', Decimal('338.25'),
100, Decimal('9.99'))
def test_to_dict_stock():
"""to_dict converts dates and Decimals"""
got = to_dict(STOCK)
assert got == {'symbol': 'TSLA', 'date': '2018-11-22', 'open': '338.19', 'high': '338.64',
'low': '337.60', 'close': '338.19', 'volume': 365607}, got
def test_to_dict_is_json_ready():
"""the result can be passed straight to json.dumps"""
text = json.dumps(to_dict(TRADE))
assert '"timestamp": "2018-11-22T10:05:12"' in text, text
def test_round_trip():
"""JSON round trip gives equal objects with exact types"""
for obj in (STOCK, TRADE):
back = from_dict(type(obj), json.loads(json.dumps(to_dict(obj))))
assert back == obj, back
back = from_dict(Stock, to_dict(STOCK))
assert type(back.date) is date and str(back.low) == '337.60', back
def test_other_dataclass():
"""works for any dataclass, not just Stock and Trade"""
@dataclass
class Dividend:
symbol: str
paid: date
amount: Decimal
d = Dividend('MSFT', date(2018, 12, 13), Decimal('0.46'))
assert to_dict(d) == {'symbol': 'MSFT', 'paid': '2018-12-13', 'amount': '0.46'}, to_dict(d)
assert from_dict(Dividend, to_dict(d)) == d
def test_missing_or_extra_keys():
"""missing fields and unknown keys raise ValueError"""
data = to_dict(TRADE)
for bad in ({k: v for k, v in data.items() if k != 'price'}, {**data, 'fee': '1'}):
try:
from_dict(Trade, bad)
except ValueError:
pass
else:
raise AssertionError(f'accepted bad keys: {sorted(bad)}')
Hints (3)
- A table mapping each type to a pair of functions,
{date: (date.isoformat, date.fromisoformat), ...}, keeps both functions short. fields(obj)works on instances as well as classes, andf.typeis the annotation, e.g.Decimal.- Compare
{f.name for f in fields(cls)}withdata.keys()to find missing and unexpected keys.
Show a solution
import json
from dataclasses import dataclass, fields
from datetime import date, datetime
from decimal import Decimal
@dataclass
class Stock:
symbol: str
date: date
open: Decimal
high: Decimal
low: Decimal
close: Decimal
volume: int
@dataclass
class Trade:
symbol: str
timestamp: datetime
order: str
price: Decimal
volume: int
commission: Decimal
# field type -> (to native, from native)
CONVERTERS = {
str: (str, str),
int: (int, int),
date: (date.isoformat, date.fromisoformat),
datetime: (datetime.isoformat, datetime.fromisoformat),
Decimal: (str, Decimal),
}
def to_dict(obj):
return {f.name: CONVERTERS[f.type][0](getattr(obj, f.name)) for f in fields(obj)}
def from_dict(cls, data):
names = {f.name for f in fields(cls)}
missing, extra = names - data.keys(), data.keys() - names
if missing or extra:
raise ValueError(f'missing: {sorted(missing)}, unexpected: {sorted(extra)}')
return cls(**{f.name: CONVERTERS[f.type][1](data[f.name]) for f in fields(cls)})