Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
59b30110
Commit
59b30110
authored
4 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
extra docs and typing info
parent
fc7692b6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
docs/coding/predicates.rst
+6
-4
6 additions, 4 deletions
docs/coding/predicates.rst
satella/coding/predicates.py
+5
-9
5 additions, 9 deletions
satella/coding/predicates.py
with
11 additions
and
13 deletions
docs/coding/predicates.rst
+
6
−
4
View file @
59b30110
...
@@ -2,10 +2,10 @@
...
@@ -2,10 +2,10 @@
Predicates
Predicates
==========
==========
P
redicates a
re
function
s that take something and return a boolean about truthfuln
es
s
By a p
redicate
Satella understand
s a function
of a single argument and a single return valu
es
.
of given statement. Satella contains a bunch of functions to produce these predicates
.
Satella's API drastically simplifies writing lambda
.
Satella lets you express
predicate
s in a Pythonic way, eg:
Satella lets you express
lambda
s in a Pythonic way, eg:
::
::
...
@@ -23,7 +23,6 @@ which due to Python limitations (namely __len__ being allowed to return an int o
...
@@ -23,7 +23,6 @@ which due to Python limitations (namely __len__ being allowed to return an int o
via it's method .length(), eg:
via it's method .length(), eg:
::
::
p = x.length() == 2
p = x.length() == 2
...
@@ -72,6 +71,7 @@ Or check whether an instance is of provided type
...
@@ -72,6 +71,7 @@ Or check whether an instance is of provided type
p = x.instanceof(int)
p = x.instanceof(int)
assert p(2)
assert p(2)
Only take care for x to be the first argument in all operators you use. For example,
Only take care for x to be the first argument in all operators you use. For example,
don't do this:
don't do this:
...
@@ -79,3 +79,5 @@ don't do this:
...
@@ -79,3 +79,5 @@ don't do this:
p = 2 < x < 6
p = 2 < x < 6
Because Python will compare first 2 with x using int's __gt__, which will fail.
This diff is collapsed.
Click to expand it.
satella/coding/predicates.py
+
5
−
9
View file @
59b30110
...
@@ -44,29 +44,29 @@ class Predicate:
...
@@ -44,29 +44,29 @@ class Predicate:
def
__call__
(
self
,
v
):
def
__call__
(
self
,
v
):
return
self
.
operation
(
v
)
return
self
.
operation
(
v
)
def
has_keys
(
self
,
*
keys
):
def
has_keys
(
self
,
*
keys
)
->
'
Predicate
'
:
"""
"""
Return a predicate checking whether this value has provided keys
Return a predicate checking whether this value has provided keys
"""
"""
return
make_operation_two_args
(
_has_keys
)(
self
,
keys
)
return
make_operation_two_args
(
_has_keys
)(
self
,
keys
)
def
one_of
(
self
,
*
values
):
def
one_of
(
self
,
*
values
)
->
'
Predicate
'
:
"""
"""
Return a predicate checking if x is amongst values
Return a predicate checking if x is amongst values
"""
"""
return
make_operation_two_args
(
_one_of
)(
self
,
values
)
return
make_operation_two_args
(
_one_of
)(
self
,
values
)
def
inside
(
self
,
value
)
:
def
inside
(
self
,
value
:
tp
.
Container
)
->
'
Predicate
'
:
"""
"""
Return a predicate checking if x is inside value
Return a predicate checking if x is inside value
"""
"""
return
make_operation_two_args
(
operator
.
contains
)(
self
,
value
)
return
make_operation_two_args
(
operator
.
contains
)(
self
,
value
)
def
instanceof
(
self
,
instance
)
:
def
instanceof
(
self
,
class_descriptor
)
->
'
Predicate
'
:
"""
"""
Return a predicate checking whether this value is an instance of instance
Return a predicate checking whether this value is an instance of instance
"""
"""
return
make_operation_two_args
(
isinstance
)(
self
,
instance
)
return
make_operation_two_args
(
isinstance
)(
self
,
class_descriptor
)
length
=
make_operation_single_arg
(
len
)
length
=
make_operation_single_arg
(
len
)
...
@@ -88,10 +88,6 @@ class Predicate:
...
@@ -88,10 +88,6 @@ class Predicate:
__neg__
=
make_operation_single_arg
(
lambda
y
:
-
y
)
__neg__
=
make_operation_single_arg
(
lambda
y
:
-
y
)
__invert__
=
make_operation_single_arg
(
operator
.
invert
)
__invert__
=
make_operation_single_arg
(
operator
.
invert
)
__abs__
=
make_operation_single_arg
(
abs
)
__abs__
=
make_operation_single_arg
(
abs
)
__int__
=
make_operation_single_arg
(
int
)
__float__
=
make_operation_single_arg
(
float
)
__complex__
=
make_operation_single_arg
(
complex
)
__str__
=
make_operation_single_arg
(
str
)
__truediv__
=
make_operation_two_args
(
operator
.
__truediv__
)
__truediv__
=
make_operation_two_args
(
operator
.
__truediv__
)
__floordiv__
=
make_operation_two_args
(
operator
.
floordiv
)
__floordiv__
=
make_operation_two_args
(
operator
.
floordiv
)
__mod__
=
make_operation_two_args
(
operator
.
mod
)
__mod__
=
make_operation_two_args
(
operator
.
mod
)
...
...
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