Chapter 7 worked answers: Data Input and Output
On this page
Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.
B2-07-E01 · Inspect an API envelope
Report whether acquisition is complete. Do not treat the first two records as all records.
The envelope reports three pages, so two more pages need successful retrieval and validation.
response=[{"page":1,"pages":3},[{"id":7},{"id":8}]]
complete=response[0]["page"]==response[0]["pages"]
assert not complete
print(complete)False
B2-07-E02 · Parameterize an SQL filter
Load a disposable in-memory SQLite table and select year 2023 using a parameter, with names sorted.
The placeholder supplies a value separately from SQL syntax; ORDER BY defines presentation order.
df=pd.DataFrame({"name":["N","S","E"],"year":[2022,2023,2023]})
import sqlite3
with sqlite3.connect(":memory:") as con:
df.to_sql("records",con,index=False)
result=pd.read_sql_query("SELECT name FROM records WHERE year=? ORDER BY name",con,params=(2023,))
assert result.name.tolist()==["E","S"]
print(result) name
0 E
1 S
B2-07-E03 · Verify a snapshot fingerprint
Compute a SHA-256 fingerprint and demonstrate that one changed byte changes it. Explain what the hash cannot prove.
The fingerprint checks byte identity, not the accuracy, representativeness or provenance of the original measurements.
raw=b"id,value\n1,3\n"
import hashlib
a=hashlib.sha256(raw).hexdigest();b=hashlib.sha256(raw+b" ").hexdigest()
assert a!=b and len(a)==64
print(a)5dbeedc05ed8d939ff1470bb741c1b415006236c8038aa9c6d45c110dd9e8410
Return to practice
Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.