Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
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
Code review analytics
Issue 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
caosdb
Software
caosdb-advanced-user-tools
Commits
83d90993
Verified
Commit
83d90993
authored
11 months ago
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
MAINT: Reordered table_json_conversion/xlsx_utils.py
parent
f92407a0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!107
Release v0.11.0
,
!101
ENH: table_json_conversion/xlsx_utils.py: data array schema generation
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosadvancedtools/table_json_conversion/xlsx_utils.py
+43
-26
43 additions, 26 deletions
src/caosadvancedtools/table_json_conversion/xlsx_utils.py
with
43 additions
and
26 deletions
src/caosadvancedtools/table_json_conversion/xlsx_utils.py
+
43
−
26
View file @
83d90993
...
...
@@ -18,13 +18,25 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
General utilities to work with XLSX files with (hidden) column and row annotations and typing.
"""
"""
General utilities to work with XLSX files with (hidden) column and row annotations and typing.
The most prominent functions are:
- ``p2s``: Path to string: ``[
"
some
"
,
"
path
"
] ->
"
some.path
"
``
- ``read_or_dict``: Load JSON object from path, file or dict.
This module also defines these enums:
- ColumnType
- RowType
"""
from
__future__
import
annotations
import
json
from
collections
import
OrderedDict
from
copy
import
deepcopy
from
enum
import
Enum
from
types
import
SimpleNamespace
from
typing
import
Dict
,
List
,
TextIO
,
Union
...
...
@@ -49,27 +61,9 @@ class RowType(Enum):
IGNORE
=
3
def
p2s
(
path
:
List
[
str
])
->
str
:
"""
Path to string: dot-separated.
"""
return
"
.
"
.
join
(
path
)
def
read_or_dict
(
data
:
Union
[
dict
,
str
,
TextIO
])
->
dict
:
"""
If data is a json file name or input stream, read data from there.
If it is a dict already, just return it.
"""
if
isinstance
(
data
,
dict
):
return
data
if
isinstance
(
data
,
str
):
with
open
(
data
,
encoding
=
"
utf-8
"
)
as
infile
:
data
=
json
.
load
(
infile
)
elif
hasattr
(
data
,
"
read
"
):
data
=
json
.
load
(
data
)
else
:
raise
ValueError
(
f
"
I don
'
t know how to handle the datatype of `data`:
{
type
(
data
)
}
"
)
assert
isinstance
(
data
,
dict
)
return
data
def
get_defining_paths
(
workbook
:
Workbook
)
->
dict
[
str
,
list
[
list
[
str
]]]:
...
...
@@ -243,6 +237,16 @@ def get_worksheet_for_path(path: list[str], defining_path_index: dict[str, list[
raise
KeyError
(
f
"
Could not find defining worksheet for path:
{
path
}
"
)
def
is_exploded_sheet
(
sheet
:
Worksheet
)
->
bool
:
"""
Return True if this is a an
"
exploded
"
sheet.
An exploded sheet is a sheet whose data entries are LIST valued properties of entries in another
sheet. A sheet is detected as exploded iff it has FOREIGN columns.
"""
column_types
=
_get_column_types
(
sheet
)
return
ColumnType
.
FOREIGN
.
name
in
column_types
.
values
()
def
next_row_index
(
sheet
:
Worksheet
)
->
int
:
"""
Return the index for the next data row.
...
...
@@ -251,14 +255,27 @@ def next_row_index(sheet: Worksheet) -> int:
return
sheet
.
max_row
def
is_exploded_sheet
(
sheet
:
Worksheet
)
->
bool
:
"""
Return True if this is a an
"
exploded
"
sheet.
An exploded sheet is a sheet whose data entries are LIST valued properties of entries in another
sheet. A sheet is detected as exploded iff it has FOREIGN columns.
def
p2s
(
path
:
List
[
str
])
->
str
:
"""
Path to string: dot-separated.
"""
column_types
=
_get_column_types
(
sheet
)
return
ColumnType
.
FOREIGN
.
name
in
column_types
.
values
()
return
"
.
"
.
join
(
path
)
def
read_or_dict
(
data
:
Union
[
dict
,
str
,
TextIO
])
->
dict
:
"""
If data is a json file name or input stream, read data from there.
If it is a dict already, just return it.
"""
if
isinstance
(
data
,
dict
):
return
data
if
isinstance
(
data
,
str
):
with
open
(
data
,
encoding
=
"
utf-8
"
)
as
infile
:
data
=
json
.
load
(
infile
)
elif
hasattr
(
data
,
"
read
"
):
data
=
json
.
load
(
data
)
else
:
raise
ValueError
(
f
"
I don
'
t know how to handle the datatype of `data`:
{
type
(
data
)
}
"
)
assert
isinstance
(
data
,
dict
)
return
data
def
_get_column_types
(
sheet
:
Worksheet
)
->
OrderedDict
:
...
...
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