A417 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 17 · Lab: parts lists, netlists and tests

Organizing a design's data, and proving the code that handles it

Behind every board is a pile of structured data: a bill of materials with quantities and price breaks, a netlist of which pins connect where, a log of measurements against their nominal values. Lessons 12 to 16 gave you lists, dictionaries and sets, the iteration patterns that walk them, and exceptions and tests to keep the code honest. This lab costs a production run, checks a netlist for loose ends, reads a messy log without crashing, rounds to standard values across every decade, and ranks parts by how far they're off.

Predict

A netlist lists each part with the two nodes it connects. A node that appears exactly once usually means what?

Name it

Patterns this lab uses

  • Dictionaries of records: bom = {"R1": (qty, price, price100)} keeps each part's fields together under its name; for name, (q, p1, p100) in bom.items(): unpacks them.
  • Counting: count[n] = count.get(n, 0) + 1 tallies anything, and a comprehension filters the tally.
  • Sorting by a computed key: sorted(rows, key=lambda r: r[1], reverse=True) ranks by any field.
  • Errors as data: wrap the risky line in try/except ValueError, record what failed, and keep going.
  • Edge cases first: before trusting a function, try the inputs at its boundaries, such as a value exactly between two choices or at a decade edge.

Worked example

A board uses 4 of R1 (0.010 dollars each, 0.004 from 100 up), 2 of C1 (0.050, or 0.020 from 100), 1 of U1 (1.20, or 0.95 from 100) and 1 of J1 (0.40, or 0.31 from 100). What do the parts cost for 10, 50 and 100 boards?

Show the solution
  1. Cost each part at its total quantity. The price break applies to the total quantity of each part, so the quantity per board multiplies first.
bom = {"R1": (4, 0.010, 0.004), "C1": (2, 0.050, 0.020),
       "U1": (1, 1.20, 0.95), "J1": (1, 0.40, 0.31)}

def cost(boards):
    total = 0
    for name, (q, p1, p100) in bom.items():
        n = q * boards
        total += n * (p100 if n >= 100 else p1)
    return total

print(cost(10), cost(50), cost(100))    # 17.4 82.8 131.6
  1. Ten boards. They cost 17.40 dollars, 1.74 a board.
  2. Fifty boards. The resistors (200 pieces) and capacitors (100) reach their price break but U1 and J1 don't, so it's 82.80 dollars, or 1.66 a board.
  3. A hundred boards. Everything reaches its break: 131.60 dollars, 1.32 a board.

Answer the parts cost 17.40 dollars for 10 boards, 82.80 for 50 and 131.60 for 100 (1.74, 1.66 and 1.32 dollars a board).

Worked example

Find the loose ends in the netlist R1 in–a, R2 a–out, C1 out–gnd, R3 b–gnd, V1 in–gnd.

Show the solution
  1. Count how often each node appears. A node that appears only once is a loose end.
net = [("R1", "in", "a"), ("R2", "a", "out"), ("C1", "out", "gnd"),
       ("R3", "b", "gnd"), ("V1", "in", "gnd")]
count = {}
for part, a, b in net:
    for node in (a, b):
        count[node] = count.get(node, 0) + 1
print(sorted(n for n, c in count.items() if c == 1))    # ['b']
  1. Find the loose end. Node b appears only once, on R3: that resistor hangs off ground with nothing at its other end.
  2. Say where to look. Probably R3 was meant to go to a or out. The tool can't say which, but it can say where to look.

Answer node b is the only loose end, so R3 is connected at ground alone.

Check

Using the starter's bill of materials, write cost(boards) returning the parts cost in dollars for a run, where each part's lower price applies once its total quantity reaches 100. Then set total = cost(boards) for boards = 25 and each to the cost per board.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write loose_ends(net) returning the sorted list of nodes that appear only once in a netlist of (part, node_a, node_b) tuples, and shorted(net) returning the sorted list of parts whose two ends are on the same node. Set ends and shorts for the netlist the starter picks.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter's log has lines like R7=4712.3, but some are damaged. Write read_log(lines) returning (values, bad): a dictionary from name to float for the good lines, and a list of the line numbers (counting from 1) that couldn't be read, whether the = is missing or the value isn't a number. Set values, bad = read_log(lines).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write nearest_e12(x) returning the E12 value nearest to any positive x in ratio (smallest |log⁡(v/x)|), searching the decade below, the decade of x and the decade above. Set r = [nearest_e12(x) for x in xs] for the starter's list.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter has nominal and measured dictionaries for the same parts. Set report to a list of (name, error_pct) for the parts whose error, 100(measured−nominal)/nominal, is bigger in size than tol = 1%, sorted from the largest size of error to the smallest.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

d = {"R1": 4700}. What is d.get("R2", 0)?

Explain why

The rounding function's tests include 9.6 and 0.95. Why are values like these worth testing in particular?

Practice

Keep practicing

5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.