Skip to content
Snippets Groups Projects
Commit 55a27f15 authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

Parse D-SI unit string into array

parent c26b1fbb
No related branches found
No related tags found
No related merge requests found
import re
import warnings
def toLatex(dsiString: str):
"""parses a D-SI unit string and converts it to LaTeX
Args:
dsiString (str): D-SI unit raw string
Returns:
str: the corresponding LaTeX code
"""
tree = _parseDsi(dsiString)
latexArray = []
return ''.join(latexArray)
def _parseDsi(dsiString: str):
"""parses a D-SI string into a list of lists:
List format:
First layer: items of the fraction
Second layer: three-element lists containing prefix, unit, power
Args:
dsiString (str): D-SI unit raw string
Returns:
list: list tree containing the D-SI unit
"""
tree = []
tree = _parseDsiFraction(dsiString)
for i, node in enumerate(tree):
tree[i] = _parseFractionlessDsi(node)
return tree
def _parseDsiFraction(dsiString: str):
"""parses D-SI fraction into list of fraction elements
Args:
dsiString (str): D-SI unit raw string
Raises:
RuntimeWarning: String must not contain more than one \per,
as defined in the D-SI specs
Returns:
list: List containing the strings separated by the \per
"""
tree = []
tree = dsiString.split("\per")
if len(tree) > 2:
warnings.warn(f"The dsi string contains more than one \per, does not "+
f"match specs! Given string: {dsiString}",
RuntimeWarning)
return tree
def _parseFractionlessDsi(dsiString:str):
"""parses D-SI unit string without fractions
Args:
dsiString (str): D-SI unit raw string, not containing any fractions
Raises:
RuntimeWarning: if string does not meet the specs
Returns:
list: list of three-item lists; containing prefix, unit, power
"""
items = dsiString.split("\\")
if items[0] == '': #first item of List should be empty, remove it
items.pop(0)
else:
warnings.warn(f"string should start with \\, string given was \"{dsiString}\"", RuntimeWarning)
nodes = []
tuple = ['','','']
item = items.pop(0)
while True:
if item in _dsiPrefixesLatex:
tuple[0] = item
try:
item = items.pop(0)
except IndexError:
item = ''
if item in _dsiUnitsLatex:
tuple[1] = item
try:
item = items.pop(0)
except IndexError:
item = ''
if re.match('tothe\{[0-9]\}', item):
tuple[2] = item[-2]
try:
item = items.pop(0)
except IndexError:
item = ''
if tuple == ['','','']:
tuple[1] = item
try:
item = items.pop(0)
except IndexError:
item = ''
warnings.warn(f"The identifier \"{tuple[1]}\" does not match any D-SI units!", RuntimeWarning)
nodes.append(tuple)
if len(items) == 0: break
tuple = ['','','']
return nodes
# mapping D-SI prefixes to latex
_dsiPrefixesLatex = {
'deca': r'\mathrm{da}',
'hecto': r'\mathrm{h}',
'kilo': r'\mathrm{k}',
'mega': r'\mathrm{M}',
'giga': r'\mathrm{G}',
'tera': r'\mathrm{T}',
'peta': r'\mathrm{P}',
'exa': r'\mathrm{E}',
'zetta': r'\mathrm{Z}',
'yotta': r'\mathrm{Y}',
'deci': r'\mathrm{d}',
'centi': r'\mathrm{c}',
'milli': r'\mathrm{m}',
'micro': r'\micro',
'nano': r'\mathrm{n}',
'pico': r'\mathrm{p}',
'femto': r'\mathrm{f}',
'atto': r'\mathrm{a}',
'zepto': r'\mathrm{z}',
'yocto': r'\mathrm{y}'
}
# mapping D-SI units to latex
_dsiUnitsLatex = {
'metre': r'\mathrm{m}',
'kilogram': r'\mathrm{kg}',
'second': r'\mathrm{s}',
'ampere': r'\mathrm{A}',
'kelvin': r'\mathrm{K}',
'mole': r'\mathrm{mol}',
'candela': r'\mathrm{cd}',
'one': r'\mathrm{1}',
'day': r'\mathrm{d}',
'hour': r'\mathrm{h}',
'minute': r'\mathrm{min}',
'degree': r'^\circ',
'arcminute': r"'",
'arcsecond': r"''",
'gram': r'\mathrm{g}',
'radian': r'\mathrm{rad}',
'steradian': r'\mathrm{sr}',
'hertz': r'\mathrm{Hz}',
'newton': r'\mathrm{N}',
'pascal': r'\mathrm{Pa}',
'joule': r'\mathrm{J}',
'watt': r'\mathrm{W}',
'coulomb': r'\mathrm{C}',
'volt': r'\mathrm{V}',
'farad': r'\mathrm{F}',
'ohm': r'\Omega',
'siemens': r'\mathrm{S}',
'weber': r'\mathrm{Wb}',
'tesla': r'\mathrm{T}',
'henry': r'\mathrm{H}',
'degreecelsius': r'^\circ\mathrm{C}',
'lumen': r'\mathrm{lm}',
'lux': r'\mathrm{lx}',
'becquerel': r'\mathrm{Bq}',
'sievert': r'\mathrm{Sv}',
'gray': r'\mathrm{Gy}',
'katal': r'\mathrm{kat}',
'hectare': r'\mathrm{ha}',
'litre': r'\mathrm{l}',
'tonne': r'\mathrm{t}',
'electronvolt': r'\mathrm{eV}',
'dalton': r'\mathrm{Da}',
'astronomicalunit': r'\mathrm{au}',
'neper': r'\mathrm{Np}',
'bel': r'\mathrm{B}',
'decibel': r'\mathrm{dB}'
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment