[PcbDraw] Replaced `unit` code
- So we have only one units conversion - I think the only difference is that KiBot code currently supports the locales decimal point
This commit is contained in:
parent
473d1c27e2
commit
bb6b0c6141
|
|
@ -182,3 +182,7 @@ index f8990722..17f90185 100644
|
|||
- Multiply matrices (1 line code)
|
||||
- Find the index of the smaller element (1 line code)
|
||||
- I added a replacemt for the `array` function, it just makes all matrix elements float
|
||||
|
||||
- Replaced `unit` code.
|
||||
- So we have only one units conversion
|
||||
- I think the only difference is that KiBot code currently supports the locales decimal point
|
||||
|
|
|
|||
|
|
@ -1,17 +1,7 @@
|
|||
# Author: Jan Mrázek
|
||||
# Author: Salvador E. Tropea
|
||||
# License: MIT
|
||||
from decimal import Decimal
|
||||
from typing import List
|
||||
|
||||
|
||||
def erase(string: str, what: List[str]) -> str:
|
||||
"""
|
||||
Given a string and a list of string, removes all occurrences of items from
|
||||
what in the string
|
||||
"""
|
||||
for x in what:
|
||||
string = string.replace(x, "")
|
||||
return string
|
||||
from ..bom.units import comp_match
|
||||
|
||||
|
||||
def read_resistance(value: str) -> Decimal:
|
||||
|
|
@ -20,32 +10,8 @@ def read_resistance(value: str) -> Decimal:
|
|||
|
||||
This function can raise a ValueError if the value is invalid
|
||||
"""
|
||||
p_value = erase(value, ["Ω", "Ohms", "Ohm"]).strip()
|
||||
p_value = p_value.replace(" ", "") # Sometimes there are spaces after decimal place
|
||||
unit_prefixes = {
|
||||
"m": Decimal('1e-3'),
|
||||
"R": Decimal('1'),
|
||||
"K": Decimal('1e3'),
|
||||
"k": Decimal('1e3'),
|
||||
"M": Decimal('1e6'),
|
||||
"G": Decimal('1e9')
|
||||
}
|
||||
try:
|
||||
numerical_value = None
|
||||
for prefix, table in unit_prefixes.items():
|
||||
if prefix in p_value:
|
||||
# Example: 4k7 will have the 4 converted to Decimal(4) and 7 to Decimal(0.7)
|
||||
# Then each gets multiplied by the factor and added, so 4000 + 700
|
||||
# This method ensures that 4k7 and 4k700 for example yields the same result
|
||||
split = p_value.split(prefix)
|
||||
n_whole = Decimal(split[0]) if split[0] != "" else Decimal(0)
|
||||
n_dec = Decimal('.'+split[1]) if split[1] != "" else Decimal(0)
|
||||
numerical_value = n_whole * table + n_dec * table
|
||||
break
|
||||
if numerical_value is None:
|
||||
# If this fails, a decimal.InvalidOperation is raised which is handled by the Exception catch
|
||||
numerical_value = Decimal(p_value)
|
||||
return numerical_value
|
||||
except Exception:
|
||||
pass
|
||||
raise ValueError(f"Cannot parse '{value}' to resistance")
|
||||
res = comp_match(value, 'R')
|
||||
if res is None:
|
||||
raise ValueError(f"Cannot parse '{value}' to resistance")
|
||||
v, mul, uni = res
|
||||
return Decimal(str(v))*Decimal(str(mul[0]))
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
# Project: KiBot (formerly KiPlot)
|
||||
# TODO: PIL dependency?
|
||||
# TODO: Package resources
|
||||
# TODO: replace unit.py
|
||||
# TODO: wxApp messages
|
||||
# """
|
||||
# Dependencies:
|
||||
# - from: RSVG
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from decimal import Decimal as D
|
||||
import os
|
||||
import re
|
||||
import pytest
|
||||
|
|
@ -18,6 +19,7 @@ from kibot.bom.units import get_prefix
|
|||
from kibot.__main__ import detect_kicad
|
||||
from kibot.kicad.config import KiConf
|
||||
from kibot.globals import Globals
|
||||
from kibot.PcbDraw.unit import read_resistance
|
||||
|
||||
cov = coverage.Coverage()
|
||||
mocked_check_output_FNF = True
|
||||
|
|
@ -334,3 +336,23 @@ def test_makefile_kibot_sys(test_dir):
|
|||
generate_makefile(ctx.get_out_path('Makefile'), 'pp', [], kibot_sys=True)
|
||||
ctx.search_in_file('Makefile', [r'KIBOT\?=kibot'])
|
||||
ctx.clean_up()
|
||||
|
||||
|
||||
def test_read_resistance():
|
||||
assert read_resistance("4k7") == D("4700")
|
||||
assert read_resistance("4k7") == D("4700")
|
||||
assert read_resistance("4.7R") == D("4.7")
|
||||
assert read_resistance("4R7") == D("4.7")
|
||||
assert read_resistance("0R47") == D("0.47")
|
||||
assert read_resistance("4700k") == D("4700000")
|
||||
assert read_resistance("470m") == D("0.47")
|
||||
assert read_resistance("470M") == D("470000000")
|
||||
assert read_resistance("4M7") == D("4700000")
|
||||
assert read_resistance("470") == D("470")
|
||||
assert read_resistance("470Ω") == D("470")
|
||||
assert read_resistance("470 Ω") == D("470")
|
||||
assert read_resistance("470Ohm") == D("470")
|
||||
assert read_resistance("470 Ohms") == D("470")
|
||||
assert read_resistance("R47") == D("0.47")
|
||||
assert read_resistance("1G") == D("1000000000")
|
||||
assert read_resistance("4k7000") == D("4700")
|
||||
|
|
|
|||
Loading…
Reference in New Issue