Skip to content

Commit

Permalink
Fix valueformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
mph- committed Oct 20, 2024
1 parent 14bd101 commit 872a72a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lcapy/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def latex_with_units(self, eng_format=False, show_units=True,
"""Make LaTeX string with optional units. Units are only
shown for numerical values."""

from .value_formatter import value_formatter
from .valueformatter import value_formatter

expr = self

Expand Down
4 changes: 3 additions & 1 deletion lcapy/labelmaker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .expr import Expr
from .latex import latex_format_label
from .valueformatter import value_formatter
from .valueparser import value_parser
import sympy as sym


Expand Down Expand Up @@ -55,7 +56,8 @@ def make(self, cpt, label_ports=False, style='SI'):
units_map = {'V': 'V', 'I': 'A', 'R': '$\Omega$',
'C': 'F', 'L': 'H'}

expr = cpt.args[0]
expr = value_parser(cpt.args[0])

if cpt.classname in ('Vstep', 'Istep'):
expr = '(%s) * Heaviside(t)' % expr
value_label = self._format_expr(expr)
Expand Down
25 changes: 2 additions & 23 deletions lcapy/mnacpts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .current import current
from .opts import Opts
from .node import DummyNode
from .valueparser import value_parser
import lcapy
import inspect
import sys
Expand Down Expand Up @@ -142,31 +143,9 @@ def _process_args(self, args):
The suffixes are ignored in expressions such as 10 * 42p."""

suffixes = {'f': 1e-15, 'p': 1e-12, 'n': 1e-9, 'u': 1e-6,
'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9, 'T': 1e12}

def isfloat(s):
try:
float(s)
return True
except ValueError:
return False

pargs = []
for arg in args:
if arg is None or len(arg) < 2:
pargs.append(arg)
continue

if arg.endswith('Meg'):
arg = arg[0:-2] + 'M'
elif arg.endswith('K'):
arg = arg[0:-1] + 'k'

if (arg[-1] in suffixes and isfloat(arg[0:-1])):
pargs.append(float(arg[0:-1]) * suffixes[arg[-1]])
else:
pargs.append(arg)
pargs.append(value_parser(arg))

return pargs

Expand Down
1 change: 1 addition & 0 deletions lcapy/schematics/components/cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ...labels import Labels
from ...latex import latex_format_label
from ...config import implicit_default
from ...valueparser import value_parser
from ..utils import check_boolean


Expand Down
27 changes: 27 additions & 0 deletions lcapy/valueparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def value_parser(arg):
"""Convert args such as 42p to 42e-12.
The suffixes are ignored in expressions such as 10 * 42p."""

suffixes = {'f': 1e-15, 'p': 1e-12, 'n': 1e-9, 'u': 1e-6,
'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G': 1e9, 'T': 1e12}

def isfloat(s):
try:
float(s)
return True
except ValueError:
return False

if arg is None or len(arg) < 2:
return arg

if arg.endswith('Meg'):
arg = arg[0:-2] + 'M'
elif arg.endswith('K'):
arg = arg[0:-1] + 'k'

if (arg[-1] in suffixes and isfloat(arg[0:-1])):
return float(arg[0:-1]) * suffixes[arg[-1]]
else:
return arg

0 comments on commit 872a72a

Please sign in to comment.