Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
21-22J-056
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
21-22J-056
21-22J-056
Commits
c4abd49a
Commit
c4abd49a
authored
Jan 12, 2022
by
Kaarunyan sritharan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create the validations
parent
f722cc6b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
71 additions
and
0 deletions
+71
-0
validate.py
validate.py
+71
-0
No files found.
validate.py
0 → 100644
View file @
c4abd49a
""" Defines validators or functions that create validators.
A validator is a function that when applied to data returns
a list of detected problems or an empty list otherwise.
The list pattern is used to enable validator composition."""
from
util.seqs
import
seq_not_str
,
join
from
util
import
stats
prob
=
stats
.
validate_probability
def
verify
(
validator
,
data
):
"""Asserts data via validator."""
result
=
validator
(
data
)
assert
not
result
,
f
'{result}'
def
values
(
validator_dict
):
""" Creates validator applying provided validators to data of same keys
:param validator_dict: dictionary of functions """
assert
isinstance
(
validator_dict
,
dict
)
return
lambda
data_dict
:
\
join
(
validator_dict
[
k
](
data_dict
[
k
])
for
k
in
validator_dict
.
keys
())
def
seq
(
f
,
n
=
0
):
"""Creates validator applying f to all elements of an iterable (but not a string)."""
def
seq_validator
(
objs
):
"""Function returned"""
if
not
seq_not_str
(
objs
):
return
[
f
'Sequence expected: {objs}'
]
if
n
and
n
!=
len
(
objs
):
return
[
f
'Not of length {n}: {objs}'
]
return
join
(
map
(
f
,
objs
))
return
seq_validator
def
cls
(
typ
):
""" Returns validator if input is an instance of cls."""
return
lambda
obj
:
[]
if
isinstance
(
obj
,
typ
)
\
else
[
f
'{obj} not an instance of {typ}'
]
def
aligned
(
seqs
):
"""Validates if all elements of seqs have equal length"""
return
[]
if
len
(
set
(
len
(
i
)
for
i
in
seqs
))
==
1
else
[
f
'Different lengths: {seqs}'
]
def
same
(
objs
):
""" If all are equal. """
return
[]
if
len
(
set
(
objs
))
==
1
else
[
f
'Not equal: {objs}'
]
def
counter
(
n
):
"""If n is non-negative int """
return
[]
if
isinstance
(
n
,
int
)
and
(
n
>=
0
)
else
[
f
'Not a positive integer: {n}'
]
def
size
(
n
):
"""Creates validator if iterable has length n"""
return
lambda
seq
:
[]
if
len
(
seq
)
==
n
else
[
f
'Length not {n}: {seq}'
]
def
both
(
left
,
right
):
"""Creates validator collecting results from two other validators."""
return
lambda
obj
:
join
((
left
(
obj
),
right
(
obj
)))
\ No newline at end of file
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