Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pykat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Show more breadcrumbs
finesse
pykat
Commits
e634953f
Commit
e634953f
authored
11 years ago
by
Daniel Brown
Browse files
Options
Downloads
Patches
Plain Diff
Changing SIfloat to only convert last character of the string
parent
183a2756
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pykat/SIfloat.py
+39
-22
39 additions, 22 deletions
pykat/SIfloat.py
with
39 additions
and
22 deletions
pykat/SIfloat.py
+
39
−
22
View file @
e634953f
import
os
import
re
import
pykat.exceptions
as
pkex
#staticmethod
def
SIfloat
(
value
):
if
value
==
None
:
return
value
if
type
(
value
)
==
list
:
return
[
convertToFloat
(
s
)
for
s
in
value
]
else
:
return
convertToFloat
(
value
)
def
convertToFloat
(
value
):
__suffix
=
{
'
y
'
:
'
e-24
'
,
# yocto
'
z
'
:
'
e-21
'
,
# zepto
'
a
'
:
'
e-18
'
,
# atto
...
...
@@ -28,9 +19,35 @@ def convertToFloat(value):
'
P
'
:
'
e15
'
# peta
}
value
=
str
(
value
)
def
SIfloat
(
value
):
if
value
==
None
:
return
value
if
type
(
value
)
==
list
:
return
[
convertToFloat
(
s
)
for
s
in
value
]
else
:
return
convertToFloat
(
value
)
def
convertToFloat
(
value
):
try
:
# first just try and convert the value
return
float
(
value
)
for
i
,
j
in
__suffix
.
iteritems
():
value
=
value
.
replace
(
i
,
str
(
j
))
except
ValueError
as
ex
:
# Catch any casting exeception
value
=
value
.
strip
()
# only the last value can be an SI scaling letter
last
=
value
[
-
1
]
if
last
in
__suffix
:
# remove last character and append the SI scaling
value
=
value
[
0
:
-
1
]
+
__suffix
[
last
]
else
:
raise
pkex
.
BasePyKatException
(
"
Could not convert SI scaling in
'
{0}
'
to a float
"
.
format
(
value
))
try
:
return
float
(
value
)
except
ValueError
as
ex
:
raise
pkex
.
BasePyKatException
(
"
Unable to convert
'
{0}
'
into a float
"
.
format
(
value
))
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