Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
firanka
Manage
Activity
Members
Labels
Plan
Issues
0
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
firanka
Commits
8aca5d57
Commit
8aca5d57
authored
7 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
test
parent
48cb9d2c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
firanka/series/range.py
+30
-38
30 additions, 38 deletions
firanka/series/range.py
tests/test_series/test_range.py
+8
-7
8 additions, 7 deletions
tests/test_series/test_range.py
with
38 additions
and
45 deletions
firanka/series/range.py
+
30
−
38
View file @
8aca5d57
...
...
@@ -20,65 +20,55 @@ def pre_range(fun):
class
Range
(
object
):
"""
Range of real numbers
Range of real numbers
. Immutable.
"""
def
__from_str
(
self
,
rs
):
if
rs
[
0
]
not
in
'
<(
'
:
raise
ValueError
(
'
Must start with ( or <
'
)
if
rs
[
-
1
]
not
in
'
>)
'
:
raise
ValueError
(
'
Must end with ) or >
'
)
if
'
;
'
not
in
rs
:
raise
ValueError
(
'
Separator ; required
'
)
start
,
stop
=
rs
[
1
:
-
1
].
split
(
'
;
'
)
start
=
float
(
start
)
stop
=
float
(
stop
)
return
float
(
start
),
float
(
stop
),
rs
[
0
]
==
'
<
'
,
rs
[
-
1
]
==
'
>
'
def
__from_range
(
self
,
rs
):
return
rs
.
start
,
rs
.
stop
,
rs
.
lend_inclusive
,
rs
.
rend_inclusive
def
__init__
(
self
,
*
args
):
if
len
(
args
)
==
1
:
rs
,
=
args
args
=
{
True
:
self
.
__from_range
,
False
:
self
.
__from_str
}[
isinstance
(
rs
,
type
(
self
))](
rs
)
if
isinstance
(
rs
,
type
(
self
)):
args
=
rs
.
start
,
rs
.
stop
,
rs
.
left_inc
,
rs
.
right_inc
else
:
if
rs
[
0
]
not
in
'
<(
'
:
raise
ValueError
(
'
Must start with ( or <
'
)
if
rs
[
-
1
]
not
in
'
>)
'
:
raise
ValueError
(
'
Must end with ) or >
'
)
if
'
;
'
not
in
rs
:
raise
ValueError
(
'
Separator ; required
'
)
q
=
lambda
a
,
b
:
args
[
a
]
and
math
.
isinf
(
args
[
b
])
start
,
stop
=
rs
[
1
:
-
1
].
split
(
'
;
'
)
args
=
float
(
start
),
float
(
stop
),
rs
[
0
]
==
'
<
'
,
rs
[
-
1
]
==
'
>
'
if
q
(
2
,
0
)
or
q
(
3
,
1
):
q
=
lambda
a
,
b
,
args
:
args
[
a
]
and
math
.
isinf
(
args
[
b
])
if
q
(
2
,
0
,
args
)
or
q
(
3
,
1
,
args
):
raise
ValueError
(
'
Set with sharp closing but infinity set
'
)
self
.
start
,
self
.
stop
,
self
.
lend_inclusive
,
self
.
rend_inclusive
=
args
print
(
args
)
self
.
start
,
self
.
stop
,
self
.
left_inc
,
self
.
right_inc
=
args
def
__contains__
(
self
,
x
):
if
x
==
self
.
start
:
return
self
.
le
nd
_inc
lusive
return
self
.
le
ft
_inc
if
x
==
self
.
stop
:
return
self
.
r
end_inclusive
return
self
.
r
ight_inc
return
self
.
start
<
x
<
self
.
stop
def
is_empty
(
self
):
return
(
self
.
start
==
self
.
stop
)
and
(
not
self
.
le
nd
_inc
lusive
)
and
(
not
self
.
rend_inclusive
)
print
(
self
.
start
,
self
.
stop
,
self
.
le
ft
_inc
,
self
.
right_inc
)
return
(
self
.
start
==
self
.
stop
)
and
not
(
self
.
left_inc
or
self
.
right_inc
)
def
__len__
(
self
):
return
self
.
stop
-
self
.
start
def
__repr__
(
self
):
return
'
Range(%s, %s, %s, %s)
'
%
(
repr
(
self
.
start
),
repr
(
self
.
stop
),
repr
(
self
.
lend_inclusive
),
repr
(
self
.
rend_inclusive
))
def
__bool__
(
self
):
"""
True if not empty
"""
return
not
self
.
is_empty
()
return
'
Range(%s, %s, %s, %s)
'
%
(
repr
(
self
.
start
),
repr
(
self
.
stop
),
repr
(
self
.
left_inc
),
repr
(
self
.
right_inc
))
def
__str__
(
self
):
return
'
%s%s;%s%s
'
%
(
'
<
'
if
self
.
le
nd
_inc
lusive
else
'
(
'
,
'
<
'
if
self
.
le
ft
_inc
else
'
(
'
,
self
.
start
,
self
.
stop
,
'
>
'
if
self
.
r
end_inclusive
else
'
)
'
,
'
>
'
if
self
.
r
ight_inc
else
'
)
'
,
)
@pre_range
...
...
@@ -91,29 +81,31 @@ class Range(object):
if
(
self
.
stop
<
y
.
start
)
or
(
y
.
stop
<
y
.
start
):
return
EMPTY_RANGE
if
self
.
stop
==
y
.
start
and
not
(
self
.
r
end_inclusive
and
y
.
le
nd
_inc
lusive
):
if
self
.
stop
==
y
.
start
and
not
(
self
.
r
ight_inc
and
y
.
le
ft
_inc
):
return
EMPTY_RANGE
if
self
.
start
==
y
.
start
:
start
=
self
.
start
le
nd
_inc
lusive
=
self
.
le
nd
_inc
lusive
or
y
.
le
nd
_inc
lusive
le
ft
_inc
=
self
.
le
ft
_inc
or
y
.
le
ft
_inc
else
:
start
=
y
.
start
le
nd
_inc
lusive
=
y
.
le
nd
_inc
lusive
le
ft
_inc
=
y
.
le
ft
_inc
if
self
.
stop
==
y
.
stop
:
stop
=
self
.
stop
r
end_inclusive
=
self
.
rend_inclusive
or
y
.
rend_inclusive
r
ight_inc
=
self
.
right_inc
or
y
.
right_inc
else
:
p
,
q
=
(
self
,
y
)
if
self
.
stop
<
y
.
stop
else
(
y
,
self
)
stop
=
p
.
stop
r
end_inclusive
=
p
.
rend_inclusive
and
(
stop
in
q
)
r
ight_inc
=
p
.
right_inc
and
(
stop
in
q
)
return
Range
(
start
,
stop
,
le
nd
_inc
lusive
,
rend_inclusive
)
return
Range
(
start
,
stop
,
le
ft
_inc
,
right_inc
)
@pre_range
def
__eq__
(
self
,
other
):
return
self
.
start
==
other
.
start
and
self
.
stop
==
other
.
stop
and
self
.
lend_inclusive
==
other
.
lend_inclusive
and
self
.
rend_inclusive
==
other
.
rend_inclusive
if
self
.
is_empty
()
and
other
.
is_empty
():
return
True
return
self
.
start
==
other
.
start
and
self
.
stop
==
other
.
stop
and
self
.
left_inc
==
other
.
left_inc
and
self
.
right_inc
==
other
.
right_inc
def
__hash__
(
self
):
return
hash
(
self
.
start
)
^
hash
(
self
.
stop
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_series/test_range.py
+
8
−
7
View file @
8aca5d57
# coding=UTF-8
from
__future__
import
print_function
,
absolute_import
,
division
import
six
import
unittest
from
firanka.series
import
Range
class
TestRange
(
unittest
.
TestCase
):
def
do_intersect
(
self
,
a
,
b
,
val
):
if
type
(
val
)
==
bool
:
if
bool
(
Range
(
a
).
intersection
(
b
))
!=
val
:
...
...
@@ -19,10 +16,14 @@ class TestRange(unittest.TestCase):
self
.
assertEqual
(
Range
(
b
).
intersection
(
a
),
Range
(
val
))
def
test_isempty
(
self
):
self
.
assertFalse
(
Range
(
-
1
,
-
1
,
False
,
False
))
self
.
assertTrue
(
Range
(
-
1
,
-
1
,
False
,
True
))
self
.
assertTrue
(
Range
(
-
1
,
-
1
,
False
,
False
).
is_empty
())
self
.
assertFalse
(
Range
(
-
1
,
-
1
,
False
,
True
).
is_empty
())
def
tf
(
r
,
p
):
s
=
Range
(
r
)
self
.
assertEqual
(
s
,
r
)
self
.
assertEqual
(
s
.
is_empty
(),
not
p
)
tf
(
Range
(
-
1
,
-
1
,
False
,
False
),
False
)
tf
(
Range
(
-
1
,
-
1
,
False
,
True
),
True
)
self
.
assertEqual
(
Range
(
0
,
0
,
False
,
False
),
Range
(
2
,
2
,
False
,
False
))
def
test_intersection
(
self
):
self
.
do_intersect
(
Range
(
-
10
,
-
1
,
True
,
True
),
'
<2;3>
'
,
False
)
...
...
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