Skip to content

Commit

Permalink
Move parse_number_and_unit() and NumberAndUnit definition to `wv_…
Browse files Browse the repository at this point in the history
…utils.py`
  • Loading branch information
formatc1702 committed Apr 16, 2024
1 parent 49556bd commit 3539b2f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
56 changes: 20 additions & 36 deletions src/wireviz/wv_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
SingleColor,
get_color_by_colorcode_index,
)
from wireviz.wv_utils import aspect_ratio, awg_equiv, mm2_equiv, remove_links
from wireviz.wv_utils import (
NumberAndUnit,
awg_equiv,
aspect_ratio,
mm2_equiv,
parse_number_and_unit,
remove_links,
)

# Each type alias have their legal values described in comments
# - validation might be implemented in the future
Expand Down Expand Up @@ -54,7 +61,6 @@
Side = Enum("Side", "LEFT RIGHT")
ArrowDirection = Enum("ArrowDirection", "NONE BACK FORWARD BOTH")
ArrowWeight = Enum("ArrowWeight", "SINGLE DOUBLE")
NumberAndUnit = namedtuple("NumberAndUnit", "number unit")

AUTOGENERATED_PREFIX = "AUTOGENERATED_"

Expand Down Expand Up @@ -196,34 +202,8 @@ def __post_init__(self):
partnos = tuple(partnos)
self.partnumbers = PartNumberInfo(*partnos)

self.qty = self.parse_number_and_unit(self.qty, None)
self.amount = self.parse_number_and_unit(self.amount, None)

def parse_number_and_unit(
self,
inp: Optional[Union[NumberAndUnit, float, int, str]],
default_unit: Optional[str] = None,
) -> Optional[NumberAndUnit]:
if inp is None:
return None
elif isinstance(inp, NumberAndUnit):
return inp
elif isinstance(inp, float) or isinstance(inp, int):
return NumberAndUnit(float(inp), default_unit)
elif isinstance(inp, str):
if " " in inp:
number, unit = inp.split(" ", 1)
else:
number, unit = inp, default_unit
try:
number = float(number)
except ValueError:
raise Exception(
f"{inp} is not a valid number and unit.\n"
"It must be a number, or a number and unit separated by a space."
)
else:
return NumberAndUnit(number, unit)
self.qty = parse_number_and_unit(self.qty, None)
self.amount = parse_number_and_unit(self.amount, None)

@property
def bom_hash(self) -> BomHash:
Expand Down Expand Up @@ -451,7 +431,9 @@ def __post_init__(self) -> None:
raise Exception("Loops must be between exactly two pins!")
for pin in loop:
if pin not in self.pins:
raise Exception(f'Unknown loop pin "{pin}" for connector "{self.name}"!')
raise Exception(
f'Unknown loop pin "{pin}" for connector "{self.name}"!'
)
# Make sure loop connected pins are not hidden.
# side=None, determine side to show loops during rendering
self.activate_pin(pin, side=None, is_connection=True)
Expand Down Expand Up @@ -675,8 +657,8 @@ def __post_init__(self) -> None:
# allow gauge, length, and other fields to be lists too (like part numbers),
# and assign them the same way to bundles.

self.gauge = self.parse_number_and_unit(self.gauge, "mm2")
self.length = self.parse_number_and_unit(self.length, "m")
self.gauge = parse_number_and_unit(self.gauge, "mm2")
self.length = parse_number_and_unit(self.length, "m")
self.amount = self.length # for BOM

if self.wirecount: # number of wires explicitly defined
Expand Down Expand Up @@ -753,9 +735,11 @@ def __post_init__(self) -> None:
index=index_offset,
id=id,
label="Shield",
color=MultiColor(self.shield)
if isinstance(self.shield, str)
else MultiColor(None),
color=(
MultiColor(self.shield)
if isinstance(self.shield, str)
else MultiColor(None)
),
parent=self.designator,
)

Expand Down
29 changes: 29 additions & 0 deletions src/wireviz/wv_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-

import re
from collections import namedtuple
from pathlib import Path
from typing import List, Optional, Union

NumberAndUnit = namedtuple("NumberAndUnit", "number unit")

awg_equiv_table = {
"0.09": "28",
"0.14": "26",
Expand Down Expand Up @@ -76,6 +79,32 @@ def get_single_key_and_value(d: dict):
return next(iter(d.items()))


def parse_number_and_unit(
inp: Optional[Union[NumberAndUnit, float, int, str]],
default_unit: Optional[str] = None,
) -> Optional[NumberAndUnit]:
if inp is None:
return None
elif isinstance(inp, NumberAndUnit):
return inp
elif isinstance(inp, float) or isinstance(inp, int):
return NumberAndUnit(float(inp), default_unit)
elif isinstance(inp, str):
if " " in inp:
number, unit = inp.split(" ", 1)
else:
number, unit = inp, default_unit
try:
number = float(number)
except ValueError:
raise Exception(
f"{inp} is not a valid number and unit.\n"
"It must be a number, or a number and unit separated by a space."
)
else:
return NumberAndUnit(number, unit)


def int2tuple(inp):
if isinstance(inp, tuple):
output = inp
Expand Down

0 comments on commit 3539b2f

Please sign in to comment.