Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
plotID_python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
plotID
plotID_python
Commits
7a3ba3c8
Commit
7a3ba3c8
authored
2 years ago
by
Mayr, Hannes
Committed by
Hock, Martin
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Move list validation to own function
parent
df7f25c2
No related branches found
No related tags found
4 merge requests
!41
Include latest changes in main branch
,
!37
Merge dev upstream changes into improve/metadata
,
!34
Include architecture diagram in docs
,
!29
Move list validation to own function
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
plotid/plotoptions.py
+54
-1
54 additions, 1 deletion
plotid/plotoptions.py
plotid/publish.py
+8
-37
8 additions, 37 deletions
plotid/publish.py
plotid/save_plot.py
+1
-1
1 addition, 1 deletion
plotid/save_plot.py
tests/test_publish.py
+4
-1
4 additions, 1 deletion
tests/test_publish.py
with
67 additions
and
40 deletions
plotid/plotoptions.py
+
54
−
1
View file @
7a3ba3c8
...
...
@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
"""
Contains the PlotOptions and PlotIDTransfer classes.
"""
import
os
class
PlotOptions
:
"""
...
...
@@ -10,7 +12,10 @@ class PlotOptions:
Methods
-------
__init__
validate_input : Check if input is correct type.
validate_input
Check if input is correct type.
validate_list
Check if all elements of a given list are of certain type.
Attributes
----------
...
...
@@ -99,7 +104,55 @@ class PlotIDTransfer:
def
__init__
(
self
,
figs
,
figure_ids
):
self
.
figs
=
figs
self
.
figure_ids
=
figure_ids
self
.
figure_ids
=
validate_list
(
self
.
figure_ids
)
def
__str__
(
self
):
"""
Representation if an object of this class is printed.
"""
return
str
(
self
.
__class__
)
+
"
:
"
+
str
(
self
.
__dict__
)
def
validate_list
(
list_var
,
elt_type
=
str
,
is_file
=
False
):
"""
Validate if contents of a list are of specific type.
Parameters
----------
list_var : list or str
List or single string which contents will be validated.
elt_type : datatype, optional
Datatype of which the list elements must be type of. Otherwise
an Error will be raised. The default is str.
is_file : boolean, optional
Flag to indicate if the list contains paths to files. If True the
strings will be checked if they correspond to an existing file.
The default is False.
Raises
------
TypeError
If one of the list elements is not of type elt_type.
FileNotFoundError
If strings are also checked for existing files and one of the files
does not exist.
Returns
-------
list_var as list
"""
if
isinstance
(
list_var
,
elt_type
):
list_var
=
[
list_var
]
if
isinstance
(
list_var
,
list
):
for
elt
in
list_var
:
if
not
isinstance
(
elt
,
elt_type
):
raise
TypeError
(
f
'
The list of
{
list_var
}
contains an
'
f
'
object which is not of type
{
elt_type
}
.
'
)
if
is_file
:
# Check if directory and files exist
if
not
os
.
path
.
exists
(
elt
):
raise
FileNotFoundError
(
'
The specified directory
'
f
'
/file
{
elt
}
does not exist.
'
)
else
:
raise
TypeError
(
f
'
The specified
{
list_var
}
are neither a
'
f
'
{
elt_type
}
nor a list of
{
elt_type
}
.
'
)
return
list_var
This diff is collapsed.
Click to expand it.
plotid/publish.py
+
8
−
37
View file @
7a3ba3c8
...
...
@@ -17,7 +17,7 @@ import shutil
import
sys
import
warnings
from
plotid.save_plot
import
save_plot
from
plotid.plotoptions
import
PlotIDTransfer
from
plotid.plotoptions
import
PlotIDTransfer
,
validate_list
class
PublishOptions
:
...
...
@@ -69,53 +69,25 @@ class PublishOptions:
"""
# Check if IDs are str
if
isinstance
(
self
.
figure_ids
,
str
):
self
.
figure_ids
=
[
self
.
figure_ids
]
if
isinstance
(
self
.
figure_ids
,
list
):
for
identifier
in
self
.
figure_ids
:
if
not
isinstance
(
identifier
,
str
):
raise
TypeError
(
'
The list of figure_ids contains an object
'
'
which is not a string.
'
)
else
:
raise
TypeError
(
'
The specified figure_ids are neither a string nor
'
'
a list of strings.
'
)
self
.
figure_ids
=
validate_list
(
self
.
figure_ids
)
# Check if plot_name is a string or a list of strings
self
.
plot_names
=
validate_list
(
self
.
plot_names
)
if
not
os
.
path
.
isfile
(
sys
.
argv
[
0
]):
raise
FileNotFoundError
(
'
Cannot copy original python script.
'
'
Running publish from a shell is not
'
'
possible.
'
)
if
isinstance
(
self
.
src_datapaths
,
str
):
self
.
src_datapaths
=
[
self
.
src_datapaths
]
if
isinstance
(
self
.
src_datapaths
,
list
):
for
path
in
self
.
src_datapaths
:
if
not
isinstance
(
path
,
str
):
raise
TypeError
(
f
'
{
path
}
is not a string.
'
)
# Check if source directory and files exist
if
not
os
.
path
.
exists
(
path
):
raise
FileNotFoundError
(
'
The specified source directory
'
f
'
/file
{
path
}
does not exist.
'
)
else
:
raise
TypeError
(
'
The source directory/files are neither
'
'
a string nor a list.
'
)
# Check if self.src_datapaths are strings and existing files.
self
.
src_datapaths
=
validate_list
(
self
.
src_datapaths
,
is_file
=
True
)
# Check if destination directory is allowed path
if
not
os
.
path
.
exists
(
self
.
dst_path_head
):
raise
FileNotFoundError
(
'
The specified destination directory
'
f
'
{
self
.
dst_path_head
}
does not exist.
'
)
# Check if plot_name is a string or a list of strings
if
isinstance
(
self
.
plot_names
,
str
):
self
.
plot_names
=
[
self
.
plot_names
]
if
isinstance
(
self
.
plot_names
,
list
):
for
name
in
self
.
plot_names
:
if
not
isinstance
(
name
,
str
):
raise
TypeError
(
'
The list of plot_names contains an object
'
'
which is not a string.
'
)
else
:
raise
TypeError
(
'
The specified plot_names is neither a string nor
'
'
a list of strings.
'
)
# Check if data_storage is a string
if
not
isinstance
(
self
.
data_storage
,
str
):
raise
TypeError
(
'
The specified data_storage method is not a
'
...
...
@@ -139,7 +111,6 @@ class PublishOptions:
"""
# Export plot figure to picture.
plot_paths
=
save_plot
(
self
.
figure
,
self
.
plot_names
)
print
(
plot_paths
)
match
self
.
data_storage
:
case
'
centralized
'
:
self
.
centralized_data_storage
()
...
...
This diff is collapsed.
Click to expand it.
plotid/save_plot.py
+
1
−
1
View file @
7a3ba3c8
...
...
@@ -27,7 +27,7 @@ def save_plot(figures, plot_names, extension='png'):
Returns
-------
plot_path :
str or
list of str
plot_path : list of str
Names of the created pictures.
"""
# Check if figs is a valid figure or a list of valid figures
...
...
This diff is collapsed.
Click to expand it.
tests/test_publish.py
+
4
−
1
View file @
7a3ba3c8
...
...
@@ -47,7 +47,10 @@ class TestPublish(unittest.TestCase):
'
from a Python script. Therefore, the script cannot be
'
'
copied.
'
)
def
test_publish
(
self
):
"""
Test publish and check if an exported picture file exists.
"""
"""
Test publish and check if an exported picture file exists.
The destination path is given with trailing slash.
"""
publish
(
PlotIDTransfer
(
FIG
,
'
testID
'
),
SRC_DIR
,
DST_PATH
+
'
/
'
,
PIC_NAME
,
data_storage
=
'
individual
'
)
assert
os
.
path
.
isfile
(
os
.
path
.
join
(
DST_PATH
,
'
testID
'
,
...
...
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