Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Validata
Validata Core
Commits
7ca2d604
Commit
7ca2d604
authored
Apr 03, 2020
by
Pierre Dittgen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Harden code to handle non string values
parent
8092a314
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
2 deletions
+22
-2
validata_core/custom_checks/compare_columns_value.py
validata_core/custom_checks/compare_columns_value.py
+22
-2
No files found.
validata_core/custom_checks/compare_columns_value.py
View file @
7ca2d604
...
...
@@ -101,20 +101,40 @@ class CompareColumnsValue(object):
.
format
(
self
.
column
,
value1
,
OP_LABELS
[
self
.
op
],
self
.
column2
,
value2
),
{
'column1'
:
self
.
column
,
'value1'
:
value1
,
'column2'
:
self
.
column2
,
'value2'
:
value2
,
'op'
:
OP_LABELS
[
self
.
op
]})
@
staticmethod
def
is_a_number
(
value
):
"""Return True if value is an int, a float or a string representation of a number."""
if
type
(
value
)
in
(
int
,
float
):
return
True
if
not
isinstance
(
value
,
str
):
return
False
if
value
.
isnumeric
():
return
True
try
:
float
(
value
)
return
True
except
ValueError
:
return
False
@
staticmethod
def
compute_comparison_str
(
value1
,
op
,
value2
):
""" Computes comparison_str """
# number vs number
if
v
alue
1
.
isnumer
ic
()
and
v
alue
2
.
isnumer
ic
(
):
if
CompareColumnsV
alue
.
is
_a_
num
b
er
(
value1
)
and
CompareColumnsV
alue
.
is
_a_
num
b
er
(
value2
):
return
'{} {} {}'
.
format
(
value1
,
op
,
value2
)
# string vs string
if
not
value1
.
isnumeric
()
and
not
value2
.
isnumeric
(
):
if
isinstance
(
value1
,
str
)
and
isinstance
(
value2
,
str
):
n_value1
=
value1
.
replace
(
'"'
,
'
\\
"'
)
n_value2
=
value2
.
replace
(
'"'
,
'
\\
"'
)
return
'"{}" {} "{}"'
.
format
(
n_value1
,
op
,
n_value2
)
# thing vs thing, compare string repr
if
type
(
value1
)
==
type
(
value2
):
return
f
"'
{
value1
}
'
{
op
}
'
{
value2
}
'"
# potato vs cabbage?
return
None
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment