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.
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.
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:
band A is the first significant digit of the strength
band B is the second significant digit of the strength
band C is the third significant digit of the strength (this band is optional)
band D is the decimal multiplier
band E indicates the tolerance of the strength in percent (this band is optional)
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.
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 line of the file contains for information fields that are separated by commas: i) name of the color, 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.
The specification indicates that a certain color can not be used for a certain property by placing a hyphen (-) in the corresponding information 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). The file that specifies the current international standard (IEC 60062:20163) for the electronic color code looks like this:
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:
Write a function read_specs that takes the location (str) of a CSV file containing the specification of an electronic color code. The function must return a dictionary (dict) whose keys are the names (str) of the colors in the given specification, with the names of the colors converted to lowercase. The dictionary must map each color onto another dictionary (dict), that itself maps the following properties names (str) onto the corresponding property value (int or float) assigned to that color by the specification:
digit: the value of the color as a significant digit (int)
multiplier: the value of the color as a decimal multiplier (int)
tolerance: the value of the color as a tolerance (float)
The dictionary may only contain key/value pairs where the specification assigns a property value to the color. In other words, there should be no key/value pairs corresponding to information fields containing a hyphen (-) in the given CSV file.
Write a function lookup that takes three arguments: i) a color (str), ii) a property name (str; digit, multiplier or tolerance) and iii) the specification of an electronic color code, represented as a dictionary (dict) that is formatted as the dictionaries returned by the function read_specs. The function must return the value (int or float) that the given specification assigns to the given color for the given property. In case the given specification does not assign a value to the given color for the given property, an AssertionError must be raised with the message invalid code.
Write a function resistance that takes two arguments: i) a sequence of colored bands and ii) the specification of an electronic color code, represented as a dictionary (dict) that is formatted as the dictionaries returned by the function read_specs. The colored bands must be given as a string (str) containing a sequence of colors that are separated by spaces. If this is not the case, an AssertionError must be raised with the message invalid code. Otherwise the function must return a string (str) containing the strength and the tolerance of a resistor that is labeled with the given sequence of colored bands, formatted according to the following template:
strengthΩ (±tolerance%)
where the strength is expressed as a decimal number with three decimal digits and the accuracy as a decimal number with two decimal digits. Use rounding to represent decimal numbers with a fixed number of decimal digits.
The functions lookup and resistance should not make a distinction between uppercase and lowercase letters in the names of the colors and the properties.
In the following interactive session we assume the CSV file IEC60062_2016.csv4 to be located in the current directory.
>>> specs = read_specs('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. The figure below explains how to determine the strength and the tolerance of resistors labeled with the patterns of colored bands from these three examples.