Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python_DSI_Parser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Benedikt
python_DSI_Parser
Commits
55a27f15
Commit
55a27f15
authored
1 year ago
by
Vanessa Stehr
Browse files
Options
Downloads
Patches
Plain Diff
Parse D-SI unit string into array
parent
c26b1fbb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dsiParser.py
+184
-0
184 additions, 0 deletions
dsiParser.py
with
184 additions
and
0 deletions
dsiParser.py
0 → 100644
+
184
−
0
View file @
55a27f15
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment