Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Unsupervised Video Summarization
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
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
Hussain Kanafani
Unsupervised Video Summarization
Commits
5a847958
Commit
5a847958
authored
4 years ago
by
Hussain Kanafani
Browse files
Options
Downloads
Patches
Plain Diff
util methods with unit tests added
parent
29288690
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
unit-test/__init__.py
+0
-0
0 additions, 0 deletions
unit-test/__init__.py
unit-test/test_utils.py
+50
-0
50 additions, 0 deletions
unit-test/test_utils.py
utils.py
+35
-0
35 additions, 0 deletions
utils.py
with
85 additions
and
0 deletions
unit-test/__init__.py
0 → 100644
+
0
−
0
View file @
5a847958
This diff is collapsed.
Click to expand it.
unit-test/test_utils.py
0 → 100644
+
50
−
0
View file @
5a847958
from
unittest
import
TestCase
import
unittest.mock
as
mock
from
utils
import
digits_in_string
,
make_directory
,
read_json
import
random
import
os.path
as
osp
import
os
import
json
class
TestUtils
(
TestCase
):
def
setUp
(
self
):
self
.
current_dir
=
os
.
getcwd
()
def
tearDown
(
self
):
for
dir
in
os
.
listdir
(
self
.
current_dir
):
if
(
osp
.
isdir
(
dir
)):
os
.
rmdir
(
dir
)
def
test_digits_in_string
(
self
):
txt
=
'
video10
'
digit
=
10
self
.
assertEqual
(
digits_in_string
(
txt
),
int
(
digit
))
self
.
assertEqual
(
type
(
digits_in_string
(
txt
)),
int
)
def
test_make_directory
(
self
):
dir
=
os
.
path
.
join
(
self
.
current_dir
,
'
test_dir
'
+
str
(
random
.
randint
(
1
,
10000
)))
print
(
dir
)
dir
=
make_directory
(
dir
)
self
.
assertTrue
(
osp
.
exists
(
dir
),
False
)
def
test_open_json_file
(
self
):
# test valid JSON
read_data
=
json
.
dumps
({
'
a
'
:
1
,
'
b
'
:
2
,
'
c
'
:
3
})
mock_open
=
mock
.
mock_open
(
read_data
=
read_data
)
with
mock
.
patch
(
'
builtins.open
'
,
mock_open
):
result
=
read_json
(
'
file
'
)
self
.
assertEqual
({
'
a
'
:
1
,
'
b
'
:
2
,
'
c
'
:
3
},
result
)
# test invalid JSON
read_data
=
''
mock_open
=
mock
.
mock_open
(
read_data
=
read_data
)
with
mock
.
patch
(
"
builtins.open
"
,
mock_open
):
with
self
.
assertRaises
(
ValueError
)
as
context
:
read_json
(
'
file
'
)
self
.
assertEqual
(
'
file is not valid
'
,
str
(
context
.
exception
))
# test file does not exist
with
self
.
assertRaises
(
IOError
)
as
context
:
read_json
(
'
null
'
)
self
.
assertEqual
(
'
null does not exist.
'
,
str
(
context
.
exception
))
This diff is collapsed.
Click to expand it.
utils.py
0 → 100644
+
35
−
0
View file @
5a847958
import
re
import
json
import
os
def
read_json
(
file_path
):
try
:
with
open
(
file_path
,
'
r
'
)
as
f
:
try
:
return
json
.
load
(
f
)
except
ValueError
:
raise
ValueError
(
'
{} is not valid
'
.
format
(
file_path
))
except
IOError
:
raise
IOError
(
'
{} does not exist.
'
.
format
(
file_path
))
def
write_json
(
obj
,
file_path
):
make_directory
(
os
.
path
.
dirname
(
file_path
))
with
open
(
file_path
,
'
w
'
)
as
f
:
json
.
dump
(
obj
,
f
)
def
digits_in_string
(
text
):
non_digit
=
re
.
compile
(
"
\D
"
)
return
int
(
non_digit
.
sub
(
""
,
text
))
def
make_directory
(
directory
):
if
not
os
.
path
.
exists
(
directory
):
try
:
os
.
makedirs
(
directory
)
except
OSError
as
err
:
raise
return
directory
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