Resistors are passive two-terminal electrical components that implement electrical resistance as a circuit element. In electronic circuits, resistors are used to reduce current flow, adjust signal levels, divide voltages, bias active elements, and terminate transmission lines, among other uses.

resistor
A 2260 Ω (1% precision) resistor with five colored bands (E96 series). From left to right the colors red-red-blue-brown-brown encode the values 2-2-6-1-1. The last two brown bands indicate the multiplier ($$\times 10$$) and the tolerance (1%). The wider gap in between these bands compared to the gaps between the other bands is not that easy to distinguish in this case.

The electronic color code (current international standard: IEC 60062:20161) is used to indicate the strength of a resistor. A resistor is marked with three, four or five colored bands because in repairing mounted resistors it turned out that it was not always easy to read printed text on these small components. Especially if the printed side was not clearly visible because it was directed towards the chassis or another component. However, colored bands also have their drawbacks, especially for color blind people. Overheating of a resistor or dirt accumulation may also make it impossible to distinguish brown from red or orange.

color code on a resistor
The strength of a resistor is indicated by 3, 4 or 5 colored bands.

To distinguish left from right there is a wider gap between the penultimate (C/D) and the last (E) band. The strength of the resistor is encoded by the colored bands in the following way:

The IEC 60062:20162 standard prescribes the following color codes:

color significant digit multiplier tolerance (%)
(none) - - 20
PINK - $$\times 10^{-3}$$ -
SILVER - $$\times 10^{-2}$$ 10
GOLD - $$\times 10^{-1}$$ 5
BLACK 0 $$\times 10^{0}$$ -
BROWN 1 $$\times 10^{1}$$ 1
RED 2 $$\times 10^{2}$$ 2
ORANGE 3 $$\times 10^{3}$$ -
YELLOW 4 $$\times 10^{4}$$ -
GREEN 5 $$\times 10^{5}$$ 0.5
BLUE 6 $$\times 10^{6}$$ 0.25
VIOLET 7 $$\times 10^{7}$$ 0.1
GRAY 8 $$\times 10^{8}$$ 0.05
WHITE 9 $$\times 10^{9}$$ -

For example, consider a resistor with four colored bands: YELLOW-VIOLET-RED-GOLD (from left to right). The first digit of its strength is 4 (yellow in the table above), the second digit is 7 (violet), followed by 2 zeros (red = $$\times 10^{2}$$): 4700 ohms. Gold signifies that the tolerance is ±5%, so the real resistance could lie anywhere between 4465 and 4935 ohms.

Assignment

The specification of an electronic color code is defined in a comma-separated values (CSV) file, with each record describing the properties of a single color in the standard. Each record (line) of the file contains four comma-separated information fields: i) color name, ii) value of the color as a significant digit of the strength, iii) value of the color as a decimal multiplier (value $$m$$ means multiplication by $$10^m$$) and iv) value of the color as the tolerance of the resistor in percent.

An information field containing a hyphen (-) indicates that the specification does not allow to use the color (record) for the property (field). The tolerance of a resistor in the absence of band E (three colored bands) is represented in the specification as the tolerance of the fictitious color NONE. By definition, the color NONE has hyphens for properties ii) and iii). This is the CSV-file specifying the current international standard (IEC 60062:20163) for the electronic color code:

color,significant digit,multiplicator,tolerance (%)
NONE,-,-,20
PINK,-,-3,-
SILVER,-,-2,10
GOLD,-,-1,5
BLACK,0,0,-
BROWN,1,1,1
RED,2,2,2
ORANGE,3,3,-
YELLOW,4,4,-
GREEN,5,5,0.5
BLUE,6,6,0.25
VIOLET,7,7,0.1
GRAY,8,8,0.05
WHITE,9,9,-

Your task:

The functions lookup and resistance should not make a distinction between uppercase and lowercase letters in the names of the given colors and the properties.

Example

In the following interactive session we assume the CSV file IEC60062_2016.csv4 to be located in the current directory.

>>> specs = read_specification('IEC60062_2016.csv5')
>>> specs
{'none': {'tolerance': 20.0}, 'pink': {'multiplier': -3}, 'silver': {'multiplier': -2, 'tolerance': 10.0}, 'gold': {'multiplier': -1, 'tolerance': 5.0}, 'black': {'digit': 0, 'multiplier': 0}, 'brown': {'digit': 1, 'multiplier': 1, 'tolerance': 1.0}, 'red': {'digit': 2, 'multiplier': 2, 'tolerance': 2.0}, 'orange': {'digit': 3, 'multiplier': 3}, 'yellow': {'digit': 4, 'multiplier': 4}, 'green': {'digit': 5, 'multiplier': 5, 'tolerance': 0.5}, 'blue': {'digit': 6, 'multiplier': 6, 'tolerance': 0.25}, 'violet': {'digit': 7, 'multiplier': 7, 'tolerance': 0.1}, 'gray': {'digit': 8, 'multiplier': 8, 'tolerance': 0.05}, 'white': {'digit': 9, 'multiplier': 9}}

>>> lookup('BLUE', 'digit', specs)
6
>>> lookup('black', 'MULTIPLIER', specs)
0
>>> lookup('PINK', 'TOLERANCE', specs)
Traceback (most recent call last):
AssertionError: invalid code
>>> lookup('none', 'tolerance', specs)
20.0

>>> resistance('yellow violet red gold', specs)
'4700.000Ω (±5.00%)'
>>> resistance('orange orange brown', specs)
'330.000Ω (±20.00%)'
>>> resistance('GRAY RED ORANGE GOLD', specs)
'82000.000Ω (±5.00%)'
>>> resistance('green red brown black brown', specs)
'521.000Ω (±1.00%)'
>>> resistance('green gold brown', specs)
Traceback (most recent call last):
AssertionError: invalid code

Note that the first example of the function resistance corresponds to the example that is worked out in detail below the table in the introduction of this assignment. The other three examples for the function resistance have been taken from the figure in the introduction of this assignment. This explains how to determine the strength and the tolerance of resistors labeled with the patterns of colored bands from these three examples:

examples: function resistance
Explanation how the strength and the tolerance must be determined for resistors labeled with the patterns of colored bands from three examples used in testing the function resistance.