utils + parallel resistors calculator
This commit is contained in:
27
utils.py
Normal file
27
utils.py
Normal file
@@ -0,0 +1,27 @@
|
||||
#!/bin/python
|
||||
import re
|
||||
|
||||
def parse_resistors(raw_input):
|
||||
values = []
|
||||
tokens = raw_input.replace(',', ' ').split()
|
||||
|
||||
for token in tokens:
|
||||
match = re.fullmatch(r'([0-9]*\.?[0-9]+)\s*([KM]?)', token)
|
||||
if not match:
|
||||
raise ValueError(f"Invalid component value '{token}'")
|
||||
|
||||
number = float(match.group(1))
|
||||
if number <= 0:
|
||||
raise ValueError(f"Invalid component value '{token}' (must be positive)")
|
||||
|
||||
suffix = match.group(2)
|
||||
if suffix == 'K':
|
||||
multiplier = 1e3
|
||||
elif suffix == 'M':
|
||||
multiplier = 1e6
|
||||
else:
|
||||
multiplier = 1.0
|
||||
|
||||
values.append(number * multiplier)
|
||||
|
||||
return values
|
||||
Reference in New Issue
Block a user